KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > ParametersTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import junit.framework.TestCase;
25
26 /**
27  *
28  * @author Andrei Badea
29  */

30 public class ParametersTest extends TestCase {
31
32     public ParametersTest(String JavaDoc testName) {
33         super(testName);
34     }
35
36     public void testNotNull() throws Exception JavaDoc {
37         assertNPEOnNull(Parameters.class.getMethod("notNull", CharSequence JavaDoc.class, Object JavaDoc.class));
38         Parameters.notNull("param", "");
39     }
40
41     public void testNotEmpty() throws Exception JavaDoc {
42         assertNPEOnNull(Parameters.class.getMethod("notEmpty", CharSequence JavaDoc.class, CharSequence JavaDoc.class));
43         try {
44             Parameters.notEmpty("param", "");
45             fail("Should have thrown IAE");
46         } catch (IllegalArgumentException JavaDoc e) {}
47         Parameters.notEmpty("param", "foo");
48     }
49
50     public void testNotWhitespace() throws Exception JavaDoc {
51         assertNPEOnNull(Parameters.class.getMethod("notWhitespace", CharSequence JavaDoc.class, CharSequence JavaDoc.class));
52         try {
53             Parameters.notWhitespace("param", "");
54             fail("Should have thrown IAE");
55         } catch (IllegalArgumentException JavaDoc e) {}
56         try {
57             Parameters.notWhitespace("param", " ");
58             fail("Should have thrown IAE");
59         } catch (IllegalArgumentException JavaDoc e) {}
60         Parameters.notWhitespace("param", " foo ");
61     }
62
63     public void testJavaIdentifier() throws Exception JavaDoc {
64         assertNPEOnNull(Parameters.class.getMethod("javaIdentifier", CharSequence JavaDoc.class, CharSequence JavaDoc.class));
65         try {
66             Parameters.javaIdentifier("param", "");
67             fail("Should have thrown IAE");
68         } catch (IllegalArgumentException JavaDoc e) {}
69         try {
70             Parameters.javaIdentifier("param", "foo#Method");
71             fail("Should have thrown IAE");
72         } catch (IllegalArgumentException JavaDoc e) {}
73         Parameters.javaIdentifier("param", "fooMethod");
74     }
75
76     public void testJavaIdentifierOrNull() throws Exception JavaDoc {
77         try {
78             Parameters.javaIdentifierOrNull("param", "");
79             fail("Should have thrown IAE");
80         } catch (IllegalArgumentException JavaDoc e) {}
81         try {
82             Parameters.javaIdentifierOrNull("param", "foo#Method");
83             fail("Should have thrown IAE");
84         } catch (IllegalArgumentException JavaDoc e) {}
85         Parameters.javaIdentifierOrNull("param", null);
86         Parameters.javaIdentifierOrNull("param", "fooMethod");
87     }
88
89     private void assertNPEOnNull(Method JavaDoc method) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc {
90         try {
91             method.invoke(null, "param", null);
92             fail("Should have thrown NPE");
93         } catch (InvocationTargetException JavaDoc e) {
94             Throwable JavaDoc target = e.getTargetException();
95             assertEquals(NullPointerException JavaDoc.class, target.getClass());
96             // ensure the NPE was thrown by us, not by the VM
97
assertEquals("The param parameter cannot be null", target.getMessage());
98         }
99     }
100 }
101
Popular Tags