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.constructor;
9
10 import junit.framework.TestCase;
11
12 /***
13 * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
14 */
15 public class ConstructorAdviceTest extends TestCase {
16 private static String s_logCall = "";
17
18 private static String s_logExecution = "";
19
20 public ConstructorAdviceTest() {
21 }
22
23 public ConstructorAdviceTest(String name) {
24 super(name);
25 }
26
27 public void testCallAroundAdvice() {
28 s_logCall = "";
29 TestAroundAdvice test = new TestAroundAdvice(1L, new Object(), new String[]{});
30 assertEquals("beforeCall init afterCall ", s_logCall);
31 assertNotNull(test);
32 }
33
34 public void testCallBeforeAdvice() {
35 s_logCall = "";
36 TestBeforeAdvice test = new TestBeforeAdvice();
37 assertEquals("preCall init ", s_logCall);
38 assertNotNull(test);
39 }
40
41 public void testCallAfterAdvice() {
42 s_logCall = "";
43 TestAfterAdvice test = new TestAfterAdvice("test");
44 assertEquals("test postCall ", s_logCall);
45 assertNotNull(test);
46 }
47
48 public void testCallBeforeAfterAdvice() {
49 s_logCall = "";
50 TestBeforeAfterAdvice test = new TestBeforeAfterAdvice(
51 new String[]{
52 "test"
53 }
54 );
55 assertEquals("preCall test postCall ", s_logCall);
56 assertNotNull(test);
57 }
58
59 public void testCallReturnFalseType() {
60 s_logCall = "";
61 TestReturnFalseType test = null;
62 try {
63 test = new TestReturnFalseType();
64 } catch (ClassCastException e) {
65 return;
66 }
67 fail("this point should not have been reached a class cast exception should have been thrown");
68 }
69
70 public void testExecutionAroundAdvice() {
71 s_logExecution = "";
72 TestAroundAdvice test = new TestAroundAdvice(1L, new Object(), new String[]{});
73 assertEquals("beforeExecution init afterExecution ", s_logExecution);
74 assertNotNull(test);
75 assertTrue(test instanceof TestAroundAdvice);
76 }
77
78 public void testExecutionBeforeAdvice() {
79 s_logExecution = "";
80 TestBeforeAdvice test = new TestBeforeAdvice();
81 assertEquals("preExecution init ", s_logExecution);
82 assertNotNull(test);
83 assertTrue(test instanceof TestBeforeAdvice);
84 }
85
86 public void testExecutionAfterAdvice() {
87 s_logExecution = "";
88 TestAfterAdvice test = new TestAfterAdvice("test");
89 assertEquals("init postExecution ", s_logExecution);
90 assertNotNull(test);
91 assertTrue(test instanceof TestAfterAdvice);
92 }
93
94 public void testExecutionBeforeAfterAdvice() {
95 s_logExecution = "";
96 TestBeforeAfterAdvice test = new TestBeforeAfterAdvice(new String[]{"test"});
97 assertEquals("preExecution init postExecution ", s_logExecution);
98 assertNotNull(test);
99 assertTrue(test instanceof TestBeforeAfterAdvice);
100 }
101
102 public void testExecutionReturnFalseType() {
103 s_logExecution = "";
104 TestReturnFalseType test = null;
105 test = new TestReturnFalseType();
106 if (!test.m_updatedByAdvice) {
107 fail("should have been updated by advice");
108 }
109 }
110
111 public static void main(String[] args) {
112 junit.textui.TestRunner.run(suite());
113 }
114
115 public static junit.framework.Test suite() {
116 return new junit.framework.TestSuite(ConstructorAdviceTest.class);
117 }
118
119 public static void logCall(final String wasHere) {
120 s_logCall += wasHere;
121 }
122
123 public static void logExecution(final String wasHere) {
124 s_logExecution += wasHere;
125 }
126 }