KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

45     //-------------------- Basics --------------------------------
46
/**
47      * Public Constructor
48      */

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

60     public static void main(String JavaDoc args[]) {
61         //check for standard runtime parameters
62
TestUtil.parseParams(args);
63
64         //launch the test
65
if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String JavaDoc[] {testClass});
66         else junit.swingui.TestRunner.main(new String JavaDoc[] {testClass});
67     }
68
69     
70     //-------------------- Actual Tests --------------------------
71
//Note: all the methods herein should follow the testXXXX naming convention
72
//Also keep in mind that local vars set in one test method are NOT retained
73
//when the next method is invoked because JUnit makes a separate instance of
74
//the test class for each testXXXX method!!!
75

76     /**
77      * Test String types
78      */

79     public void testString() {
80         //start by building a statemap
81
DefaultStateMap sm = new DefaultStateMap();
82         sm.putState("val_s1","foo");
83         sm.putState("val_s2","null");
84         sm.putState("val_s3",new Integer JavaDoc(123));
85         //ilc_022702.1_start
86
// use assertAll* methods
87
ValidTypeValidator v = new ValidTypeValidator();
88         this.assertAllValid(v, sm, FormType.STRING);
89
90         //checkIt(sm, FormType.STRING);
91
//ilc_022702.1_end
92
/*
93         //now lets create a form map
94         DefaultFormMap fm = new DefaultFormMap();
95         Iterator it = sm.getKeys().iterator();
96         while (it.hasNext()) {
97             String key = (String) it.next();
98             fm.defineElement(new DefaultFormElement(key, FormType.STRING, null, new ValidTypeValidator()));
99         }
100
101         //mow map the StateMap into the FormMap
102         fm.map(sm);
103         
104         //now get all the form elements
105         it = sm.getKeys().iterator();
106         while (it.hasNext()) {
107             String key = (String) it.next();
108             DefaultFormElement el = (DefaultFormElement) fm.getElement(key);
109             DefaultFormValidator v = (DefaultFormValidator) el.getValidator();
110             if (key.startsWith("val")) assertValid("Error validating element for key:"+key, v, el, null);
111             else assertInvalid("Error invalidating element for key:"+key, v, el, null);
112         }
113 */

114     }
115     
116     /**
117      * Test Boolean types - this test ensures that if we pass in a
118      * null FormElement or try to validate the length on a Boolean
119      * it will always generate a ValidationException
120      */

121     public void testBoolean() {
122         //start by building a statemap
123
DefaultStateMap sm = new DefaultStateMap();
124         sm.putState("val_b1","yes");
125         sm.putState("val_b2","no");
126         sm.putState("val_b3","true");
127         sm.putState("val_b4","false");
128         sm.putState("val_b5","on");
129         sm.putState("val_b6","off");
130
131         // ilc_022702.2_start
132
// use the assertAllValid/Invalid methods
133
ValidTypeValidator v = new ValidTypeValidator();
134
135         this.assertAllValid(v, sm, FormType.BOOLEAN);
136
137         sm = null;
138         sm = new DefaultStateMap();
139
140         sm.putState("inval_b7","boogle");
141         sm.putState("inval_b8","blah");
142         sm.putState("inval_b9","foo");
143         sm.putState("inval_b10","bar");
144         this.assertAllInvalid(v, sm, FormType.BOOLEAN);
145         // use ValidatorTestCase.assertAll*** method instead
146
//checkIt(sm, FormType.BOOLEAN);
147
// ilc_022702.2_end
148

149 /*
150         //now lets create a form map
151         DefaultFormMap fm = new DefaultFormMap();
152         Iterator it = sm.getKeys().iterator();
153         while (it.hasNext()) {
154             String key = (String) it.next();
155             fm.defineElement(new DefaultFormElement(key, FormType.BOOLEAN, null, new ValidTypeValidator()));
156         }
157
158         //mow map the StateMap into the FormMap
159         fm.map(sm);
160         
161         //now get all the form elements
162         it = sm.getKeys().iterator();
163         while (it.hasNext()) {
164             String key = (String) it.next();
165             DefaultFormElement el = (DefaultFormElement) fm.getElement(key);
166             DefaultFormValidator v = (DefaultFormValidator) el.getValidator();
167             if (key.startsWith("val")) assertValid("Error validating element for key:"+key, v, el, null);
168             else assertInvalid("Error invalidating element for key:"+key, v, el, null);
169         }
170 */

171     }
172     
173     /**
174      * Test Integer types
175      */

176     public void testInteger() {
177         //start by building a statemap
178
DefaultStateMap sm = new DefaultStateMap();
179         sm.putState("val_i1","123");
180         sm.putState("val_i2","123.0");
181         sm.putState("val_i4",new Integer JavaDoc(123));
182         sm.putState("val_i5",new Float JavaDoc(123));
183         sm.putState("val_i6",new Double JavaDoc(123));
184         sm.putState("val_i8",new Short JavaDoc((short) 123));
185
186
187
188         // ilc_022702.3_start
189
// use the assertAllValid/Invalid methods
190
ValidTypeValidator v = new ValidTypeValidator();
191
192         this.assertAllValid(v, sm, FormType.INTEGER);
193
194         sm = null;
195         sm = new DefaultStateMap();
196         sm.putState("inval_i1","3.141592654");
197         sm.putState("inval_i2","13b");
198         sm.putState("inval_i3",new Long JavaDoc(2147483648L));
199         this.assertAllInvalid(v, sm, FormType.INTEGER);
200
201         //checkIt(sm, FormType.INTEGER);
202
// ilc_022702.3_end
203

204
205 // sm.put("val_i3","true");
206
// sm.put("val_i4","false");
207
// sm.put("val_i5","on");
208
// sm.put("val_i6","off");
209
// sm.put("val_i7",null);
210
// sm.put("inval_i8","boogle");
211
// sm.put("inval_i9","blah");
212
// sm.put("inval_i10","foo");
213

214
215     }
216
217     /**
218      * Test Date types - this test ensures that if we try to validate
219      * the length on a Date it will always generate a ValidationException
220      */

221     public void testDate() {
222       //ilc_022702.4_start
223
DefaultStateMap sm = new DefaultStateMap();
224       ValidTypeValidator v = new ValidTypeValidator();
225       DateFormat aDateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
226       aDateFormat.setLenient(false);
227
228
229       try {
230         sm.putState("Valid Date1", TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1"));
231         sm.putState("Valid Date2", TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29"));
232         sm.putState("Valid Date3", TestUtil.dateStringInDefaultLocaleShortForm("2010", "12", "31"));
233         sm.putState("Valid Date4", TestUtil.dateStringInDefaultLocaleShortForm("2010", "12", "31"));
234         sm.putState("Valid Date5", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2001", "1", "1")));
235         sm.putState("Valid Date6", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2000", "2", "29")));
236         sm.putState("Valid Date5", aDateFormat.parse(TestUtil.dateStringInDefaultLocaleShortForm("2010", "12", "31")));
237       } catch(java.text.ParseException JavaDoc ex) {
238         fail( "testDate failed because " + ex.getMessage() );
239       }
240
241       this.assertAllValid(v, sm, FormType.DATE);
242
243       //ilc_022702.4_end
244
}
245
246     /**
247      * Test Long types
248      */

249     public void testLong() {
250       //ilc_022702.5_start
251
DefaultStateMap sm = new DefaultStateMap();
252       ValidTypeValidator v = new ValidTypeValidator();
253
254       sm.putState("Valid Long1", "123");
255       sm.putState("Valid Long2", "1234.0");
256       sm.putState("Valid Long3", new Integer JavaDoc(123));
257       sm.putState("Valid Long4", new Float JavaDoc(123));
258       sm.putState("Valid Long5", new Double JavaDoc(123));
259       sm.putState("Valid Long6", new Short JavaDoc((short) 123));
260       this.assertAllValid(v, sm, FormType.LONG);
261
262       sm = null;
263       sm = new DefaultStateMap();
264
265       sm.putState("Invalid Long1", "foo");
266       sm.putState("invalid Long2", "1234.4");
267       sm.putState("invalid Long4", new Float JavaDoc(123.23));
268       sm.putState("invalid Long5", new Double JavaDoc(123.44));
269       this.assertAllInvalid(v, sm, FormType.LONG);
270
271       //ilc_022702.5_end
272
}
273
274     /**
275      * Test Short types
276      */

277     public void testShort() {
278       //ilc_022702.6_start
279
DefaultStateMap sm = new DefaultStateMap();
280       ValidTypeValidator v = new ValidTypeValidator();
281
282       sm.putState("Valid Short1", "123");
283       sm.putState("Valid Short2", "123.0");
284       sm.putState("Valid Short3", new Integer JavaDoc(123));
285       sm.putState("Valid Short4", new Float JavaDoc(123));
286       sm.putState("Valid Short5", new Double JavaDoc(123));
287       sm.putState("Valid Short6", new Short JavaDoc((short) 123));
288       this.assertAllValid(v, sm, FormType.SHORT);
289
290      sm = null;
291      sm = new DefaultStateMap();
292
293       sm.putState("Invalid Short1", "foo");
294       sm.putState("invalid Short2", "1234.4");
295       sm.putState("invalid Short4", new Float JavaDoc(123.23));
296       sm.putState("invalid Short5", new Double JavaDoc(123.44));
297       sm.putState("invalid Short6", new Integer JavaDoc(123456));
298       this.assertAllInvalid(v, sm, FormType.SHORT);
299
300       //ilc_022702.6_end
301
}
302
303     /**
304      * Test Double types
305      */

306     public void testDouble() {
307       //ilc_022702.7_start
308
DefaultStateMap sm = new DefaultStateMap();
309       ValidTypeValidator v = new ValidTypeValidator();
310
311       sm.putState("Valid Double1", "123");
312       sm.putState("Valid Double2", "123.0");
313       sm.putState("Valid Double3", new Integer JavaDoc(123));
314       sm.putState("Valid Double4", new Float JavaDoc(123));
315       sm.putState("Valid Double5", new Double JavaDoc(123));
316       sm.putState("Valid Double6", new Short JavaDoc((short) 123));
317       this.assertAllValid(v, sm, FormType.DOUBLE);
318
319       sm = null;
320       sm = new DefaultStateMap();
321
322       sm.putState("Invalid Double1", "foo");
323       this.assertAllInvalid(v, sm, FormType.DOUBLE);
324
325       //ilc_022702.7_end
326
}
327
328     /**
329      * Test Float types
330      */

331     public void testFloat() {
332       //ilc_022702.8_start
333
DefaultStateMap sm = new DefaultStateMap();
334       ValidTypeValidator v = new ValidTypeValidator();
335
336       sm.putState("Valid Float1", "123");
337       sm.putState("Valid Float2", "123.0");
338       sm.putState("Valid Float3", new Integer JavaDoc(123));
339       sm.putState("Valid Float4", new Float JavaDoc(123));
340       sm.putState("Valid Float5", new Double JavaDoc(123));
341       sm.putState("Valid Float6", new Short JavaDoc((short) 123));
342       this.assertAllValid(v, sm, FormType.FLOAT);
343
344       sm = null;
345       sm = new DefaultStateMap();
346
347       sm.putState("Invalid Float1", "foo");
348       this.assertAllInvalid(v, sm, FormType.FLOAT);
349
350       //ilc_022702.8_end
351
}
352     
353     protected void checkIt(StateMap sm, FormType ft) {
354         //now lets create a form map
355
DefaultFormMap fm = new DefaultFormMap();
356         Iterator it = sm.getStateKeys().iterator();
357         while (it.hasNext()) {
358             String JavaDoc key = (String JavaDoc) it.next();
359             fm.defineElement(new DefaultFormElement(key, ft, null, new ValidTypeValidator()));
360         }
361
362         //mow map the StateMap into the FormMap
363
fm.map(sm);
364         
365         //now get all the form elements
366
it = sm.getStateKeys().iterator();
367         while (it.hasNext()) {
368             String JavaDoc key = (String JavaDoc) it.next();
369             DefaultFormElement el = (DefaultFormElement) fm.getElement(key);
370             DefaultFormValidator v = (DefaultFormValidator) el.getValidator();
371             // ilc_022202.2_start
372
// make sure we're validating the origVal
373
//if (key.startsWith("val")) assertValid("Error validating element for key:"+key, v, el, null);
374
if (key.startsWith("val")) assertValid("Error validating element for key:"+key+" value: "+el.getOrigVal(), v, el, el.getOrigVal());
375             // else assertInvalid("Error invalidating element for key:"+key, v, el, null);
376
else assertInvalid("Error invalidating element for key:"+key+" value: "+el.getOrigVal(), v, el, el.getOrigVal());
377             // ilc_022102.1_end
378
}
379     }
380     
381     
382 }
383
Popular Tags