1 package dynaop; 2 3 import dynaop.util.Classes; 4 import junit.framework.TestCase; 5 6 11 public class InvocationTest extends TestCase { 12 13 public void testToString() { 14 InvocationImpl i = new InvocationImpl(); 15 16 i.setMethod(Classes.EQUALS_METHOD); 17 i.setProxy(new MockProxy()); 18 19 i.toString(); 20 21 i.setArguments(new Object [1]); 22 i.setTarget(new Object ()); 23 i.setInterceptors(new Interceptor[0]); 24 25 i.toString(); 26 } 27 28 public void testWithNoMixin() throws Throwable { 29 try { 30 InvocationImpl i = new InvocationImpl(); 31 32 i.setArguments(new Object [] { null, null }); 33 i.setMethod(A.class.getMethod("a", 34 new Class [] { String .class, Object .class })); 35 36 i.proceed(); 37 38 fail(); 39 } 40 catch (NullPointerException e) { 41 } 42 } 43 44 public void testWithInterceptors() throws Throwable { 45 TestInterceptor[] interceptors = new TestInterceptor[] { 46 new TestInterceptor(), 47 new TestInterceptor(), 48 new TestInterceptor() 49 }; 50 51 A target = new A(); 52 String foo = "foo"; 53 Object bar = new Object (); 54 55 InvocationImpl i = new InvocationImpl(); 56 57 i.setInterceptors(interceptors); 58 i.setTarget(target); 59 i.setArguments(new Object [] { foo, bar }); 60 i.setMethod(A.class.getMethod("a", 61 new Class [] { String .class, Object .class })); 62 i.setProxy(new MockProxy()); 63 64 assertEquals("result", i.proceed()); 65 66 interceptors[0].assertCalled(); 67 interceptors[1].assertCalled(); 68 interceptors[2].assertCalled(); 69 70 target.assertCalled(); 71 target.test(foo, bar); 72 } 73 74 public void testMultipleInvocations() throws Throwable { 75 TestInterceptor[] interceptors = new TestInterceptor[] { 76 new TestInterceptor(), 77 new TestInterceptor(), 78 new TestInterceptor() 79 }; 80 81 A target = new A(); 82 83 InvocationImpl i = new InvocationImpl(); 84 85 i.setInterceptors(interceptors); 86 i.setTarget(target); 87 i.setArguments(new Object [] { "foo", "bar" }); 88 i.setMethod(A.class.getMethod("a", 89 new Class [] { String .class, Object .class })); 90 i.setProxy(new MockProxy()); 91 92 i.proceed(); 93 i.proceed(); 94 i.proceed(); 95 96 for (int index = 0; index < interceptors.length; index++) 97 assertEquals(3, interceptors[index].calls); 98 assertEquals(3, target.calls); 99 } 100 101 public static class A { 102 103 String foo; 104 Object bar; 105 boolean called; 106 int calls; 107 108 public String a(String foo, Object bar) { 109 this.called = true; 110 this.foo = foo; 111 this.bar = bar; 112 this.calls++; 113 return "result"; 114 } 115 116 void test(String foo, Object bar) { 117 assertEquals(foo, this.foo); 118 assertEquals(bar, this.bar); 119 } 120 121 void assertCalled() { 122 assertTrue(this.called); 123 } 124 } 125 126 127 static class TestInterceptor implements Interceptor { 128 129 boolean called; 130 int calls = 0; 131 132 public Object intercept(Invocation i) throws Throwable { 133 this.called = true; 134 calls++; 135 return i.proceed(); 136 } 137 138 void assertCalled() { 139 assertTrue(this.called); 140 } 141 } 142 } 143 | Popular Tags |