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.enclosingsjp;
9   
10  import java.lang.reflect.Constructor;
11  import java.lang.reflect.Method;
12  import java.util.ArrayList;
13  import java.util.List;
14  
15  import org.codehaus.aspectwerkz.joinpoint.ConstructorSignature;
16  import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
17  import org.codehaus.aspectwerkz.joinpoint.MethodSignature;
18  import org.codehaus.aspectwerkz.joinpoint.Signature;
19  import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
20  
21  import junit.framework.TestCase;
22  
23  
24  public class EnclosingSJPTest extends TestCase {
25  	private static List s_enclosingStaticJPList = new ArrayList();
26  	
27  	
28  	/***
29  	 * @see junit.framework.TestCase#setUp()
30  	 */
31  	protected void setUp() throws Exception {
32  		s_enclosingStaticJPList.clear();
33  	}
34  
35  	public static void registerEnclosingSJP(EnclosingStaticJoinPoint esjp) {
36  		s_enclosingStaticJPList.add(esjp);
37  	}
38  	
39  	public void testConstructorEnclosing() throws NoSuchMethodException {
40  		EnclosingTarget et = new EnclosingTarget();
41  		
42  		Class[] expectedSignaturesTypes = new Class[] {
43  				ConstructorSignature.class,
44  				ConstructorSignature.class,
45  				ConstructorSignature.class,
46  				ConstructorSignature.class,
47  				ConstructorSignature.class,
48  				ConstructorSignature.class,
49  				ConstructorSignature.class,
50  				ConstructorSignature.class
51  		};
52  		
53  		JoinPointType[] expectedJPTypes = new JoinPointType[] {
54  				JoinPointType.CONSTRUCTOR_EXECUTION,
55  				JoinPointType.CONSTRUCTOR_EXECUTION,
56  				JoinPointType.CONSTRUCTOR_EXECUTION,
57  				JoinPointType.CONSTRUCTOR_EXECUTION,
58  				JoinPointType.CONSTRUCTOR_EXECUTION,
59  				JoinPointType.CONSTRUCTOR_EXECUTION,
60  				JoinPointType.CONSTRUCTOR_EXECUTION,
61  				JoinPointType.CONSTRUCTOR_EXECUTION
62  		};
63  		
64  		check(expectedSignaturesTypes,
65  		      expectedJPTypes,
66  		      s_enclosingStaticJPList);
67  		
68  		Constructor ctor = et.getClass().getConstructor(new Class[0]);
69  		
70  		for(int i = 0; i < s_enclosingStaticJPList.size(); i++) {
71  			ConstructorSignature ctorSig = 
72  				(ConstructorSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(i)).getSignature();
73  			
74  			assertTrue("" + ctor.toString(),
75  					ctor.equals(ctorSig.getConstructor()));
76  		}
77  	}
78  	
79  	public void testHandlerEnclosing() throws NoSuchMethodException {
80  		try {
81  			throw new IllegalAccessException("msg1");
82  		} catch(IllegalAccessException iae) {
83  			;
84  		}
85  		
86  		EnclosingTarget et = new EnclosingTarget(1);
87  		
88  		Class[] expectedSignaturesTypes = new Class[] {
89  				MethodSignature.class,
90  				MethodSignature.class,
91  				ConstructorSignature.class,
92  				ConstructorSignature.class
93  		};
94  		
95  		JoinPointType[] expectedJPTypes = new JoinPointType[] {
96  				JoinPointType.METHOD_EXECUTION,
97  				JoinPointType.METHOD_EXECUTION,
98  				JoinPointType.CONSTRUCTOR_EXECUTION,
99  				JoinPointType.CONSTRUCTOR_EXECUTION
100 		};
101 		
102 		check(expectedSignaturesTypes,
103 		      expectedJPTypes,
104 		      s_enclosingStaticJPList);
105 		
106 		Constructor ctor = et.getClass().getConstructor(new Class[] {int.class});
107 		Method		meth = getClass().getMethod("testHandlerEnclosing", null);
108 
109 		assertTrue(
110 				meth.toString(),
111 				meth.equals(
112 				((MethodSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(0))
113 						.getSignature()).getMethod())
114 		);
115 		assertTrue(
116 				meth.toString(),
117 				meth.equals(
118 				((MethodSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(1))
119 						.getSignature()).getMethod())
120 		);
121 		assertTrue(
122 				ctor.toString(),
123 				ctor.equals(
124 				((ConstructorSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(2))
125 						.getSignature()).getConstructor())
126 		);
127 		assertTrue(
128 				ctor.toString(),
129 				ctor.equals(
130 	            ((ConstructorSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(3))
131 	            		.getSignature()).getConstructor())
132 		);
133 	}
134 	
135 	public void testGetSet() throws NoSuchMethodException {
136 		EnclosingTarget et = new EnclosingTarget(new Object());
137 		
138 		Class[] expectedSignatureTypes = new Class[] {
139 				ConstructorSignature.class,
140 				ConstructorSignature.class,
141 				ConstructorSignature.class,
142 				MethodSignature.class,
143 				MethodSignature.class,
144 				MethodSignature.class,
145 				MethodSignature.class,
146 				MethodSignature.class,
147 				MethodSignature.class
148 		};
149 		
150 		JoinPointType[] expectedJPTypes = new JoinPointType[] {
151 				JoinPointType.CONSTRUCTOR_EXECUTION,
152 				JoinPointType.CONSTRUCTOR_EXECUTION,
153 				JoinPointType.CONSTRUCTOR_EXECUTION,
154 				JoinPointType.METHOD_EXECUTION,
155 				JoinPointType.METHOD_EXECUTION,
156 				JoinPointType.METHOD_EXECUTION,
157 				JoinPointType.METHOD_EXECUTION,
158 				JoinPointType.METHOD_EXECUTION,
159 				JoinPointType.METHOD_EXECUTION
160 		};
161 		
162 		check(expectedSignatureTypes,
163 				expectedJPTypes,
164 				s_enclosingStaticJPList);
165 		
166 		Constructor ctor = et.getClass().getConstructor(new Class[] {Object.class});
167 		Method setMethod = PointcutTarget.class.getMethod("setFieldValue", new Class[] {Object.class});
168 		Method getMethod = PointcutTarget.class.getMethod("getFieldValue", null);
169 		
170 		for(int i = 0; i < 3; i++) {
171 			EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(i);
172 			Constructor enclosingCtor = ((ConstructorSignature) esjp.getSignature()).getConstructor();
173 			
174 			assertTrue(
175 					ctor.toString(),
176 					ctor.equals(enclosingCtor));
177 		}
178 		
179 		for(int i = 3; i < 6; i++) {
180 			EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(i);
181 			Method method = ((MethodSignature) esjp.getSignature()).getMethod();
182 			
183 			assertTrue(
184 					setMethod.toString(),
185 					setMethod.equals(method));
186 		}
187 		
188 		for(int i = 6; i < s_enclosingStaticJPList.size(); i++) {
189 			EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) s_enclosingStaticJPList.get(i);
190 			Method method = ((MethodSignature) esjp.getSignature()).getMethod();
191 			
192 			assertTrue(
193 					getMethod.toString(),
194 					getMethod.equals(method));
195 		}
196 
197 	}
198 	
199 	
200 	public static void main(String[] args) {
201 		junit.textui.TestRunner.run(EnclosingSJPTest.class);
202 	}
203 
204     public static junit.framework.Test suite() {
205         return new junit.framework.TestSuite(EnclosingSJPTest.class);
206     }
207 
208 	private void check(
209 			Class[] signatureClass,
210 			JoinPointType[] enclosingTypes, 
211 			List esjpList) {
212 		
213 		assertEquals(enclosingTypes.length, esjpList.size());
214 		
215 		for(int i = 0; i < enclosingTypes.length; i++) {
216 			EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) esjpList.get(i);
217 			
218 			assertNotNull("EnclosingStaticJoinPoint should never be null", esjp);
219 			
220 			assertNotNull("Signature should not be null", esjp.getSignature());
221 					
222 			Signature sig = esjp.getSignature();
223 			
224 			if(sig instanceof ConstructorSignature) {
225 				assertNotNull(((ConstructorSignature) sig).getConstructor());
226 			} else if(sig instanceof MethodSignature) {
227 				assertNotNull(((MethodSignature) sig).getMethod());
228 			} else {
229 				fail("unexpected signature type: " + sig.getClass().getName());
230 			}
231 			
232 			assertEquals("expectation on enclosing JP type failed", 
233 					enclosingTypes[i], 
234 					esjp.getType());
235 			
236 			assertTrue("expectation on enclosing Signature class failed", 
237 					(signatureClass[i].isAssignableFrom(esjp.getSignature().getClass())));
238 
239 		}
240 	}
241 }