diff options
author | adinn <adinn@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2010-04-08 08:12:32 +0000 |
---|---|---|
committer | adinn <adinn@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2010-04-08 08:12:32 +0000 |
commit | bd1c47e0f555ada2d250a52f4a3b6697107486b0 (patch) | |
tree | a328392fa9d2749b5738096aa6f18083a82b6844 /src/test | |
parent | afb9e148bf74f4726a783869feaa3f0e54c2a261 (diff) | |
download | javassist-bd1c47e0f555ada2d250a52f4a3b6697107486b0.tar.gz javassist-bd1c47e0f555ada2d250a52f4a3b6697107486b0.zip |
fixes for JASSIST-42 and JASSIST-97
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@522 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/test')
3 files changed, 267 insertions, 1 deletions
diff --git a/src/test/test/javassist/proxy/ProxyCacheGCTest.java b/src/test/test/javassist/proxy/ProxyCacheGCTest.java index 8f583a37..062872b7 100644 --- a/src/test/test/javassist/proxy/ProxyCacheGCTest.java +++ b/src/test/test/javassist/proxy/ProxyCacheGCTest.java @@ -4,6 +4,7 @@ import javassist.*; import javassist.util.proxy.MethodFilter; import javassist.util.proxy.MethodHandler; import javassist.util.proxy.ProxyFactory; +import javassist.util.proxy.ProxyObject; import junit.framework.TestCase; /** @@ -92,13 +93,13 @@ public class ProxyCacheGCTest extends TestCase MethodFilter filter = (MethodFilter)javaFilterClass.newInstance(); // ok, now create a factory and a proxy class and proxy from that factory - factory.setHandler(handler); factory.setFilter(filter); factory.setSuperclass(javaTargetClass); // factory.setSuperclass(Object.class); Class proxyClass = factory.createClass(); Object target = proxyClass.newInstance(); + ((ProxyObject)target).setHandler(handler); } catch (Exception e) { e.printStackTrace(); fail("cannot create proxy " + e); diff --git a/src/test/test/javassist/proxy/ProxyFactoryCompatibilityTest.java b/src/test/test/javassist/proxy/ProxyFactoryCompatibilityTest.java new file mode 100644 index 00000000..069d0c9c --- /dev/null +++ b/src/test/test/javassist/proxy/ProxyFactoryCompatibilityTest.java @@ -0,0 +1,114 @@ +package test.javassist.proxy; + +import javassist.*; +import javassist.util.proxy.MethodFilter; +import javassist.util.proxy.MethodHandler; +import javassist.util.proxy.ProxyFactory; +import javassist.util.proxy.ProxyObject; +import junit.framework.TestCase; + +import java.lang.reflect.Method; + +/** + * test which checks that it is still possible to use the old style proxy factory api + * to create proxy classes which set their own handler. it checks that caching is + * automatically disabled if this legacy api is used. it also exercises the new style + * api, ensuring that caching works correctly with this model. + */ +public class ProxyFactoryCompatibilityTest extends TestCase +{ + private ClassPool basePool; + MethodFilter filter; + MethodHandler handler; + + protected void setUp() + { + basePool = ClassPool.getDefault(); + filter = new MethodFilter() { + public boolean isHandled(Method m) { + return !m.getName().equals("finalize"); + } + }; + + handler = new MethodHandler() { + public Object invoke(Object self, Method m, Method proceed, + Object[] args) throws Throwable { + System.out.println("calling: " + m.getName()); + return proceed.invoke(self, args); // execute the original method. + } + }; + } + + public void testFactoryCompatibility() throws Exception + { + // create a factory which, by default, uses caching + ProxyFactory factory = new ProxyFactory(); + factory.setSuperclass(TestClass.class); + factory.setInterfaces(new Class[] { TestInterface.class}); + factory.setFilter(filter); + + // create the same class twice and check that it is reused + Class proxyClass1 = factory.createClass(); + System.out.println("created first class " + proxyClass1.getName()); + TestClass proxy1 = (TestClass)proxyClass1.newInstance(); + ((ProxyObject) proxy1).setHandler(handler); + proxy1.testMethod(); + assertTrue(proxy1.isTestCalled()); + + Class proxyClass2 = factory.createClass(); + System.out.println("created second class " + proxyClass2.getName()); + TestClass proxy2 = (TestClass)proxyClass2.newInstance(); + ((ProxyObject) proxy2).setHandler(handler); + proxy2.testMethod(); + assertTrue(proxy2.isTestCalled()); + + assertTrue(proxyClass1 == proxyClass2); + + // create a factory which, by default, uses caching then set the handler so it creates + // classes which do not get cached. + ProxyFactory factory2 = new ProxyFactory(); + factory.setSuperclass(TestClass.class); + factory.setInterfaces(new Class[] { TestInterface.class}); + factory.setFilter(filter); + factory.setHandler(handler); + + // create the same class twice and check that it is reused + Class proxyClass3 = factory.createClass(); + System.out.println("created third class " + proxyClass3.getName()); + TestClass proxy3 = (TestClass)proxyClass3.newInstance(); + proxy3.testMethod(); + assertTrue(proxy3.isTestCalled()); + + Class proxyClass4 = factory.createClass(); + System.out.println("created fourth class " + proxyClass4.getName()); + TestClass proxy4 = (TestClass)proxyClass4.newInstance(); + proxy4.testMethod(); + assertTrue(proxy4.isTestCalled()); + + assertTrue(proxyClass3 != proxyClass4); + } + + /** + * test class used as the super for the proxy + */ + public static class TestClass { + private boolean testCalled = false; + public void testMethod() + { + // record the call + testCalled = true; + } + public boolean isTestCalled() + { + return testCalled; + } + } + + /** + * test interface used as an interface implemented by the proxy + */ + public static interface TestInterface { + public void testMethod(); + } + +}
\ No newline at end of file diff --git a/src/test/test/javassist/proxy/ProxySerializationTest.java b/src/test/test/javassist/proxy/ProxySerializationTest.java new file mode 100644 index 00000000..e79bcb48 --- /dev/null +++ b/src/test/test/javassist/proxy/ProxySerializationTest.java @@ -0,0 +1,151 @@ +package test.javassist.proxy; + +import javassist.util.proxy.*; +import junit.framework.TestCase; + +import java.io.*; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Test to ensure that serialization and deserialization of javassist proxies via + * {@link javassist.util.proxy.ProxyObjectOutputStream} and @link javassist.util.proxy.ProxyObjectInputStream} + * reuses classes located in the proxy cache. This tests the fixes provided for JASSIST-42 and JASSIST-97. + */ +public class ProxySerializationTest extends TestCase +{ + public void testSerialization() + { + ProxyFactory factory = new ProxyFactory(); + factory.setSuperclass(TestClass.class); + factory.setInterfaces(new Class[] {TestInterface.class}); + + factory.setUseWriteReplace(true); + Class proxyClass = factory.createClass(new TestFilter()); + + MethodHandler handler = new TestHandler(); + + // first try serialization using writeReplace + + try { + String name = "proxytest_1"; + Constructor constructor = proxyClass.getConstructor(String.class); + TestClass proxy = (TestClass)constructor.newInstance(name); + ((ProxyObject)proxy).setHandler(handler); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(bos); + out.writeObject(proxy); + out.close(); + byte[] bytes = bos.toByteArray(); + ByteArrayInputStream bis = new ByteArrayInputStream(bytes); + ObjectInputStream in = new ObjectInputStream(bis); + TestClass newProxy = (TestClass)in.readObject(); + // inherited fields should have been deserialized + assert(newProxy.getName() == null); + // since we are reading into the same JVM the new proxy should have the same class as the old proxy + assert(newProxy.getClass() == proxy.getClass()); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + + // second try serialization using proxy object output/input streams + + factory.setUseWriteReplace(false); + proxyClass = factory.createClass(new TestFilter()); + + try { + String name = "proxytest_2"; + Constructor constructor = proxyClass.getConstructor(String.class); + TestClass proxy = (TestClass)constructor.newInstance(name); + ((ProxyObject)proxy).setHandler(handler); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ProxyObjectOutputStream out = new ProxyObjectOutputStream(bos); + out.writeObject(proxy); + out.close(); + byte[] bytes = bos.toByteArray(); + ByteArrayInputStream bis = new ByteArrayInputStream(bytes); + ProxyObjectInputStream in = new ProxyObjectInputStream(bis); + TestClass newProxy = (TestClass)in.readObject(); + // inherited fields should have been deserialized + assert(proxy.getName() == newProxy.getName()); + // since we are reading into the same JVM the new proxy should have the same class as the old proxy + assert(newProxy.getClass() == proxy.getClass()); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + public static class TestFilter implements MethodFilter, Serializable + { + public boolean isHandled(Method m) { + if (m.getName().equals("getName")) { + return true; + } + return false; + } + + public boolean equals(Object o) + { + if (o instanceof TestFilter) { + // all test filters are equal + return true; + } + + return false; + } + + public int hashCode() + { + return TestFilter.class.hashCode(); + } + } + + public static class TestHandler implements MethodHandler, Serializable + { + public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable + { + return proceed.invoke(self, args); + } + public boolean equals(Object o) + { + if (o instanceof TestHandler) { + // all test handlers are equal + return true; + } + + return false; + } + + public int hashCode() + { + return TestHandler.class.hashCode(); + } + } + + public static class TestClass implements Serializable + { + public String name; + + public TestClass() + { + } + + public TestClass(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } + } + + public static interface TestInterface + { + public String getName(); + } +} |