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(
57 "test.StaticMethodAdviceTestjoinPointMetaDataparameterjava.lang.Stringjava.lang.Stringresult",
58 pointcutName
59 );
60 }
61
62 public void testHasPointcutButNoAdvice() {
63 try {
64 hasPointcutButNoAdvice();
65 } catch (Exception e) {
66 fail();
67 }
68 }
69
70 public void testAnonymousAdviced() {
71 try {
72 anonymousAdviced();
73 } catch (Exception e) {
74 fail();
75 }
76 }
77
78 public void testReturnPrimitiveAndNullFromAdvice() {
79 try {
80 assertEquals(0L, getPrimitiveAndNullFromAdvice());
81 } catch (NullPointerException e) {
82 fail(
83 "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)"
84 );
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
235 public void testVariousArguments2() {
236 assertEquals(
237 (int) 2.3F
238 + 1
239 + "dummy".hashCode()
240 + this.hashCode()
241 + (int) 34L
242 + "test".hashCode(), variousParams2(2.3F, 1, "dummy", this, 34L, "test")
243 );
244 }
245
246 public void testVariousArguments4() {
247 assertEquals(
248 "dummy", takesArrayAsArgument(
249 new String[]{
250 "dummy", "test"
251 }
252 )[0]
253 );
254 assertEquals(
255 "test", takesArrayAsArgument(
256 new String[]{
257 "dummy", "test"
258 }
259 )[1]
260 );
261 }
262
263 public static void main(String[] args) {
264 junit.textui.TestRunner.run(suite());
265 }
266
267 public static junit.framework.Test suite() {
268 return new junit.framework.TestSuite(StaticMethodAdviceTest.class);
269 }
270
271
272 public static void log(final String wasHere) {
273 m_logString += wasHere;
274 }
275
276 private static void nonAdvisedMethod() {
277 }
278
279 public static void methodAdvicedMethod() {
280 log("invocation ");
281 }
282
283 private static void methodAdvicedMethodNewThread() {
284 log("invocation ");
285 }
286
287 public static void multipleMethodAdvicedMethod() {
288 log("invocation ");
289 }
290
291 private static void multipleChainedMethodAdvicedMethod() {
292 log("invocation ");
293 }
294
295 public static void multipleMethodAndPrePostAdvicedMethod() {
296 log("invocation ");
297 }
298
299 public static void methodAdvicedWithPreAndPost() {
300 log("invocation ");
301 }
302
303 public static void multipleMethodAdvicedWithPreAndPost() {
304 log("invocation ");
305 }
306
307 public static void methodAdviceWithMultiplePreAndPostAdviced() {
308 log("invocation ");
309 }
310
311 private static void multiplePointcutsMethod() {
312 log("invocation ");
313 }
314
315 public static void exceptionThrower() throws Throwable {
316 throw new UnsupportedOperationException("this is a test");
317 }
318
319 public static String joinPointMetaData(String param) {
320 return "result";
321 }
322
323 private static void hasPointcutButNoAdvice() {
324 }
325
326 public static String postAdviced() {
327 return "test";
328 }
329
330 public static void anonymousAdviced() {
331 }
332
333 public static void throwsException() throws Exception {
334 throw new Exception("test");
335 }
336
337 public static void throwsRuntimeException() {
338 throw new RuntimeException("test");
339 }
340
341 public static void throwsError() {
342 throw new Error("test");
343 }
344
345 private static void noParams() throws RuntimeException {
346 }
347
348 private static long longParam(long arg) {
349 return arg;
350 }
351
352 public static int intParam(int arg) {
353 return arg;
354 }
355
356 public static short shortParam(short arg) {
357 return arg;
358 }
359
360 public static double doubleParam(double arg) {
361 return arg;
362 }
363
364 public static float floatParam(float arg) {
365 return arg;
366 }
367
368 public static byte byteParam(byte arg) {
369 return arg;
370 }
371
372 public static boolean booleanParam(boolean arg) {
373 return arg;
374 }
375
376 private static char charParam(char arg) {
377 return arg;
378 }
379
380 private static Object objectParam(Object arg) {
381 return arg;
382 }
383
384 private static int variousParams1(String str, int i, float f, Object o, long l) throws RuntimeException {
385 return str.hashCode() + i + (int) f + o.hashCode() + (int) l;
386 }
387
388 public static int variousParams2(float f, int i, String str1, Object o, long l, String str2)
389 throws RuntimeException {
390 return (int) f + i + str1.hashCode() + o.hashCode() + (int) l + str2.hashCode();
391 }
392
393 public static float variousParams3(String s, long y, String t, String r, String e, int w, String q) {
394 return 2.5F;
395 }
396
397 public static String[] takesArrayAsArgument(String[] arr) {
398 return arr;
399 }
400
401 public short[] shortArrayParam(short[] arg) {
402 return arg;
403 }
404
405 public boolean[] booleanArrayParam(boolean[] arg) {
406 return arg;
407 }
408
409 public byte[] byteArrayParam(byte[] arg) {
410 return arg;
411 }
412
413 public long[] longArrayParam(long[] arg) {
414 return arg;
415 }
416
417 public float[] floatArrayParam(float[] arg) {
418 return arg;
419 }
420
421 public char[] charArrayParam(char[] arg) {
422 return arg;
423 }
424
425 public int[] intArrayParam(int[] arg) {
426 return arg;
427 }
428
429 public static void getVoid() throws RuntimeException {
430 }
431
432 public static long getLong() throws RuntimeException {
433 return 1L;
434 }
435
436 public static int getInt() throws RuntimeException {
437 return 1;
438 }
439
440 public static short getShort() throws RuntimeException {
441 return 1;
442 }
443
444 private static double getDouble() throws RuntimeException {
445 return 1.1D;
446 }
447
448 public static float getFloat() throws RuntimeException {
449 return 1.1F;
450 }
451
452 public static byte getByte() throws RuntimeException {
453 return Byte.parseByte("1");
454 }
455
456 public static char getChar() throws RuntimeException {
457 return 'A';
458 }
459
460 private static boolean getBoolean() throws RuntimeException {
461 return true;
462 }
463
464 private static long getPrimitiveAndNullFromAdvice() throws RuntimeException {
465 return 123456789L;
466 }
467 }