1 4 package com.tc.common.proxy; 5 6 import com.tc.common.proxy.subpkg.Factory; 7 import com.tc.common.proxy.subpkg.TestInterface; 8 9 import java.io.IOException ; 10 import java.lang.reflect.Proxy ; 11 12 import junit.framework.TestCase; 13 14 17 public class DelegatingInvocationHandlerTest extends TestCase { 18 19 public static interface TheInterface { 20 String a(String arg); 21 22 String b(String arg); 23 24 String c(String arg); 25 26 void d() throws IOException ; 27 28 void e() throws IOException ; 29 } 30 31 public static class Handler { 32 private int numACalls; 33 private String lastAArg; 34 35 public Handler() { 36 reset(); 37 } 38 39 public String a(String arg) { 40 ++numACalls; 41 lastAArg = arg; 42 return "handler.a"; 43 } 44 45 public void reset() { 46 numACalls = 0; 47 lastAArg = null; 48 } 49 50 public String getLastAArg() { 51 return this.lastAArg; 52 } 53 54 public int getNumACalls() { 55 return this.numACalls; 56 } 57 58 public void d() throws IOException { 59 throw new IOException ("handler"); 60 } 61 } 62 63 public static class Delegate { 64 private int numACalls; 65 private String lastAArg; 66 private int numBCalls; 67 private String lastBArg; 68 69 public Delegate() { 70 reset(); 71 } 72 73 public String a(String arg) { 74 ++numACalls; 75 lastAArg = arg; 76 return "delegate.a"; 77 } 78 79 public String b(String arg) { 80 ++numBCalls; 81 lastBArg = arg; 82 return "delegate.b"; 83 } 84 85 public void d() { 86 } 88 89 public void e() throws IOException { 90 throw new IOException ("delegate"); 91 } 92 93 public void reset() { 94 numACalls = 0; 95 lastAArg = null; 96 } 97 98 public String getLastAArg() { 99 return this.lastAArg; 100 } 101 102 public String getLastBArg() { 103 return this.lastBArg; 104 } 105 106 public int getNumACalls() { 107 return this.numACalls; 108 } 109 110 public int getNumBCalls() { 111 return this.numBCalls; 112 } 113 } 114 115 private Handler handler; 116 private Delegate delegate; 117 private DelegatingInvocationHandler invocationHandler; 118 private TheInterface theProxy; 119 120 protected void setUp() throws Exception { 121 this.handler = new Handler(); 122 this.delegate = new Delegate(); 123 this.invocationHandler = new DelegatingInvocationHandler(delegate, handler); 124 theProxy = (TheInterface) Proxy.newProxyInstance(DelegatingInvocationHandlerTest.class.getClassLoader(), 125 new Class [] { TheInterface.class }, invocationHandler); 126 } 127 128 public void testDelegates() throws Exception { 129 String value = theProxy.a("x"); 130 assertEquals("handler.a", value); 131 assertEquals(1, handler.getNumACalls()); 132 assertEquals("x", handler.getLastAArg()); 133 assertEquals(0, delegate.getNumACalls()); 134 assertEquals(0, delegate.getNumBCalls()); 135 136 handler.reset(); 137 138 value = theProxy.b("y"); 139 assertEquals("delegate.b", value); 140 assertEquals(0, handler.getNumACalls()); 141 assertEquals(0, delegate.getNumACalls()); 142 assertEquals(1, delegate.getNumBCalls()); 143 assertEquals("y", delegate.getLastBArg()); 144 } 145 146 public void testException() throws Exception { 147 try { 148 theProxy.e(); 149 fail("e() didn't throw exception"); 150 } catch (IOException ioe) { 151 assertTrue(ioe.getMessage().indexOf("delegate") >= 0); 152 } 153 154 try { 155 theProxy.d(); 156 fail("d() didn't throw exception"); 157 } catch (IOException ioe) { 158 assertTrue(ioe.getMessage().indexOf("handler") >= 0); 159 } 160 } 161 162 public void testNonPublicDelegate() { 163 TestInterface proxy = (TestInterface) DelegateHelper.createDelegate(TestInterface.class, Factory.getInstance()); 164 proxy.method(); 165 } 166 167 } 168 | Popular Tags |