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