• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test.javassist.proxy;
2 
3 import javassist.util.proxy.*;
4 import junit.framework.TestCase;
5 
6 import java.io.*;
7 import java.lang.reflect.Constructor;
8 import java.lang.reflect.InvocationTargetException;
9 import java.lang.reflect.Method;
10 
11 /**
12  * Test to ensure that serialization and deserialization of javassist proxies via
13  * {@link javassist.util.proxy.ProxyObjectOutputStream} and  @link javassist.util.proxy.ProxyObjectInputStream}
14  * reuses classes located in the proxy cache. This tests the fixes provided for JASSIST-42 and JASSIST-97.
15  */
16 public class ProxySerializationTest extends TestCase
17 {
testSerialization()18     public void testSerialization()
19     {
20         ProxyFactory factory = new ProxyFactory();
21         factory.setSuperclass(TestClass.class);
22         factory.setInterfaces(new Class[] {TestInterface.class});
23 
24         factory.setUseWriteReplace(true);
25         Class proxyClass = factory.createClass(new TestFilter());
26 
27         MethodHandler handler = new TestHandler();
28 
29         // first try serialization using writeReplace
30 
31         try {
32             String name = "proxytest_1";
33             Constructor constructor = proxyClass.getConstructor(new Class[] {String.class});
34             TestClass proxy = (TestClass)constructor.newInstance(new Object[] {name});
35             ((ProxyObject)proxy).setHandler(handler);
36             ByteArrayOutputStream bos = new ByteArrayOutputStream();
37             ObjectOutputStream out = new ObjectOutputStream(bos);
38             out.writeObject(proxy);
39             out.close();
40             byte[] bytes = bos.toByteArray();
41             ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
42             ObjectInputStream in = new ObjectInputStream(bis);
43             TestClass newProxy = (TestClass)in.readObject();
44             // inherited fields should not have been deserialized
45             assertTrue("new name should be null", newProxy.getName() == null);
46             // since we are reading into the same JVM the new proxy should have the same class as the old proxy
47             assertTrue("classes should be equal", newProxy.getClass() == proxy.getClass());
48         } catch (Exception e) {
49             e.printStackTrace();
50             fail();
51         }
52 
53         // second try serialization using proxy object output/input streams
54 
55         factory.setUseWriteReplace(false);
56         proxyClass = factory.createClass(new TestFilter());
57 
58         try {
59             String name = "proxytest_2";
60             Constructor constructor = proxyClass.getConstructor(new Class[] {String.class});
61             TestClass proxy = (TestClass)constructor.newInstance(new Object[] {name});
62             ((ProxyObject)proxy).setHandler(handler);
63             ByteArrayOutputStream bos = new ByteArrayOutputStream();
64             ProxyObjectOutputStream out = new ProxyObjectOutputStream(bos);
65             out.writeObject(proxy);
66             out.close();
67             byte[] bytes = bos.toByteArray();
68             ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
69             ProxyObjectInputStream in = new ProxyObjectInputStream(bis);
70             TestClass newProxy = (TestClass)in.readObject();
71             // inherited fields should have been deserialized
72             assertTrue("names should be equal", proxy.getName().equals(newProxy.getName()));
73             // since we are reading into the same JVM the new proxy should have the same class as the old proxy
74             assertTrue("classes should still be equal", newProxy.getClass() == proxy.getClass());
75         } catch (Exception e) {
76             e.printStackTrace();
77             fail();
78         }
79     }
80 
81     public static class TestFilter implements MethodFilter, Serializable
82     {
isHandled(Method m)83         public boolean isHandled(Method m) {
84             if (m.getName().equals("getName")) {
85                 return true;
86             }
87             return false;
88         }
89 
equals(Object o)90         public boolean equals(Object o)
91         {
92             if (o instanceof TestFilter) {
93                 // all test filters are equal
94                 return true;
95             }
96 
97             return false;
98         }
99 
hashCode()100         public int hashCode()
101         {
102             return TestFilter.class.hashCode();
103         }
104     }
105 
106     public static class TestHandler implements MethodHandler, Serializable
107     {
invoke(Object self, Method thisMethod, Method proceed, Object[] args)108         public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
109         {
110             return proceed.invoke(self, args);
111         }
equals(Object o)112         public boolean equals(Object o)
113         {
114             if (o instanceof TestHandler) {
115                 // all test handlers are equal
116                 return true;
117             }
118 
119             return false;
120         }
121 
hashCode()122         public int hashCode()
123         {
124             return TestHandler.class.hashCode();
125         }
126     }
127 
128     public static class TestClass implements Serializable
129     {
130         public String name;
131 
TestClass()132         public TestClass()
133         {
134         }
135 
TestClass(String name)136         public TestClass(String name)
137         {
138             this.name = name;
139         }
140 
getName()141         public String getName()
142         {
143             return name;
144         }
145     }
146 
147     public static interface TestInterface
148     {
getName()149         public String getName();
150     }
151 }
152