KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > PerInstanceSerializationTest


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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 JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.io.Serializable JavaDoc;
19 import java.io.ObjectOutput JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
30  */

31 public class PerInstanceSerializationTest extends TestCase implements Serializable JavaDoc {
32
33     static StringBuffer JavaDoc s_log = new StringBuffer JavaDoc();
34     static void log(String JavaDoc 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 JavaDoc();
44         PerInstanceSerializationTest instance = new PerInstanceSerializationTest();
45         instance.doStuff();// advised, new aspect gets created
46
Object JavaDoc theAspect = Aspects.aspectOf(TestAspect.class, instance);
47
48         try {
49             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc("instance.ser"));
50             out.writeObject(instance);
51             instance = null;
52             out.close();
53
54             File JavaDoc file = new File JavaDoc("instance.ser");
55             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(file));
56             PerInstanceSerializationTest unserInstance = (PerInstanceSerializationTest) in.readObject();
57             in.close();
58             Object JavaDoc theAspect2 = Aspects.aspectOf(TestAspect.class, unserInstance);
59             unserInstance.doStuff();
60
61             // the perInstance aspect does not get serialized but another instance is made available
62
assertFalse(theAspect.hashCode() == theAspect2.hashCode());
63             assertEquals("newAspect around doStuff newAspect around doStuff ", s_log.toString());
64         } catch (Exception JavaDoc e) {
65             e.printStackTrace();
66         }
67     }
68
69
70     public static void main(String JavaDoc[] 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 JavaDoc around(StaticJoinPoint joinpoint) throws Throwable JavaDoc {
87             log("around");
88             return joinpoint.proceed();
89         }
90     }
91
92 }
93
Popular Tags