1 package dynaop; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.ObjectInputStream ; 7 import java.io.ObjectOutputStream ; 8 import java.io.Serializable ; 9 import java.lang.reflect.Method ; 10 import java.util.ArrayList ; 11 12 import net.sf.cglib.Enhancer; 13 import net.sf.cglib.MethodInterceptor; 14 import net.sf.cglib.MethodProxy; 15 16 import junit.framework.TestCase; 17 18 23 public class CglibProxyHandleTest extends TestCase { 24 25 public void testEnhance() { 26 Bar bar = (Bar) Enhancer.enhance(Bar.class, 27 new MethodInterceptorImpl()); 28 bar.setA(5); 29 bar.setE(new ArrayList ()); 30 assertEquals(5, bar.getA()); 31 } 32 33 public void testSerializable() throws Throwable { 34 Bar bar = (Bar) Enhancer.enhance(Bar.class, 35 new Class [] { Proxy.class }, 36 new MethodInterceptorImpl()); 37 38 Object e = new ArrayList (); 39 40 bar.setA(5); 41 bar.setB(true); 42 bar.setC("c"); 43 bar.setD("d"); 44 bar.setE(e); 45 bar.setTee("changed"); 46 47 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 48 new ObjectOutputStream (bout).writeObject( 49 new CglibProxyHandle((Proxy) bar)); 50 Handle handle = (Handle) new ObjectInputStream ( 51 new ByteArrayInputStream (bout.toByteArray())).readObject(); 52 bar = (Bar) handle.getProxy(); 53 54 assertEquals(5, bar.getA()); 55 assertEquals(true, bar.isB()); 56 assertEquals("c", bar.getC()); 57 assertEquals("d", bar.getD()); 58 assertEquals(e, bar.getE()); 59 assertEquals("initial", bar.getTee()); 60 assertTrue(bar.isWriteObjectCalled()); 61 assertTrue(bar.isReadObjectCalled()); 62 } 63 64 70 71 static class MethodInterceptorImpl implements MethodInterceptor, 72 Serializable { 73 74 Object target; 75 76 public Object intercept(Object o, Method method, Object [] args, 77 MethodProxy methodProxy) throws Throwable { 78 return methodProxy.invokeSuper(o, args); 79 } 80 } 81 82 public static class Tee { 83 84 String tee = "initial"; 85 86 public String getTee() { 87 return tee; 88 } 89 90 public void setTee(String tee) { 91 this.tee = tee; 92 } 93 } 94 95 public static class Foo extends Tee implements Serializable { 96 97 static String staticField = "static"; 98 int a; 99 private boolean b; 100 transient String c; 101 boolean writeObjectCalled; 102 transient boolean readObjectCalled; 103 104 private void writeObject(ObjectOutputStream out) throws IOException { 105 writeObjectCalled = true; 106 out.writeUTF(this.c); 107 out.defaultWriteObject(); 108 } 109 110 private void readObject(ObjectInputStream in) throws IOException , 111 ClassNotFoundException { 112 readObjectCalled = true; 113 this.c = in.readUTF(); 114 in.defaultReadObject(); 115 } 116 117 public int getA() { 118 return a; 119 } 120 121 public void setA(int a) { 122 this.a = a; 123 } 124 125 public boolean isB() { 126 return b; 127 } 128 129 public void setB(boolean b) { 130 this.b = b; 131 } 132 133 public String getC() { 134 return c; 135 } 136 137 public void setC(String c) { 138 this.c = c; 139 } 140 141 public boolean isReadObjectCalled() { 142 return readObjectCalled; 143 } 144 145 public boolean isWriteObjectCalled() { 146 return writeObjectCalled; 147 } 148 149 } 150 151 public static class Bar extends Foo implements Serializable { 152 153 String d; 154 Object e; 155 156 public String getD() { 157 return d; 158 } 159 160 public void setD(String d) { 161 this.d = d; 162 } 163 164 public Object getE() { 165 return e; 166 } 167 168 public void setE(Object e) { 169 this.e = e; 170 } 171 } 172 } 173 | Popular Tags |