1 package ch.elca.el4j.tests.util.codingsupport;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import org.aopalliance.intercept.MethodInterceptor;
11 import org.aopalliance.intercept.MethodInvocation;
12 import org.junit.Test;
13 import org.springframework.aop.support.DelegatingIntroductionInterceptor;
14
15 import ch.elca.el4j.tests.util.codingsupport.testclasses.A;
16 import ch.elca.el4j.tests.util.codingsupport.testclasses.B;
17 import ch.elca.el4j.tests.util.codingsupport.testclasses.DefaultPerson;
18 import ch.elca.el4j.util.codingsupport.AopHelper;
19
20
21
22
23 public class AopHelperTest {
24
25 @Test
26 public void testIsProxied() {
27 DefaultPerson p = new DefaultPerson();
28
29 assertFalse(AopHelper.isProxied(p));
30
31 p = AopHelper.addAdvice(p, new AMixin());
32
33 assertTrue(AopHelper.isProxied(p));
34 }
35
36 @Test
37 public void testAdviceAddingOrder() {
38
39 List<Integer> globalList = new ArrayList<Integer>();
40
41 DefaultPerson p = new DefaultPerson();
42 p = AopHelper.addAdvice(p, 0, new AMixin(globalList),
43 new CInterceptor(globalList),
44 new BMixin(globalList));
45 p.setAge(7);
46
47 assertEquals(3,globalList.size());
48 assertEquals(1,globalList.get(0).intValue());
49 assertEquals(2,globalList.get(1).intValue());
50 assertEquals(3,globalList.get(2).intValue());
51
52 assertEquals(7, p.getAge());
53 }
54
55
56
57 @Test
58 public void testAopHelperAddAdvice() {
59 List<Integer> globalList = new ArrayList<Integer>();
60
61 DefaultPerson p = new DefaultPerson();
62
63 p = AopHelper.addAdvice(p, new AMixin(globalList));
64
65
66 p = AopHelper.addAdvice(p, new BMixin(globalList));
67
68 p = AopHelper.addAdvice(p, new CInterceptor(globalList));
69
70 p.setAge(11);
71
72 assertEquals(3,globalList.size());
73 assertEquals(2,globalList.get(0).intValue());
74 assertEquals(3,globalList.get(1).intValue());
75 assertEquals(1,globalList.get(2).intValue());
76
77
78
79
80
81
82
83 System.out.println("B is: "+((B)p).getB());
84 System.out.println("A is: "+((A)p).getA());
85
86 System.out.println("\nBefore calling getAge: ");
87 System.out.println("Age is: "+p.getAge());
88
89
90 try {
91 Thread.sleep(1000);
92 } catch (InterruptedException e) {
93 e.printStackTrace();
94 }
95 System.out.println("\n\nRemoving all advice");
96
97 p = (DefaultPerson)AopHelper.removeAllAdvice(p);
98 System.out.println("Age is: "+p.getAge());
99
100 }
101
102 @Test
103 public void testLightAopHelperUsage() {
104 DefaultPerson p = new DefaultPerson();
105
106 p = AopHelper.addAdvice(p, new MethodInterceptor(){
107
108 public Object invoke(MethodInvocation invocation) throws Throwable {
109 System.out.println(" Invocation on my test object "+invocation.getMethod().getName());
110 Object returnValue = invocation.proceed();
111 return returnValue;
112 }
113
114 });
115
116 p.getAge();
117
118 }
119
120 }
121
122
123
124
125 @SuppressWarnings("serial")
126 class CInterceptor implements MethodInterceptor{
127
128 public CInterceptor() { }
129
130 List<Integer> globalList;
131
132 public CInterceptor(List<Integer> globalList) {
133 this.globalList = globalList;
134 }
135
136 public Object invoke(MethodInvocation invocation) throws Throwable {
137 if (globalList != null) {
138 globalList.add(2);
139 }
140 System.out.println(" In invoker C "+invocation.getMethod().getName());
141 Object returnValue = invocation.proceed();
142 return returnValue;
143 }
144 }
145
146
147 @SuppressWarnings("serial")
148 class AMixin extends DelegatingIntroductionInterceptor implements A{
149 int a = 42;
150
151 public AMixin() { }
152
153 List<Integer> globalList;
154
155 public AMixin(List<Integer> globalList) {
156 this.globalList = globalList;
157 }
158
159 @Override
160 public Object invoke(MethodInvocation invocation) throws Throwable {
161 if (globalList != null) {
162 globalList.add(1);
163 }
164
165 System.out.println(" In invoker of mixin A "+invocation.getMethod().getName());
166
167 Exception e = new Exception();
168 System.out.println (" Stack trace:");
169 e.printStackTrace();
170 System.out.println (" \n");
171
172 return super.invoke(invocation);
173
174 }
175
176 public int getA() {
177 return a;
178 }
179 }
180
181 @SuppressWarnings("serial")
182 class BMixin extends DelegatingIntroductionInterceptor implements B{
183 String b = "Carpe diem";
184
185 public BMixin(){ }
186
187 List<Integer> globalList;
188
189 public BMixin(List<Integer> globalList) {
190 this.globalList = globalList;
191 }
192
193 @Override
194 public Object invoke(MethodInvocation invocation) throws Throwable {
195 if (globalList != null) {
196 globalList.add(3);
197 }
198 System.out.println(" In invoker of mixin B "+invocation.getMethod().getName());
199 return super.invoke(invocation);
200 }
201
202 public String getB() {
203 return b;
204 }
205 }