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;
9   
10  import junit.framework.TestCase;
11  
12  import java.io.PrintStream;
13  
14  /***
15   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16   */
17  public class FieldAdviceTest extends TestCase {
18      private static String s_logString = "";
19  
20      private static long s_setStaticFieldAroundAdviced = 0L;
21  
22      private static int s_setStaticFieldPreAdviced = 0;
23  
24      private static String s_setStaticFieldPostAdviced = "string";
25  
26      private static double s_setStaticFieldPrePostAdviced = 0.000D;
27  
28      private static long s_getStaticFieldAroundAdviced = 1L;
29  
30      private static int s_getStaticFieldPreAdviced = 1;
31  
32      private static String s_getStaticFieldPostAdviced = "string";
33  
34      private static double s_getStaticFieldPrePostAdviced = 1.1111D;
35  
36      private long m_setFieldAroundAdviced = 0L;
37  
38      private int m_setFieldAroundAdvicedWithNullAdvice = 0;
39  
40      private String m_setFieldAroundAdvicedObjectWithNullAdvice = new String("0");
41  
42      private String m_setFieldAroundAdvicedObjectWithAPI = new String("0");
43  
44      private int m_setFieldAroundAdvicedWithAPI = 0;
45  
46      private int m_setFieldPreAdviced = 0;
47  
48      private String m_setFieldPostAdviced = "string";
49  
50      private double m_setFieldPrePostAdviced = 0.000D;
51  
52      private long m_getFieldAroundAdviced = 1L;
53  
54      private String m_getFieldAroundAdvicedWithNullAdvice = "string";
55  
56      private double m_getFieldPreAdviced = 1.0000D;
57  
58      private int m_getFieldPostAdviced = 1;
59  
60      private int m_getFieldPrePostAdviced = 1;
61  
62      public FieldAdviceTest() {
63      }
64  
65      public FieldAdviceTest(String name) {
66          super(name);
67      }
68  
69      public void testSetMemberFieldAroundAdviced() {
70          s_logString = "";
71          try {
72              setFieldAroundAdviced();
73              assertEquals("before after ", s_logString);
74              assertEquals(187, m_setFieldAroundAdviced);
75          } catch (Exception e) {
76              fail();
77          }
78      }
79  
80      public void testSetMemberFieldAroundAdvicedWithNullAdvice() {
81          s_logString = "";
82          try {
83              setFieldAroundAdvicedWithNullAdvice();
84              assertEquals("before after ", s_logString);
85  
86              //CAUTION: null advice for @Set leave the assigned value
87              //The advice return value is ignored
88              assertEquals(187, m_setFieldAroundAdvicedWithNullAdvice);
89          } catch (Exception e) {
90              fail();
91          }
92      }
93  
94      public void testSetMemberFieldAroundAdvicedObjectWithNullAdvice() {
95          s_logString = "";
96          try {
97              setFieldAroundAdvicedObjectWithNullAdvice();
98              assertEquals("before after ", s_logString);
99  
100             //CAUTION: null advice for @Set leave the assigned value
101             //The advice return value is ignored
102             assertEquals("1", m_setFieldAroundAdvicedObjectWithNullAdvice);
103         } catch (Exception e) {
104             fail();
105         }
106     }
107 
108     //FIXME - activate when proceed(args) will be supported 
109 
110 //    public void testSetMemberFieldAroundAdvicedObjectWithAPI() {
111 //        s_logString = "";
112 //        try {
113 //            setFieldAroundAdvicedObjectWithAPI();
114 //            assertEquals("before after ", s_logString);
115 //
116 //            //The advice is using the Signature API to alter the assigned value
117 //            assertEquals("byAdvice", m_setFieldAroundAdvicedObjectWithAPI);
118 //        } catch (Exception e) {
119 //            fail();
120 //        }
121 //    }
122 //
123 //    public void testSetMemberFieldAroundAdvicedWithAPI() {
124 //        s_logString = "";
125 //        try {
126 //            setFieldAroundAdvicedWithAPI();
127 //            assertEquals("before after ", s_logString);
128 //
129 //            //The advice is using the Signature API to alter the assigned value
130 //            assertEquals(3, m_setFieldAroundAdvicedWithAPI);
131 //        } catch (Exception e) {
132 //            fail();
133 //        }
134 //    }
135 
136     public void testGetMemberFieldAroundAdviced() {
137         s_logString = "";
138         try {
139             long i = getFieldAroundAdviced(); // int default value
140             assertEquals("before after ", s_logString);
141             assertEquals(1L, i);
142         } catch (Exception e) {
143             fail();
144         }
145     }
146 
147     public void testGetMemberFieldAroundAdvicedWithNullAdvice() {
148         s_logString = "";
149         try {
150             String i = getFieldAroundAdvicedWithNullAdvice();
151             assertEquals("before after ", s_logString);
152             assertEquals(null, i);
153         } catch (Exception e) {
154             fail();
155         }
156     }
157 
158     public void testSetFieldPreAdviced() {
159         s_logString = "";
160         try {
161             setFieldPreAdviced();
162             assertEquals("pre1 pre2 ", s_logString);
163         } catch (Exception e) {
164             fail();
165         }
166     }
167 
168     public void testSetFieldPostAdviced() {
169         s_logString = "";
170         try {
171             setFieldPostAdviced();
172             assertEquals("post2 post1 ", s_logString);
173         } catch (Exception e) {
174             fail();
175         }
176     }
177 
178     public void testSetFieldPrePostAdviced() {
179         s_logString = "";
180         try {
181             setFieldPrePostAdviced();
182             assertEquals("pre1 pre2 post2 post1 ", s_logString);
183         } catch (Exception e) {
184             fail();
185         }
186     }
187 
188     public void testGetFieldPreAdviced() {
189         s_logString = "";
190         try {
191             getFieldPreAdviced();
192             assertEquals("pre1 pre2 ", s_logString);
193         } catch (Exception e) {
194             e.printStackTrace();
195             fail();
196         }
197     }
198 
199     public void testGetFieldPostAdviced() {
200         s_logString = "";
201         try {
202             getFieldPostAdviced();
203             assertEquals("post2 post1 ", s_logString);
204         } catch (Exception e) {
205             fail();
206         }
207     }
208 
209     public void testGetFieldPrePostAdviced() {
210         s_logString = "";
211         try {
212             getFieldPrePostAdviced();
213             assertEquals("pre1 pre2 post2 post1 ", s_logString);
214         } catch (Exception e) {
215             fail();
216         }
217     }
218 
219     public void testSetStaticFieldAroundAdviced() {
220         s_logString = "";
221         try {
222             setStaticFieldAroundAdviced();
223             assertEquals("before after ", s_logString);
224             assertEquals(3, s_setStaticFieldAroundAdviced);
225         } catch (Exception e) {
226             fail();
227         }
228     }
229 
230     public void testGetStaticFieldAroundAdviced() {
231         s_logString = "";
232         try {
233             long i = getStaticFieldAroundAdviced();
234             assertEquals("before after ", s_logString);
235             assertEquals(1L, i);
236         } catch (Exception e) {
237             fail();
238         }
239     }
240 
241     public void testSetStaticFieldPreAdviced() {
242         s_logString = "";
243         try {
244             setStaticFieldPreAdviced();
245             assertEquals("pre1 pre2 ", s_logString);
246         } catch (Exception e) {
247             fail();
248         }
249     }
250 
251     public void testSetStaticFieldPostAdviced() {
252         s_logString = "";
253         try {
254             setStaticFieldPostAdviced();
255             assertEquals("post2 post1 ", s_logString);
256         } catch (Exception e) {
257             fail();
258         }
259     }
260 
261     public void testSetStaticFieldPrePostAdviced() {
262         s_logString = "";
263         try {
264             setStaticFieldPrePostAdviced();
265             assertEquals("pre1 pre2 post2 post1 ", s_logString);
266         } catch (Exception e) {
267             fail();
268         }
269     }
270 
271     public void testGetStaticFieldPreAdviced() {
272         s_logString = "";
273         try {
274             getStaticFieldPreAdviced();
275             assertEquals("pre1 pre2 ", s_logString);
276         } catch (Exception e) {
277             fail();
278         }
279     }
280 
281     public void testGetStaticFieldPostAdviced() {
282         s_logString = "";
283         try {
284             getStaticFieldPostAdviced();
285             assertEquals("post2 post1 ", s_logString);
286         } catch (Exception e) {
287             fail();
288         }
289     }
290 
291     public void testStaticGetFieldPrePostAdviced() {
292         s_logString = "";
293         try {
294             getStaticFieldPrePostAdviced();
295             assertEquals("pre1 pre2 post2 post1 ", s_logString);
296         } catch (Exception e) {
297             fail();
298         }
299     }
300 
301     public void testPublicFieldOutOfWeaverScope() {
302         s_logString = "";
303         PrintStream out = System.out;//field get(* java.lang.System) && withincode ..
304         PrintStream err = System.err;        
305         assertEquals("adviceOnPublicField ", s_logString);
306     }
307 
308     public static void main(String[] args) {
309         junit.textui.TestRunner.run(suite());
310     }
311 
312     public static junit.framework.Test suite() {
313         return new junit.framework.TestSuite(FieldAdviceTest.class);
314     }
315 
316     // ==== methods to test ====
317     public static void log(final String wasHere) {
318         s_logString += wasHere;
319     }
320 
321     public void setFieldAroundAdviced() {
322         m_setFieldAroundAdviced = 3 + (23 * 8);
323     }
324 
325     public void setFieldAroundAdvicedWithNullAdvice() {
326         m_setFieldAroundAdvicedWithNullAdvice = 3 + (23 * 8);
327     }
328 
329     public void setFieldAroundAdvicedObjectWithNullAdvice() {
330         m_setFieldAroundAdvicedObjectWithNullAdvice = new String("1");
331     }
332 
333     public void setFieldAroundAdvicedObjectWithAPI() {
334         m_setFieldAroundAdvicedObjectWithAPI = new String("original");
335     }
336 
337     public void setFieldAroundAdvicedWithAPI() {
338         m_setFieldAroundAdvicedWithAPI = 2;
339     }
340 
341     public void setFieldPreAdviced() {
342         m_setFieldPreAdviced = 3 + (23 * 8);
343     }
344 
345     public void setFieldPostAdviced() {
346         m_setFieldPostAdviced = "asdf";
347     }
348 
349     public void setFieldPrePostAdviced() {
350         m_setFieldPrePostAdviced = 3;
351     }
352 
353     public long getFieldAroundAdviced() {
354         return m_getFieldAroundAdviced;
355     }
356 
357     public String getFieldAroundAdvicedWithNullAdvice() {
358         return m_getFieldAroundAdvicedWithNullAdvice;
359     }
360 
361     public double getFieldPreAdviced() {
362         return m_getFieldPreAdviced;
363     }
364 
365     public int getFieldPostAdviced() {
366         return m_getFieldPostAdviced;
367     }
368 
369     public int getFieldPrePostAdviced() {
370         return m_getFieldPrePostAdviced;
371     }
372 
373     public static void setStaticFieldAroundAdviced() {
374         s_setStaticFieldAroundAdviced = 3;
375     }
376 
377     public static void setStaticFieldPreAdviced() {
378         s_setStaticFieldPreAdviced = 3;
379     }
380 
381     public static void setStaticFieldPostAdviced() {
382         s_setStaticFieldPostAdviced = "asdf";
383     }
384 
385     public static void setStaticFieldPrePostAdviced() {
386         s_setStaticFieldPrePostAdviced = 3;
387     }
388 
389     public static long getStaticFieldAroundAdviced() {
390         return s_getStaticFieldAroundAdviced;
391     }
392 
393     public static int getStaticFieldPreAdviced() {
394         return s_getStaticFieldPreAdviced;
395     }
396 
397     public static String getStaticFieldPostAdviced() {
398         return s_getStaticFieldPostAdviced;
399     }
400 
401     public static double getStaticFieldPrePostAdviced() {
402         return s_getStaticFieldPrePostAdviced;
403     }
404 }