001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.selectors;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.types.Parameter;
024:
025: /**
026: * Tests Depth Selectors
027: *
028: */
029: public class DepthSelectorTest extends BaseSelectorTest {
030:
031: private Project project;
032:
033: public DepthSelectorTest(String name) {
034: super (name);
035: }
036:
037: /**
038: * Factory method from base class. This is overriden in child
039: * classes to return a specific Selector class.
040: */
041: public BaseSelector getInstance() {
042: return new DepthSelector();
043: }
044:
045: /**
046: * Test the code that validates the selector.
047: */
048: public void testValidate() {
049: DepthSelector s = (DepthSelector) getInstance();
050: try {
051: s.isSelected(basedir, filenames[0], files[0]);
052: fail("DepthSelector did not check for required fields");
053: } catch (BuildException be1) {
054: assertEquals("You must set at least one of the min or the "
055: + "max levels.", be1.getMessage());
056: }
057:
058: s = (DepthSelector) getInstance();
059: s.setMin(5);
060: s.setMax(2);
061: try {
062: s.isSelected(basedir, filenames[0], files[0]);
063: fail("DepthSelector did not check for maximum being higher "
064: + "than minimum");
065: } catch (BuildException be2) {
066: assertEquals(
067: "The maximum depth is lower than the minimum.", be2
068: .getMessage());
069: }
070:
071: s = (DepthSelector) getInstance();
072: Parameter param = new Parameter();
073: param.setName("garbage in");
074: param.setValue("garbage out");
075: Parameter[] params = new Parameter[1];
076: params[0] = param;
077: s.setParameters(params);
078: try {
079: s.isSelected(basedir, filenames[0], files[0]);
080: fail("DepthSelector did not check for valid parameter element");
081: } catch (BuildException be3) {
082: assertEquals("Invalid parameter garbage in", be3
083: .getMessage());
084: }
085:
086: s = (DepthSelector) getInstance();
087: param = new Parameter();
088: param.setName("min");
089: param.setValue("garbage out");
090: params[0] = param;
091: s.setParameters(params);
092: try {
093: s.isSelected(basedir, filenames[0], files[0]);
094: fail("DepthSelector accepted bad minimum as parameter");
095: } catch (BuildException be4) {
096: assertEquals("Invalid minimum value garbage out", be4
097: .getMessage());
098: }
099:
100: s = (DepthSelector) getInstance();
101: param = new Parameter();
102: param.setName("max");
103: param.setValue("garbage out");
104: params[0] = param;
105: s.setParameters(params);
106: try {
107: s.isSelected(basedir, filenames[0], files[0]);
108: fail("DepthSelector accepted bad maximum as parameter");
109: } catch (BuildException be5) {
110: assertEquals("Invalid maximum value garbage out", be5
111: .getMessage());
112: }
113:
114: }
115:
116: /**
117: * Tests to make sure that the selector is selecting files correctly.
118: */
119: public void testSelectionBehaviour() {
120: DepthSelector s;
121: String results;
122:
123: try {
124: makeBed();
125:
126: s = (DepthSelector) getInstance();
127: s.setMin(20);
128: s.setMax(25);
129: results = selectionString(s);
130: assertEquals("FFFFFFFFFFFF", results);
131:
132: s = (DepthSelector) getInstance();
133: s.setMin(0);
134: results = selectionString(s);
135: assertEquals("TTTTTTTTTTTT", results);
136:
137: s = (DepthSelector) getInstance();
138: s.setMin(1);
139: results = selectionString(s);
140: assertEquals("FFFFFTTTTTTT", results);
141:
142: s = (DepthSelector) getInstance();
143: s.setMax(0);
144: results = selectionString(s);
145: assertEquals("TTTTTFFFFFFF", results);
146:
147: s = (DepthSelector) getInstance();
148: s.setMin(1);
149: s.setMax(1);
150: results = selectionString(s);
151: assertEquals("FFFFFTTTFFFT", results);
152:
153: } finally {
154: cleanupBed();
155: }
156:
157: }
158:
159: }
|