1   /***************************************************************************************
2   
3    * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved.                 *
4   
5    * http://aspectwerkz.codehaus.org                                                    *
6   
7    * ---------------------------------------------------------------------------------- *
8   
9    * The software in this package is published under the terms of the LGPL license      *
10  
11   * a copy of which has been included with this distribution in the license.txt file.  *
12  
13   **************************************************************************************/
14  
15  package test.args;
16  
17  
18  
19  import test.Loggable;
20  
21  import junit.framework.TestCase;
22  
23  
24  
25  /***
26  
27   * Test for args() syntax and pointcut / advice with signatures.
28  
29   * Some tests to cover XML syntax.
30  
31   * TODO: test for CALL pc and ctor exe/call jp
32  
33   *
34  
35   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
36  
37   */
38  
39  public class ArgsAdviceTest extends TestCase implements Loggable {
40  
41  
42  
43      private String m_logString = "";
44  
45      private static String s_logString = "";
46  
47      // used for ctor call and static field set, else we use jp.getTarget()
48  
49      public static void logStatic(String s) {
50  
51          s_logString += s;
52  
53      }
54  
55  
56  
57      //args(String, String, long)
58  
59      public void testMatchAll() {
60  
61          m_logString = "";
62  
63          matchAll("a0", "a1", 2);
64  
65          assertEquals("before before1 invocation after1 after ", m_logString);
66  
67          m_logString = "";
68  
69          matchAllXML("a0", "a1", 2);
70  
71          assertEquals("before before1 invocation after1 after ", m_logString);
72  
73      }
74  
75  
76  
77      //args(..)
78  
79      public void testMatchAllWithWildcard() {
80  
81          m_logString = "";
82  
83          matchAllWithWildcard("a0", "a1", 2);
84  
85          assertEquals("before before1 invocation after1 after ", m_logString);
86  
87      }
88  
89  
90  
91      //args(s, ..)
92  
93      public void testGetFirst() {
94  
95          m_logString = "";
96  
97          getFirst("a0", "a1", 2);
98  
99          assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
100 
101         m_logString = "";
102 
103         getFirstXML("a0", "a1", 2);
104 
105         assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
106 
107 
108 
109     }
110 
111 
112 
113     //args(s, ..) as anonymous pointcut
114 
115     public void testGetFirstAnonymous() {
116 
117         m_logString = "";
118 
119         getFirstAnonymous("a0", "a1", 2);
120 
121         assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
122 
123         //TODO (low prio): anonymous pc with args() is not supported in XML - see notes in test-attribdef.xml
124 
125 //        m_logString = "";
126 
127 //        getFirstAnonymousXML("a0", "a1", 2);
128 
129 //        assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
130 
131     }
132 
133 
134 
135     //args(String, s, long) and increment it
136 
137     public void testChangeArg() {
138 
139         m_logString = "";
140 
141         changeArg("a0", new StringBuffer("a1"), 2);
142 
143         // beware: using String won't work as for regular Java behavior
144 
145         assertEquals("before a1x before1 a1xx invocation after1 a1xxx after a1xxxx ", m_logString);
146 
147     }
148 
149 
150 
151     // args(s0, s1, long), with Pc signature reversed
152 
153     public void testOrderChangedInPointcutSignature() {
154 
155         m_logString = "";
156 
157         orderChangedInPointcutSignature("a0", "a1", 2);
158 
159         assertEquals("before a1 a0 before1 a1 a0 invocation after1 a1 a0 after a1 a0 ", m_logString);
160 
161     }
162 
163 
164 
165     // args(s0, s1, long), with Advice signature reversed
166 
167     public void testOrderChangedInAdviceSignature() {
168 
169         m_logString = "";
170 
171         orderChangedInAdviceSignature("a0", "a1", 2);
172 
173         assertEquals("before a1 a0 before1 a1 a0 invocation after1 a1 a0 after a1 a0 ", m_logString);
174 
175     }
176 
177 
178 
179     // args(s0, s1, long), with Pointcut and Advice signature reversed
180 
181     public void testOrderChangedInPointcutAndAdviceSignature() {
182 
183         m_logString = "";
184 
185         orderChangedInPointcutAndAdviceSignature("a0", "a1", 2);
186 
187         assertEquals("before a0 a1 before1 a0 a1 invocation after1 a0 a1 after a0 a1 ", m_logString);
188 
189         m_logString = "";
190 
191         orderChangedInPointcutAndAdviceSignatureXML("a0", "a1", null);
192 
193         assertEquals("before a0 a1 before1 a0 a1 invocation after1 a0 a1 after a0 a1 ", m_logString);
194 
195     }
196 
197 
198 
199     //-- method call pointcuts
200 
201 
202 
203     //args(l<long>, s<String[]>)
204 
205     public void testCallGetFirstAndSecond() {
206 
207         m_logString = "";
208 
209         callGetFirstAndSecond(1L, new String[]{"s0", "s1"});
210 
211         assertEquals("before 1 s0,s1 before1 1 s0,s1 invocation after1 1 s0,s1 after 1 s0,s1 ", m_logString);
212 
213         m_logString = "";
214 
215         callGetFirstAndSecondXML(1L, new String[]{"s0", "s1"}, null);
216 
217         assertEquals("before 1 s0,s1 before1 1 s0,s1 invocation after1 1 s0,s1 after 1 s0,s1 ", m_logString);
218 
219     }
220 
221 
222 
223     //-- ctor execution
224 
225     //args(s)
226 
227     public void testCtorExecutionGetFirst() {
228 
229         //FIXME
230 
231         // looks like a bug for ctor executiona and inner class inheritance
232 
233         // see CtorLoggable and CtorExecution<init>, that has the call to CtorLoggable<init> corrupted
234 
235 //        m_logString = "";
236 
237 //        CtorExecution target = new CtorExecution("s");
238 
239 //        assertEquals("before s before1 s invocation after1 s after s ", m_logString);
240 
241 //        m_logString = "";
242 
243 //        CtorExecutionXML target2 = new CtorExecutionXML("s");
244 
245 //        assertEquals("before s before1 s invocation after1 s after s ", m_logString);
246 
247     }
248 
249 
250 
251     //-- ctor call
252 
253     //args(s)
254 
255     public void testCtorCallGetFirst() {
256 
257         s_logString = "";
258 
259         CtorCall target = new CtorCall("s");
260 
261         assertEquals("before s before1 s invocation after1 s after s ", s_logString);
262 
263         s_logString = "";
264 
265         CtorCallXML target2 = new CtorCallXML("s");
266 
267         assertEquals("before s before1 s invocation after1 s after s ", s_logString);
268 
269     }
270 
271 
272 
273     //-- field set
274 
275     private String m_field;
276 
277     private static String s_field;
278 
279     public String getField() {return m_field;}
280 
281     public static String getStaticField() {return s_field;}
282 
283     //arg(s)
284 
285     public void testFieldSetArg() {
286 
287         try {
288 
289         m_logString = "";
290 
291         m_field = "s";
292 
293         assertEquals("before null,s before1 null,s after1 s,changed after s,s ", m_logString);
294 
295         s_logString = "";
296 
297         s_field = "s";
298 
299         assertEquals("before null,s before1 null,s after1 s,changed after s,s ", s_logString);
300 
301         } catch (Error e) {
302 
303             e.printStackTrace();
304 
305         }
306 
307     }
308 
309 
310 
311 
312 
313     //-- Implementation methods
314 
315     public void log(String s) {
316 
317         m_logString += s;
318 
319     }
320 
321     public void matchAll(String a0, String a1, long a2) {
322 
323         log("invocation ");
324 
325     }
326 
327     public void matchAllXML(String a0, String a1, long a2) {
328 
329         log("invocation ");
330 
331     }
332 
333     public void matchAllWithWildcard(String a0, String a1, long a2) {
334 
335         log("invocation ");
336 
337     }
338 
339     public void getFirst(String a0, String a1, long a2) {
340 
341         log("invocation ");
342 
343     }
344 
345     public void getFirstXML(String a0, String a1, long a2) {
346 
347         log("invocation ");
348 
349     }
350 
351     public void getFirstAnonymous(String a0, String a1, long a2) {
352 
353         log("invocation ");
354 
355     }
356 
357     public void getFirstAnonymousXML(String a0, String a1, long a2) {
358 
359         log("invocation ");
360 
361     }
362 
363     public void changeArg(String a0, StringBuffer a1, long a2) {
364 
365         log("invocation ");
366 
367     }
368 
369     public void orderChangedInPointcutSignature(String a0, String a1, long a2) {
370 
371         log("invocation ");
372 
373     }
374 
375     public void orderChangedInAdviceSignature(String a0, String a1, long a2) {
376 
377         log("invocation ");
378 
379     }
380 
381     public void orderChangedInPointcutAndAdviceSignature(String a0, String a1, long a2) {
382 
383         log("invocation ");
384 
385     }
386 
387     public void orderChangedInPointcutAndAdviceSignatureXML(String a0, String a1, Object[] a2) {
388 
389         log("invocation ");
390 
391     }
392 
393 
394 
395     //-- method call
396 
397     public void callGetFirstAndSecond(long l, String[] s) {
398 
399         log("invocation ");
400 
401     }
402 
403     public void callGetFirstAndSecondXML(long l, String[] s, String[] ignore) {
404 
405         log("invocation ");
406 
407     }
408 
409 
410 
411     class CtorLoggable implements Loggable {
412 
413         public CtorLoggable() {}
414 
415         public void log(String s) {
416 
417             m_logString += s;
418 
419         }
420 
421     }
422 
423 
424 
425     //-- ctor execution
426 
427     class CtorExecution extends CtorLoggable {
428 
429         public CtorExecution(String s) {
430 
431             this.log("invocation ");
432 
433         }
434 
435     }
436 
437     class CtorExecutionXML extends CtorLoggable {
438 
439         public CtorExecutionXML(String s) {
440 
441             this.log("invocation ");
442 
443         }
444 
445     }
446 
447 
448 
449     //-- ctor call
450 
451     class CtorCall extends CtorLoggable {
452 
453         public CtorCall(String s) {
454 
455             logStatic("invocation ");
456 
457         }
458 
459     }
460 
461     class CtorCallXML extends CtorLoggable {
462 
463         public CtorCallXML(String s) {
464 
465             logStatic("invocation ");
466 
467         }
468 
469     }
470 
471 
472 
473 
474 
475     //-- JUnit
476 
477     public static void main(String[] args) {
478 
479         junit.textui.TestRunner.run(suite());
480 
481     }
482 
483 
484 
485     public static junit.framework.Test suite() {
486 
487         return new junit.framework.TestSuite(ArgsAdviceTest.class);
488 
489     }
490 
491 
492 
493 }
494