KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > ValidateTest


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28 /**
29  * Unit tests {@link org.apache.commons.lang.util.Validate}.
30  *
31  * @author Stephen Colebourne
32  * @author Norm Deane
33  * @version $Id: ValidateTest.java 155423 2005-02-26 13:08:30Z dirkv $
34  */

35 public class ValidateTest extends TestCase {
36
37     public ValidateTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] args) {
42         TestRunner.run(suite());
43     }
44
45     public static Test suite() {
46         TestSuite suite = new TestSuite(ValidateTest.class);
47         suite.setName("Validate Tests");
48         return suite;
49     }
50
51     protected void setUp() throws Exception JavaDoc {
52         super.setUp();
53     }
54
55     protected void tearDown() throws Exception JavaDoc {
56         super.tearDown();
57     }
58
59     //-----------------------------------------------------------------------
60
public void testIsTrue1() {
61         Validate.isTrue(true);
62         try {
63             Validate.isTrue(false);
64             fail("Expecting IllegalArgumentException");
65         } catch (IllegalArgumentException JavaDoc ex) {
66             assertEquals("The validated expression is false", ex.getMessage());
67         }
68     }
69
70     //-----------------------------------------------------------------------
71
public void testIsTrue2() {
72         Validate.isTrue(true, "MSG");
73         try {
74             Validate.isTrue(false, "MSG");
75             fail("Expecting IllegalArgumentException");
76         } catch (IllegalArgumentException JavaDoc ex) {
77             assertEquals("MSG", ex.getMessage());
78         }
79     }
80
81     //-----------------------------------------------------------------------
82
public void testIsTrue3() {
83         Validate.isTrue(true, "MSG", new Integer JavaDoc(6));
84         try {
85             Validate.isTrue(false, "MSG", new Integer JavaDoc(6));
86             fail("Expecting IllegalArgumentException");
87         } catch (IllegalArgumentException JavaDoc ex) {
88             assertEquals("MSG6", ex.getMessage());
89         }
90     }
91
92     //-----------------------------------------------------------------------
93
public void testIsTrue4() {
94         Validate.isTrue(true, "MSG", 7);
95         try {
96             Validate.isTrue(false, "MSG", 7);
97             fail("Expecting IllegalArgumentException");
98         } catch (IllegalArgumentException JavaDoc ex) {
99             assertEquals("MSG7", ex.getMessage());
100         }
101     }
102
103     //-----------------------------------------------------------------------
104
public void testIsTrue5() {
105         Validate.isTrue(true, "MSG", 7.4d);
106         try {
107             Validate.isTrue(false, "MSG", 7.4d);
108             fail("Expecting IllegalArgumentException");
109         } catch (IllegalArgumentException JavaDoc ex) {
110             assertEquals("MSG7.4", ex.getMessage());
111         }
112     }
113
114     //-----------------------------------------------------------------------
115
public void testNotNull1() {
116         Validate.notNull(new Object JavaDoc());
117         try {
118             Validate.notNull(null);
119             fail("Expecting IllegalArgumentException");
120         } catch (IllegalArgumentException JavaDoc ex) {
121             assertEquals("The validated object is null", ex.getMessage());
122         }
123     }
124
125     //-----------------------------------------------------------------------
126
public void testNotNull2() {
127         Validate.notNull(new Object JavaDoc(), "MSG");
128         try {
129             Validate.notNull(null, "MSG");
130             fail("Expecting IllegalArgumentException");
131         } catch (IllegalArgumentException JavaDoc ex) {
132             assertEquals("MSG", ex.getMessage());
133         }
134     }
135
136     //-----------------------------------------------------------------------
137
public void testNotEmptyArray1() {
138         Validate.notEmpty(new Object JavaDoc[] {null});
139         try {
140             Validate.notEmpty((Object JavaDoc[]) null);
141             fail("Expecting IllegalArgumentException");
142         } catch (IllegalArgumentException JavaDoc ex) {
143             assertEquals("The validated array is empty", ex.getMessage());
144         }
145         try {
146             Validate.notEmpty(new Object JavaDoc[0]);
147             fail("Expecting IllegalArgumentException");
148         } catch (IllegalArgumentException JavaDoc ex) {
149             assertEquals("The validated array is empty", ex.getMessage());
150         }
151     }
152
153     //-----------------------------------------------------------------------
154
public void testNotEmptyArray2() {
155         Validate.notEmpty(new Object JavaDoc[] {null}, "MSG");
156         try {
157             Validate.notEmpty((Object JavaDoc[]) null, "MSG");
158             fail("Expecting IllegalArgumentException");
159         } catch (IllegalArgumentException JavaDoc ex) {
160             assertEquals("MSG", ex.getMessage());
161         }
162         try {
163             Validate.notEmpty(new Object JavaDoc[0], "MSG");
164             fail("Expecting IllegalArgumentException");
165         } catch (IllegalArgumentException JavaDoc ex) {
166             assertEquals("MSG", ex.getMessage());
167         }
168     }
169
170     //-----------------------------------------------------------------------
171
public void testNotEmptyCollection1() {
172         Collection JavaDoc coll = new ArrayList JavaDoc();
173         try {
174             Validate.notEmpty((Collection JavaDoc) null);
175             fail("Expecting IllegalArgumentException");
176         } catch (IllegalArgumentException JavaDoc ex) {
177             assertEquals("The validated collection is empty", ex.getMessage());
178         }
179         try {
180             Validate.notEmpty(coll);
181             fail("Expecting IllegalArgumentException");
182         } catch (IllegalArgumentException JavaDoc ex) {
183             assertEquals("The validated collection is empty", ex.getMessage());
184         }
185         coll.add(new Integer JavaDoc(8));
186         Validate.notEmpty(coll);
187     }
188
189     //-----------------------------------------------------------------------
190
public void testNotEmptyCollection2() {
191         Collection JavaDoc coll = new ArrayList JavaDoc();
192         try {
193             Validate.notEmpty((Collection JavaDoc) null, "MSG");
194             fail("Expecting IllegalArgumentException");
195         } catch (IllegalArgumentException JavaDoc ex) {
196             assertEquals("MSG", ex.getMessage());
197         }
198         try {
199             Validate.notEmpty(coll, "MSG");
200             fail("Expecting IllegalArgumentException");
201         } catch (IllegalArgumentException JavaDoc ex) {
202             assertEquals("MSG", ex.getMessage());
203         }
204         coll.add(new Integer JavaDoc(8));
205         Validate.notEmpty(coll, "MSG");
206     }
207
208     //-----------------------------------------------------------------------
209
public void testNotEmptyMap1() {
210         Map JavaDoc map = new HashMap JavaDoc();
211         try {
212             Validate.notEmpty((Map JavaDoc) null);
213             fail("Expecting IllegalArgumentException");
214         } catch (IllegalArgumentException JavaDoc ex) {
215             assertEquals("The validated map is empty", ex.getMessage());
216         }
217         try {
218             Validate.notEmpty(map);
219             fail("Expecting IllegalArgumentException");
220         } catch (IllegalArgumentException JavaDoc ex) {
221             assertEquals("The validated map is empty", ex.getMessage());
222         }
223         map.put("ll", new Integer JavaDoc(8));
224         Validate.notEmpty(map);
225     }
226
227     //-----------------------------------------------------------------------
228
public void testNotEmptyMap2() {
229         Map JavaDoc map = new HashMap JavaDoc();
230         try {
231             Validate.notEmpty((Map JavaDoc) null, "MSG");
232             fail("Expecting IllegalArgumentException");
233         } catch (IllegalArgumentException JavaDoc ex) {
234             assertEquals("MSG", ex.getMessage());
235         }
236         try {
237             Validate.notEmpty(map, "MSG");
238             fail("Expecting IllegalArgumentException");
239         } catch (IllegalArgumentException JavaDoc ex) {
240             assertEquals("MSG", ex.getMessage());
241         }
242         map.put("ll", new Integer JavaDoc(8));
243         Validate.notEmpty(map, "MSG");
244     }
245
246     //-----------------------------------------------------------------------
247
public void testNotEmptyString1() {
248         Validate.notEmpty("hjl");
249         try {
250             Validate.notEmpty((String JavaDoc) null);
251             fail("Expecting IllegalArgumentException");
252         } catch (IllegalArgumentException JavaDoc ex) {
253             assertEquals("The validated string is empty", ex.getMessage());
254         }
255         try {
256             Validate.notEmpty("");
257             fail("Expecting IllegalArgumentException");
258         } catch (IllegalArgumentException JavaDoc ex) {
259             assertEquals("The validated string is empty", ex.getMessage());
260         }
261     }
262
263     //-----------------------------------------------------------------------
264
public void testNotEmptyString2() {
265         Validate.notEmpty("a", "MSG");
266         try {
267             Validate.notEmpty((String JavaDoc) null, "MSG");
268             fail("Expecting IllegalArgumentException");
269         } catch (IllegalArgumentException JavaDoc ex) {
270             assertEquals("MSG", ex.getMessage());
271         }
272         try {
273             Validate.notEmpty("", "MSG");
274             fail("Expecting IllegalArgumentException");
275         } catch (IllegalArgumentException JavaDoc ex) {
276             assertEquals("MSG", ex.getMessage());
277         }
278     }
279
280     //-----------------------------------------------------------------------
281
public void testNoNullElementsArray1() {
282         String JavaDoc[] array = new String JavaDoc[] {"a", "b"};
283         Validate.noNullElements(array);
284         try {
285             Validate.noNullElements((Object JavaDoc[]) null);
286             fail("Expecting IllegalArgumentException");
287         } catch (IllegalArgumentException JavaDoc ex) {
288             assertEquals("The validated object is null", ex.getMessage());
289         }
290         array[1] = null;
291         try {
292             Validate.noNullElements(array);
293             fail("Expecting IllegalArgumentException");
294         } catch (IllegalArgumentException JavaDoc ex) {
295             assertEquals("The validated array contains null element at index: 1", ex.getMessage());
296         }
297     }
298
299     //-----------------------------------------------------------------------
300
public void testNoNullElementsArray2() {
301         String JavaDoc[] array = new String JavaDoc[] {"a", "b"};
302         Validate.noNullElements(array, "MSG");
303         try {
304             Validate.noNullElements((Object JavaDoc[]) null, "MSG");
305             fail("Expecting IllegalArgumentException");
306         } catch (IllegalArgumentException JavaDoc ex) {
307             assertEquals("The validated object is null", ex.getMessage());
308         }
309         array[1] = null;
310         try {
311             Validate.noNullElements(array, "MSG");
312             fail("Expecting IllegalArgumentException");
313         } catch (IllegalArgumentException JavaDoc ex) {
314             assertEquals("MSG", ex.getMessage());
315         }
316     }
317
318     //-----------------------------------------------------------------------
319
public void testNoNullElementsCollection1() {
320         List JavaDoc coll = new ArrayList JavaDoc();
321         coll.add("a");
322         coll.add("b");
323         Validate.noNullElements(coll);
324         try {
325             Validate.noNullElements((Collection JavaDoc) null);
326             fail("Expecting IllegalArgumentException");
327         } catch (IllegalArgumentException JavaDoc ex) {
328             assertEquals("The validated object is null", ex.getMessage());
329         }
330         coll.set(1, null);
331         try {
332             Validate.noNullElements(coll);
333             fail("Expecting IllegalArgumentException");
334         } catch (IllegalArgumentException JavaDoc ex) {
335             assertEquals("The validated collection contains null element at index: 1", ex.getMessage());
336         }
337     }
338
339     //-----------------------------------------------------------------------
340
public void testNoNullElementsCollection2() {
341         List JavaDoc coll = new ArrayList JavaDoc();
342         coll.add("a");
343         coll.add("b");
344         Validate.noNullElements(coll, "MSG");
345         try {
346             Validate.noNullElements((Collection JavaDoc) null, "MSG");
347             fail("Expecting IllegalArgumentException");
348         } catch (IllegalArgumentException JavaDoc ex) {
349             assertEquals("The validated object is null", ex.getMessage());
350         }
351         coll.set(1, null);
352         try {
353             Validate.noNullElements(coll, "MSG");
354             fail("Expecting IllegalArgumentException");
355         } catch (IllegalArgumentException JavaDoc ex) {
356             assertEquals("MSG", ex.getMessage());
357         }
358     }
359
360     //-----------------------------------------------------------------------
361
public void testAllElementsOfType() {
362         List JavaDoc coll = new ArrayList JavaDoc();
363         coll.add("a");
364         coll.add("b");
365         Validate.allElementsOfType(coll, String JavaDoc.class, "MSG");
366         try {
367             Validate.allElementsOfType(coll, Integer JavaDoc.class, "MSG");
368             fail("Expecting IllegalArgumentException");
369         } catch (IllegalArgumentException JavaDoc ex) {
370             assertEquals("MSG", ex.getMessage());
371         }
372         coll.set(1, Boolean.FALSE);
373         try {
374             Validate.allElementsOfType(coll, String JavaDoc.class);
375             fail("Expecting IllegalArgumentException");
376         } catch (IllegalArgumentException JavaDoc ex) {
377             assertEquals("The validated collection contains an element not of type java.lang.String at index: 1", ex.getMessage());
378         }
379         
380         coll = new ArrayList JavaDoc();
381         coll.add(new Integer JavaDoc(5));
382         coll.add(new Double JavaDoc(2.0d));
383         Validate.allElementsOfType(coll, Number JavaDoc.class, "MSG");
384         try {
385             Validate.allElementsOfType(coll, Integer JavaDoc.class, "MSG");
386             fail("Expecting IllegalArgumentException");
387         } catch (IllegalArgumentException JavaDoc ex) {
388             assertEquals("MSG", ex.getMessage());
389         }
390         try {
391             Validate.allElementsOfType(coll, Double JavaDoc.class, "MSG");
392             fail("Expecting IllegalArgumentException");
393         } catch (IllegalArgumentException JavaDoc ex) {
394             assertEquals("MSG", ex.getMessage());
395         }
396     }
397
398 }
399
Popular Tags