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 * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
15 */
16 public class MemberMethodAdviceTest extends TestCase implements Loggable {
17 private String java = "a field that can make Javassist confused, AW-147 item2, fixed in AW 1.0-beta1";
18
19 private String m_logString = "";
20
21 public MemberMethodAdviceTest() {
22
23 }
24
25 public void testBeforeAroundAroundAfterAdvice() {
26 m_logString = "";
27 try {
28 beforeAroundAfterAdvicedMethod();
29 } catch (Exception e) {
30 e.printStackTrace();
31 }
32 assertEquals("pre before1 before2 invocation after2 after1 post ", m_logString);
33 }
34
35 public void testBeforeAdvice() {
36 m_logString = "";
37 beforeAdvicedMethod();
38 assertEquals("pre invocation ", m_logString);
39 }
40
41 public void testAfterAdvice() {
42 m_logString = "";
43 afterAdvicedMethod();
44 assertEquals("invocation post ", m_logString);
45 }
46
47 public void testBeforeAfterAdvice() {
48 m_logString = "";
49 beforeAfterAdvicedMethod();
50 assertEquals("pre invocation post ", m_logString);
51 }
52
53 public void testAroundAdvice() {
54 m_logString = "";
55 methodAdvicedMethod();
56 assertEquals("before1 invocation after1 ", m_logString);
57 }
58
59 public void testAroundAdvice2() {
60 m_logString = "";
61 methodAdvicedMethod(0);
62 assertEquals("invocation ", m_logString);
63 }
64
65 public void testAroundAdviceNewThread() {
66 m_logString = "";
67
68 methodAdvicedMethodNewThread();
69 assertEquals("before before invocation after after ", m_logString);
70 }
71
72 public void testMultipleAroundAdvices() {
73 m_logString = "";
74 multipleMethodAdvicedMethod();
75 assertEquals("before1 before2 invocation after2 after1 ", m_logString);
76 }
77
78 public void testMultipleChainedAroundAdvices() {
79 m_logString = "";
80 multipleChainedMethodAdvicedMethod();
81 assertEquals("before1 before2 invocation after2 after1 ", m_logString);
82 }
83
84 public void testMultiplePointcuts() {
85 m_logString = "";
86 multiplePointcutsMethod();
87 assertEquals("before2 before1 invocation after1 after2 ", m_logString);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public void testHasPointcutButNoAdvice() {
104 try {
105 hasPointcutButNoAdvice();
106 } catch (Exception e) {
107 fail();
108 }
109 }
110
111 public void testAnonymousAdviced() {
112 try {
113 anonymousAdviced();
114 } catch (Exception e) {
115 fail();
116 }
117 }
118
119 public void testThrowException() {
120 try {
121 exceptionThrower();
122 } catch (Throwable e) {
123 assertTrue(e instanceof UnsupportedOperationException);
124 return;
125 }
126 fail("this point should never be reached");
127 }
128
129 public void testThrowExceptionChecked() {
130 try {
131 exceptionThrowerChecked();
132 } catch (Throwable e) {
133 assertTrue(e instanceof CheckedException);
134 return;
135 }
136 fail("this point should never be reached");
137 }
138
139 public void testReturnVoid() {
140 getVoid();
141 }
142
143 public void testReturnLong() {
144 assertEquals(1L, getLong());
145 }
146
147 public void testReturnInt() {
148 assertEquals(1, getInt());
149 }
150
151 public void testReturnShort() {
152 assertEquals(1, getShort());
153 }
154
155 public void testReturnDouble() {
156 assertEquals(new Double(1.1D), new Double(getDouble()));
157 }
158
159 public void testReturnFloat() {
160 assertEquals(new Float(1.1F), new Float(getFloat()));
161 }
162
163 public void testReturnByte() {
164 assertEquals(Byte.parseByte("1"), getByte());
165 }
166
167 public void testReturnChar() {
168 assertEquals('A', getChar());
169 }
170
171 public void testReturnPrimitiveAndNullFromAdvice() {
172 try {
173 assertEquals(0L, getPrimitiveAndNullFromAdvice());
174 } catch (NullPointerException e) {
175 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)");
176 }
177 }
178
179 public void testReturnBoolean() {
180 assertEquals(true, getBoolean());
181 }
182
183 public void testNoArgs() {
184 noParams();
185 }
186
187 public void testIntArg() {
188 assertEquals(12, intParam(12));
189 }
190
191 public void testLongArg() {
192 assertEquals(12L, longParam(12L));
193 }
194
195 public void testShortArg() {
196 assertEquals(3, shortParam((short) 3));
197 }
198
199 public void testDoubleArg() {
200 assertEquals(new Double(2.3D), new Double(doubleParam(2.3D)));
201 }
202
203 public void testFloatArg() {
204 assertEquals(new Float(2.3F), new Float(floatParam(2.3F)));
205 }
206
207 public void testByteArg() {
208 assertEquals(Byte.parseByte("1"), byteParam(Byte.parseByte("1")));
209 }
210
211 public void testCharArg() {
212 assertEquals('B', charParam('B'));
213 }
214
215 public void testBooleanArg() {
216 assertEquals(false, booleanParam(false));
217 }
218
219 public void testObjectArg() {
220 assertEquals(this, objectParam(this));
221 }
222
223 public void testObjectArrayArg() {
224 String[] array = new String[] {
225 "one", "two", "three"
226 };
227 assertTrue(arrayParam(array)[0].equals(array[0]));
228 assertTrue(arrayParam(array)[1].equals(array[1]));
229 assertTrue(arrayParam(array)[2].equals(array[2]));
230 }
231
232 public void testCharArrayArg() {
233 char[] array = new char[] {
234 'A', 'B', 'C'
235 };
236 assertTrue(charArrayParam(array)[0] == array[0]);
237 assertTrue(charArrayParam(array)[1] == array[1]);
238 assertTrue(charArrayParam(array)[2] == array[2]);
239 }
240
241 public void testLongArrayArg() {
242 long[] array = new long[] {
243 1L, 2L, 3L
244 };
245 assertTrue(longArrayParam(array)[0] == array[0]);
246 assertTrue(longArrayParam(array)[1] == array[1]);
247 assertTrue(longArrayParam(array)[2] == array[2]);
248 }
249
250 public void testIntArrayArg() {
251 int[] array = new int[] {
252 1, 2, 3
253 };
254 assertTrue(intArrayParam(array)[0] == array[0]);
255 assertTrue(intArrayParam(array)[1] == array[1]);
256 assertTrue(intArrayParam(array)[2] == array[2]);
257 }
258
259 public void testShortArrayArg() {
260 short[] array = new short[] {
261 1, 2, 3
262 };
263 assertTrue(shortArrayParam(array)[0] == array[0]);
264 assertTrue(shortArrayParam(array)[1] == array[1]);
265 assertTrue(shortArrayParam(array)[2] == array[2]);
266 }
267
268 public void testBooleanArrayArg() {
269 boolean[] array = new boolean[] {
270 true, false
271 };
272 assertTrue(booleanArrayParam(array)[0] == array[0]);
273 assertTrue(booleanArrayParam(array)[1] == array[1]);
274 }
275
276 public void testByteArrayArg() {
277 byte[] array = new byte[] {
278 1, 2, 3
279 };
280 assertTrue(byteArrayParam(array)[0] == array[0]);
281 assertTrue(byteArrayParam(array)[1] == array[1]);
282 assertTrue(byteArrayParam(array)[2] == array[2]);
283 }
284
285 public void testFloatArrayArg() {
286 float[] array = new float[] {
287 1.1F, 2.1F, 3.1F
288 };
289 assertTrue(floatArrayParam(array)[0] == array[0]);
290 assertTrue(floatArrayParam(array)[1] == array[1]);
291 assertTrue(floatArrayParam(array)[2] == array[2]);
292 }
293
294 public void testVariousArguments1() {
295 assertEquals(
296 "dummy".hashCode() + 1 + (int) 2.3F,
297 this.hashCode() + (int) 34L,
298 variousParams1("dummy", 1, 2.3F, this, 34L));
299 }
300
301 public void testVariousArguments2() {
302 assertEquals((int) 2.3F
303 + 1
304 + "dummy".hashCode()
305 + this.hashCode()
306 + (int) 34L
307 + "test".hashCode(), variousParams2(2.3F, 1, "dummy", this, 34L, "test"));
308 }
309
310 public void testVariousArguments4() {
311 assertEquals("dummy", takesArrayAsArgument(new String[] {
312 "dummy", "test"
313 })[0]);
314 assertEquals("test", takesArrayAsArgument(new String[] {
315 "dummy", "test"
316 })[1]);
317 }
318
319 public void testLongParamNoAroundAdvice() {
320 assertEquals(12L, longNoAroundAdvice(12L));
321 }
322
323 public void testWithincodeCtor() {
324 MemberMethodAdviceTest me = new MemberMethodAdviceTest(123);
325 assertEquals("ctor call post ", me.m_logString);
326 }
327
328 public static void main(String[] args) {
329 junit.textui.TestRunner.run(suite());
330 }
331
332 public static junit.framework.Test suite() {
333 return new junit.framework.TestSuite(MemberMethodAdviceTest.class);
334 }
335
336
337 public void log(final String wasHere) {
338 m_logString += wasHere;
339 }
340
341 private void nonAdvisedMethod() {
342 }
343
344 private void methodAdvicedMethod() {
345 log("invocation ");
346 }
347
348 private void methodAdvicedMethod(int o) {
349 log("invocation ");
350 }
351
352 public void beforeAroundAfterAdvicedMethod() {
353 log("invocation ");
354 }
355
356 public void beforeAdvicedMethod() {
357 log("invocation ");
358 }
359
360 public void afterAdvicedMethod() {
361 log("invocation ");
362 }
363
364 public void beforeAfterAdvicedMethod() {
365 log("invocation ");
366 }
367
368 public void methodAdvicedMethodNewThread() {
369 log("invocation ");
370 }
371
372 public void multipleMethodAdvicedMethod() {
373 log("invocation ");
374 }
375
376 public void multipleChainedMethodAdvicedMethod() {
377 log("invocation ");
378 }
379
380 public void multiplePointcutsMethod() {
381 log("invocation ");
382 }
383
384 public void multipleMethodAndPrePostAdvicedMethod() {
385 log("invocation ");
386 }
387
388 public void methodAdvicedWithPreAndPost() {
389 log("invocation ");
390 }
391
392 public void multipleMethodAdvicedWithPreAndPost() {
393 log("invocation ");
394 }
395
396 private void methodAdviceWithMultiplePreAndPostAdviced() {
397 log("invocation ");
398 }
399
400 public void exceptionThrower() throws Throwable {
401 throw new UnsupportedOperationException("this is a test");
402 }
403
404 public void exceptionThrowerChecked() throws CheckedException {
405 throw new CheckedException();
406 }
407
408 public String joinPointMetaData(String param) {
409 return "result";
410 }
411
412 public void hasPointcutButNoAdvice() {
413 }
414
415 public String postAdviced() {
416 return "test";
417 }
418
419 public void anonymousAdviced() {
420 }
421
422 public void throwsException() throws Exception {
423 throw new Exception("test");
424 }
425
426 public void throwsRuntimeException() {
427 throw new RuntimeException("test");
428 }
429
430 public void throwsError() {
431 throw new Error("test");
432 }
433
434 public void noParams() throws RuntimeException {
435 }
436
437 public long longParam(long arg) {
438 return arg;
439 }
440
441 public long longNoAroundAdvice(long arg) {
442 return arg;
443 }
444
445 public int intParam(int arg) {
446 return arg;
447 }
448
449 public short shortParam(short arg) {
450 return arg;
451 }
452
453 public double doubleParam(double arg) {
454 return arg;
455 }
456
457 private float floatParam(float arg) {
458 return arg;
459 }
460
461 public byte byteParam(byte arg) {
462 return arg;
463 }
464
465 public boolean booleanParam(boolean arg) {
466 return arg;
467 }
468
469 public char charParam(char arg) {
470 return arg;
471 }
472
473 protected Object objectParam(Object arg) {
474 return arg;
475 }
476
477 public String[] arrayParam(String[] arg) {
478 return arg;
479 }
480
481 public long[] longArrayParam(long[] arg) {
482 return arg;
483 }
484
485 public float[] floatArrayParam(float[] arg) {
486 return arg;
487 }
488
489 public char[] charArrayParam(char[] arg) {
490 return arg;
491 }
492
493 public int[] intArrayParam(int[] arg) {
494 return arg;
495 }
496
497 public short[] shortArrayParam(short[] arg) {
498 return arg;
499 }
500
501 public boolean[] booleanArrayParam(boolean[] arg) {
502 return arg;
503 }
504
505 public byte[] byteArrayParam(byte[] arg) {
506 return arg;
507 }
508
509 public int variousParams1(String str, int i, float f, Object o, long l) throws RuntimeException {
510 return str.hashCode() + i + (int) f + o.hashCode() + (int) l;
511 }
512
513 private int variousParams2(float f, int i, String str1, Object o, long l, String str2) throws RuntimeException {
514 return (int) f + i + str1.hashCode() + o.hashCode() + (int) l + str2.hashCode();
515 }
516
517 public float variousParams3(String s, long y, String t, String r, String e, int w, String q) {
518 return 2.5F;
519 }
520
521 public String[] takesArrayAsArgument(String[] arr) {
522 return arr;
523 }
524
525 protected void getVoid() throws RuntimeException {
526 }
527
528 public long getLong() throws RuntimeException {
529 return 1L;
530 }
531
532 public int getInt() throws RuntimeException {
533 return 1;
534 }
535
536 public short getShort() throws RuntimeException {
537 return 1;
538 }
539
540 public double getDouble() throws RuntimeException {
541 return 1.1D;
542 }
543
544 public float getFloat() throws RuntimeException {
545 return 1.1F;
546 }
547
548 public byte getByte() throws RuntimeException {
549 return Byte.parseByte("1");
550 }
551
552 public char getChar() throws RuntimeException {
553 return 'A';
554 }
555
556 private boolean getBoolean() throws RuntimeException {
557 return true;
558 }
559
560 public long getPrimitiveAndNullFromAdvice() throws RuntimeException {
561 return 123456789L;
562 }
563
564 private static class CheckedException extends Exception {
565 public CheckedException() {
566 super();
567 }
568 }
569
570 public MemberMethodAdviceTest(int dummy) {
571 log("ctor ");
572 callWithincodeCtor();
573 }
574
575 public void callWithincodeCtor() {
576 log("call ");
577 }
578
579 }