1   /*
2    * $Id: AdvisedClassFilterExpressionTest.java,v 1.2 2004/12/20 13:14:21 avasseur Exp $
3    * $Date: 2004/12/20 13:14:21 $
4    */
5   package test.expression;
6   
7   
8   import org.codehaus.aspectwerkz.exception.DefinitionException;
9   import org.codehaus.aspectwerkz.expression.ExpressionContext;
10  import org.codehaus.aspectwerkz.expression.ExpressionInfo;
11  import org.codehaus.aspectwerkz.expression.ExpressionNamespace;
12  import org.codehaus.aspectwerkz.expression.PointcutType;
13  import org.codehaus.aspectwerkz.reflect.ClassInfo;
14  import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
15  import org.codehaus.aspectwerkz.reflect.FieldInfo;
16  import org.codehaus.aspectwerkz.reflect.MethodInfo;
17  import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
18  import org.codehaus.aspectwerkz.reflect.impl.java.JavaMethodInfo;
19  
20  import junit.framework.TestSuite;
21  
22  /***
23   * Test unit for AdvisedClassFilterExpressionVisitor
24   * 
25   * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
26   * @version $Revision: 1.2 $
27   */
28  public class AdvisedClassFilterExpressionTest extends AnnotationExpressionTest {
29  
30      static {
31          try {
32              setMeUp();
33          } catch (Throwable t) {
34              throw new Error(t.toString());
35          }
36      }
37  
38  	public void testAdvisedConstructor() {
39  		assertTrue(
40  				new ExpressionInfo(
41  						"call(@DefaultConstructor test.*.AnnotationTarget.new())", 
42  						NAMESPACE).getExpression().match(
43  				new ExpressionContext(PointcutType.CALL, s_constructor, null)
44  				)
45  		);
46  
47  		assertFalse(
48  				new ExpressionInfo(
49  						"call(@OtherConstructor test.*.AnnotationTarget.new())", 
50  						NAMESPACE).getExpression().match(
51  				new ExpressionContext(PointcutType.CALL, s_constructor, null)
52  				)
53  		);
54  		
55  		assertTrue(
56  				new ExpressionInfo(
57  						"call(@OtherConstructor test.*.AnnotationTarget.new())", 
58  						NAMESPACE).getAdvisedClassFilterExpression().match(
59  				new ExpressionContext(PointcutType.CALL, s_constructor, null)
60  				)
61  		);
62  		
63  		assertTrue(
64  				new ExpressionInfo(
65  						"call(@DefaultConstructor test.expression.AnnotationTarget.new())", 
66  						NAMESPACE).getExpression().match(
67  				new ExpressionContext(PointcutType.CALL, s_constructor, null)
68  				)
69  		);
70  
71  		assertFalse(
72  				new ExpressionInfo(
73  						"call(@OtherConstructor test.expression.AnnotationTarget.new())", 
74  						NAMESPACE).getExpression().match(
75  				new ExpressionContext(PointcutType.CALL, s_constructor, null)
76  				)
77  		);
78  		
79  		assertTrue(
80  				new ExpressionInfo(
81  						"call(@OtherConstructor test.expression.AnnotationTarget.new())", 
82  						NAMESPACE).getAdvisedClassFilterExpression().match(
83  				new ExpressionContext(PointcutType.CALL, s_constructor, null)
84  				)
85  		);
86  		
87  		assertFalse(
88  				new ExpressionInfo(
89  						"call(@OtherConstructor)", 
90  						NAMESPACE).getExpression().match(
91  				new ExpressionContext(PointcutType.CALL, s_constructor, null)
92  				)
93  		);
94  		
95  		// no type info
96  		assertTrue(
97  				new ExpressionInfo(
98  						"call(@OtherConstructor)", 
99  						NAMESPACE).getAdvisedClassFilterExpression().match(
100 				new ExpressionContext(PointcutType.CALL, s_constructor, null)
101 				)
102 		);
103 		
104 		assertFalse(
105 				new ExpressionInfo(
106 						"call(@DefaultConstructor test.expression.AnnotationTargetWRONG.new())", 
107 						NAMESPACE).getAdvisedClassFilterExpression().match(
108 				new ExpressionContext(PointcutType.CALL, s_constructor, null)
109 				)
110 		);
111 
112 		assertTrue(
113 				new ExpressionInfo(
114 						"within(test.expression.AnnotationTarget) && execution(@DefaultConstructor)",
115 						NAMESPACE).getAdvisedClassFilterExpression().match(
116 				new ExpressionContext(PointcutType.EXECUTION, s_constructor, s_declaringType)
117 				)
118 		);
119 		
120 		assertFalse(
121 				new ExpressionInfo(
122 						"within(test.expression.AnnotationTarget) && execution(@OtherConstructor)",
123 						NAMESPACE).getExpression().match(
124 				new ExpressionContext(PointcutType.EXECUTION, s_constructor, s_declaringType)
125 				)
126 		);
127 		
128 		assertTrue(
129 				new ExpressionInfo(
130 						"within(test.expression.AnnotationTarget) && execution(@OtherConstructor)",
131 						NAMESPACE).getAdvisedClassFilterExpression().match(
132 				new ExpressionContext(PointcutType.EXECUTION, s_constructor, s_declaringType)
133 				)
134 		);
135 		
136 		assertFalse(
137 				new ExpressionInfo(
138 						"within(test.expressionFAKE.*) && execution(@DefaultConstructor)",
139 						NAMESPACE).getExpression().match(
140 				new ExpressionContext(PointcutType.EXECUTION, s_constructor, s_declaringType)
141 				)
142 		);
143 		
144 		assertFalse(
145 				new ExpressionInfo(
146 						"within(test.expressionFAKE.*) && execution(@DefaultConstructor)",
147 						NAMESPACE).getAdvisedClassFilterExpression().match(
148 				new ExpressionContext(PointcutType.EXECUTION, s_constructor, s_declaringType)
149 				)
150 		);
151 		
152 		
153 		assertTrue(
154 				new ExpressionInfo(
155 						"within(@Service test.expression.*) && execution(@DefaultConstructor test.expression.*.new())",
156 						NAMESPACE).getExpression().match(
157 				new ExpressionContext(PointcutType.EXECUTION, s_innerConstructor, s_innerType)
158 				)
159 		);
160 		
161 		assertFalse(
162 				new ExpressionInfo(
163 						"within(!@Service test.expression.*) && execution(@DefaultConstructor test.expression.*.new())",
164 						NAMESPACE).getAdvisedClassFilterExpression().match(
165 				new ExpressionContext(PointcutType.EXECUTION, s_innerConstructor, s_innerType)
166 				)
167 		);
168 
169 		assertTrue(
170 				new ExpressionInfo(
171 						"withincode(@DefaultConstructor test.expression.AnnotationTarget$ClassLevelAnnotation.new())",
172 						NAMESPACE).getExpression().match(
173 				new ExpressionContext(PointcutType.WITHIN, s_innerConstructor, s_innerConstructor)
174 				)
175 		);
176 	}
177 	
178 	public void testAdvisedMethod() {
179 		assertTrue(
180 				new ExpressionInfo(
181 						"call(@Asynch void test.*.AnnotationTarget.methodOneAsynch())", 
182 						NAMESPACE).getExpression().match(
183 				new ExpressionContext(PointcutType.CALL, s_method, null)
184 				)
185 		);
186 
187 		assertFalse(
188 				new ExpressionInfo(
189 						"call(@Synch void test.*.AnnotationTarget.methodOneAsynch())", 
190 						NAMESPACE).getExpression().match(
191 				new ExpressionContext(PointcutType.CALL, s_method, null)
192 				)
193 		);
194 		
195 		assertTrue(
196 				new ExpressionInfo(
197 						"call(@Synch int test.*.AnnotationTarget.methodOneAsynch())", 
198 						NAMESPACE).getAdvisedClassFilterExpression().match(
199 				new ExpressionContext(PointcutType.CALL, s_method, null)
200 				)
201 		);
202 		
203 		assertFalse(
204 				new ExpressionInfo(
205 						"call(@Synch)", 
206 						NAMESPACE).getExpression().match(
207 				new ExpressionContext(PointcutType.CALL, s_method, null)
208 				)
209 		);
210 		
211 		// no type info
212 		assertTrue(
213 				new ExpressionInfo(
214 						"call(@Synch)", 
215 						NAMESPACE).getAdvisedClassFilterExpression().match(
216 				new ExpressionContext(PointcutType.CALL, s_method, null)
217 				)
218 		);
219 		
220 		assertFalse(
221 				new ExpressionInfo(
222 						"call(@Asynch * test.expression.AnnotationTargetWRONG.methodOneAsynch())", 
223 						NAMESPACE).getAdvisedClassFilterExpression().match(
224 				new ExpressionContext(PointcutType.CALL, s_method, null)
225 				)
226 		);
227 
228 		assertFalse(
229 				new ExpressionInfo(
230 						"within(test.expression.AnnotationTarget) && execution(@Synch)",
231 						NAMESPACE).getExpression().match(
232 				new ExpressionContext(PointcutType.EXECUTION, s_method, s_declaringType)
233 				)
234 		);
235 		
236 		// HINT no type in execution
237 		assertTrue(
238 				new ExpressionInfo(
239 						"within(test.expression.AnnotationTarget) && execution(@Synch)",
240 						NAMESPACE).getAdvisedClassFilterExpression().match(
241 				new ExpressionContext(PointcutType.EXECUTION, s_method, s_declaringType)
242 				)
243 		);
244 		
245 		assertTrue(
246 				new ExpressionInfo(
247 						"within(@Service test.expression.AnnotationTarget$ClassLevelAnnotation) && execution(@Asynch void innerMethodAsynch())",
248 						NAMESPACE).getExpression().match(
249 				new ExpressionContext(PointcutType.EXECUTION, s_innerMethod, s_innerType)
250 				)
251 		);
252 		
253 		assertFalse(
254 				new ExpressionInfo(
255 						"within(!@Service test.expression.AnnotationTarget$ClassLevelAnnotation) && execution(@Asynch void innerMethodAsynch())",
256 						NAMESPACE).getAdvisedClassFilterExpression().match(
257 				new ExpressionContext(PointcutType.EXECUTION, s_innerMethod, s_innerType)
258 				)
259 		);
260 		
261 		assertTrue(
262 				new ExpressionInfo(
263 						"withincode(@Asynch void test.expression.AnnotationTarget$ClassLevelAnnotation.innerMethodAsynch())",
264 						NAMESPACE).getExpression().match(
265 				new ExpressionContext(PointcutType.WITHIN, s_innerMethod, s_innerMethod)
266 				)
267 		);
268 		
269 		// HINT annotation and return not considered
270 		assertTrue(
271 				new ExpressionInfo(
272 						"withincode(!@Asynch int test.expression.AnnotationTarget$ClassLevelAnnotation.innerMethodAsynch())",
273 						NAMESPACE).getAdvisedClassFilterExpression().match(
274 				new ExpressionContext(PointcutType.WITHIN, s_innerMethod, s_innerMethod)
275 				)
276 		);
277 	}
278 	
279 	public void testAdvisedHasMethod() {
280         assertTrue(
281                 new ExpressionInfo(
282                         "hasmethod(@Asynch void test.expression.AnnotationTarget.methodOneAsynch())",
283                         NAMESPACE).getExpression().match(
284                 new ExpressionContext(PointcutType.EXECUTION, s_method, null)
285 				)
286         );
287         
288         assertFalse(
289                 new ExpressionInfo(
290                         "hasmethod(!@Asynch int test.expression.AnnotationTarget.methodOneAsynch())",
291                         NAMESPACE).getAdvisedClassFilterExpression().match(
292                 new ExpressionContext(PointcutType.EXECUTION, s_method, null)
293 				)
294         );
295         
296         assertTrue(
297                 new ExpressionInfo(
298                         "hasmethod(@DefaultConstructor test.expression.*.new())",
299                         NAMESPACE).getExpression().match(
300                 new ExpressionContext(PointcutType.EXECUTION, s_innerConstructor, null)
301 				)
302         );
303         
304         assertFalse(
305                 new ExpressionInfo(
306                         "hasmethod(!@DefaultConstructor test.expression.*.new())",
307                         NAMESPACE).getAdvisedClassFilterExpression().match(
308                 new ExpressionContext(PointcutType.EXECUTION, s_innerConstructor, null)
309 				)
310         );
311 	}
312 
313     public void testAdvisedHasField() throws Exception {
314         assertTrue(
315                 new ExpressionInfo(
316                         "hasfield(@Persistable java.lang.Object+ test.expression.AnnotationTarget.m_annotatedField)",
317                         NAMESPACE).getExpression().match(
318                 new ExpressionContext(PointcutType.GET, s_field, s_declaringType)
319 				)
320         );
321         
322         assertFalse(
323                 new ExpressionInfo(
324                         "hasfield(!@Persistable int test.expression.AnnotationTarget.m_annotatedField)",
325                         NAMESPACE).getAdvisedClassFilterExpression().match(
326                 new ExpressionContext(PointcutType.GET, s_field, s_declaringType)
327 				)
328         );
329     }
330     
331     public void testAdvisedFieldAttributes() {
332     	assertTrue(
333                 new ExpressionInfo(
334                 		"set(@Persistable java.lang.Object+ test.expression.AnnotationTarget.m_annotatedField)", 
335 						NAMESPACE).getExpression().match(
336                 new ExpressionContext(PointcutType.SET, s_field, null)
337 				)
338         );
339     	
340     	assertTrue(
341                 new ExpressionInfo(
342                 		"set(!@Persistable int test.expression.AnnotationTarget.m_annotatedField)", 
343 						NAMESPACE).getAdvisedClassFilterExpression().match(
344                 new ExpressionContext(PointcutType.SET, s_field, null)
345 				)
346         );
347     	
348     	// HINT wrong field type
349     	assertFalse(
350     			new ExpressionInfo(
351     					"get(@Persistable int test.expression.AnnotationTarget.m_annotatedField) && !within(@Service)",
352 						NAMESPACE).getExpression().match(
353 				new ExpressionContext(PointcutType.GET, s_field, s_declaringType)
354 				)
355 		);
356     	
357     	// HINT field type ignored
358     	assertTrue(
359     			new ExpressionInfo(
360     					"get(@Persistable int test.expression.AnnotationTarget.m_annotatedField) && !within(@Service)",
361 						NAMESPACE).getAdvisedClassFilterExpression().match(
362 				new ExpressionContext(PointcutType.GET, s_field, s_declaringType)
363 				)
364 		);
365 
366     	assertFalse(
367     			new ExpressionInfo(
368     					"get(@Persistable java.lang.Object m_innerField) && within(!@Service test.expression.*)",
369 						NAMESPACE).getExpression().match(
370 				new ExpressionContext(PointcutType.GET, s_innerField, s_innerType)
371 				)
372 		);
373     	
374     	// HINT annotations and types ignored
375     	assertTrue(
376     			new ExpressionInfo(
377     					"get(!@Persistable String m_innerField) && within(@Service test.expression.*)",
378 						NAMESPACE).getAdvisedClassFilterExpression().match(
379 				new ExpressionContext(PointcutType.GET, s_innerField, s_innerType)
380 				)
381 		);
382     }
383     
384 	public static junit.framework.Test suite() {
385         return new junit.framework.TestSuite(AdvisedClassFilterExpressionTest.class);
386 	}
387 
388 	public static void main(String[] args) {
389 		junit.textui.TestRunner.run(suite());
390 	}
391 
392 }