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: */
014: public class StaticMethodAdviceTest extends TestCase {
015: private static String m_logString = "";
016:
017: public StaticMethodAdviceTest() {
018: }
019:
020: public StaticMethodAdviceTest(String name) {
021: super (name);
022: }
023:
024: public void testMethodAdvice() {
025: m_logString = "";
026: methodAdvicedMethod();
027: assertEquals("before1 invocation after1 ", m_logString);
028: }
029:
030: public void testMethodAdviceNewThread() {
031: m_logString = "";
032: methodAdvicedMethodNewThread();
033: assertEquals("before invocation after ", m_logString);
034: }
035:
036: public void testMultipleChainedMethodAdvices() {
037: m_logString = "";
038: multipleChainedMethodAdvicedMethod();
039: assertEquals("before1 before2 invocation after2 after1 ",
040: m_logString);
041: }
042:
043: public void testMultiplePointcuts() {
044: try {
045: m_logString = "";
046: multiplePointcutsMethod();
047: assertEquals("before1 before2 invocation after2 after1 ",
048: m_logString);
049: } catch (Throwable t) {
050: t.printStackTrace();
051: }
052: }
053:
054: public void testGetJoinPointMetaData() {
055: String param = "parameter";
056: String pointcutName = joinPointMetaData(param);
057: assertEquals(
058: "test.StaticMethodAdviceTestjoinPointMetaDataparameterjava.lang.Stringjava.lang.Stringresult",
059: pointcutName);
060: }
061:
062: public void testHasPointcutButNoAdvice() {
063: try {
064: hasPointcutButNoAdvice();
065: } catch (Exception e) {
066: fail();
067: }
068: }
069:
070: public void testAnonymousAdviced() {
071: try {
072: anonymousAdviced();
073: } catch (Exception e) {
074: fail();
075: }
076: }
077:
078: public void testReturnPrimitiveAndNullFromAdvice() {
079: try {
080: assertEquals(0L, getPrimitiveAndNullFromAdvice());
081: } catch (NullPointerException e) {
082: 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)");
083: }
084: }
085:
086: public void testReturnVoid() {
087: getVoid();
088: }
089:
090: public void testReturnLong() {
091: assertEquals(1L, getLong());
092: }
093:
094: //
095: public void testReturnInt() {
096: assertEquals(1, getInt());
097: }
098:
099: public void testReturnShort() {
100: assertEquals(1, getShort());
101: }
102:
103: public void testReturnDouble() {
104: assertEquals(new Double(1.1D), new Double(getDouble()));
105: }
106:
107: public void testReturnFloat() {
108: assertEquals(new Float(1.1F), new Float(getFloat()));
109: }
110:
111: public void testReturnByte() {
112: assertEquals(Byte.parseByte("1"), getByte());
113: }
114:
115: public void testReturnChar() {
116: assertEquals('A', getChar());
117: }
118:
119: public void testReturnBoolean() {
120: assertEquals(true, getBoolean());
121: }
122:
123: public void testNoArgs() {
124: noParams();
125: }
126:
127: public void testIntArg() {
128: assertEquals(12, intParam(12));
129: }
130:
131: public void testLongArg() {
132: assertEquals(12L, longParam(12L));
133: }
134:
135: public void testShortArg() {
136: assertEquals(3, shortParam((short) 3));
137: }
138:
139: public void testDoubleArg() {
140: assertEquals(new Double(2.3D), new Double(doubleParam(2.3D)));
141: }
142:
143: public void testFloatArg() {
144: assertEquals(new Float(2.3F), new Float(floatParam(2.3F)));
145: }
146:
147: public void testByteArg() {
148: assertEquals(Byte.parseByte("1"),
149: byteParam(Byte.parseByte("1")));
150: }
151:
152: public void testCharArg() {
153: assertEquals('B', charParam('B'));
154: }
155:
156: public void testBooleanArg() {
157: assertEquals(false, booleanParam(false));
158: }
159:
160: public void testObjectArg() {
161: assertEquals(this , objectParam(this ));
162: }
163:
164: public void testShortArrayArg() {
165: short[] array = new short[] { 1, 2, 3 };
166: assertTrue(shortArrayParam(array)[0] == array[0]);
167: assertTrue(shortArrayParam(array)[1] == array[1]);
168: assertTrue(shortArrayParam(array)[2] == array[2]);
169: }
170:
171: public void testBooleanArrayArg() {
172: boolean[] array = new boolean[] { true, false };
173: assertTrue(booleanArrayParam(array)[0] == array[0]);
174: assertTrue(booleanArrayParam(array)[1] == array[1]);
175: }
176:
177: public void testByteArrayArg() {
178: byte[] array = new byte[] { 1, 2, 3 };
179: assertTrue(byteArrayParam(array)[0] == array[0]);
180: assertTrue(byteArrayParam(array)[1] == array[1]);
181: assertTrue(byteArrayParam(array)[2] == array[2]);
182: }
183:
184: public void testCharArrayArg() {
185: char[] array = new char[] { 'A', 'B', 'C' };
186: assertTrue(charArrayParam(array)[0] == array[0]);
187: assertTrue(charArrayParam(array)[1] == array[1]);
188: assertTrue(charArrayParam(array)[2] == array[2]);
189: }
190:
191: public void testLongArrayArg() {
192: long[] array = new long[] { 1L, 2L, 3L };
193: assertTrue(longArrayParam(array)[0] == array[0]);
194: assertTrue(longArrayParam(array)[1] == array[1]);
195: assertTrue(longArrayParam(array)[2] == array[2]);
196: }
197:
198: public void testIntArrayArg() {
199: int[] array = new int[] { 1, 2, 3 };
200: assertTrue(intArrayParam(array)[0] == array[0]);
201: assertTrue(intArrayParam(array)[1] == array[1]);
202: assertTrue(intArrayParam(array)[2] == array[2]);
203: }
204:
205: public void testFloatArrayArg() {
206: float[] array = new float[] { 1.1F, 2.1F, 3.1F };
207: assertTrue(floatArrayParam(array)[0] == array[0]);
208: assertTrue(floatArrayParam(array)[1] == array[1]);
209: assertTrue(floatArrayParam(array)[2] == array[2]);
210: }
211:
212: public void testVariousArguments1() {
213: assertEquals("dummy".hashCode() + 1 + (int) 2.3F, this
214: .hashCode()
215: + (int) 34L,
216: variousParams1("dummy", 1, 2.3F, this , 34L));
217: }
218:
219: public void testVariousArguments2() {
220: assertEquals((int) 2.3F + 1 + "dummy".hashCode()
221: + this .hashCode() + (int) 34L + "test".hashCode(),
222: variousParams2(2.3F, 1, "dummy", this , 34L, "test"));
223: }
224:
225: public void testVariousArguments4() {
226: assertEquals("dummy", takesArrayAsArgument(new String[] {
227: "dummy", "test" })[0]);
228: assertEquals("test", takesArrayAsArgument(new String[] {
229: "dummy", "test" })[1]);
230: }
231:
232: public static void main(String[] args) {
233: junit.textui.TestRunner.run(suite());
234: }
235:
236: public static junit.framework.Test suite() {
237: return new junit.framework.TestSuite(
238: StaticMethodAdviceTest.class);
239: }
240:
241: // ==== methods to test ====
242: public static void log(final String wasHere) {
243: m_logString += wasHere;
244: }
245:
246: private static void nonAdvisedMethod() {
247: }
248:
249: public static void methodAdvicedMethod() {
250: log("invocation ");
251: }
252:
253: private static void methodAdvicedMethodNewThread() {
254: log("invocation ");
255: }
256:
257: public static void multipleMethodAdvicedMethod() {
258: log("invocation ");
259: }
260:
261: private static void multipleChainedMethodAdvicedMethod() {
262: log("invocation ");
263: }
264:
265: public static void multipleMethodAndPrePostAdvicedMethod() {
266: log("invocation ");
267: }
268:
269: public static void methodAdvicedWithPreAndPost() {
270: log("invocation ");
271: }
272:
273: public static void multipleMethodAdvicedWithPreAndPost() {
274: log("invocation ");
275: }
276:
277: public static void methodAdviceWithMultiplePreAndPostAdviced() {
278: log("invocation ");
279: }
280:
281: private static void multiplePointcutsMethod() {
282: log("invocation ");
283: }
284:
285: public static void exceptionThrower() throws Throwable {
286: throw new UnsupportedOperationException("this is a test");
287: }
288:
289: public static String joinPointMetaData(String param) {
290: return "result";
291: }
292:
293: private static void hasPointcutButNoAdvice() {
294: }
295:
296: public static String postAdviced() {
297: return "test";
298: }
299:
300: public static void anonymousAdviced() {
301: }
302:
303: public static void throwsException() throws Exception {
304: throw new Exception("test");
305: }
306:
307: public static void throwsRuntimeException() {
308: throw new RuntimeException("test");
309: }
310:
311: public static void throwsError() {
312: throw new Error("test");
313: }
314:
315: private static void noParams() throws RuntimeException {
316: }
317:
318: private static long longParam(long arg) {
319: return arg;
320: }
321:
322: public static int intParam(int arg) {
323: return arg;
324: }
325:
326: public static short shortParam(short arg) {
327: return arg;
328: }
329:
330: public static double doubleParam(double arg) {
331: return arg;
332: }
333:
334: public static float floatParam(float arg) {
335: return arg;
336: }
337:
338: public static byte byteParam(byte arg) {
339: return arg;
340: }
341:
342: public static boolean booleanParam(boolean arg) {
343: return arg;
344: }
345:
346: private static char charParam(char arg) {
347: return arg;
348: }
349:
350: private static Object objectParam(Object arg) {
351: return arg;
352: }
353:
354: private static int variousParams1(String str, int i, float f,
355: Object o, long l) throws RuntimeException {
356: return str.hashCode() + i + (int) f + o.hashCode() + (int) l;
357: }
358:
359: public static int variousParams2(float f, int i, String str1,
360: Object o, long l, String str2) throws RuntimeException {
361: return (int) f + i + str1.hashCode() + o.hashCode() + (int) l
362: + str2.hashCode();
363: }
364:
365: public static float variousParams3(String s, long y, String t,
366: String r, String e, int w, String q) {
367: return 2.5F;
368: }
369:
370: public static String[] takesArrayAsArgument(String[] arr) {
371: return arr;
372: }
373:
374: public short[] shortArrayParam(short[] arg) {
375: return arg;
376: }
377:
378: public boolean[] booleanArrayParam(boolean[] arg) {
379: return arg;
380: }
381:
382: public byte[] byteArrayParam(byte[] arg) {
383: return arg;
384: }
385:
386: public long[] longArrayParam(long[] arg) {
387: return arg;
388: }
389:
390: public float[] floatArrayParam(float[] arg) {
391: return arg;
392: }
393:
394: public char[] charArrayParam(char[] arg) {
395: return arg;
396: }
397:
398: public int[] intArrayParam(int[] arg) {
399: return arg;
400: }
401:
402: public static void getVoid() throws RuntimeException {
403: }
404:
405: public static long getLong() throws RuntimeException {
406: return 1L;
407: }
408:
409: public static int getInt() throws RuntimeException {
410: return 1;
411: }
412:
413: public static short getShort() throws RuntimeException {
414: return 1;
415: }
416:
417: private static double getDouble() throws RuntimeException {
418: return 1.1D;
419: }
420:
421: public static float getFloat() throws RuntimeException {
422: return 1.1F;
423: }
424:
425: public static byte getByte() throws RuntimeException {
426: return Byte.parseByte("1");
427: }
428:
429: public static char getChar() throws RuntimeException {
430: return 'A';
431: }
432:
433: private static boolean getBoolean() throws RuntimeException {
434: return true;
435: }
436:
437: private static long getPrimitiveAndNullFromAdvice()
438: throws RuntimeException {
439: return 123456789L;
440: }
441: }
|