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