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