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 Size Selectors
027: *
028: */
029: public class SizeSelectorTest extends BaseSelectorTest {
030:
031: private Project project;
032:
033: public SizeSelectorTest(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 SizeSelector();
043: }
044:
045: /**
046: * Test the code that validates the selector.
047: */
048: public void testValidate() {
049: SizeSelector s = (SizeSelector) getInstance();
050: try {
051: s.isSelected(basedir, filenames[0], files[0]);
052: fail("SizeSelector did not check for required fields");
053: } catch (BuildException be1) {
054: assertEquals("The value attribute is required, and must "
055: + "be positive", be1.getMessage());
056: }
057:
058: s = (SizeSelector) getInstance();
059: s.setValue(-10);
060: try {
061: s.isSelected(basedir, filenames[0], files[0]);
062: fail("SizeSelector did not check for value being in the "
063: + "allowable range");
064: } catch (BuildException be2) {
065: assertEquals("The value attribute is required, and must "
066: + "be positive", be2.getMessage());
067: }
068:
069: s = (SizeSelector) getInstance();
070: Parameter param = new Parameter();
071: param.setName("garbage in");
072: param.setValue("garbage out");
073: Parameter[] params = { param };
074: s.setParameters(params);
075: try {
076: s.isSelected(basedir, filenames[0], files[0]);
077: fail("SizeSelector did not check for valid parameter element");
078: } catch (BuildException be3) {
079: assertEquals("Invalid parameter garbage in", be3
080: .getMessage());
081: }
082:
083: s = (SizeSelector) getInstance();
084: param = new Parameter();
085: param.setName("value");
086: param.setValue("garbage out");
087: params[0] = param;
088: s.setParameters(params);
089: try {
090: s.isSelected(basedir, filenames[0], files[0]);
091: fail("SizeSelector accepted bad value as parameter");
092: } catch (BuildException be4) {
093: assertEquals("Invalid size setting garbage out", be4
094: .getMessage());
095: }
096:
097: s = (SizeSelector) getInstance();
098: Parameter param1 = new Parameter();
099: Parameter param2 = new Parameter();
100: param1.setName("value");
101: param1.setValue("5");
102: param2.setName("units");
103: param2.setValue("garbage out");
104: params = new Parameter[2];
105: params[0] = param1;
106: params[1] = param2;
107: try {
108: s.setParameters(params);
109: s.isSelected(basedir, filenames[0], files[0]);
110: fail("SizeSelector accepted bad units as parameter");
111: } catch (BuildException be5) {
112: assertEquals(
113: "garbage out is not a legal value for this attribute",
114: be5.getMessage());
115: }
116:
117: }
118:
119: /**
120: * Tests to make sure that the selector is selecting files correctly.
121: */
122: public void testSelectionBehaviour() {
123: SizeSelector s;
124: String results;
125:
126: SizeSelector.ByteUnits kilo = new SizeSelector.ByteUnits();
127: kilo.setValue("K");
128: SizeSelector.ByteUnits kibi = new SizeSelector.ByteUnits();
129: kibi.setValue("Ki");
130: SizeSelector.ByteUnits tibi = new SizeSelector.ByteUnits();
131: tibi.setValue("Ti");
132: SizeSelector.SizeComparisons less = new SizeSelector.SizeComparisons();
133: less.setValue("less");
134: SizeSelector.SizeComparisons equal = new SizeSelector.SizeComparisons();
135: equal.setValue("equal");
136: SizeSelector.SizeComparisons more = new SizeSelector.SizeComparisons();
137: more.setValue("more");
138:
139: try {
140: makeBed();
141:
142: s = (SizeSelector) getInstance();
143: s.setValue(10);
144: s.setWhen(less);
145: results = selectionString(s);
146: assertEquals("TFFFFFFFFFFT", results);
147:
148: s = (SizeSelector) getInstance();
149: s.setValue(10);
150: s.setWhen(more);
151: results = selectionString(s);
152: assertEquals("TTTTTTTTTTTT", results);
153:
154: s = (SizeSelector) getInstance();
155: s.setValue(32);
156: s.setWhen(equal);
157: results = selectionString(s);
158: assertEquals("TFFFTFFFFFFT", results);
159:
160: s = (SizeSelector) getInstance();
161: s.setValue(7);
162: s.setWhen(more);
163: s.setUnits(kilo);
164: results = selectionString(s);
165: assertEquals("TFTFFTTTTTTT", results);
166:
167: s = (SizeSelector) getInstance();
168: s.setValue(7);
169: s.setWhen(more);
170: s.setUnits(kibi);
171: results = selectionString(s);
172: assertEquals("TFTFFFTTFTTT", results);
173:
174: s = (SizeSelector) getInstance();
175: s.setValue(99999);
176: s.setWhen(more);
177: s.setUnits(tibi);
178: results = selectionString(s);
179: assertEquals("TFFFFFFFFFFT", results);
180:
181: s = (SizeSelector) getInstance();
182: Parameter param1 = new Parameter();
183: Parameter param2 = new Parameter();
184: Parameter param3 = new Parameter();
185: param1.setName("value");
186: param1.setValue("20");
187: param2.setName("units");
188: param2.setValue("Ki");
189: param3.setName("when");
190: param3.setValue("more");
191: Parameter[] params = { param1, param2, param3 };
192: s.setParameters(params);
193: results = selectionString(s);
194: assertEquals("TFFFFFFTFFTT", results);
195: } finally {
196: cleanupBed();
197: }
198:
199: }
200:
201: }
|