KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > SizeSelectorTest


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.types.selectors;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.util.*;
23 import org.apache.tools.ant.BuildFileTest;
24 import org.apache.tools.ant.types.Parameter;
25
26 import junit.framework.TestCase;
27 import junit.framework.AssertionFailedError;
28
29 /**
30  * Tests Size Selectors
31  *
32  */

33 public class SizeSelectorTest extends BaseSelectorTest {
34
35     private Project project;
36
37     public SizeSelectorTest(String JavaDoc name) {
38         super(name);
39     }
40
41     /**
42      * Factory method from base class. This is overriden in child
43      * classes to return a specific Selector class.
44      */

45     public BaseSelector getInstance() {
46         return new SizeSelector();
47     }
48
49     /**
50      * Test the code that validates the selector.
51      */

52     public void testValidate() {
53         SizeSelector s = (SizeSelector)getInstance();
54         try {
55             s.isSelected(basedir,filenames[0],files[0]);
56             fail("SizeSelector did not check for required fields");
57         } catch (BuildException be1) {
58             assertEquals("The value attribute is required, and must "
59                     + "be positive", be1.getMessage());
60         }
61
62         s = (SizeSelector)getInstance();
63         s.setValue(-10);
64         try {
65             s.isSelected(basedir,filenames[0],files[0]);
66             fail("SizeSelector did not check for value being in the "
67                     + "allowable range");
68         } catch (BuildException be2) {
69             assertEquals("The value attribute is required, and must "
70                     + "be positive", be2.getMessage());
71         }
72
73         s = (SizeSelector)getInstance();
74         Parameter param = new Parameter();
75         param.setName("garbage in");
76         param.setValue("garbage out");
77         Parameter[] params = {param};
78         s.setParameters(params);
79         try {
80             s.isSelected(basedir,filenames[0],files[0]);
81             fail("SizeSelector did not check for valid parameter element");
82         } catch (BuildException be3) {
83             assertEquals("Invalid parameter garbage in", be3.getMessage());
84         }
85
86         s = (SizeSelector)getInstance();
87         param = new Parameter();
88         param.setName("value");
89         param.setValue("garbage out");
90         params[0] = param;
91         s.setParameters(params);
92         try {
93             s.isSelected(basedir,filenames[0],files[0]);
94             fail("SizeSelector accepted bad value as parameter");
95         } catch (BuildException be4) {
96             assertEquals("Invalid size setting garbage out",
97                     be4.getMessage());
98         }
99
100         s = (SizeSelector)getInstance();
101         Parameter param1 = new Parameter();
102         Parameter param2 = new Parameter();
103         param1.setName("value");
104         param1.setValue("5");
105         param2.setName("units");
106         param2.setValue("garbage out");
107         params = new Parameter[2];
108         params[0] = param1;
109         params[1] = param2;
110         try {
111             s.setParameters(params);
112             s.isSelected(basedir,filenames[0],files[0]);
113             fail("SizeSelector accepted bad units as parameter");
114         } catch (BuildException be5) {
115             assertEquals("garbage out is not a legal value for this attribute",
116                     be5.getMessage());
117         }
118
119     }
120
121     /**
122      * Tests to make sure that the selector is selecting files correctly.
123      */

124     public void testSelectionBehaviour() {
125         SizeSelector s;
126         String JavaDoc results;
127
128         SizeSelector.ByteUnits kilo = new SizeSelector.ByteUnits();
129         kilo.setValue("K");
130         SizeSelector.ByteUnits kibi = new SizeSelector.ByteUnits();
131         kibi.setValue("Ki");
132         SizeSelector.ByteUnits tibi = new SizeSelector.ByteUnits();
133         tibi.setValue("Ti");
134         SizeSelector.SizeComparisons less = new SizeSelector.SizeComparisons();
135         less.setValue("less");
136         SizeSelector.SizeComparisons equal = new SizeSelector.SizeComparisons();
137         equal.setValue("equal");
138         SizeSelector.SizeComparisons more = new SizeSelector.SizeComparisons();
139         more.setValue("more");
140
141
142         try {
143             makeBed();
144
145             s = (SizeSelector)getInstance();
146             s.setValue(10);
147             s.setWhen(less);
148             results = selectionString(s);
149             assertEquals("TFFFFFFFFFFT", results);
150
151             s = (SizeSelector)getInstance();
152             s.setValue(10);
153             s.setWhen(more);
154             results = selectionString(s);
155             assertEquals("TTTTTTTTTTTT", results);
156
157             s = (SizeSelector)getInstance();
158             s.setValue(32);
159             s.setWhen(equal);
160             results = selectionString(s);
161             assertEquals("TTFFTFFFFFFT", results);
162
163             s = (SizeSelector)getInstance();
164             s.setValue(7);
165             s.setWhen(more);
166             s.setUnits(kilo);
167             results = selectionString(s);
168             assertEquals("TFTFFTTTTTTT", results);
169
170             s = (SizeSelector)getInstance();
171             s.setValue(7);
172             s.setWhen(more);
173             s.setUnits(kibi);
174             results = selectionString(s);
175             assertEquals("TFTFFFTTFTTT", results);
176
177             s = (SizeSelector)getInstance();
178             s.setValue(99999);
179             s.setWhen(more);
180             s.setUnits(tibi);
181             results = selectionString(s);
182             assertEquals("TFFFFFFFFFFT", results);
183
184             s = (SizeSelector)getInstance();
185             Parameter param1 = new Parameter();
186             Parameter param2 = new Parameter();
187             Parameter param3 = new Parameter();
188             param1.setName("value");
189             param1.setValue("20");
190             param2.setName("units");
191             param2.setValue("Ki");
192             param3.setName("when");
193             param3.setValue("more");
194             Parameter[] params = {param1,param2,param3};
195             s.setParameters(params);
196             results = selectionString(s);
197             assertEquals("TFFFFFFTFFTT", results);
198         }
199         finally {
200             cleanupBed();
201         }
202
203     }
204
205 }
206
Popular Tags