1 6 package net.sourceforge.jwebunit.util.reflect; 7 8 import junit.framework.TestCase; 9 10 import java.lang.reflect.InvocationTargetException ; 11 12 13 public class MethodInvokerTest extends TestCase { 14 private Receiver receiver; 15 16 public MethodInvokerTest(String s) { 17 super(s); 18 } 19 20 protected void setUp() throws Exception { 21 super.setUp(); 22 receiver = new Receiver(); 23 } 24 25 public void testNoArg() throws Exception { 26 MethodInvoker invoker = new MethodInvoker(receiver, "noArg"); 27 invoker.invoke(); 28 assertTrue(receiver.noArgCalled); 29 } 30 31 public void testOneArg() throws Exception { 32 MethodInvoker invoker = new MethodInvoker(receiver, "oneArg", "anArg"); 33 invoker.invoke(); 34 assertTrue(receiver.oneArgCalled); 35 } 36 37 public void testMultipleArgs() throws Exception { 38 MethodInvoker invoker = new MethodInvoker(receiver, "multiArg", new Object []{"arg1", "arg2"}); 39 invoker.invoke(); 40 assertTrue(receiver.multiArgCalled); 41 } 42 43 public void testNoMethod() { 44 try { 45 MethodInvoker invoker = new MethodInvoker(receiver, "noMethod"); 46 invoker.invoke(); 47 fail(); 48 } catch (NoSuchMethodException e) { 49 } catch (Exception e) { 50 fail(); 51 } 52 } 53 54 public void testMethodThrowsException() throws Exception { 55 MethodInvoker invoker = new MethodInvoker(receiver, "throwRuntime"); 56 try { 57 invoker.invoke(); 58 } catch (InvocationTargetException e) { 59 assertEquals(e.getTargetException().getClass(), RuntimeException .class); 60 } 61 } 62 63 public void testPrimitiveArgs() throws Exception { 64 MethodInvoker invoker = new MethodInvoker(receiver, "primitiveArg", new Integer (1)); 65 invoker.invoke(); 66 assertTrue(receiver.primitiveArgCalled); 67 } 68 69 public void testAllPrimitives() throws Exception { 70 Object [] args = new Object []{new Boolean (true), new Byte ((byte) 1), 71 new Character ('c'), new Double (1), 72 new Float (1), new Integer (1), 73 new Long (1), new Short ((short) 1)}; 74 MethodInvoker invoker = new MethodInvoker(receiver, "allPrimitives", args); 75 invoker.invoke(); 76 assertTrue(receiver.allPrimitivesCalled); 77 } 78 79 class Receiver { 80 private boolean noArgCalled; 81 private boolean oneArgCalled; 82 private boolean multiArgCalled; 83 private boolean primitiveArgCalled; 84 private boolean allPrimitivesCalled; 85 86 public void noArg() { 87 noArgCalled = true; 88 } 89 90 public void oneArg(String arg) { 91 oneArgCalled = true; 92 } 93 94 public void multiArg(String arg1, String arg2) { 95 multiArgCalled = true; 96 } 97 98 public void primitiveArg(int i) { 99 primitiveArgCalled = true; 100 } 101 102 public void allPrimitives(boolean bool, byte b, 103 char c, double d, 104 float f, int i, 105 long l, short s) { 106 allPrimitivesCalled = true; 107 } 108 109 public void throwRuntime() { 110 throw new RuntimeException (); 111 } 112 } 113 } 114 | Popular Tags |