1 8 package test; 9 10 import junit.framework.TestCase; 11 import org.codehaus.aspectwerkz.AspectContext; 12 import org.codehaus.aspectwerkz.aspect.management.Aspects; 13 import org.codehaus.aspectwerkz.aspect.management.NoAspectBoundException; 14 import org.codehaus.aspectwerkz.annotation.Before; 15 import org.codehaus.aspectwerkz.annotation.Aspect; 16 17 import java.io.PrintStream ; 18 19 22 public class QNameTest extends TestCase { 23 24 static StringBuffer s_log = new StringBuffer (); 25 static void log(String s) { 26 s_log.append(s).append(" "); 27 } 28 29 void doStuff() { 30 log("doStuff"); 31 } 32 33 void doGC1() { 34 log("doGC1"); 35 System.gc(); 36 System.gc(); 37 doGC2(); 38 } 39 40 void doGC2() { 41 log("doGC2"); 42 } 43 44 void doPerJVM() { 45 log("doPerJVM"); 46 PrintStream fieldGet = System.out; 47 } 48 49 void doPerClass() { 50 log("doPerClass"); 51 PrintStream fieldGet = System.out; 52 } 53 54 void doPerInstance() { 55 log("doPerInstance"); 56 PrintStream fieldGet = System.out; 57 } 58 59 public void testQNames() { 60 s_log = new StringBuffer (); 61 doStuff(); 62 assertEquals("1 jdk5test/Aspect_1 2 jdk5test/Aspect_2 before-1 before-2 doStuff ", s_log.toString()); 64 65 TestAspect a = (TestAspect)Aspects.aspectOf("jdk5test/Aspect_1"); 66 assertEquals("1", a.p); 67 68 TestAspect b = (TestAspect)Aspects.aspectOf("jdk5test/Aspect_2"); 69 assertEquals("2", b.p); 70 71 try { 74 TestAspect c = (TestAspect)Aspects.aspectOf(TestAspect.class); 75 fail("should fail"); 76 } catch (NoAspectBoundException t) { 77 ; 78 } 79 } 80 81 public void testPerX() { 82 s_log = new StringBuffer (); 83 doPerJVM(); 84 assertEquals("doPerJVM before ", s_log.toString()); 85 86 s_log = new StringBuffer (); 87 doPerClass(); 88 assertEquals("doPerClass before ", s_log.toString()); 89 90 s_log = new StringBuffer (); 91 doPerInstance(); 92 assertEquals("doPerInstance before ", s_log.toString()); 93 } 94 95 public void testPerJVMAndGC() { 96 s_log = new StringBuffer (); 97 doGC1(); 98 assertEquals("AspectGC before1 doGC1 before2 doGC2 ", s_log.toString()); 99 } 100 101 public static void main(String [] args) { 102 junit.textui.TestRunner.run(suite()); 103 } 104 105 public static junit.framework.Test suite() { 106 return new junit.framework.TestSuite(QNameTest.class); 107 } 108 109 public static class TestAspect { 110 111 String p; 112 113 public TestAspect(AspectContext ctx) { 114 p = ctx.getParameter("p"); 115 log(p); 116 log(ctx.getAspectDefinition().getQualifiedName()); 117 } 118 119 @Before("execution(* test.QNameTest.doStuff())") 120 public void before() { 121 log("before-"+p); 122 } 123 } 124 125 public static class AspectJVM { 126 @Before("withincode(* test.QNameTest.doPerJVM()) && get(* java.lang.System.out)") 127 public void before() { 128 log("before"); 129 } 130 } 131 132 @Aspect("perClass") 133 public static class AspectClass { 134 @Before("withincode(* test.QNameTest.doPerClass()) && get(* java.lang.System.out)") 135 public void before() { 136 log("before"); 137 } 138 } 139 140 @Aspect("perInstance") 141 public static class AspectInstance { 142 @Before("withincode(* test.QNameTest.doPerInstance()) && get(* java.lang.System.out)") 143 public void before() { 144 log("before"); 145 } 146 } 147 148 public static class AspectGC { 149 150 public AspectGC() { 151 log("AspectGC"); 152 } 153 154 @Before("execution(* test.QNameTest.doGC1())") 155 public void before1() { 156 log("before1"); 157 } 158 @Before("execution(* test.QNameTest.doGC2())") 159 public void before2() { 160 log("before2"); 161 } 162 } 163 164 } 165 | Popular Tags |