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.BuildFileTest;
|
---|
23 | import org.apache.tools.ant.types.Parameter;
|
---|
24 | import org.apache.tools.ant.util.JavaEnvUtils;
|
---|
25 |
|
---|
26 | import java.text.SimpleDateFormat;
|
---|
27 | import java.text.ParsePosition;
|
---|
28 | import java.util.Date;
|
---|
29 |
|
---|
30 | import junit.framework.TestCase;
|
---|
31 | import junit.framework.AssertionFailedError;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Tests Date Selectors.
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class DateSelectorTest extends BaseSelectorTest {
|
---|
38 |
|
---|
39 | private Project project;
|
---|
40 |
|
---|
41 | public DateSelectorTest(String name) {
|
---|
42 | super(name);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Factory method from base class. This is overriden in child
|
---|
47 | * classes to return a specific Selector class.
|
---|
48 | */
|
---|
49 | public BaseSelector getInstance() {
|
---|
50 | return new DateSelector();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Test the code that validates the selector.
|
---|
55 | */
|
---|
56 | public void testValidate() {
|
---|
57 | DateSelector s = (DateSelector)getInstance();
|
---|
58 | try {
|
---|
59 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
60 | fail("DateSelector did not check for required fields");
|
---|
61 | } catch (BuildException be1) {
|
---|
62 | assertEquals("You must provide a datetime or the number of "
|
---|
63 | + "milliseconds.", be1.getMessage());
|
---|
64 | }
|
---|
65 |
|
---|
66 | s = (DateSelector)getInstance();
|
---|
67 | s.setDatetime("01/01/1969 01:01 AM");
|
---|
68 | try {
|
---|
69 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
70 | fail("DateSelector did not check for Datetime being in the "
|
---|
71 | + "allowable range");
|
---|
72 | } catch (BuildException be2) {
|
---|
73 | assertEquals("Date of 01/01/1969 01:01 AM results in negative "
|
---|
74 | + "milliseconds value relative to epoch (January 1, "
|
---|
75 | + "1970, 00:00:00 GMT).", be2.getMessage());
|
---|
76 | }
|
---|
77 |
|
---|
78 | s = (DateSelector)getInstance();
|
---|
79 | s.setDatetime("this is not a date");
|
---|
80 | try {
|
---|
81 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
82 | fail("DateSelector did not check for Datetime being in a "
|
---|
83 | + "valid format");
|
---|
84 | } catch (BuildException be3) {
|
---|
85 | assertEquals("Date of this is not a date"
|
---|
86 | + " Cannot be parsed correctly. It should be in"
|
---|
87 | + " MM/DD/YYYY HH:MM AM_PM format.", be3.getMessage());
|
---|
88 | }
|
---|
89 |
|
---|
90 | s = (DateSelector)getInstance();
|
---|
91 | Parameter param = new Parameter();
|
---|
92 | param.setName("garbage in");
|
---|
93 | param.setValue("garbage out");
|
---|
94 | Parameter[] params = new Parameter[1];
|
---|
95 | params[0] = param;
|
---|
96 | s.setParameters(params);
|
---|
97 | try {
|
---|
98 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
99 | fail("DateSelector did not check for valid parameter element");
|
---|
100 | } catch (BuildException be4) {
|
---|
101 | assertEquals("Invalid parameter garbage in", be4.getMessage());
|
---|
102 | }
|
---|
103 |
|
---|
104 | s = (DateSelector)getInstance();
|
---|
105 | param = new Parameter();
|
---|
106 | param.setName("millis");
|
---|
107 | param.setValue("garbage out");
|
---|
108 | params[0] = param;
|
---|
109 | s.setParameters(params);
|
---|
110 | try {
|
---|
111 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
112 | fail("DateSelector did not check for valid millis parameter");
|
---|
113 | } catch (BuildException be5) {
|
---|
114 | assertEquals("Invalid millisecond setting garbage out",
|
---|
115 | be5.getMessage());
|
---|
116 | }
|
---|
117 |
|
---|
118 | s = (DateSelector)getInstance();
|
---|
119 | param = new Parameter();
|
---|
120 | param.setName("granularity");
|
---|
121 | param.setValue("garbage out");
|
---|
122 | params[0] = param;
|
---|
123 | s.setParameters(params);
|
---|
124 | try {
|
---|
125 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
126 | fail("DateSelector did not check for valid granularity parameter");
|
---|
127 | } catch (BuildException be6) {
|
---|
128 | assertEquals("Invalid granularity setting garbage out",
|
---|
129 | be6.getMessage());
|
---|
130 | }
|
---|
131 |
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Tests to make sure that the selector is selecting files correctly.
|
---|
136 | */
|
---|
137 | public void testSelectionBehaviour() {
|
---|
138 | DateSelector s;
|
---|
139 | String results;
|
---|
140 |
|
---|
141 | DateSelector.TimeComparisons before = new
|
---|
142 | DateSelector.TimeComparisons();
|
---|
143 | before.setValue("before");
|
---|
144 | DateSelector.TimeComparisons equal = new
|
---|
145 | DateSelector.TimeComparisons();
|
---|
146 | equal.setValue("equal");
|
---|
147 | DateSelector.TimeComparisons after = new
|
---|
148 | DateSelector.TimeComparisons();
|
---|
149 | after.setValue("after");
|
---|
150 |
|
---|
151 | try {
|
---|
152 | makeBed();
|
---|
153 |
|
---|
154 | s = (DateSelector)getInstance();
|
---|
155 | s.setDatetime("10/10/1999 1:45 PM");
|
---|
156 | s.setWhen(before);
|
---|
157 | results = selectionString(s);
|
---|
158 | assertEquals("TFFFFFFFFFFT", results);
|
---|
159 |
|
---|
160 | s = (DateSelector)getInstance();
|
---|
161 | s.setDatetime("10/10/1999 1:45 PM");
|
---|
162 | s.setWhen(before);
|
---|
163 | s.setCheckdirs(true);
|
---|
164 | results = selectionString(s);
|
---|
165 | assertEquals("FFFFFFFFFFFF", results);
|
---|
166 |
|
---|
167 | s = (DateSelector)getInstance();
|
---|
168 | s.setDatetime("10/10/1999 1:45 PM");
|
---|
169 | s.setWhen(after);
|
---|
170 | results = selectionString(s);
|
---|
171 | assertEquals("TTTTTTTTTTTT", results);
|
---|
172 |
|
---|
173 | if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
|
---|
174 | s = (DateSelector)getInstance();
|
---|
175 | s.setDatetime("11/21/2001 4:54 AM");
|
---|
176 | s.setWhen(before);
|
---|
177 | results = selectionString(s);
|
---|
178 | assertEquals("TFTFFFFFFFFT", results);
|
---|
179 |
|
---|
180 | s = (DateSelector)getInstance();
|
---|
181 | s.setDatetime("11/21/2001 4:55 AM");
|
---|
182 | SimpleDateFormat formatter = new SimpleDateFormat();
|
---|
183 | Date d = formatter.parse("11/21/2001 4:55 AM",new ParsePosition(0));
|
---|
184 |
|
---|
185 | long milliseconds = s.getMillis();
|
---|
186 | s.setWhen(equal);
|
---|
187 | results = selectionString(s);
|
---|
188 | assertEquals("TTFFTFFFTTTT", results);
|
---|
189 |
|
---|
190 | s = (DateSelector)getInstance();
|
---|
191 | s.setMillis(milliseconds);
|
---|
192 | s.setWhen(equal);
|
---|
193 | results = selectionString(s);
|
---|
194 | assertEquals("TTFFTFFFTTTT", results);
|
---|
195 |
|
---|
196 | s = (DateSelector)getInstance();
|
---|
197 | s.setDatetime("11/21/2001 4:56 AM");
|
---|
198 | s.setWhen(after);
|
---|
199 | results = selectionString(s);
|
---|
200 | assertEquals("TFFTFTTTFFFT", results);
|
---|
201 |
|
---|
202 | s = (DateSelector)getInstance();
|
---|
203 | Parameter param1 = new Parameter();
|
---|
204 | Parameter param2 = new Parameter();
|
---|
205 | param1.setName("datetime");
|
---|
206 | param1.setValue("11/21/2001 4:56 AM");
|
---|
207 | param2.setName("when");
|
---|
208 | param2.setValue("after");
|
---|
209 | Parameter[] params = {param1,param2};
|
---|
210 | s.setParameters(params);
|
---|
211 | results = selectionString(s);
|
---|
212 | assertEquals("TFFTFTTTFFFT", results);
|
---|
213 | }
|
---|
214 | try {
|
---|
215 | makeMirror();
|
---|
216 |
|
---|
217 | s = (DateSelector)getInstance();
|
---|
218 | long testtime = mirrorfiles[5].lastModified();
|
---|
219 | s.setMillis(testtime);
|
---|
220 | s.setWhen(after);
|
---|
221 | s.setGranularity(2);
|
---|
222 | results = mirrorSelectionString(s);
|
---|
223 | assertEquals("TFFFFTTTTTTT", results);
|
---|
224 |
|
---|
225 | s = (DateSelector)getInstance();
|
---|
226 | testtime = mirrorfiles[6].lastModified();
|
---|
227 | s.setMillis(testtime);
|
---|
228 | s.setWhen(before);
|
---|
229 | s.setGranularity(2);
|
---|
230 | results = mirrorSelectionString(s);
|
---|
231 | assertEquals("TTTTTTTFFFFT", results);
|
---|
232 | }
|
---|
233 | finally {
|
---|
234 | cleanupMirror();
|
---|
235 | }
|
---|
236 |
|
---|
237 | }
|
---|
238 | finally {
|
---|
239 | cleanupBed();
|
---|
240 | }
|
---|
241 |
|
---|
242 | }
|
---|
243 |
|
---|
244 | }
|
---|