1 8 package test; 9 10 import org.codehaus.aspectwerkz.joinpoint.JoinPoint; 11 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint; 12 import org.codehaus.aspectwerkz.annotation.Aspect; 13 import org.codehaus.aspectwerkz.annotation.Around; 14 import org.codehaus.aspectwerkz.aspect.management.Aspects; 15 16 import java.util.Map ; 17 import java.util.HashMap ; 18 import java.io.Serializable ; 19 import java.io.ObjectOutput ; 20 import java.io.ObjectOutputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.File ; 23 import java.io.ObjectInputStream ; 24 import java.io.FileInputStream ; 25 26 import junit.framework.TestCase; 27 28 31 public class PerInstanceSerializationTest extends TestCase implements Serializable { 32 33 static StringBuffer s_log = new StringBuffer (); 34 static void log(String s) { 35 s_log.append(s).append(" "); 36 } 37 38 void doStuff() { 39 log("doStuff"); 40 } 41 42 public void testSerializeTarget() { 43 s_log = new StringBuffer (); 44 PerInstanceSerializationTest instance = new PerInstanceSerializationTest(); 45 instance.doStuff(); Object theAspect = Aspects.aspectOf(TestAspect.class, instance); 47 48 try { 49 ObjectOutput out = new ObjectOutputStream (new FileOutputStream ("instance.ser")); 50 out.writeObject(instance); 51 instance = null; 52 out.close(); 53 54 File file = new File ("instance.ser"); 55 ObjectInputStream in = new ObjectInputStream (new FileInputStream (file)); 56 PerInstanceSerializationTest unserInstance = (PerInstanceSerializationTest) in.readObject(); 57 in.close(); 58 Object theAspect2 = Aspects.aspectOf(TestAspect.class, unserInstance); 59 unserInstance.doStuff(); 60 61 assertFalse(theAspect.hashCode() == theAspect2.hashCode()); 63 assertEquals("newAspect around doStuff newAspect around doStuff ", s_log.toString()); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 69 70 public static void main(String [] args) { 71 junit.textui.TestRunner.run(suite()); 72 } 73 74 public static junit.framework.Test suite() { 75 return new junit.framework.TestSuite(PerInstanceSerializationTest.class); 76 } 77 78 @Aspect("perInstance") 79 public static class TestAspect { 80 81 public TestAspect() { 82 log("newAspect"); 83 } 84 85 @Around("execution(void test.PerInstanceSerializationTest.doStuff(..))") 86 public Object around(StaticJoinPoint joinpoint) throws Throwable { 87 log("around"); 88 return joinpoint.proceed(); 89 } 90 } 91 92 } 93 | Popular Tags |