KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestMaxLengthValidator.java,v 1.8 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 MaxLengthValidator works correctly.
36  */

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

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

48     public TestMaxLengthValidator(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
MaxLengthValidator v1 = new MaxLengthValidator(-1);
81         MaxLengthValidator v2 = new MaxLengthValidator(0);
82         MaxLengthValidator v3 = new MaxLengthValidator(5);
83         DefaultFormElement el = new DefaultFormElement("key", FormType.STRING);
84
85         // ilc_022502.1_start
86
// use a StateMap for testing to make sure elements go through mapping
87
DefaultStateMap sm = new DefaultStateMap();
88
89         // Validate -1 max length
90
sm.putState("valid string -1 length", null);
91         sm.putState("invalid string -1 length1", "");
92         sm.putState("invalid string -1 length1", " ");
93         assertAllValid(v1, sm, FormType.STRING);
94
95         sm = null;
96         sm = new DefaultStateMap();
97         // we've decided "" is equivalant of null
98
//sm.putState("invalid string -1 length1", "");
99
sm.putState("invalid string -1 length2", "foo");
100         sm.putState("invalid string -1 length3", "foofoo");
101         assertAllInvalid(v1, sm, FormType.STRING);
102
103         // Validate 0 max length
104
sm = null;
105         sm = new DefaultStateMap();
106         sm.putState("valid string 0 length1", null);
107         sm.putState("valid string 0 length2", "");
108         assertAllValid(v2, sm, FormType.STRING);
109
110         sm = null;
111         sm = new DefaultStateMap();
112         sm.putState("Invalid string 0 length1", "f");
113         sm.putState("Invalid string 0 length1", "foo");
114         sm.putState("invalid string 0 length2", "foofoo");
115         assertAllInvalid(v2, sm, FormType.STRING);
116
117         // Validate 5 max length
118
sm = null;
119         sm = new DefaultStateMap();
120         sm.putState("valid string 5 length1", null);
121         sm.putState("valid string 5 length2", "");
122         sm.putState("valid string 5 length3", "foo");
123         sm.putState("valid string 5 length3", "foofo");
124         assertAllValid(v3, sm, FormType.STRING);
125
126         sm = null;
127         sm = new DefaultStateMap();
128         sm.putState("invalid string 5 length1", "foofoo");
129         assertAllInvalid(v3, sm, FormType.STRING);
130
131         // ilc_022502.1_end
132

133         // ilc_022502.2_start
134
/* use state maps instead
135         //v1
136         assertValid("Error validating null.length()<=-1", v1, el, null);
137         assertInvalid("Error invalidating ''.length()<=-1", v1, el, "");
138         assertInvalid("Error invalidating foo.length()<=-1", v1, el, "foo");
139         assertInvalid("Error invalidating foofoo.length()<=-1", v1, el, "foofoo");
140         
141         //v2
142         assertValid("Error validating null.length()<=0", v2, el, null);
143         assertValid("Error validating ''.length()<=0", v2, el, "");
144         assertInvalid("Error invalidating foo.length()<=0", v2, el, "foo");
145         assertInvalid("Error invalidating foofoo.length()<=0", v2, el, "foofoo");
146         
147         //v3
148         assertValid("Error validating null.length()<=5", v3, el, null);
149         assertValid("Error validating ''.length()<=5", v3, el, "");
150         assertValid("Error validating foo.length()<=5", v3, el, "foo");
151         assertInvalid("Error invalidating foofoo.length()<=5", v3, el, "foofoo");
152         */

153         // ilc_022502.2_end
154
}
155     
156     /**
157      * Test Boolean types - this test ensures that if we pass in a
158      * null FormElement or try to validate the length on a Boolean
159      * it will always generate a ValidationException
160      */

161     public void testBoolean() {
162         //create a form validator and excercise it
163
MaxLengthValidator v = new MaxLengthValidator(5);
164         DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN);
165         Boolean JavaDoc bT = new Boolean JavaDoc(true);
166         Boolean JavaDoc bF = new Boolean JavaDoc(false);
167
168         // ilc_022502.3_start
169
// use a StateMap for testing to make sure elements go through mapping
170
DefaultStateMap sm = new DefaultStateMap();
171
172         // Validate -1 max length
173
sm.putState("valid bool 5 length", null);
174         assertAllValid(v, sm, FormType.BOOLEAN);
175
176         sm = null;
177         sm = new DefaultStateMap();
178         sm.putState("invalid bool 5 length1", bT);
179         sm.putState("invalid bool 5 length1", bF);
180         assertAllInvalid(v, sm, FormType.BOOLEAN);
181         // ilc_022502.3_end
182

183         // ilc_022502.4_start
184
// use StateMap to test
185
/*
186         //v
187         assertInvalid("Error invalidating null element", v, null, "foo");
188         assertInvalid("Error invalidating Boolean(true)", v, el, bT);
189         assertInvalid("Error invalidating Boolean(false)", v, el, bF);
190         */

191         // ilc_022502.4_end
192
}
193     
194     /**
195      * Test Integer types
196      */

197     public void testInteger() {
198         //create a form validator and excercise it
199
MaxLengthValidator v1 = new MaxLengthValidator(-1);
200         MaxLengthValidator v2 = new MaxLengthValidator(0);
201         MaxLengthValidator v3 = new MaxLengthValidator(4);
202         DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER);
203         Integer JavaDoc i = new Integer JavaDoc(1234);
204
205         // ilc_022502.5_start
206
// use a StateMap for testing to make sure elements go through mapping
207
DefaultStateMap sm = new DefaultStateMap();
208
209         // Validate -1 max length
210
sm.putState("valid integer -1 length", null);
211         assertAllValid(v1, sm, FormType.INTEGER);
212
213         sm = null;
214         sm = new DefaultStateMap();
215         sm.putState("invalid integer -1 length", i);
216         assertAllInvalid(v1, sm, FormType.INTEGER);
217
218         // Validate 0 max length
219
sm = null;
220         sm = new DefaultStateMap();
221         sm.putState("valid integer 0 length", null);
222         assertAllValid(v2, sm, FormType.INTEGER);
223
224         sm = null;
225         sm = new DefaultStateMap();
226         sm.putState("invalid integer 0 length", i);
227         assertAllInvalid(v2, sm, FormType.INTEGER);
228
229         // Validate 5 max length
230
sm = null;
231         sm = new DefaultStateMap();
232         sm.putState("valid integer 5 length1", null);
233         sm.putState("valid integer 5 length2", i);
234         assertAllValid(v3, sm, FormType.INTEGER);
235
236         sm = null;
237         sm = new DefaultStateMap();
238         sm.putState("invalid integer 0 length", new Integer JavaDoc(12345));
239         assertAllInvalid(v3, sm, FormType.INTEGER);
240         // ilc_022502.5_end
241

242         // ilc_022502.6_start
243
// use statemap instead
244
/*
245         //v1
246         assertValid("Error validating null.length()<=-1", v1, el, null);
247         assertInvalid("Error invalidating i.length()<=-1", v1, el, i);
248         
249         //v2
250         assertValid("Error validating null.length()<=0", v2, el, null);
251         assertInvalid("Error invalidating i.length()<=0", v2, el, i);
252         
253         //v3
254         assertValid("Error validating null.length()<=5", v3, el, null);
255         assertValid("Error validating i.length()<=5", v3, el, i);
256         */

257         // ilc_022502.6_end
258
}
259
260     /**
261      * Test Date types - this test ensures that if we try to validate
262      * the length on a Date it will always generate a ValidationException
263      */

264     public void testDate() {
265         //create a form validator and excercise it
266
MaxLengthValidator v = new MaxLengthValidator(5);
267         DefaultFormElement el = new DefaultFormElement("key", FormType.DATE);
268         Date d = new Date();
269
270         // ilc_022502.7_start
271
// use a StateMap for testing to make sure elements go through mapping
272
DefaultStateMap sm = new DefaultStateMap();
273
274         // Validate -1 max length
275
sm.putState("valid date 5 length", null);
276         assertAllValid(v, sm, FormType.DATE);
277
278         sm = null;
279         sm = new DefaultStateMap();
280         sm.putState("invalid date 5 length1", d);
281         assertAllInvalid(v, sm, FormType.BOOLEAN);
282         // ilc_022502.7_end
283

284         // ilc_022502.8_start
285
// use StateMap to test
286
/*
287         //v
288         assertValid("Error validating null date", v, el, null);
289         assertInvalid("Error invalidating Date", v, el, d);
290         */

291         // ilc_022502.8_end
292
}
293
294     /**
295      * Test Long types
296      */

297     public void testLong() {
298         //create a form validator and excercise it
299
MaxLengthValidator v1 = new MaxLengthValidator(-1);
300         MaxLengthValidator v2 = new MaxLengthValidator(0);
301         MaxLengthValidator v3 = new MaxLengthValidator(5);
302         DefaultFormElement el = new DefaultFormElement("key", FormType.LONG);
303         Long JavaDoc l = new Long JavaDoc(123);
304
305         // ilc_022502.9_start
306
// use a StateMap for testing to make sure elements go through mapping
307
Long JavaDoc l4 = new Long JavaDoc(1234);
308         Long JavaDoc l5 = new Long JavaDoc(12345);
309         Long JavaDoc l6 = new Long JavaDoc(123456);
310
311         DefaultStateMap sm = new DefaultStateMap();
312
313         // Validate -1 max length
314
sm.putState("valid long -1 length", null);
315         assertAllValid(v1, sm, FormType.LONG);
316
317         sm = null;
318         sm = new DefaultStateMap();
319         sm.putState("invalid long -1 length1", l);
320         assertAllInvalid(v1, sm, FormType.LONG);
321
322         // validate 0 max length
323
sm = null;
324         sm = new DefaultStateMap();
325         sm.putState("invalid long 0 length1", null);
326         assertAllValid(v2, sm, FormType.LONG);
327
328         sm = null;
329         sm = new DefaultStateMap();
330         sm.putState("invalid long 0 length1", l);
331         assertAllInvalid(v2, sm, FormType.LONG);
332
333         // validate 5 max length
334
sm = null;
335         sm = new DefaultStateMap();
336         sm.putState("invalid long 5 length1", null);
337         sm.putState("invalid long 5 length2", l4);
338         sm.putState("invalid long 5 length3", l5);
339         assertAllValid(v3, sm, FormType.LONG);
340
341         sm = null;
342         sm = new DefaultStateMap();
343         sm.putState("invalid long 5 length1", l6);
344         assertAllInvalid(v3, sm, FormType.LONG);
345
346
347         // ilc_022502.9_end
348

349         // ilc_022502.10_start
350
// use StateMap to test
351
/*
352         //v1
353         assertValid("Error validating null.length()<=-1", v1, el, null);
354         assertInvalid("Error invalidating l.length()<=-1", v1, el, l);
355         
356         //v2
357         assertValid("Error validating null.length()<=0", v2, el, null);
358         assertInvalid("Error invalidating l.length()<=0", v2, el, l);
359         
360         //v3
361         assertValid("Error validating null.length()<=5", v3, el, null);
362         assertValid("Error validating l.length()<=5", v3, el, l);
363         */

364         // ilc_022502.10_end
365
}
366
367     /**
368      * Test Short types
369      */

370     public void testShort() {
371         //create a form validator and excercise it
372
MaxLengthValidator v1 = new MaxLengthValidator(-1);
373         MaxLengthValidator v2 = new MaxLengthValidator(0);
374         MaxLengthValidator v3 = new MaxLengthValidator(5);
375         DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT);
376         Short JavaDoc s = new Short JavaDoc((short)123);
377
378         // ilc_022502.11_start
379
// use a StateMap for testing to make sure elements go through mapping
380

381         DefaultStateMap sm = new DefaultStateMap();
382
383         // Validate -1 max length
384
sm.putState("valid short -1 length", null);
385         assertAllValid(v1, sm, FormType.SHORT);
386
387         sm = null;
388         sm = new DefaultStateMap();
389         sm.putState("invalid short -1 length1", s);
390         assertAllInvalid(v1, sm, FormType.SHORT);
391
392         // validate 0 max length
393
sm = null;
394         sm = new DefaultStateMap();
395         sm.putState("valid short 0 length1", null);
396         assertAllValid(v2, sm, FormType.SHORT);
397
398         sm = null;
399         sm = new DefaultStateMap();
400         sm.putState("invalid short 0 length1", s);
401         assertAllInvalid(v2, sm, FormType.SHORT);
402
403         // validate 5 max length
404
sm = null;
405         sm = new DefaultStateMap();
406         sm.putState("valid short 5 length1", null);
407         sm.putState("valid short 5 length2", s);
408         assertAllValid(v3, sm, FormType.SHORT);
409
410
411         // ilc_022502.11_end
412

413         // ilc_022502.12_start
414
// use StateMap to test
415
//v1
416
/*
417         assertValid("Error validating null.length()<=-1", v1, el, null);
418         assertInvalid("Error invalidating s.length()<=-1", v1, el, s);
419         
420         //v2
421         assertValid("Error validating null.length()<=0", v2, el, null);
422         assertInvalid("Error invalidating s.length()<=0", v2, el, s);
423         
424         //v3
425         assertValid("Error validating null.length()<=5", v3, el, null);
426         assertValid("Error validating s.length()<=5", v3, el, s);
427         */

428         // ilc_022502.12_end
429
}
430
431     /**
432      * Test Double types
433      */

434     public void testDouble() {
435         //create a form validator and excercise it
436
MaxLengthValidator v1 = new MaxLengthValidator(-1);
437         MaxLengthValidator v2 = new MaxLengthValidator(0);
438         MaxLengthValidator v3 = new MaxLengthValidator(5);
439         DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE);
440         Double JavaDoc d = new Double JavaDoc(123);
441
442         // ilc_022502.13_start
443
// use a StateMap for testing to make sure elements go through mapping
444

445         DefaultStateMap sm = new DefaultStateMap();
446
447         // Validate -1 max length
448
sm.putState("valid double -1 length", null);
449         assertAllValid(v1, sm, FormType.DOUBLE);
450
451         sm = null;
452         sm = new DefaultStateMap();
453         sm.putState("invalid double -1 length1", d);
454         assertAllInvalid(v1, sm, FormType.DOUBLE);
455
456         // validate 0 max length
457
sm = null;
458         sm = new DefaultStateMap();
459         sm.putState("valid double 0 length1", null);
460         assertAllValid(v2, sm, FormType.DOUBLE);
461
462         sm = null;
463         sm = new DefaultStateMap();
464         sm.putState("invalid double 0 length1", d);
465         assertAllInvalid(v2, sm, FormType.DOUBLE);
466
467         // validate 5 max length
468
sm = null;
469         sm = new DefaultStateMap();
470         sm.putState("valid double 5 length1", null);
471         sm.putState("valid double 5 length2", d);
472         assertAllValid(v3, sm, FormType.DOUBLE);
473
474
475         // ilc_022502.13_end
476

477         // ilc_022502.14_start
478
// use StateMap to test
479
/*
480         //v1
481         assertValid("Error validating null.length()<=-1", v1, el, null);
482         assertInvalid("Error invalidating d.length()<=-1", v1, el, d);
483         
484         //v2
485         assertValid("Error validating null.length()<=0", v2, el, null);
486         assertInvalid("Error invalidating d.length()<=0", v2, el, d);
487         
488         //v3
489         assertValid("Error validating null.length()<=5", v3, el, null);
490         assertValid("Error validating d.length()<=5", v3, el, d);
491         */

492         // ilc_022502.14_end
493
}
494
495     /**
496      * Test Float types
497      */

498     public void testFloat() {
499         //create a form validator and excercise it
500
MaxLengthValidator v1 = new MaxLengthValidator(-1);
501         MaxLengthValidator v2 = new MaxLengthValidator(0);
502         MaxLengthValidator v3 = new MaxLengthValidator(5);
503         DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT);
504         Float JavaDoc f = new Float JavaDoc(1.23);
505
506         // ilc_022502.15_start
507
// use a StateMap for testing to make sure elements go through mapping
508

509         DefaultStateMap sm = new DefaultStateMap();
510
511         // Validate -1 max length
512
sm.putState("valid float -1 length", null);
513         assertAllValid(v1, sm, FormType.FLOAT);
514
515         sm = null;
516         sm = new DefaultStateMap();
517         sm.putState("invalid float -1 length1", f);
518         assertAllInvalid(v1, sm, FormType.FLOAT);
519
520         // validate 0 max length
521
sm = null;
522         sm = new DefaultStateMap();
523         sm.putState("valid float 0 length1", null);
524         assertAllValid(v2, sm, FormType.FLOAT);
525
526         sm = null;
527         sm = new DefaultStateMap();
528         sm.putState("invalid float 0 length1", f);
529         assertAllInvalid(v2, sm, FormType.FLOAT);
530
531         // validate 5 max length
532
sm = null;
533         sm = new DefaultStateMap();
534         sm.putState("valid float 5 length1", null);
535         sm.putState("valid float 5 length2", f);
536         assertAllValid(v3, sm, FormType.FLOAT);
537
538
539         // ilc_022502.15_end
540

541         // ilc_022502.16_start
542
// use StateMap to test
543
/*
544         //v1
545         assertValid("Error validating null.length()<=-1", v1, el, null);
546         assertInvalid("Error invalidating f.length()<=-1", v1, el, f);
547         
548         //v2
549         assertValid("Error validating null.length()<=0", v2, el, null);
550         assertInvalid("Error invalidating f.length()<=0", v2, el, f);
551         
552         //v3
553         assertValid("Error validating null.length()<=5", v3, el, null);
554         assertValid("Error validating f.length()<=5", v3, el, f);
555         */

556         // ilc_022502.16_end
557
}
558     
559 }
560
Popular Tags