View Javadoc

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   * This class shows some exceptions on stdout (for information purposes)
22   * 
23   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/util/codingsupport/AopHelperTest.java $
24   */
25  public class AopHelperTest {
26  
27  	@Test
28  	public void testIsProxied() {
29  		DefaultPerson p = new DefaultPerson();
30  		
31  		assertFalse(AopHelper.isProxied(p));
32  		
33  		p = AopHelper.addAdvice(p, new AMixin());
34  		
35  		assertTrue(AopHelper.isProxied(p));
36  	}
37  
38  	@Test
39  	public void testAdviceAddingOrder() {
40  		// for ordering tests we use this global list
41  		List<Integer> globalList = new ArrayList<Integer>();
42  		
43  		DefaultPerson p = new DefaultPerson();
44  		p = AopHelper.addAdvice(p, 0, new AMixin(globalList),
45  			new CInterceptor(globalList),
46  			new BMixin(globalList));
47  		p.setAge(7);
48  		
49  		assertEquals(3,globalList.size());
50  		assertEquals(1,globalList.get(0).intValue());
51  		assertEquals(2,globalList.get(1).intValue());
52  		assertEquals(3,globalList.get(2).intValue());
53  		
54  		assertEquals(7, p.getAge());
55  	}
56  	
57  	// TODO add test that tests the MixinMixer with a spring config file/ auto proxy creator
58  	
59  	@Test
60  	public void testAopHelperAddAdvice() {
61  			List<Integer> globalList = new ArrayList<Integer>();
62  		
63  			DefaultPerson p = new DefaultPerson();
64  			
65  			p = AopHelper.addAdvice(p, new AMixin(globalList));
66  			//System.out.println(DataDumper.dump(((IntroductionInfo)p).getInterfaces()));
67  			
68  			p = AopHelper.addAdvice(p, new BMixin(globalList));
69  
70  			p = AopHelper.addAdvice(p, new CInterceptor(globalList));
71  			
72  			p.setAge(11);
73  			
74  			assertEquals(3,globalList.size());
75  			assertEquals(2,globalList.get(0).intValue());
76  			assertEquals(3,globalList.get(1).intValue());
77  			assertEquals(1,globalList.get(2).intValue());
78  			
79  			
80  			// print all interfaces p now implements
81  //			System.out.println(DataDumper.dump(p.getClass().getInterfaces() ));
82  //			System.out.println(DataDumper.dump(((IntroductionInfo)p).getInterfaces()));
83  			
84  
85  			System.out.println("B is: "+((B)p).getB());
86  			System.out.println("A is: "+((A)p).getA());
87  			
88  			System.out.println("\nBefore calling getAge: ");
89  			System.out.println("Age is: "+p.getAge());
90  
91  			
92  			try {
93  				Thread.sleep(1000); // to sync with stderr (of exception output)
94  			} catch (InterruptedException e) {
95  				e.printStackTrace();
96  			}
97  			System.out.println("\n\nRemoving all advice");
98  			
99  			p = (DefaultPerson)AopHelper.removeAllAdvice(p);
100 			System.out.println("Age is: "+p.getAge());
101 //			System.out.println(DataDumper.dump(p.getClass().getInterfaces() ));
102 		}
103 
104 	@Test
105 	public void testLightAopHelperUsage() {
106 			DefaultPerson p = new DefaultPerson();
107 			
108 			p = AopHelper.addAdvice(p, new MethodInterceptor(){
109 
110 				public Object invoke(MethodInvocation invocation) throws Throwable {
111 					System.out.println(" Invocation on my test object "+invocation.getMethod().getName());
112 					Object returnValue = invocation.proceed();
113 					return returnValue;
114 				}
115 				
116 			});
117 			
118 			p.getAge();
119 			
120 		}
121 	
122 	}
123 
124 // some helper test classes:
125 
126 
127 	@SuppressWarnings("serial")
128 	class CInterceptor implements MethodInterceptor{
129 		
130 		public CInterceptor() { }
131 		
132 		List<Integer> globalList;
133 		
134 		public CInterceptor(List<Integer> globalList) {
135 			this.globalList = globalList;
136 		}
137 		
138 		public Object invoke(MethodInvocation invocation) throws Throwable {
139 			if (globalList != null) {
140 				globalList.add(2);
141 			}
142 			System.out.println(" In invoker C "+invocation.getMethod().getName());
143 			Object returnValue = invocation.proceed();
144 			return returnValue;
145 		}
146 	}
147 
148 
149 	@SuppressWarnings("serial")
150 	class AMixin extends DelegatingIntroductionInterceptor implements A{
151 		int a = 42;
152 		
153 		public AMixin() { }
154 		
155 		List<Integer> globalList;
156 		
157 		public AMixin(List<Integer> globalList) {
158 			this.globalList = globalList;
159 		}
160 		
161 		@Override
162 		public Object invoke(MethodInvocation invocation) throws Throwable {
163 			if (globalList != null) {
164 				globalList.add(1);
165 			}
166 			
167 			System.out.println(" In invoker of mixin A "+invocation.getMethod().getName());
168 			
169 			Exception e = new Exception();
170 			System.out.println (" Stack trace:");
171 			e.printStackTrace();
172 			System.out.println (" \n");
173 			
174 			return super.invoke(invocation);
175 			
176 		}
177 
178 		public int getA() {
179 			return a;
180 		}
181 	}
182 
183 	@SuppressWarnings("serial")
184 	class BMixin extends DelegatingIntroductionInterceptor implements B{
185 		String b = "Carpe diem";
186 				
187 		public BMixin(){ }
188 
189 		List<Integer> globalList;
190 		
191 		public BMixin(List<Integer> globalList) {
192 			this.globalList = globalList;
193 		}
194 		
195 		@Override
196 		public Object invoke(MethodInvocation invocation) throws Throwable {
197 			if (globalList != null) {
198 				globalList.add(3);
199 			}
200 			System.out.println(" In invoker of mixin B "+invocation.getMethod().getName());
201 			return super.invoke(invocation);
202 		}
203 
204 		public String getB() {
205 			return b;
206 		}
207 	}