您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ProxyFactoryCompatibilityTest.java 4.2KB

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