001: /**************************************************************************************
002: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test;
008:
009: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
010: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
011: import org.codehaus.aspectwerkz.annotation.Aspect;
012: import org.codehaus.aspectwerkz.annotation.Around;
013: import org.codehaus.aspectwerkz.aspect.management.Aspects;
014:
015: import java.util.Map;
016: import java.util.HashMap;
017: import java.io.Serializable;
018: import java.io.ObjectOutput;
019: import java.io.ObjectOutputStream;
020: import java.io.FileOutputStream;
021: import java.io.File;
022: import java.io.ObjectInputStream;
023: import java.io.FileInputStream;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
029: */
030: public class PerInstanceSerializationTest extends TestCase implements
031: Serializable {
032:
033: static StringBuffer s_log = new StringBuffer();
034:
035: static void log(String s) {
036: s_log.append(s).append(" ");
037: }
038:
039: void doStuff() {
040: log("doStuff");
041: }
042:
043: public void testSerializeTarget() {
044: s_log = new StringBuffer();
045: PerInstanceSerializationTest instance = new PerInstanceSerializationTest();
046: instance.doStuff();// advised, new aspect gets created
047: Object theAspect = Aspects.aspectOf(TestAspect.class, instance);
048:
049: try {
050: ObjectOutput out = new ObjectOutputStream(
051: new FileOutputStream("instance.ser"));
052: out.writeObject(instance);
053: instance = null;
054: out.close();
055:
056: File file = new File("instance.ser");
057: ObjectInputStream in = new ObjectInputStream(
058: new FileInputStream(file));
059: PerInstanceSerializationTest unserInstance = (PerInstanceSerializationTest) in
060: .readObject();
061: in.close();
062: Object theAspect2 = Aspects.aspectOf(TestAspect.class,
063: unserInstance);
064: unserInstance.doStuff();
065:
066: // the perInstance aspect does not get serialized but another instance is made available
067: assertFalse(theAspect.hashCode() == theAspect2.hashCode());
068: assertEquals(
069: "newAspect around doStuff newAspect around doStuff ",
070: s_log.toString());
071: } catch (Exception e) {
072: e.printStackTrace();
073: }
074: }
075:
076: public static void main(String[] args) {
077: junit.textui.TestRunner.run(suite());
078: }
079:
080: public static junit.framework.Test suite() {
081: return new junit.framework.TestSuite(
082: PerInstanceSerializationTest.class);
083: }
084:
085: @Aspect("perInstance")
086: public static class TestAspect {
087:
088: public TestAspect() {
089: log("newAspect");
090: }
091:
092: @Around("execution(void test.PerInstanceSerializationTest.doStuff(..))")
093: public Object around(StaticJoinPoint joinpoint)
094: throws Throwable {
095: log("around");
096: return joinpoint.proceed();
097: }
098: }
099:
100: }
|