001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.util;
043:
044: import java.lang.reflect.InvocationTargetException;
045: import java.lang.reflect.Method;
046: import junit.framework.TestCase;
047:
048: /**
049: *
050: * @author Andrei Badea
051: */
052: public class ParametersTest extends TestCase {
053:
054: public ParametersTest(String testName) {
055: super (testName);
056: }
057:
058: public void testNotNull() throws Exception {
059: assertNPEOnNull(Parameters.class.getMethod("notNull",
060: CharSequence.class, Object.class));
061: Parameters.notNull("param", "");
062: }
063:
064: public void testNotEmpty() throws Exception {
065: assertNPEOnNull(Parameters.class.getMethod("notEmpty",
066: CharSequence.class, CharSequence.class));
067: try {
068: Parameters.notEmpty("param", "");
069: fail("Should have thrown IAE");
070: } catch (IllegalArgumentException e) {
071: }
072: Parameters.notEmpty("param", "foo");
073: }
074:
075: public void testNotWhitespace() throws Exception {
076: assertNPEOnNull(Parameters.class.getMethod("notWhitespace",
077: CharSequence.class, CharSequence.class));
078: try {
079: Parameters.notWhitespace("param", "");
080: fail("Should have thrown IAE");
081: } catch (IllegalArgumentException e) {
082: }
083: try {
084: Parameters.notWhitespace("param", " ");
085: fail("Should have thrown IAE");
086: } catch (IllegalArgumentException e) {
087: }
088: Parameters.notWhitespace("param", " foo ");
089: }
090:
091: public void testJavaIdentifier() throws Exception {
092: assertNPEOnNull(Parameters.class.getMethod("javaIdentifier",
093: CharSequence.class, CharSequence.class));
094: try {
095: Parameters.javaIdentifier("param", "");
096: fail("Should have thrown IAE");
097: } catch (IllegalArgumentException e) {
098: }
099: try {
100: Parameters.javaIdentifier("param", "foo#Method");
101: fail("Should have thrown IAE");
102: } catch (IllegalArgumentException e) {
103: }
104: Parameters.javaIdentifier("param", "fooMethod");
105: }
106:
107: public void testJavaIdentifierOrNull() throws Exception {
108: try {
109: Parameters.javaIdentifierOrNull("param", "");
110: fail("Should have thrown IAE");
111: } catch (IllegalArgumentException e) {
112: }
113: try {
114: Parameters.javaIdentifierOrNull("param", "foo#Method");
115: fail("Should have thrown IAE");
116: } catch (IllegalArgumentException e) {
117: }
118: Parameters.javaIdentifierOrNull("param", null);
119: Parameters.javaIdentifierOrNull("param", "fooMethod");
120: }
121:
122: private void assertNPEOnNull(Method method)
123: throws IllegalAccessException, InvocationTargetException,
124: NoSuchMethodException {
125: try {
126: method.invoke(null, "param", null);
127: fail("Should have thrown NPE");
128: } catch (InvocationTargetException e) {
129: Throwable target = e.getTargetException();
130: assertEquals(NullPointerException.class, target.getClass());
131: // ensure the NPE was thrown by us, not by the VM
132: assertEquals("The param parameter cannot be null", target
133: .getMessage());
134: }
135: }
136: }
|