KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestDateValidator.java,v 1.10 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
29 import org.enhydra.barracuda.core.forms.*;
30 import org.apache.log4j.*;
31 import org.enhydra.barracuda.plankton.data.*;
32 import org.enhydra.barracuda.testbed.*;
33
34 /**
35  * This test verifies that DateValidator works correctly.
36  */

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

45     //-------------------- Basics --------------------------------
46

47     /**
48      * Public Constructor
49      */

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

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

100     /**
101      * Test String types
102      */

103     public void testString() {
104         logger.info("Locale:"+Locale.getDefault());
105
106         DateValidator v = new DateValidator();
107         DefaultFormElement el = new DefaultFormElement("key", FormType.STRING);
108
109         // ilc_022502.1_start
110
// added going through the mapping process
111
DefaultStateMap sm = new DefaultStateMap();
112         sm.putState("InvalidString1", "foo");
113         sm.putState("InvalidString2", TestUtil.dateStringInDefaultLocaleShortForm("2000", "1", "32"));
114         sm.putState("InvalidString3", TestUtil.dateStringInDefaultLocaleShortForm("1999", "13", "1"));
115         sm.putState("InvalidString4", TestUtil.dateStringInDefaultLocaleShortForm("2001", "2", "29"));
116         sm.putState("InvalidString5", TestUtil.dateStringInDefaultLocaleShortForm("2000", "01", "32"));
117         sm.putState("InvalidString6", TestUtil.dateStringInDefaultLocaleShortForm("1999", "13", "01"));
118         sm.putState("InvalidString7", TestUtil.dateStringInDefaultLocaleShortForm("2001", "02", "29"));
119         sm.putState("InvalidString8", TestUtil.dateStringInDefaultLocaleShortForm("2002", "01", "32"));
120         sm.putState("InvalidString9", TestUtil.dateStringInDefaultLocaleShortForm("2010", "13", "01"));
121         sm.putState("InvalidString10", TestUtil.dateStringInDefaultLocaleShortForm("2013", "02", "29"));
122
123
124         this.assertAllInvalid(v, sm, FormType.STRING);
125
126         sm = null;
127         sm = new DefaultStateMap();
128         sm.putState("ValidString1", "");
129         sm.putState("ValidString2", null);
130         sm.putState("ValidString3", TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1"));
131         sm.putState("ValidString4", TestUtil.dateStringInDefaultLocaleShortForm("2005", "1", "1"));
132         sm.putState("ValidString5", TestUtil.dateStringInDefaultLocaleShortForm("2001", "12", "1"));
133         sm.putState("ValidString6", TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29"));
134         sm.putState("ValidString7", TestUtil.dateStringInDefaultLocaleShortForm("2004", "2", "29"));
135         sm.putState("ValidString8", TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31"));
136         sm.putState("ValidString9", TestUtil.dateStringInDefaultLocaleShortForm("2010", "12", "31"));
137         sm.putState("ValidString10", TestUtil.dateStringInDefaultLocaleShortForm("2001", "01", "01"));
138         sm.putState("ValidString11", TestUtil.dateStringInDefaultLocaleShortForm("2005", "01", "01"));
139         sm.putState("ValidString12", TestUtil.dateStringInDefaultLocaleShortForm("2001", "12", "01"));
140         sm.putState("ValidString13", TestUtil.dateStringInDefaultLocaleShortForm("2000", "02", "29"));
141         sm.putState("ValidString14", TestUtil.dateStringInDefaultLocaleShortForm("2004", "02", "29"));
142
143         this.assertAllValid(v, sm, FormType.STRING);
144     }
145
146     public void testLong() {
147 /*
148         DateValidator v = new DateValidator();
149         DefaultFormElement el = new DefaultFormElement("key", FormType.LONG);
150
151         // ilc_022502.3_start
152         // do testing by going through the mapping process
153         DefaultStateMap sm = new DefaultStateMap();
154
155         sm.putState("InvalidLong1", new Long(0));
156         sm.putState("InvalidLong1", new Long(1));
157         sm.putState("InvalidLong1", new Long(-1));
158         sm.putState("InvalidLong1", new Long(Long.MIN_VALUE));
159         sm.putState("InvalidLong1", new Long(Long.MAX_VALUE));
160
161         this.assertAllInvalid(v, sm, FormType.LONG);
162 */

163     }
164     
165     public void testBoolean() {
166 /*
167         DateValidator v = new DateValidator();
168         DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN);
169
170         // ilc_022502.5_start
171         // use a StateMap for testing to make sure elements go through mapping
172         DefaultStateMap sm = new DefaultStateMap();
173
174         sm.putState("BooleanInvalid1", Boolean.FALSE);
175         sm.putState("BooleanInvalid2", Boolean.TRUE);
176
177         this.assertAllInvalid(v, sm, FormType.BOOLEAN);
178
179         sm = null;
180         sm = new DefaultStateMap();
181         sm.putState("BooleanValid1", null);
182         this.assertAllValid(v, sm, FormType.BOOLEAN);
183
184         // ilc_022502.5_end
185 */

186     }
187     
188     public void testShort() {
189 /*
190         DateValidator v = new DateValidator();
191         DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT);
192         // ilc_022502.7_start
193         // use a StateMap for testing to make sure elements go through mapping
194         DefaultStateMap sm = new DefaultStateMap();
195
196         sm.putState("ShortInvalid1", new Short((short)0));
197         sm.putState("ShortInvalid2", new Short((short)1));
198         sm.putState("ShortInvalid3", new Short((short)-1));
199         sm.putState("ShortInvalid4", new Short(Short.MIN_VALUE));
200         sm.putState("ShortInvalid5", new Short(Short.MAX_VALUE));
201
202
203         this.assertAllInvalid(v, sm, FormType.SHORT);
204
205         sm = null;
206         sm = new DefaultStateMap();
207         sm.putState("ShortValid1", null);
208         this.assertAllValid(v, sm, FormType.SHORT);
209
210         // ilc_022502.7_end
211 */

212     }
213     
214     public void testInteger() {
215 /*
216         DateValidator v = new DateValidator();
217         DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER);
218         // ilc_022502.9_start
219         // use a StateMap for testing to make sure elements go through mapping
220         DefaultStateMap sm = new DefaultStateMap();
221
222         sm.putState("IntegerInvalid1", new Integer(0));
223         sm.putState("IntegerInvalid2", new Integer(1));
224         sm.putState("IntegerInvalid3", new Integer(-1));
225         sm.putState("IntegerInvalid4", new Integer(Integer.MIN_VALUE));
226         sm.putState("IntegerInvalid5", new Integer(Integer.MAX_VALUE));
227
228         this.assertAllInvalid(v, sm, FormType.INTEGER);
229
230         sm = null;
231         sm = new DefaultStateMap();
232         sm.putState("IntegerValid1", null);
233         this.assertAllValid(v, sm, FormType.INTEGER);
234
235         // ilc_022502.9_end
236 */

237     }
238     
239     public void testDate() {
240 /*
241         DateValidator v = new DateValidator();
242         DefaultFormElement el = new DefaultFormElement("key", FormType.DATE);
243
244         DateFormat aDateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
245         aDateFormat.setLenient(false);
246
247
248         // ilc_022202.11_start
249         // use a StateMap for testing to make sure elements go through mapping
250         DefaultStateMap sm = new DefaultStateMap();
251
252         try {
253           sm.putState("DateValid1", null);
254           sm.putState("DateValid1", new Date());
255           sm.putState("DateValid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1")));
256           sm.putState("DateValid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2005", "1", "1")));
257           sm.putState("DateValid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29")));
258           sm.putState("DateValid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2004", "2", "29")));
259           sm.putState("DateValid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("1999", "12", "31")));
260           sm.putState("DateValid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2001", "01", "01")));
261           sm.putState("DateValid1", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2005", "01", "1")));
262
263         } catch(java.text.ParseException ex) {
264           fail( "testDate failed because " + ex.getMessage() );
265         }
266         // ilc_022502.11_end
267 */

268     }
269     
270     public void testDouble() {
271 /*
272         DateValidator v = new DateValidator();
273         DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE);
274         // ilc_022502.13_start
275         // use a StateMap for testing to make sure elements go through mapping
276         DefaultStateMap sm = new DefaultStateMap();
277
278         sm.putState("DoubleInvalid1", new Double(0));
279         sm.putState("DoubleInvalid2", new Double(1));
280         sm.putState("DoubleInvalid3", new Double(-1));
281         sm.putState("DoubleInvalid4", new Double(Double.MIN_VALUE));
282         sm.putState("DoubleInvalid5", new Double(Double.MAX_VALUE));
283
284         this.assertAllInvalid(v, sm, FormType.DOUBLE);
285
286         sm = null;
287         sm = new DefaultStateMap();
288         sm.putState("DoubleValid1", null);
289         this.assertAllValid(v, sm, FormType.DOUBLE);
290
291         // ilc_022502.13_end
292 */

293     }
294     
295     public void testFloat() {
296 /*
297         DateValidator v = new DateValidator();
298         DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT);
299         // ilc_022502.15_start
300         // use a StateMap for testing to make sure elements go through mapping
301         DefaultStateMap sm = new DefaultStateMap();
302
303         sm.putState("FloatInvalid1", new Float(0));
304         sm.putState("FloatInvalid2", new Float(1));
305         sm.putState("FloatInvalid3", new Float(-1));
306         sm.putState("FloatInvalid4", new Float(Float.MIN_VALUE));
307         sm.putState("FloatInvalid5", new Float(Float.MAX_VALUE));
308
309         this.assertAllInvalid(v, sm, FormType.FLOAT);
310
311         sm = null;
312         sm = new DefaultStateMap();
313         sm.putState("FloatValid1", null);
314         this.assertAllValid(v, sm, FormType.FLOAT);
315
316         // ilc_022502.15_end
317 */

318     }
319 }
320
Popular Tags