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.args;
9   
10  import test.Loggable;
11  import junit.framework.TestCase;
12  
13  /***
14   * Test for args() syntax and pointcut / advice with signatures.
15   * Some tests to cover XML syntax.
16   * TODO: test for CALL pc and ctor exe/call jp
17   *
18   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
19   */
20  public class ArgsAdviceTest extends TestCase implements Loggable {
21  
22      private String m_logString = "";
23      private static String s_logString = "";
24  
25      // used for ctor call and static field set, else we use jp.getTarget()
26      public static void logStatic(String s) {
27          s_logString += s;
28      }
29  
30      // execution(* m(..)) && args(i)
31      public void testSingleAndDotDot() {
32          m_logString = "";
33          singleAndDotDot(1);
34          assertEquals("before 1 invocation ", m_logString);
35      }
36  
37      // all bounded :(long l, String s, int[][] matrix)
38      public void testWithArray() {
39          m_logString = "";
40          int[][] iis = new int[][]{{1, 2}, {3}};
41          withArray(1L, "h", iis);
42          assertEquals("before 1 h 1-2-3- invocation ", m_logString);
43      }
44  
45      //args(String, String, long)
46      public void testMatchAll() {
47          m_logString = "";
48          matchAll("a0", "a1", 2);
49          assertEquals("before before1 invocation after1 after ", m_logString);
50          m_logString = "";
51          matchAllXML("a0", "a1", 2);
52          assertEquals("before before1 invocation after1 after ", m_logString);
53      }
54  
55      //args(..)
56      public void testMatchAllWithWildcard() {
57          m_logString = "";
58          matchAllWithWildcard("a0", "a1", 2);
59          assertEquals("before before1 invocation after1 after ", m_logString);
60      }
61  
62      //args(s, ..)
63      public void testGetFirst() {
64          m_logString = "";
65          getFirst("a0", "a1", 2);
66          assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
67          m_logString = "";
68          getFirstXML("a0", "a1", 2);
69          assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
70  
71      }
72  
73      //args(s, ..) as anonymous pointcut
74      public void testGetFirstAnonymous() {
75          m_logString = "";
76          getFirstAnonymous("a0", "a1", 2);
77          assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
78          //TODO (low prio): anonymous pc with args() is not supported in XML - see notes in aop.xml
79  //        m_logString = "";
80  //        getFirstAnonymousXML("a0", "a1", 2);
81  //        assertEquals("before a0 before1 a0 invocation after1 a0 after a0 ", m_logString);
82      }
83  
84      //args(String, s, long) and increment it
85      public void testChangeArg() {
86          m_logString = "";
87          changeArg("a0", new StringBuffer("a1"), 2);
88          // beware: using String won't work as for regular Java behavior
89          assertEquals("before a1x before1 a1xx invocation after1 a1xxx after a1xxxx ", m_logString);
90      }
91  
92      // args(s0, s1, long), with Pc signature reversed
93      public void testOrderChangedInPointcutSignature() {
94          m_logString = "";
95          orderChangedInPointcutSignature("a0", "a1", 2);
96          assertEquals("before a1 a0 before1 a1 a0 invocation after1 a1 a0 after a1 a0 ", m_logString);
97      }
98  
99      // args(s0, s1, long), with Advice signature reversed
100     public void testOrderChangedInAdviceSignature() {
101         m_logString = "";
102         orderChangedInAdviceSignature("a0", "a1", 2);
103         assertEquals("before a1 a0 before1 a1 a0 invocation after1 a1 a0 after a1 a0 ", m_logString);
104     }
105 
106     // args(s0, s1, long), with Pointcut and Advice signature reversed
107     public void testOrderChangedInPointcutAndAdviceSignature() {
108         m_logString = "";
109         orderChangedInPointcutAndAdviceSignature("a0", "a1", 2);
110         assertEquals("before a0 a1 before1 a0 a1 invocation after1 a0 a1 after a0 a1 ", m_logString);
111         m_logString = "";
112         orderChangedInPointcutAndAdviceSignatureXML("a0", "a1", null);
113         assertEquals("before a0 a1 before1 a0 a1 invocation after1 a0 a1 after a0 a1 ", m_logString);
114     }
115 
116     //-- method call pointcuts
117 
118     //args(l<long>, s<String[]>)
119     public void testCallGetFirstAndSecond() {
120         m_logString = "";
121         callGetFirstAndSecond(1L, new String[]{"s0", "s1"});
122         assertEquals("before 1 s0,s1 before1 1 s0,s1 invocation after1 1 s0,s1 after 1 s0,s1 ", m_logString);
123         m_logString = "";
124         callGetFirstAndSecondXML(1L, new String[]{"s0", "s1"}, null);
125         assertEquals("before 1 s0,s1 before1 1 s0,s1 invocation after1 1 s0,s1 after 1 s0,s1 ", m_logString);
126     }
127 
128     //-- ctor execution
129     //args(s)
130     public void testCtorExecutionGetFirst() {
131         //FIXME
132         // looks like a bug for ctor executiona and inner class inheritance
133         // see CtorLoggable and CtorExecution<init>, that has the call to CtorLoggable<init> corrupted
134         m_logString = "";
135         CtorExecution target = new CtorExecution("s");
136         assertEquals("before s before1 s invocation after1 s after s ", m_logString);
137         m_logString = "";
138         CtorExecutionXML target2 = new CtorExecutionXML("s");
139         assertEquals("before s before1 s invocation after1 s after s ", m_logString);
140     }
141 
142     //-- ctor call
143     //args(s)
144     public void testCtorCallGetFirst() {
145         s_logString = "";
146         CtorCall target = new CtorCall("s");
147         assertEquals("before s before1 s invocation after1 s after s ", s_logString);
148         s_logString = "";
149         CtorCallXML target2 = new CtorCallXML("s");
150         assertEquals("before s before1 s invocation after1 s after s ", s_logString);
151     }
152 
153     //-- field set
154     private String m_field;
155     private static String s_field;
156 
157     public String getField() {
158         return m_field;
159     }
160 
161     public static String getStaticField() {
162         return s_field;
163     }
164 
165     //arg(s)
166     public void testFieldSetArg() {
167         try {
168             m_logString = "";
169             m_field = "s";
170             assertEquals("before null,s before1 null,s after1 s,changed after s,s ", m_logString);
171             s_logString = "";
172             s_field = "s";
173             assertEquals("before null,s before1 null,s after1 s,changed after s,s ", s_logString);
174         } catch (Error e) {
175             e.printStackTrace();
176         }
177     }
178 
179 
180     //-- Implementation methods
181     public void log(String s) {
182         m_logString += s;
183     }
184 
185     public void singleAndDotDot(int i) {
186         log("invocation ");
187     }
188 
189     public void withArray(long l, String s, int[][] matrix) {
190         log("invocation ");
191     }
192 
193     public void matchAll(String a0, String a1, long a2) {
194         log("invocation ");
195     }
196 
197     public void matchAllXML(String a0, String a1, long a2) {
198         log("invocation ");
199     }
200 
201     public void matchAllWithWildcard(String a0, String a1, long a2) {
202         log("invocation ");
203     }
204 
205     public void getFirst(String a0, String a1, long a2) {
206         log("invocation ");
207     }
208 
209     public void getFirstXML(String a0, String a1, long a2) {
210         log("invocation ");
211     }
212 
213     public void getFirstAnonymous(String a0, String a1, long a2) {
214         log("invocation ");
215     }
216 
217     public void getFirstAnonymousXML(String a0, String a1, long a2) {
218         log("invocation ");
219     }
220 
221     public void changeArg(String a0, StringBuffer a1, long a2) {
222         log("invocation ");
223     }
224 
225     public void orderChangedInPointcutSignature(String a0, String a1, long a2) {
226         log("invocation ");
227     }
228 
229     public void orderChangedInAdviceSignature(String a0, String a1, long a2) {
230         log("invocation ");
231     }
232 
233     public void orderChangedInPointcutAndAdviceSignature(String a0, String a1, long a2) {
234         log("invocation ");
235     }
236 
237     public void orderChangedInPointcutAndAdviceSignatureXML(String a0, String a1, Object[] a2) {
238         log("invocation ");
239     }
240 
241     //-- method call
242     public void callGetFirstAndSecond(long l, String[] s) {
243         log("invocation ");
244     }
245 
246     public void callGetFirstAndSecondXML(long l, String[] s, String[] ignore) {
247         log("invocation ");
248     }
249 
250     class CtorLoggable implements Loggable {
251         public CtorLoggable() {
252         }
253 
254         public void log(String s) {
255             m_logString += s;
256         }
257     }
258 
259     //-- ctor execution
260     class CtorExecution extends CtorLoggable {
261         public CtorExecution(String s) {
262             this.log("invocation ");
263         }
264     }
265 
266     class CtorExecutionXML extends CtorLoggable {
267         public CtorExecutionXML(String s) {
268             this.log("invocation ");
269         }
270     }
271 
272     //-- ctor call
273     class CtorCall extends CtorLoggable {
274         public CtorCall(String s) {
275             logStatic("invocation ");
276         }
277     }
278 
279     class CtorCallXML extends CtorLoggable {
280         public CtorCallXML(String s) {
281             logStatic("invocation ");
282         }
283     }
284 
285 
286     //-- JUnit
287     public static void main(String[] args) {
288         junit.textui.TestRunner.run(suite());
289     }
290 
291     public static junit.framework.Test suite() {
292         return new junit.framework.TestSuite(ArgsAdviceTest.class);
293     }
294 
295 }