001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.commons.lang;
020:
021: import java.lang.reflect.Constructor;
022: import java.lang.reflect.Modifier;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032: import junit.textui.TestRunner;
033:
034: /**
035: * Unit tests {@link org.apache.commons.lang.util.Validate}.
036: *
037: * @author Stephen Colebourne
038: * @author Norm Deane
039: * @version $Id: ValidateTest.java 437554 2006-08-28 06:21:41Z bayard $
040: */
041: public class ValidateTest extends TestCase {
042:
043: public ValidateTest(String name) {
044: super (name);
045: }
046:
047: public static void main(String[] args) {
048: TestRunner.run(suite());
049: }
050:
051: public static Test suite() {
052: TestSuite suite = new TestSuite(ValidateTest.class);
053: suite.setName("Validate Tests");
054: return suite;
055: }
056:
057: protected void setUp() throws Exception {
058: super .setUp();
059: }
060:
061: protected void tearDown() throws Exception {
062: super .tearDown();
063: }
064:
065: //-----------------------------------------------------------------------
066: public void testIsTrue1() {
067: Validate.isTrue(true);
068: try {
069: Validate.isTrue(false);
070: fail("Expecting IllegalArgumentException");
071: } catch (IllegalArgumentException ex) {
072: assertEquals("The validated expression is false", ex
073: .getMessage());
074: }
075: }
076:
077: //-----------------------------------------------------------------------
078: public void testIsTrue2() {
079: Validate.isTrue(true, "MSG");
080: try {
081: Validate.isTrue(false, "MSG");
082: fail("Expecting IllegalArgumentException");
083: } catch (IllegalArgumentException ex) {
084: assertEquals("MSG", ex.getMessage());
085: }
086: }
087:
088: //-----------------------------------------------------------------------
089: public void testIsTrue3() {
090: Validate.isTrue(true, "MSG", new Integer(6));
091: try {
092: Validate.isTrue(false, "MSG", new Integer(6));
093: fail("Expecting IllegalArgumentException");
094: } catch (IllegalArgumentException ex) {
095: assertEquals("MSG6", ex.getMessage());
096: }
097: }
098:
099: //-----------------------------------------------------------------------
100: public void testIsTrue4() {
101: Validate.isTrue(true, "MSG", 7);
102: try {
103: Validate.isTrue(false, "MSG", 7);
104: fail("Expecting IllegalArgumentException");
105: } catch (IllegalArgumentException ex) {
106: assertEquals("MSG7", ex.getMessage());
107: }
108: }
109:
110: //-----------------------------------------------------------------------
111: public void testIsTrue5() {
112: Validate.isTrue(true, "MSG", 7.4d);
113: try {
114: Validate.isTrue(false, "MSG", 7.4d);
115: fail("Expecting IllegalArgumentException");
116: } catch (IllegalArgumentException ex) {
117: assertEquals("MSG7.4", ex.getMessage());
118: }
119: }
120:
121: //-----------------------------------------------------------------------
122: public void testNotNull1() {
123: Validate.notNull(new Object());
124: try {
125: Validate.notNull(null);
126: fail("Expecting IllegalArgumentException");
127: } catch (IllegalArgumentException ex) {
128: assertEquals("The validated object is null", ex
129: .getMessage());
130: }
131: }
132:
133: //-----------------------------------------------------------------------
134: public void testNotNull2() {
135: Validate.notNull(new Object(), "MSG");
136: try {
137: Validate.notNull(null, "MSG");
138: fail("Expecting IllegalArgumentException");
139: } catch (IllegalArgumentException ex) {
140: assertEquals("MSG", ex.getMessage());
141: }
142: }
143:
144: //-----------------------------------------------------------------------
145: public void testNotEmptyArray1() {
146: Validate.notEmpty(new Object[] { null });
147: try {
148: Validate.notEmpty((Object[]) null);
149: fail("Expecting IllegalArgumentException");
150: } catch (IllegalArgumentException ex) {
151: assertEquals("The validated array is empty", ex
152: .getMessage());
153: }
154: try {
155: Validate.notEmpty(new Object[0]);
156: fail("Expecting IllegalArgumentException");
157: } catch (IllegalArgumentException ex) {
158: assertEquals("The validated array is empty", ex
159: .getMessage());
160: }
161: }
162:
163: //-----------------------------------------------------------------------
164: public void testNotEmptyArray2() {
165: Validate.notEmpty(new Object[] { null }, "MSG");
166: try {
167: Validate.notEmpty((Object[]) null, "MSG");
168: fail("Expecting IllegalArgumentException");
169: } catch (IllegalArgumentException ex) {
170: assertEquals("MSG", ex.getMessage());
171: }
172: try {
173: Validate.notEmpty(new Object[0], "MSG");
174: fail("Expecting IllegalArgumentException");
175: } catch (IllegalArgumentException ex) {
176: assertEquals("MSG", ex.getMessage());
177: }
178: }
179:
180: //-----------------------------------------------------------------------
181: public void testNotEmptyCollection1() {
182: Collection coll = new ArrayList();
183: try {
184: Validate.notEmpty((Collection) null);
185: fail("Expecting IllegalArgumentException");
186: } catch (IllegalArgumentException ex) {
187: assertEquals("The validated collection is empty", ex
188: .getMessage());
189: }
190: try {
191: Validate.notEmpty(coll);
192: fail("Expecting IllegalArgumentException");
193: } catch (IllegalArgumentException ex) {
194: assertEquals("The validated collection is empty", ex
195: .getMessage());
196: }
197: coll.add(new Integer(8));
198: Validate.notEmpty(coll);
199: }
200:
201: //-----------------------------------------------------------------------
202: public void testNotEmptyCollection2() {
203: Collection coll = new ArrayList();
204: try {
205: Validate.notEmpty((Collection) null, "MSG");
206: fail("Expecting IllegalArgumentException");
207: } catch (IllegalArgumentException ex) {
208: assertEquals("MSG", ex.getMessage());
209: }
210: try {
211: Validate.notEmpty(coll, "MSG");
212: fail("Expecting IllegalArgumentException");
213: } catch (IllegalArgumentException ex) {
214: assertEquals("MSG", ex.getMessage());
215: }
216: coll.add(new Integer(8));
217: Validate.notEmpty(coll, "MSG");
218: }
219:
220: //-----------------------------------------------------------------------
221: public void testNotEmptyMap1() {
222: Map map = new HashMap();
223: try {
224: Validate.notEmpty((Map) null);
225: fail("Expecting IllegalArgumentException");
226: } catch (IllegalArgumentException ex) {
227: assertEquals("The validated map is empty", ex.getMessage());
228: }
229: try {
230: Validate.notEmpty(map);
231: fail("Expecting IllegalArgumentException");
232: } catch (IllegalArgumentException ex) {
233: assertEquals("The validated map is empty", ex.getMessage());
234: }
235: map.put("ll", new Integer(8));
236: Validate.notEmpty(map);
237: }
238:
239: //-----------------------------------------------------------------------
240: public void testNotEmptyMap2() {
241: Map map = new HashMap();
242: try {
243: Validate.notEmpty((Map) null, "MSG");
244: fail("Expecting IllegalArgumentException");
245: } catch (IllegalArgumentException ex) {
246: assertEquals("MSG", ex.getMessage());
247: }
248: try {
249: Validate.notEmpty(map, "MSG");
250: fail("Expecting IllegalArgumentException");
251: } catch (IllegalArgumentException ex) {
252: assertEquals("MSG", ex.getMessage());
253: }
254: map.put("ll", new Integer(8));
255: Validate.notEmpty(map, "MSG");
256: }
257:
258: //-----------------------------------------------------------------------
259: public void testNotEmptyString1() {
260: Validate.notEmpty("hjl");
261: try {
262: Validate.notEmpty((String) null);
263: fail("Expecting IllegalArgumentException");
264: } catch (IllegalArgumentException ex) {
265: assertEquals("The validated string is empty", ex
266: .getMessage());
267: }
268: try {
269: Validate.notEmpty("");
270: fail("Expecting IllegalArgumentException");
271: } catch (IllegalArgumentException ex) {
272: assertEquals("The validated string is empty", ex
273: .getMessage());
274: }
275: }
276:
277: //-----------------------------------------------------------------------
278: public void testNotEmptyString2() {
279: Validate.notEmpty("a", "MSG");
280: try {
281: Validate.notEmpty((String) null, "MSG");
282: fail("Expecting IllegalArgumentException");
283: } catch (IllegalArgumentException ex) {
284: assertEquals("MSG", ex.getMessage());
285: }
286: try {
287: Validate.notEmpty("", "MSG");
288: fail("Expecting IllegalArgumentException");
289: } catch (IllegalArgumentException ex) {
290: assertEquals("MSG", ex.getMessage());
291: }
292: }
293:
294: //-----------------------------------------------------------------------
295: public void testNoNullElementsArray1() {
296: String[] array = new String[] { "a", "b" };
297: Validate.noNullElements(array);
298: try {
299: Validate.noNullElements((Object[]) null);
300: fail("Expecting IllegalArgumentException");
301: } catch (IllegalArgumentException ex) {
302: assertEquals("The validated object is null", ex
303: .getMessage());
304: }
305: array[1] = null;
306: try {
307: Validate.noNullElements(array);
308: fail("Expecting IllegalArgumentException");
309: } catch (IllegalArgumentException ex) {
310: assertEquals(
311: "The validated array contains null element at index: 1",
312: ex.getMessage());
313: }
314: }
315:
316: //-----------------------------------------------------------------------
317: public void testNoNullElementsArray2() {
318: String[] array = new String[] { "a", "b" };
319: Validate.noNullElements(array, "MSG");
320: try {
321: Validate.noNullElements((Object[]) null, "MSG");
322: fail("Expecting IllegalArgumentException");
323: } catch (IllegalArgumentException ex) {
324: assertEquals("The validated object is null", ex
325: .getMessage());
326: }
327: array[1] = null;
328: try {
329: Validate.noNullElements(array, "MSG");
330: fail("Expecting IllegalArgumentException");
331: } catch (IllegalArgumentException ex) {
332: assertEquals("MSG", ex.getMessage());
333: }
334: }
335:
336: //-----------------------------------------------------------------------
337: public void testNoNullElementsCollection1() {
338: List coll = new ArrayList();
339: coll.add("a");
340: coll.add("b");
341: Validate.noNullElements(coll);
342: try {
343: Validate.noNullElements((Collection) null);
344: fail("Expecting IllegalArgumentException");
345: } catch (IllegalArgumentException ex) {
346: assertEquals("The validated object is null", ex
347: .getMessage());
348: }
349: coll.set(1, null);
350: try {
351: Validate.noNullElements(coll);
352: fail("Expecting IllegalArgumentException");
353: } catch (IllegalArgumentException ex) {
354: assertEquals(
355: "The validated collection contains null element at index: 1",
356: ex.getMessage());
357: }
358: }
359:
360: //-----------------------------------------------------------------------
361: public void testNoNullElementsCollection2() {
362: List coll = new ArrayList();
363: coll.add("a");
364: coll.add("b");
365: Validate.noNullElements(coll, "MSG");
366: try {
367: Validate.noNullElements((Collection) null, "MSG");
368: fail("Expecting IllegalArgumentException");
369: } catch (IllegalArgumentException ex) {
370: assertEquals("The validated object is null", ex
371: .getMessage());
372: }
373: coll.set(1, null);
374: try {
375: Validate.noNullElements(coll, "MSG");
376: fail("Expecting IllegalArgumentException");
377: } catch (IllegalArgumentException ex) {
378: assertEquals("MSG", ex.getMessage());
379: }
380: }
381:
382: //-----------------------------------------------------------------------
383: public void testAllElementsOfType() {
384: List coll = new ArrayList();
385: coll.add("a");
386: coll.add("b");
387: Validate.allElementsOfType(coll, String.class, "MSG");
388: Validate.allElementsOfType(coll, String.class);
389: try {
390: Validate.allElementsOfType(coll, Integer.class, "MSG");
391: fail("Expecting IllegalArgumentException");
392: } catch (IllegalArgumentException ex) {
393: assertEquals("MSG", ex.getMessage());
394: }
395: coll.set(1, Boolean.FALSE);
396: try {
397: Validate.allElementsOfType(coll, String.class);
398: fail("Expecting IllegalArgumentException");
399: } catch (IllegalArgumentException ex) {
400: assertEquals(
401: "The validated collection contains an element not of type java.lang.String at index: 1",
402: ex.getMessage());
403: }
404:
405: coll = new ArrayList();
406: coll.add(new Integer(5));
407: coll.add(new Double(2.0d));
408: Validate.allElementsOfType(coll, Number.class, "MSG");
409: try {
410: Validate.allElementsOfType(coll, Integer.class, "MSG");
411: fail("Expecting IllegalArgumentException");
412: } catch (IllegalArgumentException ex) {
413: assertEquals("MSG", ex.getMessage());
414: }
415: try {
416: Validate.allElementsOfType(coll, Double.class, "MSG");
417: fail("Expecting IllegalArgumentException");
418: } catch (IllegalArgumentException ex) {
419: assertEquals("MSG", ex.getMessage());
420: }
421: }
422:
423: public void testConstructor() {
424: assertNotNull(new Validate());
425: Constructor[] cons = Validate.class.getDeclaredConstructors();
426: assertEquals(1, cons.length);
427: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
428: assertEquals(true, Modifier.isPublic(Validate.class
429: .getModifiers()));
430: assertEquals(false, Modifier.isFinal(Validate.class
431: .getModifiers()));
432: }
433:
434: }
|