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: import java.text.SimpleDateFormat;
026: import java.text.ParsePosition;
027: import java.util.Date;
028:
029: /**
030: * Tests Date Selectors.
031: *
032: */
033: public class DateSelectorTest extends BaseSelectorTest {
034:
035: private Project project;
036:
037: public DateSelectorTest(String name) {
038: super (name);
039: }
040:
041: /**
042: * Factory method from base class. This is overriden in child
043: * classes to return a specific Selector class.
044: */
045: public BaseSelector getInstance() {
046: return new DateSelector();
047: }
048:
049: /**
050: * Test the code that validates the selector.
051: */
052: public void testValidate() {
053: DateSelector s = (DateSelector) getInstance();
054: try {
055: s.isSelected(basedir, filenames[0], files[0]);
056: fail("DateSelector did not check for required fields");
057: } catch (BuildException be1) {
058: assertEquals(
059: "You must provide a datetime or the number of "
060: + "milliseconds.", be1.getMessage());
061: }
062:
063: s = (DateSelector) getInstance();
064: s.setDatetime("01/01/1969 01:01 AM");
065: try {
066: s.isSelected(basedir, filenames[0], files[0]);
067: fail("DateSelector did not check for Datetime being in the "
068: + "allowable range");
069: } catch (BuildException be2) {
070: assertEquals(
071: "Date of 01/01/1969 01:01 AM results in negative "
072: + "milliseconds value relative to epoch (January 1, "
073: + "1970, 00:00:00 GMT).", be2.getMessage());
074: }
075:
076: s = (DateSelector) getInstance();
077: s.setDatetime("this is not a date");
078: try {
079: s.isSelected(basedir, filenames[0], files[0]);
080: fail("DateSelector did not check for Datetime being in a "
081: + "valid format");
082: } catch (BuildException be3) {
083: assertEquals("Date of this is not a date"
084: + " Cannot be parsed correctly. It should be in"
085: + " MM/DD/YYYY HH:MM AM_PM format.", be3
086: .getMessage());
087: }
088:
089: s = (DateSelector) getInstance();
090: Parameter param = new Parameter();
091: param.setName("garbage in");
092: param.setValue("garbage out");
093: Parameter[] params = new Parameter[1];
094: params[0] = param;
095: s.setParameters(params);
096: try {
097: s.isSelected(basedir, filenames[0], files[0]);
098: fail("DateSelector did not check for valid parameter element");
099: } catch (BuildException be4) {
100: assertEquals("Invalid parameter garbage in", be4
101: .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", be5
115: .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", be6
129: .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 DateSelector.TimeComparisons();
142: before.setValue("before");
143: DateSelector.TimeComparisons equal = new DateSelector.TimeComparisons();
144: equal.setValue("equal");
145: DateSelector.TimeComparisons after = new DateSelector.TimeComparisons();
146: after.setValue("after");
147:
148: try {
149: makeBed();
150:
151: s = (DateSelector) getInstance();
152: s.setDatetime("10/10/1999 1:45 PM");
153: s.setWhen(before);
154: results = selectionString(s);
155: assertEquals("TFFFFFFFFFFT", results);
156:
157: s = (DateSelector) getInstance();
158: s.setDatetime("10/10/1999 1:45 PM");
159: s.setWhen(before);
160: s.setCheckdirs(true);
161: results = selectionString(s);
162: assertEquals("FFFFFFFFFFFF", results);
163:
164: s = (DateSelector) getInstance();
165: s.setDatetime("10/10/1999 1:45 PM");
166: s.setWhen(after);
167: results = selectionString(s);
168: assertEquals("TTTTTTTTTTTT", results);
169:
170: s = (DateSelector) getInstance();
171: s.setDatetime("11/21/2001 4:54 AM");
172: s.setWhen(before);
173: results = selectionString(s);
174: assertEquals("TFTFFFFFFFFT", results);
175:
176: s = (DateSelector) getInstance();
177: s.setDatetime("11/21/2001 4:55 AM");
178: SimpleDateFormat formatter = new SimpleDateFormat();
179: Date d = formatter.parse("11/21/2001 4:55 AM",
180: new ParsePosition(0));
181:
182: long milliseconds = s.getMillis();
183: s.setWhen(equal);
184: results = selectionString(s);
185: assertEquals("TTFFTFFFTTTT", results);
186:
187: s = (DateSelector) getInstance();
188: s.setMillis(milliseconds);
189: s.setWhen(equal);
190: results = selectionString(s);
191: assertEquals("TTFFTFFFTTTT", results);
192:
193: s = (DateSelector) getInstance();
194: s.setDatetime("11/21/2001 4:56 AM");
195: s.setWhen(after);
196: results = selectionString(s);
197: assertEquals("TFFTFTTTFFFT", results);
198:
199: s = (DateSelector) getInstance();
200: Parameter param1 = new Parameter();
201: Parameter param2 = new Parameter();
202: param1.setName("datetime");
203: param1.setValue("11/21/2001 4:56 AM");
204: param2.setName("when");
205: param2.setValue("after");
206: Parameter[] params = { param1, param2 };
207: s.setParameters(params);
208: results = selectionString(s);
209: assertEquals("TFFTFTTTFFFT", results);
210: try {
211: makeMirror();
212:
213: s = (DateSelector) getInstance();
214: long testtime = mirrorfiles[5].lastModified();
215: s.setMillis(testtime);
216: s.setWhen(after);
217: s.setGranularity(2);
218: results = mirrorSelectionString(s);
219: assertEquals("TFFFFTTTTTTT", results);
220:
221: s = (DateSelector) getInstance();
222: testtime = mirrorfiles[6].lastModified();
223: s.setMillis(testtime);
224: s.setWhen(before);
225: s.setGranularity(2);
226: results = mirrorSelectionString(s);
227: assertEquals("TTTTTTTFFFFT", results);
228: } finally {
229: cleanupMirror();
230: }
231:
232: } finally {
233: cleanupBed();
234: }
235:
236: }
237:
238: }
|