001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test;
008:
009: import junit.framework.TestCase;
010:
011: /**
012: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
013: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
014: */
015: public class MemberMethodAdviceTest extends TestCase implements
016: Loggable {
017: private String java = "a field that can make bytecode tools confused, AW-147 item2, fixed in AW 1.0-beta1";
018:
019: private String m_logString = "";
020:
021: public MemberMethodAdviceTest() {
022: MemberMethodAdviceTest fake = new MemberMethodAdviceTest(
023: new Long(0));
024: }
025:
026: //AW-393 test case
027: public MemberMethodAdviceTest(Integer fake) {
028: }
029:
030: //AW-393 test case
031: public MemberMethodAdviceTest(Long l) {
032: this (new Integer(0));
033: }
034:
035: public void testBeforeAroundAroundAfterAdvice() {
036: m_logString = "";
037: try {
038: beforeAroundAfterAdvicedMethod();
039: } catch (Exception e) {
040: e.printStackTrace();
041: }
042: assertEquals(
043: "pre before1 before2 invocation after2 after1 post ",
044: m_logString);
045: }
046:
047: public void testBeforeAdvice() {
048: m_logString = "";
049: beforeAdvicedMethod();
050: assertEquals("pre invocation ", m_logString);
051: }
052:
053: public void testAfterAdvice() {
054: m_logString = "";
055: afterAdvicedMethod();
056: assertEquals("invocation post ", m_logString);
057: }
058:
059: public void testBeforeAfterAdvice() {
060: m_logString = "";
061: beforeAfterAdvicedMethod();
062: assertEquals("pre invocation post ", m_logString);
063: }
064:
065: public void testAroundAdvice() {
066: m_logString = "";
067: methodAdvicedMethod();
068: assertEquals("before1 invocation after1 ", m_logString);
069: }
070:
071: public void testAroundAdvice2() {
072: m_logString = "";
073: methodAdvicedMethod(0);
074: assertEquals("invocation ", m_logString);
075: }
076:
077: public void testAroundAdviceNewThread() {
078: m_logString = "";
079: // call + execution advice
080: methodAdvicedMethodNewThread();
081: assertEquals("before before invocation after after ",
082: m_logString);
083: }
084:
085: public void testMultipleAroundAdvices() {
086: m_logString = "";
087: multipleMethodAdvicedMethod();
088: assertEquals("before1 before2 invocation after2 after1 ",
089: m_logString);
090: }
091:
092: public void testMultipleChainedAroundAdvices() {
093: m_logString = "";
094: multipleChainedMethodAdvicedMethod();
095: assertEquals("before1 before2 invocation after2 after1 ",
096: m_logString);
097: }
098:
099: public void testMultiplePointcuts() {
100: m_logString = "";
101: multiplePointcutsMethod();
102: assertEquals("before2 before1 invocation after1 after2 ",
103: m_logString);
104: }
105:
106: // public void testGetJoinPointMetaData() {
107: // String param = "parameter";
108: // assertEquals(
109: // getClass().getName() +
110: // "___AW_$_AW_$joinPointMetaData$_AW_$1$_AW_$test_MemberMethodAdviceTest" +
111: // hashCode() +
112: // param +
113: // param.getClass().getName() +
114: // "java.lang.String" +
115: // "result",
116: // joinPointMetaData(param)
117: // );
118: // }
119: public void testHasPointcutButNoAdvice() {
120: try {
121: hasPointcutButNoAdvice();
122: } catch (Exception e) {
123: fail();
124: }
125: }
126:
127: public void testAnonymousAdviced() {
128: try {
129: anonymousAdviced();
130: } catch (Exception e) {
131: fail();
132: }
133: }
134:
135: public void testThrowException() {
136: try {
137: exceptionThrower();
138: } catch (Throwable e) {
139: assertTrue(e instanceof UnsupportedOperationException);
140: return;
141: }
142: fail("this point should never be reached");
143: }
144:
145: public void testThrowExceptionChecked() {
146: try {
147: exceptionThrowerChecked();
148: } catch (Throwable e) {
149: assertTrue(e instanceof CheckedException);
150: return;
151: }
152: fail("this point should never be reached");
153: }
154:
155: public void testReturnVoid() {
156: getVoid();
157: }
158:
159: public void testReturnLong() {
160: assertEquals(1L, getLong());
161: }
162:
163: public void testReturnInt() {
164: assertEquals(1, getInt());
165: }
166:
167: public void testReturnShort() {
168: assertEquals(1, getShort());
169: }
170:
171: public void testReturnDouble() {
172: assertEquals(new Double(1.1D), new Double(getDouble()));
173: }
174:
175: public void testReturnFloat() {
176: assertEquals(new Float(1.1F), new Float(getFloat()));
177: }
178:
179: public void testReturnByte() {
180: assertEquals(Byte.parseByte("1"), getByte());
181: }
182:
183: public void testReturnChar() {
184: assertEquals('A', getChar());
185: }
186:
187: public void testReturnPrimitiveAndNullFromAdvice() {
188: try {
189: assertEquals(0L, getPrimitiveAndNullFromAdvice());
190: } catch (NullPointerException e) {
191: 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)");
192: }
193: }
194:
195: public void testReturnBoolean() {
196: assertEquals(true, getBoolean());
197: }
198:
199: public void testNoArgs() {
200: noParams();
201: }
202:
203: public void testIntArg() {
204: assertEquals(12, intParam(12));
205: }
206:
207: public void testLongArg() {
208: assertEquals(12L, longParam(12L));
209: }
210:
211: public void testShortArg() {
212: assertEquals(3, shortParam((short) 3));
213: }
214:
215: public void testDoubleArg() {
216: assertEquals(new Double(2.3D), new Double(doubleParam(2.3D)));
217: }
218:
219: public void testFloatArg() {
220: assertEquals(new Float(2.3F), new Float(floatParam(2.3F)));
221: }
222:
223: public void testByteArg() {
224: assertEquals(Byte.parseByte("1"),
225: byteParam(Byte.parseByte("1")));
226: }
227:
228: public void testCharArg() {
229: assertEquals('B', charParam('B'));
230: }
231:
232: public void testBooleanArg() {
233: assertEquals(false, booleanParam(false));
234: }
235:
236: public void testObjectArg() {
237: assertEquals(this , objectParam(this ));
238: }
239:
240: public void testObjectArrayArg() {
241: String[] array = new String[] { "one", "two", "three" };
242: assertTrue(arrayParam(array)[0].equals(array[0]));
243: assertTrue(arrayParam(array)[1].equals(array[1]));
244: assertTrue(arrayParam(array)[2].equals(array[2]));
245: }
246:
247: public void testCharArrayArg() {
248: char[] array = new char[] { 'A', 'B', 'C' };
249: assertTrue(charArrayParam(array)[0] == array[0]);
250: assertTrue(charArrayParam(array)[1] == array[1]);
251: assertTrue(charArrayParam(array)[2] == array[2]);
252: }
253:
254: public void testLongArrayArg() {
255: long[] array = new long[] { 1L, 2L, 3L };
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[] { 1, 2, 3 };
263: assertTrue(intArrayParam(array)[0] == array[0]);
264: assertTrue(intArrayParam(array)[1] == array[1]);
265: assertTrue(intArrayParam(array)[2] == array[2]);
266: }
267:
268: public void testShortArrayArg() {
269: short[] array = new short[] { 1, 2, 3 };
270: assertTrue(shortArrayParam(array)[0] == array[0]);
271: assertTrue(shortArrayParam(array)[1] == array[1]);
272: assertTrue(shortArrayParam(array)[2] == array[2]);
273: }
274:
275: public void testBooleanArrayArg() {
276: boolean[] array = new boolean[] { true, false };
277: assertTrue(booleanArrayParam(array)[0] == array[0]);
278: assertTrue(booleanArrayParam(array)[1] == array[1]);
279: }
280:
281: public void testByteArrayArg() {
282: byte[] array = new byte[] { 1, 2, 3 };
283: assertTrue(byteArrayParam(array)[0] == array[0]);
284: assertTrue(byteArrayParam(array)[1] == array[1]);
285: assertTrue(byteArrayParam(array)[2] == array[2]);
286: }
287:
288: public void testFloatArrayArg() {
289: float[] array = new float[] { 1.1F, 2.1F, 3.1F };
290: assertTrue(floatArrayParam(array)[0] == array[0]);
291: assertTrue(floatArrayParam(array)[1] == array[1]);
292: assertTrue(floatArrayParam(array)[2] == array[2]);
293: }
294:
295: public void testVariousArguments1() {
296: assertEquals("dummy".hashCode() + 1 + (int) 2.3F, this
297: .hashCode()
298: + (int) 34L,
299: variousParams1("dummy", 1, 2.3F, this , 34L));
300: }
301:
302: public void testVariousArguments2() {
303: assertEquals((int) 2.3F + 1 + "dummy".hashCode()
304: + this .hashCode() + (int) 34L + "test".hashCode(),
305: variousParams2(2.3F, 1, "dummy", this , 34L, "test"));
306: }
307:
308: public void testVariousArguments4() {
309: assertEquals("dummy", takesArrayAsArgument(new String[] {
310: "dummy", "test" })[0]);
311: assertEquals("test", takesArrayAsArgument(new String[] {
312: "dummy", "test" })[1]);
313: }
314:
315: public void testLongParamNoAroundAdvice() {
316: assertEquals(12L, longNoAroundAdvice(12L));
317: }
318:
319: public void testWithincodeCtor() {
320: MemberMethodAdviceTest me = new MemberMethodAdviceTest(123);
321: assertEquals("ctor call post ", me.m_logString);
322: }
323:
324: public static void main(String[] args) {
325: junit.textui.TestRunner.run(suite());
326: }
327:
328: public static junit.framework.Test suite() {
329: return new junit.framework.TestSuite(
330: MemberMethodAdviceTest.class);
331: }
332:
333: // ==== methods to test ====
334: public void log(final String wasHere) {
335: m_logString += wasHere;
336: }
337:
338: private void nonAdvisedMethod() {
339: }
340:
341: private void methodAdvicedMethod() {
342: log("invocation ");
343: }
344:
345: private void methodAdvicedMethod(int o) {
346: log("invocation ");
347: }
348:
349: public void beforeAroundAfterAdvicedMethod() {
350: log("invocation ");
351: }
352:
353: public void beforeAdvicedMethod() {
354: log("invocation ");
355: }
356:
357: public void afterAdvicedMethod() {
358: log("invocation ");
359: }
360:
361: public void beforeAfterAdvicedMethod() {
362: log("invocation ");
363: }
364:
365: public void methodAdvicedMethodNewThread() {
366: log("invocation ");
367: }
368:
369: public void multipleMethodAdvicedMethod() {
370: log("invocation ");
371: }
372:
373: public void multipleChainedMethodAdvicedMethod() {
374: log("invocation ");
375: }
376:
377: public void multiplePointcutsMethod() {
378: log("invocation ");
379: }
380:
381: public void multipleMethodAndPrePostAdvicedMethod() {
382: log("invocation ");
383: }
384:
385: public void methodAdvicedWithPreAndPost() {
386: log("invocation ");
387: }
388:
389: public void multipleMethodAdvicedWithPreAndPost() {
390: log("invocation ");
391: }
392:
393: private void methodAdviceWithMultiplePreAndPostAdviced() {
394: log("invocation ");
395: }
396:
397: public void exceptionThrower() throws Throwable {
398: throw new UnsupportedOperationException("this is a test");
399: }
400:
401: public void exceptionThrowerChecked() throws CheckedException {
402: throw new CheckedException();
403: }
404:
405: public String joinPointMetaData(String param) {
406: return "result";
407: }
408:
409: public void hasPointcutButNoAdvice() {
410: }
411:
412: public String postAdviced() {
413: return "test";
414: }
415:
416: public void anonymousAdviced() {
417: }
418:
419: public void throwsException() throws Exception {
420: throw new Exception("test");
421: }
422:
423: public void throwsRuntimeException() {
424: throw new RuntimeException("test");
425: }
426:
427: public void throwsError() {
428: throw new Error("test");
429: }
430:
431: public void noParams() throws RuntimeException {
432: }
433:
434: public long longParam(long arg) {
435: return arg;
436: }
437:
438: public long longNoAroundAdvice(long arg) {
439: return arg;
440: }
441:
442: public int intParam(int arg) {
443: return arg;
444: }
445:
446: public short shortParam(short arg) {
447: return arg;
448: }
449:
450: public double doubleParam(double arg) {
451: return arg;
452: }
453:
454: private float floatParam(float arg) {
455: return arg;
456: }
457:
458: public byte byteParam(byte arg) {
459: return arg;
460: }
461:
462: public boolean booleanParam(boolean arg) {
463: return arg;
464: }
465:
466: public char charParam(char arg) {
467: return arg;
468: }
469:
470: protected Object objectParam(Object arg) {
471: return arg;
472: }
473:
474: public String[] arrayParam(String[] arg) {
475: return arg;
476: }
477:
478: public long[] longArrayParam(long[] arg) {
479: return arg;
480: }
481:
482: public float[] floatArrayParam(float[] arg) {
483: return arg;
484: }
485:
486: public char[] charArrayParam(char[] arg) {
487: return arg;
488: }
489:
490: public int[] intArrayParam(int[] arg) {
491: return arg;
492: }
493:
494: public short[] shortArrayParam(short[] arg) {
495: return arg;
496: }
497:
498: public boolean[] booleanArrayParam(boolean[] arg) {
499: return arg;
500: }
501:
502: public byte[] byteArrayParam(byte[] arg) {
503: return arg;
504: }
505:
506: public int variousParams1(String str, int i, float f, Object o,
507: long l) throws RuntimeException {
508: return str.hashCode() + i + (int) f + o.hashCode() + (int) l;
509: }
510:
511: private int variousParams2(float f, int i, String str1, Object o,
512: long l, String str2) throws RuntimeException {
513: return (int) f + i + str1.hashCode() + o.hashCode() + (int) l
514: + str2.hashCode();
515: }
516:
517: public float variousParams3(String s, long y, String t, String r,
518: String e, int w, String q) {
519: return 2.5F;
520: }
521:
522: public String[] takesArrayAsArgument(String[] arr) {
523: return arr;
524: }
525:
526: protected void getVoid() throws RuntimeException {
527: }
528:
529: public long getLong() throws RuntimeException {
530: return 1L;
531: }
532:
533: public int getInt() throws RuntimeException {
534: return 1;
535: }
536:
537: public short getShort() throws RuntimeException {
538: return 1;
539: }
540:
541: public double getDouble() throws RuntimeException {
542: return 1.1D;
543: }
544:
545: public float getFloat() throws RuntimeException {
546: return 1.1F;
547: }
548:
549: public byte getByte() throws RuntimeException {
550: return Byte.parseByte("1");
551: }
552:
553: public char getChar() throws RuntimeException {
554: return 'A';
555: }
556:
557: private boolean getBoolean() throws RuntimeException {
558: return true;
559: }
560:
561: public long getPrimitiveAndNullFromAdvice() throws RuntimeException {
562: return 123456789L;
563: }
564:
565: private static class CheckedException extends Exception {
566: public CheckedException() {
567: super ();
568: }
569: }
570:
571: public MemberMethodAdviceTest(int dummy) {
572: log("ctor ");
573: callWithincodeCtor();
574: }
575:
576: public void callWithincodeCtor() {
577: log("call ");
578: }
579:
580: }
|