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 /***
13 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
14 */
15 public class StaticMethodAdviceTest extends TestCase {
16 private static String m_logString = "";
17
18 public StaticMethodAdviceTest() {
19 }
20
21 public StaticMethodAdviceTest(String name) {
22 super(name);
23 }
24
25 public void testMethodAdvice() {
26 m_logString = "";
27 methodAdvicedMethod();
28 assertEquals("before1 invocation after1 ", m_logString);
29 }
30
31 public void testMethodAdviceNewThread() {
32 m_logString = "";
33 methodAdvicedMethodNewThread();
34 assertEquals("before invocation after ", m_logString);
35 }
36
37 public void testMultipleChainedMethodAdvices() {
38 m_logString = "";
39 multipleChainedMethodAdvicedMethod();
40 assertEquals("before1 before2 invocation after2 after1 ", m_logString);
41 }
42
43 public void testMultiplePointcuts() {
44 try {
45 m_logString = "";
46 multiplePointcutsMethod();
47 assertEquals("before1 before2 invocation after2 after1 ", m_logString);
48 } catch (Throwable t) {
49 t.printStackTrace();
50 }
51 }
52
53 public void testGetJoinPointMetaData() {
54 String param = "parameter";
55 String pointcutName = joinPointMetaData(param);
56 assertEquals(getClass().getName()
57 + "___AW_access$original$_AW_$joinPointMetaData$_AW_$1$_AW_$test_StaticMethodAdviceTest"
58 + param
59 + param.getClass().getName()
60 + "java.lang.String"
61 + "result", pointcutName);
62 }
63
64 public void testHasPointcutButNoAdvice() {
65 try {
66 hasPointcutButNoAdvice();
67 } catch (Exception e) {
68 fail();
69 }
70 }
71
72 public void testAnonymousAdviced() {
73 try {
74 anonymousAdviced();
75 } catch (Exception e) {
76 fail();
77 }
78 }
79
80 public void testReturnPrimitiveAndNullFromAdvice() {
81 try {
82 assertEquals(0L, getPrimitiveAndNullFromAdvice());
83 } catch (NullPointerException e) {
84 fail("If method that returns a primitive has an advice that returns NULL then it causes a NPE. The NULL should be handled in bytecode and it should return the default value for the primitive (wrapped)");
85 }
86 }
87
88 public void testReturnVoid() {
89 getVoid();
90 }
91
92 public void testReturnLong() {
93 assertEquals(1L, getLong());
94 }
95
96
97 public void testReturnInt() {
98 assertEquals(1, getInt());
99 }
100
101 public void testReturnShort() {
102 assertEquals(1, getShort());
103 }
104
105 public void testReturnDouble() {
106 assertEquals(new Double(1.1D), new Double(getDouble()));
107 }
108
109 public void testReturnFloat() {
110 assertEquals(new Float(1.1F), new Float(getFloat()));
111 }
112
113 public void testReturnByte() {
114 assertEquals(Byte.parseByte("1"), getByte());
115 }
116
117 public void testReturnChar() {
118 assertEquals('A', getChar());
119 }
120
121 public void testReturnBoolean() {
122 assertEquals(true, getBoolean());
123 }
124
125 public void testNoArgs() {
126 noParams();
127 }
128
129 public void testIntArg() {
130 assertEquals(12, intParam(12));
131 }
132
133 public void testLongArg() {
134 assertEquals(12L, longParam(12L));
135 }
136
137 public void testShortArg() {
138 assertEquals(3, shortParam((short) 3));
139 }
140
141 public void testDoubleArg() {
142 assertEquals(new Double(2.3D), new Double(doubleParam(2.3D)));
143 }
144
145 public void testFloatArg() {
146 assertEquals(new Float(2.3F), new Float(floatParam(2.3F)));
147 }
148
149 public void testByteArg() {
150 assertEquals(Byte.parseByte("1"), byteParam(Byte.parseByte("1")));
151 }
152
153 public void testCharArg() {
154 assertEquals('B', charParam('B'));
155 }
156
157 public void testBooleanArg() {
158 assertEquals(false, booleanParam(false));
159 }
160
161 public void testObjectArg() {
162 assertEquals(this, objectParam(this));
163 }
164
165 public void testShortArrayArg() {
166 short[] array = new short[] {
167 1, 2, 3
168 };
169 assertTrue(shortArrayParam(array)[0] == array[0]);
170 assertTrue(shortArrayParam(array)[1] == array[1]);
171 assertTrue(shortArrayParam(array)[2] == array[2]);
172 }
173
174 public void testBooleanArrayArg() {
175 boolean[] array = new boolean[] {
176 true, false
177 };
178 assertTrue(booleanArrayParam(array)[0] == array[0]);
179 assertTrue(booleanArrayParam(array)[1] == array[1]);
180 }
181
182 public void testByteArrayArg() {
183 byte[] array = new byte[] {
184 1, 2, 3
185 };
186 assertTrue(byteArrayParam(array)[0] == array[0]);
187 assertTrue(byteArrayParam(array)[1] == array[1]);
188 assertTrue(byteArrayParam(array)[2] == array[2]);
189 }
190
191 public void testCharArrayArg() {
192 char[] array = new char[] {
193 'A', 'B', 'C'
194 };
195 assertTrue(charArrayParam(array)[0] == array[0]);
196 assertTrue(charArrayParam(array)[1] == array[1]);
197 assertTrue(charArrayParam(array)[2] == array[2]);
198 }
199
200 public void testLongArrayArg() {
201 long[] array = new long[] {
202 1L, 2L, 3L
203 };
204 assertTrue(longArrayParam(array)[0] == array[0]);
205 assertTrue(longArrayParam(array)[1] == array[1]);
206 assertTrue(longArrayParam(array)[2] == array[2]);
207 }
208
209 public void testIntArrayArg() {
210 int[] array = new int[] {
211 1, 2, 3
212 };
213 assertTrue(intArrayParam(array)[0] == array[0]);
214 assertTrue(intArrayParam(array)[1] == array[1]);
215 assertTrue(intArrayParam(array)[2] == array[2]);
216 }
217
218 public void testFloatArrayArg() {
219 float[] array = new float[] {
220 1.1F, 2.1F, 3.1F
221 };
222 assertTrue(floatArrayParam(array)[0] == array[0]);
223 assertTrue(floatArrayParam(array)[1] == array[1]);
224 assertTrue(floatArrayParam(array)[2] == array[2]);
225 }
226
227 public void testVariousArguments1() {
228 assertEquals(
229 "dummy".hashCode() + 1 + (int) 2.3F,
230 this.hashCode() + (int) 34L,
231 variousParams1("dummy", 1, 2.3F, this, 34L));
232 }
233
234 public void testVariousArguments2() {
235 assertEquals((int) 2.3F
236 + 1
237 + "dummy".hashCode()
238 + this.hashCode()
239 + (int) 34L
240 + "test".hashCode(), variousParams2(2.3F, 1, "dummy", this, 34L, "test"));
241 }
242
243 public void testVariousArguments4() {
244 assertEquals("dummy", takesArrayAsArgument(new String[] {
245 "dummy", "test"
246 })[0]);
247 assertEquals("test", takesArrayAsArgument(new String[] {
248 "dummy", "test"
249 })[1]);
250 }
251
252 public static void main(String[] args) {
253 junit.textui.TestRunner.run(suite());
254 }
255
256 public static junit.framework.Test suite() {
257 return new junit.framework.TestSuite(StaticMethodAdviceTest.class);
258 }
259
260
261 public static void log(final String wasHere) {
262 m_logString += wasHere;
263 }
264
265 private static void nonAdvisedMethod() {
266 }
267
268 public static void methodAdvicedMethod() {
269 log("invocation ");
270 }
271
272 private static void methodAdvicedMethodNewThread() {
273 log("invocation ");
274 }
275
276 public static void multipleMethodAdvicedMethod() {
277 log("invocation ");
278 }
279
280 private static void multipleChainedMethodAdvicedMethod() {
281 log("invocation ");
282 }
283
284 public static void multipleMethodAndPrePostAdvicedMethod() {
285 log("invocation ");
286 }
287
288 public static void methodAdvicedWithPreAndPost() {
289 log("invocation ");
290 }
291
292 public static void multipleMethodAdvicedWithPreAndPost() {
293 log("invocation ");
294 }
295
296 public static void methodAdviceWithMultiplePreAndPostAdviced() {
297 log("invocation ");
298 }
299
300 public static void multiplePointcutsMethod() {
301 log("invocation ");
302 }
303
304 public static void exceptionThrower() throws Throwable {
305 throw new UnsupportedOperationException("this is a test");
306 }
307
308 public static String joinPointMetaData(String param) {
309 return "result";
310 }
311
312 public static void hasPointcutButNoAdvice() {
313 }
314
315 public static String postAdviced() {
316 return "test";
317 }
318
319 public static void anonymousAdviced() {
320 }
321
322 public static void throwsException() throws Exception {
323 throw new Exception("test");
324 }
325
326 public static void throwsRuntimeException() {
327 throw new RuntimeException("test");
328 }
329
330 public static void throwsError() {
331 throw new Error("test");
332 }
333
334 public static void noParams() throws RuntimeException {
335 }
336
337 private static long longParam(long arg) {
338 return arg;
339 }
340
341 public static int intParam(int arg) {
342 return arg;
343 }
344
345 public static short shortParam(short arg) {
346 return arg;
347 }
348
349 public static double doubleParam(double arg) {
350 return arg;
351 }
352
353 public static float floatParam(float arg) {
354 return arg;
355 }
356
357 public static byte byteParam(byte arg) {
358 return arg;
359 }
360
361 public static boolean booleanParam(boolean arg) {
362 return arg;
363 }
364
365 private static char charParam(char arg) {
366 return arg;
367 }
368
369 public static Object objectParam(Object arg) {
370 return arg;
371 }
372
373 private static int variousParams1(String str, int i, float f, Object o, long l) throws RuntimeException {
374 return str.hashCode() + i + (int) f + o.hashCode() + (int) l;
375 }
376
377 public static int variousParams2(float f, int i, String str1, Object o, long l, String str2) throws RuntimeException {
378 return (int) f + i + str1.hashCode() + o.hashCode() + (int) l + str2.hashCode();
379 }
380
381 public static float variousParams3(
382 String s,
383 long y,
384 String t,
385 String r,
386 String e,
387 int w,
388 String q) {
389 return 2.5F;
390 }
391
392 public static String[] takesArrayAsArgument(String[] arr) {
393 return arr;
394 }
395
396 public short[] shortArrayParam(short[] arg) {
397 return arg;
398 }
399
400 public boolean[] booleanArrayParam(boolean[] arg) {
401 return arg;
402 }
403
404 public byte[] byteArrayParam(byte[] arg) {
405 return arg;
406 }
407
408 public long[] longArrayParam(long[] arg) {
409 return arg;
410 }
411
412 public float[] floatArrayParam(float[] arg) {
413 return arg;
414 }
415
416 public char[] charArrayParam(char[] arg) {
417 return arg;
418 }
419
420 public int[] intArrayParam(int[] arg) {
421 return arg;
422 }
423
424 public static void getVoid() throws RuntimeException {
425 }
426
427 public static long getLong() throws RuntimeException {
428 return 1L;
429 }
430
431 public static int getInt() throws RuntimeException {
432 return 1;
433 }
434
435 public static short getShort() throws RuntimeException {
436 return 1;
437 }
438
439 private static double getDouble() throws RuntimeException {
440 return 1.1D;
441 }
442
443 public static float getFloat() throws RuntimeException {
444 return 1.1F;
445 }
446
447 public static byte getByte() throws RuntimeException {
448 return Byte.parseByte("1");
449 }
450
451 public static char getChar() throws RuntimeException {
452 return 'A';
453 }
454
455 private static boolean getBoolean() throws RuntimeException {
456 return true;
457 }
458
459 public static long getPrimitiveAndNullFromAdvice() throws RuntimeException {
460 return 123456789L;
461 }
462 }