KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestEqualsValidator.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
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 the EqualsValidator works correctly.
36  */

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

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

48     public TestEqualsValidator(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
String JavaDoc s1 = "foo";
81         EqualsValidator v1 = new EqualsValidator(s1);
82         EqualsValidator v2 = new EqualsValidator(null);
83         DefaultFormElement el = new DefaultFormElement("key", FormType.STRING);
84
85         // ilc_031902.1_start
86
// use a StateMap for testing to make sure elements go through mapping
87
DefaultStateMap sm = new DefaultStateMap();
88         sm.putState("valid equals1", null);
89         sm.putState("valid equals2", "foo");
90         assertAllValid(v1, sm, FormType.STRING);
91
92         sm = new DefaultStateMap();
93         sm.putState("invalid equals1", "foo2");
94         assertAllInvalid(v1, sm, FormType.STRING);
95
96         sm = new DefaultStateMap();
97         sm.putState("valid equal null", null);
98         assertAllValid(v2, sm, FormType.STRING);
99
100         sm = new DefaultStateMap();
101         sm.putState("invalid equal null", "foo");
102         assertAllInvalid(v2, sm, FormType.STRING);
103         // ilc_0311902.1_end
104

105
106         // ilc_031902.2_start
107
// use a statemap above
108
/*
109         //v1
110         assertValid("Error validating foo==foo", v1, el, s1);
111         assertValid("Error validating foo.equals(foo)", v1, el, "foo");
112         assertInvalid("Error invalidating foo.equals(foo2)", v1, el, "foo2");
113         assertInvalid("Error invalidating foo.equals(null)", v1, el, null);
114
115         //v2
116         assertValid("Error validating null==null", v2, el, null);
117         assertInvalid("Error invalidating null==foo2", v2, el, "foo2");
118         */

119         // ilc_031902.2_end
120

121     }
122     
123     /**
124      * Test Boolean types
125      */

126     public void testBoolean() {
127         //create a form validator and excercise it
128
Boolean JavaDoc bT = new Boolean JavaDoc(true);
129         Boolean JavaDoc bF = new Boolean JavaDoc(false);
130         EqualsValidator v1 = new EqualsValidator(bT);
131         EqualsValidator v2 = new EqualsValidator(bF);
132         EqualsValidator v3 = new EqualsValidator(null);
133         DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN);
134
135         // ilc_031902.3_start
136
// use a StateMap for testing to make sure elements go through mapping
137
DefaultStateMap sm = new DefaultStateMap();
138         sm.putState("valid boolean equals true 1", null);
139         sm.putState("valid boolean equals true 2", bT);
140         sm.putState("valid boolean equals true 3", "true");
141         sm.putState("valid boolean equals true 4", "yes");
142         assertAllValid(v1, sm, FormType.BOOLEAN);
143
144         sm = new DefaultStateMap();
145         sm.putState("invalid boolean equals true 1", bF);
146         sm.putState("invalid boolean equals true 2", "false");
147         sm.putState("invalid boolean equals true 3", "no");
148         assertAllInvalid(v1, sm, FormType.BOOLEAN);
149
150         sm = new DefaultStateMap();
151         sm.putState("valid boolean equals false 1", null);
152         sm.putState("valid boolean equals false 2", bF);
153         sm.putState("valid boolean equals false 3", "false");
154         sm.putState("valid boolean equals false 4", "no");
155         assertAllValid(v2, sm, FormType.BOOLEAN);
156
157         sm = new DefaultStateMap();
158         sm.putState("invalid boolean equals false 1", bT);
159         sm.putState("invalid boolean equals false 2", "true");
160         sm.putState("invalid boolean equals false 2", "yes");
161         assertAllInvalid(v2, sm, FormType.BOOLEAN);
162
163         sm = new DefaultStateMap();
164         sm.putState("valid boolean equals null 1", null);
165         assertAllValid(v3, sm, FormType.BOOLEAN);
166
167         sm = new DefaultStateMap();
168         sm.putState("invalid boolean equals null 1", bT);
169         sm.putState("invalid boolean equals null 2", bF);
170         sm.putState("invalid boolean equals null 3", "true");
171         sm.putState("invalid boolean equals null 4", "false");
172         sm.putState("invalid boolean equals null 5", "no");
173         sm.putState("invalid boolean equals null 6", "yes");
174         assertAllInvalid(v3, sm, FormType.BOOLEAN);
175
176         // ilc_031902.3 end
177

178
179         // ilc_031902.4_start
180
// use a statemap above
181
/*
182         //v1
183         assertValid("Error validating true==true", v1, el, bT);
184         assertValid("Error validating true.equals(true)", v1, el, new Boolean(true));
185         assertInvalid("Error invalidating true.equals(false)", v1, el, bF);
186         assertInvalid("Error invalidating true.equals(null)", v1, el, null);
187
188         //v2
189         assertValid("Error validating false==false", v2, el, bF);
190         assertValid("Error validating false.equals(false)", v2, el, new Boolean(false));
191         assertInvalid("Error invalidating false.equals(true)", v2, el, bT);
192         assertInvalid("Error invalidating false.equals(null)", v2, el, null);
193
194         //v3
195         assertValid("Error validating null==null", v3, el, null);
196         assertInvalid("Error invalidating null==true", v3, el, bT);
197         assertInvalid("Error invalidating null==false", v3, el, bF);
198         */

199         // ilc_031902.4_end
200
}
201     
202     /**
203      * Test Integer types
204      */

205     public void testInteger() {
206         //create a form validator and excercise it
207
Integer JavaDoc i1 = new Integer JavaDoc(1000);
208         EqualsValidator v1 = new EqualsValidator(i1);
209         EqualsValidator v2 = new EqualsValidator(null);
210         DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER);
211
212         // ilc_031902.5_start
213
// use a StateMap for testing to make sure elements go through mapping
214
DefaultStateMap sm = new DefaultStateMap();
215         sm.putState("valid integer equals 1000 1", null);
216         sm.putState("valid integer equals 1000 2", new Integer JavaDoc(1000));
217         assertAllValid(v1, sm, FormType.INTEGER);
218
219         sm = new DefaultStateMap();
220         sm.putState("invalid integer equals 1000", new Integer JavaDoc(2000));
221         assertAllInvalid(v1, sm, FormType.INTEGER);
222
223         sm = new DefaultStateMap();
224         sm.putState("valid integer equals null", null);
225         assertAllValid(v2, sm, FormType.INTEGER);
226
227         sm = new DefaultStateMap();
228         sm.putState("invalid integer equals null", i1);
229         assertAllInvalid(v2, sm, FormType.INTEGER);
230
231         // ilc_031902.5 end
232

233         // ilc_031902.6 start
234
// use statemap above
235
/*
236         //v1
237         assertValid("Error validating i1==i1", v1, el, i1);
238         assertValid("Error validating i1.equals(1000)", v1, el, new Integer(1000));
239         assertInvalid("Error invalidating i1.equals(2000)", v1, el, new Integer(2000));
240         assertInvalid("Error invalidating i1.equals(null)", v1, el, null);
241
242         //v2
243         assertValid("Error validating null==null", v2, el, null);
244         assertInvalid("Error invalidating null.equals(2000)", v2, el, new Integer(2000));
245       */

246       // ilc_031902.6 end
247
}
248
249     /**
250      * Test Date types
251      */

252     public void testDate() {
253         //create a form validator and excercise it
254
Date d1 = new Date();
255         Date d2 = new Date(d1.getTime());
256         Date d3 = new Date(65000);
257         EqualsValidator v1 = new EqualsValidator(d1);
258         EqualsValidator v2 = new EqualsValidator(null);
259         DefaultFormElement el = new DefaultFormElement("key", FormType.DATE);
260
261         // ilc_031902.7 start
262
// use a StateMap for testing to make sure elements go through mapping
263
DefaultStateMap sm = new DefaultStateMap();
264         sm.putState("1 valid date equals " + d1, null);
265         sm.putState("2 valid date equals " + d1, d1);
266         sm.putState("2 valid date equals " + d1, d2);
267         assertAllValid(v1, sm, FormType.DATE);
268
269         sm = new DefaultStateMap();
270         sm.putState("1 invalid date equals " + d1, d3);
271         assertAllInvalid(v1, sm, FormType.DATE);
272
273         sm = new DefaultStateMap();
274         sm.putState("1 valid date equals null", null);
275         assertAllValid(v2, sm, FormType.DATE);
276
277         sm = new DefaultStateMap();
278         sm.putState("1 invalid date equals null", d3);
279         assertAllInvalid(v2, sm, FormType.DATE);
280
281         // ilc_031902.7 end
282

283         // ilc_031902.8 start
284
// use statemap above
285
/*
286         //v1
287         assertValid("Error validating d1==d1", v1, el, d1);
288         assertValid("Error validating d1.equals(d2)", v1, el, d2);
289         assertInvalid("Error invalidating d1.equals(d3)", v1, el, d3);
290         assertInvalid("Error invalidating d1.equals(null)", v1, el, null);
291
292         //v2
293         assertValid("Error validating null==null", v2, el, null);
294         assertInvalid("Error invalidating null.equals(d3)", v2, el, d3);
295         */

296         // ilc_031902.8 end
297
}
298
299     /**
300      * Test Long types
301      */

302     public void testLong() {
303         //create a form validator and excercise it
304
Long JavaDoc l1 = new Long JavaDoc(1000);
305         EqualsValidator v1 = new EqualsValidator(l1);
306         EqualsValidator v2 = new EqualsValidator(null);
307         DefaultFormElement el = new DefaultFormElement("key", FormType.LONG);
308
309         // ilc_031902.9 start
310
// use a StateMap for testing to make sure elements go through mapping
311
DefaultStateMap sm = new DefaultStateMap();
312         sm.putState("1 valid long equals " + l1, null);
313         sm.putState("2 valid long equals " + l1, new Long JavaDoc(1000));
314         sm.putState("3 valid long equals " + l1, "1000");
315         assertAllValid(v1, sm, FormType.LONG);
316
317         sm = new DefaultStateMap();
318         sm.putState("1 invalid long equals " + l1, new Long JavaDoc(2000));
319         sm.putState("1 invalid long equals " + l1, "2000");
320         assertAllInvalid(v1, sm, FormType.LONG);
321
322         sm = new DefaultStateMap();
323         sm.putState("1 valid long equals null", null);
324         sm.putState("1 valid long equals null", "");
325         sm.putState("1 valid long equals null", " ");
326         assertAllValid(v2, sm, FormType.LONG);
327
328         sm = new DefaultStateMap();
329         sm.putState("1 invalid long equals null", l1);
330         assertAllInvalid(v2, sm, FormType.LONG);
331
332
333         // ilc_031902.9 end
334

335         // ilc_031902.10 start
336
// use statemap above
337
/*
338         //v1
339         assertValid("Error validating l1==l1", v1, el, l1);
340         assertValid("Error validating l1.equals(1000)", v1, el, new Long(1000));
341         assertInvalid("Error invalidating l1.equals(2000)", v1, el, new Long(2000));
342         assertInvalid("Error invalidating l1.equals(null)", v1, el, null);
343
344         //v2
345         assertValid("Error validating null==null", v2, el, null);
346         assertInvalid("Error invalidating null.equals(2000)", v2, el, new Long(2000));
347         */

348         // ilc_031902.10 end
349
}
350
351     /**
352      * Test Short types
353      */

354     public void testShort() {
355         //create a form validator and excercise it
356
Short JavaDoc s1 = new Short JavaDoc((short) 1000);
357         EqualsValidator v1 = new EqualsValidator(s1);
358         EqualsValidator v2 = new EqualsValidator(null);
359         DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT);
360
361         // ilc_031902.11 start
362
// use a StateMap for testing to make sure elements go through mapping
363
DefaultStateMap sm = new DefaultStateMap();
364         sm.putState("1 valid short equals " + s1, null);
365         sm.putState("2 valid short equals " + s1, new Short JavaDoc((short)1000));
366         sm.putState("3 valid short equals " + s1, "1000");
367         assertAllValid(v1, sm, FormType.SHORT);
368
369         sm = new DefaultStateMap();
370         sm.putState("1 invalid short equals " + s1, new Short JavaDoc((short)2000));
371         sm.putState("2 invalid short equals " + s1, "2000");
372         assertAllInvalid(v1, sm, FormType.SHORT);
373
374         sm = new DefaultStateMap();
375         sm.putState("1 valid short equals null", null);
376         sm.putState("2 valid short equals null", "");
377         sm.putState("3 valid short equals null", " ");
378         assertAllValid(v2, sm, FormType.SHORT);
379
380         sm = new DefaultStateMap();
381         sm.putState("1 invalid short equals null", s1);
382         assertAllInvalid(v2, sm, FormType.SHORT);
383         // ilc_031902.11 end
384

385         // ilc_031902.12 start
386
// use statemap above
387
/*
388         //v1
389         assertValid("Error validating s1==s1", v1, el, s1);
390         assertValid("Error validating s1.equals(1000)", v1, el, new Short((short) 1000));
391         assertInvalid("Error invalidating s1.equals(2000)", v1, el, new Short((short) 2000));
392         assertInvalid("Error invalidating s1.equals(null)", v1, el, null);
393
394         //v2
395         assertValid("Error validating null==null", v2, el, null);
396         assertInvalid("Error invalidating null.equals(2000)", v2, el, new Short((short) 2000));
397         */

398         // ilc_031902.12 end
399
}
400
401     /**
402      * Test Double types
403      */

404     public void testDouble() {
405         //create a form validator and excercise it
406
Double JavaDoc d1 = new Double JavaDoc(1000);
407         EqualsValidator v1 = new EqualsValidator(d1);
408         EqualsValidator v2 = new EqualsValidator(null);
409         DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE);
410
411         // ilc_031902.13 start
412
// use a StateMap for testing to make sure elements go through mapping
413
DefaultStateMap sm = new DefaultStateMap();
414         sm.putState("1 valid double equals " + d1, null);
415         sm.putState("2 valid double equals " + d1, new Double JavaDoc(1000));
416         sm.putState("3 valid double equals " + d1, "1000");
417         assertAllValid(v1, sm, FormType.DOUBLE);
418
419         sm = new DefaultStateMap();
420         sm.putState("1 invalid double equals " + d1, new Double JavaDoc(2000));
421         sm.putState("2 invalid double equals " + d1, "2000");
422         assertAllInvalid(v1, sm, FormType.DOUBLE);
423
424         sm = new DefaultStateMap();
425         sm.putState("1 valid double equals null", null);
426         sm.putState("2 valid double equals null", "");
427         sm.putState("3 valid double equals null", " ");
428         assertAllValid(v2, sm, FormType.DOUBLE);
429
430         sm = new DefaultStateMap();
431         sm.putState("1 invalid double equals null", d1);
432         assertAllInvalid(v2, sm, FormType.DOUBLE);
433         // ilc_031902.13 end
434

435         // ilc_031902.14 start
436
// use statemap above
437
/*
438         //v1
439         assertValid("Error validating d1==d1", v1, el, d1);
440         assertValid("Error validating d1.equals(1000)", v1, el, new Double(1000));
441         assertInvalid("Error invalidating d1.equals(2000)", v1, el, new Double(2000));
442         assertInvalid("Error invalidating d1.equals(null)", v1, el, null);
443
444         //v2
445         assertValid("Error validating null==null", v2, el, null);
446         assertInvalid("Error invalidating null.equals(2000)", v2, el, new Double(2000));
447         */

448         // ilc_031902.14 end
449
}
450
451     /**
452      * Test Float types
453      */

454     public void testFloat() {
455         //create a form validator and excercise it
456
Float JavaDoc f1 = new Float JavaDoc(1000);
457         EqualsValidator v1 = new EqualsValidator(f1);
458         EqualsValidator v2 = new EqualsValidator(null);
459         DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT);
460
461         // ilc_031902.15 start
462
// use a StateMap for testing to make sure elements go through mapping
463
DefaultStateMap sm = new DefaultStateMap();
464         sm.putState("1 valid float equals " + f1, null);
465         sm.putState("2 valid float equals " + f1, new Float JavaDoc(1000));
466         sm.putState("3 valid float equals " + f1, "1000");
467         assertAllValid(v1, sm, FormType.FLOAT);
468
469         sm = new DefaultStateMap();
470         sm.putState("1 invalid float equals " + f1, new Float JavaDoc(2000));
471         sm.putState("2 invalid float equals " + f1, "2000");
472         assertAllInvalid(v1, sm, FormType.FLOAT);
473
474         sm = new DefaultStateMap();
475         sm.putState("1 valid float equals null", null);
476         sm.putState("2 valid float equals null", "");
477         sm.putState("3 valid float equals null", " ");
478         assertAllValid(v2, sm, FormType.FLOAT);
479
480         sm = new DefaultStateMap();
481         sm.putState("1 invalid float equals null", f1);
482         assertAllInvalid(v2, sm, FormType.FLOAT);
483         // ilc_031902.15 end
484

485         // ilc_031902.16 start
486
// use statemap above
487
/*
488         //v1
489         assertValid("Error validating f1==f1", v1, el, f1);
490         assertValid("Error validating f1.equals(1000)", v1, el, new Float(1000));
491         assertInvalid("Error invalidating f1.equals(2000)", v1, el, new Float(2000));
492         assertInvalid("Error invalidating f1.equals(null)", v1, el, null);
493
494         //v2
495         assertValid("Error validating null==null", v2, el, null);
496         assertInvalid("Error invalidating null.equals(2000)", v2, el, new Float(2000));
497         */

498         // ilc_031902.16 end
499
}
500     
501 }
502
Popular Tags