KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestMinLengthValidator.java,v 1.9 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
25 import org.w3c.dom.*;
26 import junit.framework.*;
27
28 import org.enhydra.barracuda.core.forms.*;
29 import org.enhydra.barracuda.plankton.data.*;
30 import org.apache.log4j.*;
31 import org.enhydra.barracuda.testbed.*;
32
33
34 /**
35  * This test verifies that MinLengthValidator works correctly.
36  */

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

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

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

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

75     /**
76      * Test String types
77      */

78     public void testString() {
79         //create a form validator and excercise it
80
MinLengthValidator v1 = new MinLengthValidator(-1);
81         MinLengthValidator v2 = new MinLengthValidator(0);
82         MinLengthValidator v3 = new MinLengthValidator(5);
83         DefaultFormElement el = new DefaultFormElement("key", FormType.STRING);
84
85         // ilc_022602.1_start
86
// use a StateMap for testing to make sure elements go through mapping
87
DefaultStateMap sm = new DefaultStateMap();
88
89         // Validate -1 min length
90
// we've decided "" is equivalant of null
91
sm.putState("valid string -1 length", null);
92         sm.putState("valid string -1 length1", "");
93         sm.putState("valid string -1 length1", " ");
94         assertAllValid(v1, sm, FormType.STRING);
95
96         // Validate 0 min length
97
sm = null;
98         sm = new DefaultStateMap();
99         sm.putState("valid string 0 length1", null);
100         sm.putState("valid string 0 length2", "");
101         assertAllValid(v2, sm, FormType.STRING);
102
103         // Validate 5 min length
104
sm = null;
105         sm = new DefaultStateMap();
106         sm.putState("valid string 5 length1", null);
107         sm.putState("valid string 5 length2", "");
108         sm.putState("valid string 5 length3", "12345");
109         sm.putState("valid string 5 length4", "123456");
110         assertAllValid(v3, sm, FormType.STRING);
111
112         sm = null;
113         sm = new DefaultStateMap();
114         sm.putState("invalid string 5 length1", "1234");
115         assertAllInvalid(v3, sm, FormType.STRING);
116         // ilc_022602.1_end
117

118         // ilc_022602.2_start
119
/* use state maps instead
120         //v1
121         assertValid("Error validating null.length()>=-1", v1, el, null);
122         assertValid("Error validating ''.length()>=-1", v1, el, "");
123         assertValid("Error validating foo.length()>=-1", v1, el, "foo");
124         assertValid("Error validating foofoo.length()>=-1", v1, el, "foofoo");
125         
126         //v2
127         assertValid("Error validating null.length()>=0", v2, el, null);
128         assertValid("Error validating ''.length()>=0", v2, el, "");
129         assertValid("Error validating foo.length()>=0", v2, el, "foo");
130         assertValid("Error validating foofoo.length()>=0", v2, el, "foofoo");
131         
132         //v3
133         assertInvalid("Error invalidating null.length()>=5", v3, el, null);
134         assertInvalid("Error invalidating ''.length()>=5", v3, el, "");
135         assertInvalid("Error invalidating foo.length()>=5", v3, el, "foo");
136         assertValid("Error validating foofoo.length()>=5", v3, el, "foofoo");
137         */

138         // ilc_022602.2_end
139
}
140     
141     /**
142      * Test Boolean types - this test ensures that if we pass in a
143      * null FormElement or try to validate the length on a Boolean
144      * it will always generate a ValidationException
145      */

146     public void testBoolean() {
147         //create a form validator and excercise it
148
MinLengthValidator v = new MinLengthValidator(5);
149         DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN);
150         Boolean JavaDoc bT = new Boolean JavaDoc(true);
151         Boolean JavaDoc bF = new Boolean JavaDoc(false);
152
153         // ilc_022602.3_start
154
// use a StateMap for testing to make sure elements go through mapping
155
DefaultStateMap sm = new DefaultStateMap();
156
157         // Validate -1 min length
158
sm.putState("valid bool 5 length", null);
159         assertAllValid(v, sm, FormType.BOOLEAN);
160
161         sm = null;
162         sm = new DefaultStateMap();
163         sm.putState("invalid bool 5 length1", bT);
164         sm.putState("invalid bool 5 length1", bF);
165         assertAllInvalid(v, sm, FormType.BOOLEAN);
166
167         // ilc_022602.3_end
168

169         // ilc_022602.4_start
170
// use StateMap to test
171
/*
172         //v
173         assertInvalid("Error invalidating null element", v, null, "foo");
174         assertInvalid("Error invalidating Boolean(true)", v, el, bT);
175         assertInvalid("Error invalidating Boolean(false)", v, el, bF);
176         */

177         // ilc_022602.4_end
178
}
179     
180     /**
181      * Test Integer types
182      */

183     public void testInteger() {
184         //create a form validator and excercise it
185
MinLengthValidator v1 = new MinLengthValidator(-1);
186         MinLengthValidator v2 = new MinLengthValidator(0);
187         MinLengthValidator v3 = new MinLengthValidator(4);
188         DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER);
189         Integer JavaDoc i = new Integer JavaDoc(1234);
190
191         // ilc_022602.5_start
192
// use a StateMap for testing to make sure elements go through mapping
193
DefaultStateMap sm = new DefaultStateMap();
194
195         // Validate -1 min length
196
sm.putState("valid integer -1 length", null);
197         assertAllValid(v1, sm, FormType.INTEGER);
198
199         // Validate 0 min length
200
sm = null;
201         sm = new DefaultStateMap();
202         sm.putState("valid integer 0 length", null);
203         assertAllValid(v2, sm, FormType.INTEGER);
204
205         // Validate 5 min length
206
sm = null;
207         sm = new DefaultStateMap();
208         sm.putState("valid integer 5 length1", null);
209         sm.putState("valid integer 5 length2", i);
210         assertAllValid(v3, sm, FormType.INTEGER);
211
212         sm = null;
213         sm = new DefaultStateMap();
214         sm.putState("invalid integer 0 length", new Integer JavaDoc(123));
215         assertAllInvalid(v3, sm, FormType.INTEGER);
216         // ilc_022602.5_end
217

218         // ilc_022602.6_start
219
// use statemap instead
220
/*
221         //v1
222         assertValid("Error validating null.length()>=-1", v1, el, null);
223         assertValid("Error validating i.length()>=-1", v1, el, i);
224         
225         //v2
226         assertValid("Error validating null.length()>=0", v2, el, null);
227         assertValid("Error validating i.length()>=0", v2, el, i);
228         
229         //v3
230         assertInvalid("Error invalidating null.length()>=5", v3, el, null);
231         assertInvalid("Error invalidating i.length()>=5", v3, el, i);
232         */

233         // ilc_022602.6_end
234
}
235
236     /**
237      * Test Date types - this test ensures that if we try to validate
238      * the length on a Date it will always generate a ValidationException
239      */

240     public void testDate() {
241         //create a form validator and excercise it
242
MinLengthValidator v = new MinLengthValidator(5);
243         DefaultFormElement el = new DefaultFormElement("key", FormType.DATE);
244         Date d = new Date();
245
246         // ilc_022602.7_start
247
// use a StateMap for testing to make sure elements go through mapping
248
DefaultStateMap sm = new DefaultStateMap();
249
250         // Validate -1 min length
251
sm.putState("valid date 5 length", null);
252         assertAllValid(v, sm, FormType.DATE);
253
254         sm = null;
255         sm = new DefaultStateMap();
256         sm.putState("invalid date 5 length1", d);
257         assertAllInvalid(v, sm, FormType.BOOLEAN);
258
259         // ilc_022602.7_end
260

261         // ilc_022602.8_start
262
// use StateMap to test
263
/*
264         //v
265         assertInvalid("Error invalidating null date", v, el, null);
266         assertInvalid("Error invalidating Date", v, el, d);
267         */

268         // ilc_022602.8_start
269
}
270
271     /**
272      * Test Long types
273      */

274     public void testLong() {
275         //create a form validator and excercise it
276
MinLengthValidator v1 = new MinLengthValidator(-1);
277         MinLengthValidator v2 = new MinLengthValidator(0);
278         MinLengthValidator v3 = new MinLengthValidator(5);
279         DefaultFormElement el = new DefaultFormElement("key", FormType.LONG);
280         Long JavaDoc l = new Long JavaDoc(123);
281
282         // ilc_022602.9_start
283
// use a StateMap for testing to make sure elements go through mapping
284
Long JavaDoc l4 = new Long JavaDoc(1234);
285         Long JavaDoc l5 = new Long JavaDoc(12345);
286         Long JavaDoc l6 = new Long JavaDoc(123456);
287
288         DefaultStateMap sm = new DefaultStateMap();
289
290         // Validate -1 min length
291
sm.putState("valid long -1 length1", null);
292         sm.putState("valid long -1 length2", l);
293         sm.putState("valid long -1 length3", l4);
294         sm.putState("valid long -1 length4", l5);
295         sm.putState("valid long -1 length4", l6);
296         assertAllValid(v1, sm, FormType.LONG);
297
298
299         // validate 0 min length
300
sm = null;
301         sm = new DefaultStateMap();
302         sm.putState("invalid long 0 length1", null);
303         sm.putState("invalid long 0 length2", l);
304         sm.putState("invalid long 0 length3", l4);
305         sm.putState("invalid long 0 length4", l5);
306         sm.putState("invalid long 0 length5", l6);
307         assertAllValid(v2, sm, FormType.LONG);
308
309         // validate 5 min length
310
sm = null;
311         sm = new DefaultStateMap();
312         sm.putState("invalid long 5 length1", null);
313         sm.putState("invalid long 5 length2", l5);
314         sm.putState("invalid long 5 length3", l6);
315         assertAllValid(v3, sm, FormType.LONG);
316
317         sm = null;
318         sm = new DefaultStateMap();
319         sm.putState("invalid long 5 length1", l4);
320         assertAllInvalid(v3, sm, FormType.LONG);
321
322
323         // ilc_022602.9_end
324

325         // ilc_022602.10_start
326
// use StateMap to test
327
/*
328         //v1
329         assertValid("Error validating null.length()>=-1", v1, el, null);
330         assertValid("Error validating l.length()>=-1", v1, el, l);
331         
332         //v2
333         assertValid("Error validating null.length()>=0", v2, el, null);
334         assertValid("Error validating l.length()>=0", v2, el, l);
335         
336         //v3
337         assertInvalid("Error invalidating null.length()>=5", v3, el, null);
338         assertInvalid("Error invalidating l.length()>=5", v3, el, l);
339         */

340         // ilc_022602.10_end
341
}
342
343     /**
344      * Test Short types
345      */

346     public void testShort() {
347         //create a form validator and excercise it
348
MinLengthValidator v1 = new MinLengthValidator(-1);
349         MinLengthValidator v2 = new MinLengthValidator(0);
350         MinLengthValidator v3 = new MinLengthValidator(5);
351         DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT);
352         Short JavaDoc s = new Short JavaDoc((short)123);
353
354         // ilc_022602.11_start
355
// use a StateMap for testing to make sure elements go through mapping
356

357         DefaultStateMap sm = new DefaultStateMap();
358
359         // Validate -1 min length
360
sm.putState("valid short -1 length", null);
361         sm.putState("valid short -1 length2", s);
362         assertAllValid(v1, sm, FormType.SHORT);
363
364         // validate 0 min length
365
sm = null;
366         sm = new DefaultStateMap();
367         sm.putState("valid short 0 length1", null);
368         sm.putState("valid short 0 length2", s);
369         assertAllValid(v2, sm, FormType.SHORT);
370
371         // validate 5 min length
372
sm = null;
373         sm = new DefaultStateMap();
374         sm.putState("valid short 5 length1", null);
375         sm.putState("valid short 5 length2", s);
376         assertAllValid(v1, sm, FormType.SHORT);
377
378         // ilc_022602.11_end
379

380         // ilc_022602.12_start
381
// use StateMap to test
382
//v1
383
/*
384         //v1
385         assertValid("Error null.length()>=-1", v1, el, null);
386         assertValid("Error validating s.length()>=-1", v1, el, s);
387         
388         //v2
389         assertValid("Error validating null.length()>=0", v2, el, null);
390         assertValid("Error validating s.length()>=0", v2, el, s);
391         
392         //v3
393         assertInvalid("Error invalidating null.length()>=5", v3, el, null);
394         assertInvalid("Error invalidating s.length()>=5", v3, el, s);
395         */

396         // ilc_022602.12_end
397
}
398
399     /**
400      * Test Double types
401      */

402     public void testDouble() {
403         //create a form validator and excercise it
404
MinLengthValidator v1 = new MinLengthValidator(-1);
405         MinLengthValidator v2 = new MinLengthValidator(0);
406         MinLengthValidator v3 = new MinLengthValidator(5);
407         DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE);
408         Double JavaDoc d = new Double JavaDoc(123);
409
410         // ilc_022602.13_start
411
// use a StateMap for testing to make sure elements go through mapping
412

413         DefaultStateMap sm = new DefaultStateMap();
414
415         // Validate -1 min length
416
sm.putState("valid double -1 length", null);
417         sm.putState("valid double -1 length2", d);
418         assertAllValid(v1, sm, FormType.DOUBLE);
419
420         // validate 0 min length
421
sm = null;
422         sm = new DefaultStateMap();
423         sm.putState("valid double 0 length1", null);
424         sm.putState("valid double 0 length2", d);
425         assertAllValid(v2, sm, FormType.DOUBLE);
426
427         // validate 5 min length
428
sm = null;
429         sm = new DefaultStateMap();
430         sm.putState("valid double 5 length1", null);
431         assertAllValid(v1, sm, FormType.DOUBLE);
432
433         sm = null;
434         sm = new DefaultStateMap();
435         sm.putState("invalid double 5 length2", d);
436         assertAllValid(v3, sm, FormType.DOUBLE);
437
438
439         // ilc_022602.13_end
440

441         // ilc_022602.14_start
442
// use StateMap to test
443
/*
444         //v1
445         assertValid("Error null.length()>=-1", v1, el, null);
446         assertValid("Error validating d.length()>=-1", v1, el, d);
447         
448         //v2
449         assertValid("Error validating null.length()>=0", v2, el, null);
450         assertValid("Error validating d.length()>=0", v2, el, d);
451         
452         //v3
453         assertInvalid("Error invalidating null.length()>=5", v3, el, null);
454         assertValid("Error validating d.length()>=5", v3, el, d); //the reason this is valid is because the d.toString results in '123.0'
455         */

456         // ilc_022602.14_end
457
}
458
459     /**
460      * Test Float types
461      */

462     public void testFloat() {
463         //create a form validator and excercise it
464
MinLengthValidator v1 = new MinLengthValidator(-1);
465         MinLengthValidator v2 = new MinLengthValidator(0);
466         MinLengthValidator v3 = new MinLengthValidator(5);
467         DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT);
468         Float JavaDoc f = new Float JavaDoc(1.23);
469         Float JavaDoc f2 = new Float JavaDoc(1.233);
470
471         // ilc_022602.15_start
472
// use a StateMap for testing to make sure elements go through mapping
473

474         DefaultStateMap sm = new DefaultStateMap();
475
476         // Validate -1 min length
477
sm.putState("valid float -1 length", null);
478         sm.putState("valid float -1 length2", f);
479         sm.putState("valid float -1 length3", f2);
480         assertAllValid(v1, sm, FormType.FLOAT);
481
482         // validate 0 min length
483
sm = null;
484         sm = new DefaultStateMap();
485         sm.putState("valid float 0 length1", null);
486         sm.putState("valid float 0 length2", f);
487         sm.putState("valid float 0 length3", f2);
488         assertAllValid(v2, sm, FormType.FLOAT);
489
490         // validate 5 min length
491
sm = null;
492         sm = new DefaultStateMap();
493         sm.putState("valid float 5 length1", null);
494         sm.putState("valid float 5 length2", f2);
495         assertAllValid(v3, sm, FormType.FLOAT);
496
497         sm = null;
498         sm = new DefaultStateMap();
499         sm.putState("invalid float 5 length1", f);
500         assertAllInvalid(v3, sm, FormType.FLOAT);
501
502
503         // ilc_022602.15_end
504

505         // ilc_022602.16_start
506
// use StateMap to test
507
/*
508         //v1
509         assertValid("Error validating null.length()>=-1", v1, el, null);
510         assertValid("Error validating f.length()>=-1", v1, el, f);
511         
512         //v2
513         assertValid("Error validating null.length()>=0", v2, el, null);
514         assertValid("Error validating f.length()>=0", v2, el, f);
515         
516         //v3
517         assertInvalid("Error invalidating null.length()>=5", v3, el, null);
518         assertInvalid("Error invalidating f.length()>=5", v3, el, f);
519         assertValid("Error validating f2.length()>=5", v3, el, f2);
520         */

521         // ilc_022602.16_start
522
}
523     
524 }
525
Popular Tags