1 package test.javassist.proxy; 2 3 import javassist.*; 4 import javassist.util.proxy.MethodFilter; 5 import javassist.util.proxy.MethodHandler; 6 import javassist.util.proxy.Proxy; 7 import javassist.util.proxy.ProxyFactory; 8 import javassist.util.proxy.ProxyObject; 9 import junit.framework.TestCase; 10 11 import java.lang.reflect.Method; 12 13 /** 14 * test which checks that it is still possible to use the old style proxy factory api 15 * to create proxy classes which set their own handler. it checks that caching is 16 * automatically disabled if this legacy api is used. it also exercises the new style 17 * api, ensuring that caching works correctly with this model. 18 */ 19 @SuppressWarnings({"rawtypes","unchecked","unused"}) 20 public class ProxyFactoryCompatibilityTest extends TestCase 21 { 22 private ClassPool basePool; 23 MethodFilter filter; 24 MethodHandler handler; 25 setUp()26 protected void setUp() 27 { 28 basePool = ClassPool.getDefault(); 29 filter = new MethodFilter() { 30 public boolean isHandled(Method m) { 31 return !m.getName().equals("finalize"); 32 } 33 }; 34 35 handler = new MethodHandler() { 36 public Object invoke(Object self, Method m, Method proceed, 37 Object[] args) throws Throwable { 38 System.out.println("calling: " + m.getName()); 39 return proceed.invoke(self, args); // execute the original method. 40 } 41 }; 42 } 43 testFactoryCompatibility()44 public void testFactoryCompatibility() throws Exception 45 { 46 System.out.println("ProxyFactory.useCache = " + ProxyFactory.useCache); 47 // create a factory which, by default, uses caching 48 ProxyFactory factory = new ProxyFactory(); 49 factory.setSuperclass(TestClass.class); 50 factory.setInterfaces(new Class[] { TestInterface.class}); 51 factory.setFilter(filter); 52 53 // create the same class twice and check that it is reused 54 Class proxyClass1 = factory.createClass(); 55 System.out.println("created first class " + proxyClass1.getName()); 56 TestClass proxy1 = (TestClass)proxyClass1.getConstructor().newInstance(); 57 ((ProxyObject) proxy1).setHandler(handler); 58 proxy1.testMethod(); 59 assertTrue(proxy1.isTestCalled()); 60 61 Class proxyClass2 = factory.createClass(); 62 System.out.println("created second class " + proxyClass2.getName()); 63 TestClass proxy2 = (TestClass)proxyClass2.getConstructor().newInstance(); 64 ((ProxyObject) proxy2).setHandler(handler); 65 proxy2.testMethod(); 66 assertTrue(proxy2.isTestCalled()); 67 68 assertTrue(proxyClass1 == proxyClass2); 69 70 // create a factory which, by default, uses caching then set the handler so it creates 71 // classes which do not get cached. 72 ProxyFactory factory2 = new ProxyFactory(); 73 factory.setSuperclass(TestClass.class); 74 factory.setInterfaces(new Class[] { TestInterface.class}); 75 factory.setFilter(filter); 76 77 // create the same class twice and check that it is reused 78 Class proxyClass3 = factory.createClass(); 79 System.out.println("created third class " + proxyClass3.getName()); 80 TestClass proxy3 = (TestClass)proxyClass3.getConstructor().newInstance(); 81 ((Proxy)proxy3).setHandler(handler); 82 proxy3.testMethod(); 83 assertTrue(proxy3.isTestCalled()); 84 85 Class proxyClass4 = factory.createClass(); 86 System.out.println("created fourth class " + proxyClass4.getName()); 87 TestClass proxy4 = (TestClass)proxyClass4.getConstructor().newInstance(); 88 ((Proxy)proxy4).setHandler(handler); 89 proxy4.testMethod(); 90 assertTrue(proxy4.isTestCalled()); 91 92 assertTrue(proxyClass3 == proxyClass4); 93 } 94 95 /** 96 * test class used as the super for the proxy 97 */ 98 public static class TestClass { 99 private boolean testCalled = false; testMethod()100 public void testMethod() 101 { 102 // record the call 103 testCalled = true; 104 } isTestCalled()105 public boolean isTestCalled() 106 { 107 return testCalled; 108 } 109 } 110 111 /** 112 * test interface used as an interface implemented by the proxy 113 */ 114 public static interface TestInterface { testMethod()115 public void testMethod(); 116 } 117 118 }