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 junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.AspectContext;
011: import org.codehaus.aspectwerkz.aspect.management.Aspects;
012: import org.codehaus.aspectwerkz.aspect.management.NoAspectBoundException;
013: import org.codehaus.aspectwerkz.annotation.Before;
014: import org.codehaus.aspectwerkz.annotation.Aspect;
015:
016: import java.io.PrintStream;
017:
018: /**
019: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
020: */
021: public class QNameTest extends TestCase {
022:
023: static StringBuffer s_log = new StringBuffer();
024:
025: static void log(String s) {
026: s_log.append(s).append(" ");
027: }
028:
029: void doStuff() {
030: log("doStuff");
031: }
032:
033: void doGC1() {
034: log("doGC1");
035: System.gc();
036: System.gc();
037: doGC2();
038: }
039:
040: void doGC2() {
041: log("doGC2");
042: }
043:
044: void doPerJVM() {
045: log("doPerJVM");
046: PrintStream fieldGet = System.out;
047: }
048:
049: void doPerClass() {
050: log("doPerClass");
051: PrintStream fieldGet = System.out;
052: }
053:
054: void doPerInstance() {
055: log("doPerInstance");
056: PrintStream fieldGet = System.out;
057: }
058:
059: public void testQNames() {
060: s_log = new StringBuffer();
061: doStuff();
062: // note: aspect instantiation happens first due to perJVM and JP clinit
063: assertEquals(
064: "1 jdk5test/Aspect_1 2 jdk5test/Aspect_2 before-1 before-2 doStuff ",
065: s_log.toString());
066:
067: TestAspect a = (TestAspect) Aspects
068: .aspectOf("jdk5test/Aspect_1");
069: assertEquals("1", a.p);
070:
071: TestAspect b = (TestAspect) Aspects
072: .aspectOf("jdk5test/Aspect_2");
073: assertEquals("2", b.p);
074:
075: // in that case there is several aspects for Aspect.class
076: // so fails
077: try {
078: TestAspect c = (TestAspect) Aspects
079: .aspectOf(TestAspect.class);
080: fail("should fail");
081: } catch (NoAspectBoundException t) {
082: ;
083: }
084: }
085:
086: public void testPerX() {
087: s_log = new StringBuffer();
088: doPerJVM();
089: assertEquals("doPerJVM before ", s_log.toString());
090:
091: s_log = new StringBuffer();
092: doPerClass();
093: assertEquals("doPerClass before ", s_log.toString());
094:
095: s_log = new StringBuffer();
096: doPerInstance();
097: assertEquals("doPerInstance before ", s_log.toString());
098: }
099:
100: public void testPerJVMAndGC() {
101: s_log = new StringBuffer();
102: doGC1();
103: assertEquals("AspectGC before1 doGC1 before2 doGC2 ", s_log
104: .toString());
105: }
106:
107: public static void main(String[] args) {
108: junit.textui.TestRunner.run(suite());
109: }
110:
111: public static junit.framework.Test suite() {
112: return new junit.framework.TestSuite(QNameTest.class);
113: }
114:
115: public static class TestAspect {
116:
117: String p;
118:
119: public TestAspect(AspectContext ctx) {
120: p = ctx.getParameter("p");
121: log(p);
122: log(ctx.getAspectDefinition().getQualifiedName());
123: }
124:
125: @Before("execution(* test.QNameTest.doStuff())")
126: public void before() {
127: log("before-" + p);
128: }
129: }
130:
131: public static class AspectJVM {
132: @Before("withincode(* test.QNameTest.doPerJVM()) && get(* java.lang.System.out)")
133: public void before() {
134: log("before");
135: }
136: }
137:
138: @Aspect("perClass")
139: public static class AspectClass {
140: @Before("withincode(* test.QNameTest.doPerClass()) && get(* java.lang.System.out)")
141: public void before() {
142: log("before");
143: }
144: }
145:
146: @Aspect("perInstance")
147: public static class AspectInstance {
148: @Before("withincode(* test.QNameTest.doPerInstance()) && get(* java.lang.System.out)")
149: public void before() {
150: log("before");
151: }
152: }
153:
154: public static class AspectGC {
155:
156: public AspectGC() {
157: log("AspectGC");
158: }
159:
160: @Before("execution(* test.QNameTest.doGC1())")
161: public void before1() {
162: log("before1");
163: }
164:
165: @Before("execution(* test.QNameTest.doGC2())")
166: public void before2() {
167: log("before2");
168: }
169: }
170:
171: }
|