KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > validators > TestDateRangeValidator


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestDateRangeValidator.java,v 1.12 2004/02/01 05:16:33 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms.validators;
21
22 import java.io.*;
23 import java.util.*;
24 import java.text.*;
25
26 import org.w3c.dom.*;
27 import junit.framework.*;
28 import org.apache.log4j.*;
29
30 import org.enhydra.barracuda.core.forms.*;
31 import org.enhydra.barracuda.plankton.data.*;
32 import org.enhydra.barracuda.plankton.DateUtil;
33 import org.enhydra.barracuda.testbed.*;
34
35 /**
36  * This test verifies that DateRangeValidator works correctly.
37  */

38 public class TestDateRangeValidator extends ValidatorTestCase {
39
40     //common vars (customize for every test class)
41
private static String JavaDoc testClass = TestDateRangeValidator.class.getName();
42     private static Logger logger = Logger.getLogger("test."+testClass);
43
44     //variables
45

46     //-------------------- Basics --------------------------------
47

48     /**
49      * Public Constructor
50      */

51     public TestDateRangeValidator(String JavaDoc name) {
52         super(name);
53     }
54     
55     /**
56      * Every test class should have a main method so it can be run
57      * directly (when debugging tests you often will not want to run
58      * the whole suite)
59      *
60      * @param args defined in test.util.TestUtil
61      */

62     public static void main(String JavaDoc args[]) {
63         //check for standard runtime parameters
64
TestUtil.parseParams(args);
65
66         //launch the test
67
if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String JavaDoc[] {testClass});
68         else junit.swingui.TestRunner.main(new String JavaDoc[] {testClass});
69     }
70
71     public static Test suite() {
72
73         TestSuite allLocalesSuite = new TestSuite();
74
75         Locale[] availableLocales = Locale.getAvailableLocales();
76         for (int i = 0; i < availableLocales.length; i++) {
77             //csc_082603_2
78
//ok, skip the thai locale until we have someone who knows enough about it to be able
79
//to say what are valid dates and what aren't. Thai uses a solar calendar, which means
80
//what we consider to be leap years aren't. This was causing test cases to fail, and since
81
//I don't know enough about thai to know what's actually correct, for now I am just removing
82
//this from the test case.
83
if (availableLocales[i].getLanguage().equals("th") && availableLocales[i].getCountry().equals("TH")) continue;
84             
85             //build the test suite
86
TestSuite suite = new TestSuite();
87             suite.addTestSuite( TestDateRangeValidator.class );
88             allLocalesSuite.addTest( new LocaleTestSuiteSetup( suite, availableLocales[i]) );
89             logger.info("Adding:" + availableLocales[i].getCountry() );
90         }
91
92         return allLocalesSuite;
93     }
94     
95     //-------------------- Actual Tests --------------------------
96
//Note: all the methods herein should follow the testXXXX naming convention
97
//Also keep in mind that local vars set in one test method are NOT retained
98
//when the next method is invoked because JUnit makes a separate instance of
99
//the test class for each testXXXX method!!!
100

101     /**
102      * Test String types
103      */

104     public void testString() {
105         DateFormat aDateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
106         aDateFormat.setLenient(false);
107         DefaultFormElement el = new DefaultFormElement("key", FormType.STRING);
108
109         Date now = new Date();
110         DateRangeValidator afterNowValidator = new DateRangeValidator(now,null);
111
112         // ilc_022202.1_start
113
// use a StateMap for testing to make sure elements go through mapping
114
DefaultStateMap sm = new DefaultStateMap();
115
116         sm.putState("StringAfterNowInvalid1", "foo");
117         sm.putState("StringAfterNowInvalid2", TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1"));
118         sm.putState("StringAfterNowInvalid3", TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29"));
119         sm.putState("StringAfterNowInvalid4", TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31"));
120         // now should pass a range of today..null
121
//new String( aDateFormat.format( now ) ),
122
sm.putState("s2", new String JavaDoc( aDateFormat.format( DateUtil.getLastDateYesterday().getTime())));
123
124         this.assertAllInvalid(afterNowValidator, sm, FormType.STRING);
125
126         sm = null;
127         sm = new DefaultStateMap();
128         sm.putState("StringAfterNowValid1", null);
129         sm.putState("StringAfterNowValid2", "");
130         sm.putState("StringAfterNowValid3", TestUtil.dateStringInDefaultLocaleShortForm("2033", "1", "1"));
131         sm.putState("StringAfterNowValid4", TestUtil.dateStringInDefaultLocaleShortForm("2036", "2", "29"));
132         sm.putState("StringAfterNowValid5", TestUtil.dateStringInDefaultLocaleShortForm("2999", "12", "31"));
133         sm.putState("StringAfterNowValid6", new String JavaDoc( aDateFormat.format( DateUtil.getFirstDateTomorrow().getTime())));
134
135         this.assertAllValid(afterNowValidator, sm, FormType.STRING);
136
137
138
139         DateRangeValidator beforeNowValidator = new DateRangeValidator(null,now);
140
141         sm = null;
142         sm = new DefaultStateMap();
143         //isNull test strips characters
144
//sm.putState("StringBeforeNowInValid1", " ");
145
sm.putState("StringBeforeNowInValid1", "foo");
146         sm.putState("StringBeforeNowInValid2", TestUtil.dateStringInDefaultLocaleShortForm("2033", "1", "1"));
147         sm.putState("StringBeforeNowInValid3", TestUtil.dateStringInDefaultLocaleShortForm("2036", "2", "29"));
148         sm.putState("StringBeforeNowInValid4", TestUtil.dateStringInDefaultLocaleShortForm("2999", "12", "31"));
149         sm.putState("StringBeforeNowInValid5", new String JavaDoc( aDateFormat.format( DateUtil.getFirstDateTomorrow().getTime())));
150
151         this.assertAllInvalid(beforeNowValidator, sm, FormType.STRING);
152
153         sm = null;
154         sm = new DefaultStateMap();
155         sm.putState("StringBeforeNowValid1", null);
156         sm.putState("StringBeforeNowValid2", "");
157         sm.putState("StringBeforeNowValid3", TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1"));
158         sm.putState("StringBeforeNowValid4", TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29"));
159         sm.putState("StringBeforeNowValid5", TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31"));
160         sm.putState("StringBeforeNowValid6", new String JavaDoc( aDateFormat.format( now )));
161         sm.putState("StringBeforeNowValid7", new String JavaDoc( aDateFormat.format( DateUtil.getLastDateYesterday().getTime())));
162
163         this.assertAllValid(beforeNowValidator, sm, FormType.STRING);
164
165
166
167         DateRangeValidator todayValidator = new DateRangeValidator( DateUtil.getFirstDateToday().getTime(),
168                                                                     DateUtil.getLastDateToday().getTime() );
169         sm = null;
170         sm = new DefaultStateMap();
171         //isNull test strips characters
172
//sm.putState("StringTodayInvalid1", " ");
173
sm.putState("StringTodayInvalid1", "foo");
174         sm.putState("StringTodayInvalid2", TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1"));
175         sm.putState("StringTodayInvalid3", TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29"));
176         sm.putState("StringTodayInvalid4", TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31"));
177         sm.putState("StringTodayInvalid5", TestUtil.dateStringInDefaultLocaleShortForm("2036", "2", "29"));
178         sm.putState("StringTodayInvalid6", new String JavaDoc( aDateFormat.format( DateUtil.getLastDateYesterday().getTime())));
179         sm.putState("StringTodayInvalid7", new String JavaDoc( aDateFormat.format( DateUtil.getFirstDateTomorrow().getTime())));
180
181         this.assertAllInvalid(todayValidator, sm, FormType.STRING);
182
183         sm = null;
184         sm = new DefaultStateMap();
185         sm.putState("StringTodayValid1", null);
186         sm.putState("StringTodayValid2", "");
187         sm.putState("StringTodayValid3", new String JavaDoc( aDateFormat.format( new Date())));
188         sm.putState("StringTodayValid4", new String JavaDoc( aDateFormat.format( DateUtil.getFirstDateToday().getTime())));
189         sm.putState("StringTodayValid5", new String JavaDoc( aDateFormat.format( DateUtil.getLastDateToday().getTime())));
190
191         this.assertAllValid(todayValidator, sm, FormType.STRING);
192         // ilc_022202.1_end
193
}
194
195     public void testLong() {
196         DateRangeValidator v = new DateRangeValidator(new Date(),null);
197         DefaultFormElement el = new DefaultFormElement("key", FormType.LONG);
198
199         // ilc_022202.3_start
200
// use a StateMap for testing to make sure elements go through mapping
201
DefaultStateMap sm = new DefaultStateMap();
202
203         sm.putState("LongInvalid1", new Long JavaDoc(0));
204         sm.putState("LongInvalid2", new Long JavaDoc(1));
205         sm.putState("LongInvalid3", new Long JavaDoc(-1));
206         sm.putState("LongInvalid4", new Long JavaDoc(Long.MIN_VALUE));
207         sm.putState("LongInvalid5", new Long JavaDoc(Long.MAX_VALUE));
208
209         this.assertAllInvalid(v, sm, FormType.LONG);
210
211         sm = null;
212         sm = new DefaultStateMap();
213         sm.putState("LongValid1", null);
214         this.assertAllValid(v, sm, FormType.LONG);
215         // ilc_022202.3_end
216
}
217     
218     public void testBoolean() {
219         DateRangeValidator v = new DateRangeValidator(new Date(),null);
220         DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN);
221
222         // ilc_022202.5_start
223
// use a StateMap for testing to make sure elements go through mapping
224
DefaultStateMap sm = new DefaultStateMap();
225
226         sm.putState("BooleanInvalid1", Boolean.FALSE);
227         sm.putState("BooleanInvalid2", Boolean.TRUE);
228
229         this.assertAllInvalid(v, sm, FormType.BOOLEAN);
230
231         sm = null;
232         sm = new DefaultStateMap();
233         sm.putState("BooleanValid1", null);
234         this.assertAllValid(v, sm, FormType.BOOLEAN);
235         // ilc_022202.5_end
236
}
237
238     public void testShort() {
239         DateRangeValidator v = new DateRangeValidator(new Date(),null);
240         DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT);
241
242         // ilc_022202.7_start
243
// use a StateMap for testing to make sure elements go through mapping
244
DefaultStateMap sm = new DefaultStateMap();
245
246         sm.putState("ShortInvalid1", new Short JavaDoc((short)0));
247         sm.putState("ShortInvalid2", new Short JavaDoc((short)1));
248         sm.putState("ShortInvalid3", new Short JavaDoc((short)-1));
249         sm.putState("ShortInvalid4", new Short JavaDoc(Short.MIN_VALUE));
250         sm.putState("ShortInvalid5", new Short JavaDoc(Short.MAX_VALUE));
251
252
253         this.assertAllInvalid(v, sm, FormType.SHORT);
254
255         sm = null;
256         sm = new DefaultStateMap();
257         sm.putState("ShortValid1", null);
258         this.assertAllValid(v, sm, FormType.SHORT);
259         // ilc_022202.7_end
260
}
261     
262     public void testInteger() {
263         DateRangeValidator v = new DateRangeValidator(new Date(),null);
264         DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER);
265         // ilc_022202.9_start
266
// use a StateMap for testing to make sure elements go through mapping
267
DefaultStateMap sm = new DefaultStateMap();
268
269         sm.putState("IntegerInvalid1", new Integer JavaDoc(0));
270         sm.putState("IntegerInvalid2", new Integer JavaDoc(1));
271         sm.putState("IntegerInvalid3", new Integer JavaDoc(-1));
272         sm.putState("IntegerInvalid4", new Integer JavaDoc(Integer.MIN_VALUE));
273         sm.putState("IntegerInvalid5", new Integer JavaDoc(Integer.MAX_VALUE));
274
275         this.assertAllInvalid(v, sm, FormType.INTEGER);
276
277         sm = null;
278         sm = new DefaultStateMap();
279         sm.putState("IntegerValid1", null);
280         this.assertAllValid(v, sm, FormType.INTEGER);
281         // ilc_022202.9_end
282
}
283     
284     public void testDate() {
285         DateFormat aDateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
286         aDateFormat.setLenient(false);
287         DefaultFormElement el = new DefaultFormElement("key", FormType.DATE);
288
289         Date now = new Date();
290         DateRangeValidator afterNowValidator = new DateRangeValidator(now,null);
291
292         // ilc_022202.11_start
293
// use a StateMap for testing to make sure elements go through mapping
294
DefaultStateMap sm = new DefaultStateMap();
295
296         try {
297           sm.putState("DateAfterNowInvalid1", new Date(now.getTime()-1));
298           sm.putState("DateAfterNowInvalid2", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1")));
299           sm.putState("DateAfterNowInvalid3", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29")));
300           sm.putState("DateAfterNowInvalid4", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31")));
301
302           this.assertAllInvalid(afterNowValidator, sm, FormType.DATE);
303
304           sm = null;
305           sm = new DefaultStateMap();
306           sm.putState("DateAfterNowValid1", null);
307           sm.putState("DateAfterNowValid2", new Date());
308           sm.putState("DateAfterNowValid3", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2033", "1", "1")));
309           sm.putState("DateAfterNowValid4", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2036", "2", "29")));
310           sm.putState("DateAfterNowValid5", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2999", "12", "31")));
311
312           this.assertAllValid(afterNowValidator, sm, FormType.DATE);
313
314
315
316           DateRangeValidator beforeNowValidator = new DateRangeValidator(null,now);
317
318           sm = null;
319           sm = new DefaultStateMap();
320           sm.putState("DateBeforeNowInValid1", new Date(now.getTime()+1));
321           sm.putState("DateBeforeNowInValid2", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2033", "1", "1")));
322           sm.putState("DateBeforeNowInValid3", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2036", "2", "29")));
323           sm.putState("DateBeforeNowInValid4", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2999", "12", "31")));
324
325           this.assertAllInvalid(beforeNowValidator, sm, FormType.DATE);
326
327           sm = null;
328           sm = new DefaultStateMap();
329           sm.putState("DateBeforeNowValid1", null);
330           sm.putState("DateBeforeNowValid2", new Date(now.getTime()-1));
331           sm.putState("DateBeforeNowValid3", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1")));
332           sm.putState("DateBeforeNowValid4", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29")));
333           sm.putState("DateBeforeNowValid5", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31")));
334
335           this.assertAllValid(beforeNowValidator, sm, FormType.DATE);
336
337
338
339           DateRangeValidator todayValidator = new DateRangeValidator( DateUtil.getFirstDateToday().getTime(),
340                                                                       DateUtil.getLastDateToday().getTime() );
341           sm = null;
342           sm = new DefaultStateMap();
343           sm.putState("DateTodayInvalid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1")));
344           sm.putState("DateTodayInvalid2", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29")));
345           sm.putState("DateTodayInvalid3", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31")));;
346           sm.putState("DateTodayInvalid4", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2036", "2", "29")));
347           sm.putState("DateTodayInvalid5", DateUtil.getLastDateYesterday().getTime());
348           sm.putState("DateTodayInvalid6", DateUtil.getFirstDateTomorrow().getTime());
349
350           this.assertAllInvalid(todayValidator, sm, FormType.DATE);
351
352           sm = null;
353           sm = new DefaultStateMap();
354           sm.putState("DateTodayValid1", null);
355           sm.putState("DateTodayValid2", new Date());
356           sm.putState("DateTodayValid3", DateUtil.getFirstDateToday().getTime());
357           sm.putState("DateTodayValid4", DateUtil.getLastDateToday().getTime());
358
359           this.assertAllValid(todayValidator, sm, FormType.DATE);
360
361         } catch(java.text.ParseException JavaDoc ex) {
362           fail( "testDate failed for locale:"+Locale.getDefault()+" because " + ex.getMessage() );
363         }
364         // ilc_022202.11_end
365
}
366     
367     public void testDouble() {
368         DateRangeValidator v = new DateRangeValidator(new Date(),null);
369         DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE);
370         // ilc_022202.13_start
371
// use a StateMap for testing to make sure elements go through mapping
372
DefaultStateMap sm = new DefaultStateMap();
373
374         sm.putState("DoubleInvalid1", new Double JavaDoc(0));
375         sm.putState("DoubleInvalid2", new Double JavaDoc(1));
376         sm.putState("DoubleInvalid3", new Double JavaDoc(-1));
377         sm.putState("DoubleInvalid4", new Double JavaDoc(Double.MIN_VALUE));
378         sm.putState("DoubleInvalid5", new Double JavaDoc(Double.MAX_VALUE));
379
380         this.assertAllInvalid(v, sm, FormType.DOUBLE);
381
382         sm = null;
383         sm = new DefaultStateMap();
384         sm.putState("DoubleValid1", null);
385         this.assertAllValid(v, sm, FormType.DOUBLE);
386         // ilc_022202.13_end
387
}
388     
389     public void testFloat() {
390         DateRangeValidator v = new DateRangeValidator(new Date(),null);
391         DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT);
392
393         // ilc_022202.15_start
394
// use a StateMap for testing to make sure elements go through mapping
395
DefaultStateMap sm = new DefaultStateMap();
396
397         sm.putState("FloatInvalid1", new Float JavaDoc(0));
398         sm.putState("FloatInvalid2", new Float JavaDoc(1));
399         sm.putState("FloatInvalid3", new Float JavaDoc(-1));
400         sm.putState("FloatInvalid4", new Float JavaDoc(Float.MIN_VALUE));
401         sm.putState("FloatInvalid5", new Float JavaDoc(Float.MAX_VALUE));
402
403         this.assertAllInvalid(v, sm, FormType.FLOAT);
404
405         sm = null;
406         sm = new DefaultStateMap();
407         sm.putState("FloatValid1", null);
408         this.assertAllValid(v, sm, FormType.FLOAT);
409         // ilc_022202.15_end
410
}
411
412 }
413
Popular Tags