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