KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

48     public TestNotNullValidator(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
NotNullValidator v = new NotNullValidator();
81         DefaultFormElement el = new DefaultFormElement("key", FormType.STRING);
82
83         // ilc_022602.1_start
84
// use a StateMap for testing to make sure elements go through mapping
85
DefaultStateMap sm = new DefaultStateMap();
86
87         sm.putState("invalid not null1", null);
88         sm.putState("invalid not null2", "");
89         sm.putState("invalid not null3", " ");
90         assertAllInvalid(v, sm, FormType.STRING);
91
92         sm = null;
93         sm = new DefaultStateMap();
94         sm.putState("valid not null", "foofoo");
95         assertAllValid(v, sm, FormType.STRING);
96
97         // ilc_022602.1_end
98

99
100         // ilc_022602.2_start
101
/* use state maps instead
102         //v
103         assertInvalid("Error invalidating null==null", v, el, null);
104         assertInvalid("Error invalidating ''==null", v, el, "");
105         assertInvalid("Error invalidating ' '==null", v, el, " ");
106         assertValid("Error validating foo!=null", v, el, "foo");
107         */

108         // ilc_022602.2_end
109
}
110     
111     /**
112      * Test Boolean types - this test ensures that if we pass in a
113      * null FormElement or try to validate the length on a Boolean
114      * it will always generate a ValidationException
115      */

116     public void testBoolean() {
117         //create a form validator and excercise it
118
NotNullValidator v = new NotNullValidator();
119         DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN);
120
121         // ilc_022602.3_start
122
// use a StateMap for testing to make sure elements go through mapping
123
DefaultStateMap sm = new DefaultStateMap();
124
125         sm.putState("invalid not null1", null);
126         assertAllInvalid(v, sm, FormType.BOOLEAN);
127
128         sm = null;
129         sm = new DefaultStateMap();
130         sm.putState("valid not null1", new Boolean JavaDoc(true));
131         sm.putState("valid not null2", new Boolean JavaDoc(false));
132         assertAllValid(v, sm, FormType.BOOLEAN);
133
134         // ilc_022602.3_end
135

136
137         // ilc_022602.4_start
138
/* use state maps instead
139         //v
140         assertInvalid("Error invalidating null==null", v, el, null);
141         assertValid("Error validating true!=null", v, el, new Boolean(true));
142         assertValid("Error validating false!=null", v, el, new Boolean(false));
143         */

144         // ilc_022602.4_end
145
}
146     
147     /**
148      * Test Integer types
149      */

150     public void testInteger() {
151         //create a form validator and excercise it
152
NotNullValidator v = new NotNullValidator();
153         DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER);
154         Integer JavaDoc i1 = new Integer JavaDoc(123);
155
156         // ilc_022602.5_start
157
// use a StateMap for testing to make sure elements go through mapping
158
DefaultStateMap sm = new DefaultStateMap();
159
160         sm.putState("invalid not null1", null);
161         assertAllInvalid(v, sm, FormType.INTEGER);
162
163         sm = null;
164         sm = new DefaultStateMap();
165         sm.putState("valid not null1", i1);
166         assertAllValid(v, sm, FormType.INTEGER);
167
168         // ilc_022602.5_end
169

170
171         // ilc_022602.6_start
172
/* use state maps instead
173         //v
174         assertInvalid("Error invalidating null==null", v, el, null);
175         assertValid("Error validating i1!=null", v, el, i1);
176         */

177         // ilc_022602.6_end
178
}
179
180     /**
181      * Test Date types - this test ensures that if we try to validate
182      * the length on a Date it will always generate a ValidationException
183      */

184     public void testDate() {
185         //create a form validator and excercise it
186
NotNullValidator v = new NotNullValidator();
187         DefaultFormElement el = new DefaultFormElement("key", FormType.DATE);
188         Date d = new Date();
189
190         // ilc_022602.7_start
191
// use a StateMap for testing to make sure elements go through mapping
192
DefaultStateMap sm = new DefaultStateMap();
193
194         sm.putState("invalid not null1", null);
195         assertAllInvalid(v, sm, FormType.DATE);
196
197         sm = null;
198         sm = new DefaultStateMap();
199         sm.putState("valid not null1", d);
200         assertAllValid(v, sm, FormType.DATE);
201
202         // ilc_022602.7_end
203

204
205         // ilc_022602.8_start
206
/* use state maps instead
207         //v
208         assertInvalid("Error invalidating null==null", v, el, null);
209         assertValid("Error validating d!=null", v, el, d);
210         */

211         // ilc_022602.8_end
212
}
213
214     /**
215      * Test Long types
216      */

217     public void testLong() {
218         //create a form validator and excercise it
219
NotNullValidator v = new NotNullValidator();
220         DefaultFormElement el = new DefaultFormElement("key", FormType.LONG);
221         Long JavaDoc l1 = new Long JavaDoc(123);
222
223         // ilc_022602.9_start
224
// use a StateMap for testing to make sure elements go through mapping
225
DefaultStateMap sm = new DefaultStateMap();
226
227         sm.putState("invalid not null1", null);
228         assertAllInvalid(v, sm, FormType.LONG);
229
230         sm = null;
231         sm = new DefaultStateMap();
232         sm.putState("valid not null1", l1);
233         assertAllValid(v, sm, FormType.LONG);
234
235         // ilc_022602.9_end
236

237
238         // ilc_022602.10_start
239
/* use state maps instead
240         //v
241         assertInvalid("Error invalidating null==null", v, el, null);
242         assertValid("Error validating l1!=null", v, el, l1);
243         */

244         // ilc_022602.10_end
245
}
246
247     /**
248      * Test Short types
249      */

250     public void testShort() {
251         //create a form validator and excercise it
252
NotNullValidator v = new NotNullValidator();
253         DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT);
254         Short JavaDoc s1 = new Short JavaDoc((short)123);
255
256         // ilc_022602.11_start
257
// use a StateMap for testing to make sure elements go through mapping
258
DefaultStateMap sm = new DefaultStateMap();
259
260         sm.putState("invalid not null1", null);
261         assertAllInvalid(v, sm, FormType.SHORT);
262
263         sm = null;
264         sm = new DefaultStateMap();
265         sm.putState("valid not null1", s1);
266         assertAllValid(v, sm, FormType.SHORT);
267
268         // ilc_022602.11_end
269

270
271         // ilc_022602.12_start
272
/* use state maps instead
273         //v
274         assertInvalid("Error invalidating null==null", v, el, null);
275         assertValid("Error validating s1!=null", v, el, s1);
276         */

277         // ilc_022602.12_end
278
}
279
280     /**
281      * Test Double types
282      */

283     public void testDouble() {
284         //create a form validator and excercise it
285
NotNullValidator v = new NotNullValidator();
286         DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE);
287         Double JavaDoc d1 = new Double JavaDoc(123);
288
289         // ilc_022602.13_start
290
// use a StateMap for testing to make sure elements go through mapping
291
DefaultStateMap sm = new DefaultStateMap();
292
293         sm.putState("invalid not null1", null);
294         assertAllInvalid(v, sm, FormType.DOUBLE);
295
296         sm = null;
297         sm = new DefaultStateMap();
298         sm.putState("valid not null1", d1);
299         assertAllValid(v, sm, FormType.DOUBLE);
300
301         // ilc_022602.13_end
302

303
304         // ilc_022602.14_start
305
/* use state maps instead
306         //v
307         assertInvalid("Error invalidating null==null", v, el, null);
308         assertValid("Error validating d1!=null", v, el, d1);
309         */

310         // ilc_022602.14_end
311
}
312
313     /**
314      * Test Float types
315      */

316     public void testFloat() {
317         //create a form validator and excercise it
318
NotNullValidator v = new NotNullValidator();
319         DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT);
320         Float JavaDoc f1 = new Float JavaDoc(123);
321
322         // ilc_022602.15_start
323
// use a StateMap for testing to make sure elements go through mapping
324
DefaultStateMap sm = new DefaultStateMap();
325
326         sm.putState("invalid not null1", null);
327         assertAllInvalid(v, sm, FormType.FLOAT);
328
329         sm = null;
330         sm = new DefaultStateMap();
331         sm.putState("valid not null1", f1);
332         assertAllValid(v, sm, FormType.FLOAT);
333
334         // ilc_022602.15_end
335

336
337         // ilc_022602.16_start
338
/* use state maps instead
339         //v
340         assertInvalid("Error invalidating null==null", v, el, null);
341         assertValid("Error validating f1!=null", v, el, f1);
342         */

343         // ilc_022602.16_end
344
}
345     
346 }
347
Popular Tags