1   /***************************************************************************************
2   
3    * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                 *
4   
5    * http://aspectwerkz.codehaus.org                                                    *
6   
7    * ---------------------------------------------------------------------------------- *
8   
9    * The software in this package is published under the terms of the LGPL license      *
10  
11   * a copy of which has been included with this distribution in the license.txt file.  *
12  
13   **************************************************************************************/
14  
15  package test;
16  
17  
18  
19  import java.io.Serializable;
20  
21  import java.lang.reflect.Method;
22  
23  
24  
25  import junit.framework.TestCase;
26  
27  import org.codehaus.aspectwerkz.SystemLoader;
28  
29  
30  
31  /***
32  
33   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
34  
35   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
36  
37   */
38  
39  public class IntroductionTest extends TestCase {
40  
41      private ToBeIntroduced m_toBeIntroduced;
42  
43      private ToBeIntroducedUsingHasMethod m_toBeIntroducedUsingHasMethod;
44  
45      private ToBeIntroducedUsingHasField m_toBeIntroducedUsingHasField;
46  
47  
48  
49      public IntroductionTest(String name) {
50  
51          super(name);
52  
53          m_toBeIntroduced = new ToBeIntroduced();
54  
55          m_toBeIntroducedUsingHasMethod = new ToBeIntroducedUsingHasMethod();
56  
57          m_toBeIntroducedUsingHasField = new ToBeIntroducedUsingHasField();
58  
59      }
60  
61  
62  
63      public void testIntroducedComesFromInterfaces() {
64  
65          Class klass = m_toBeIntroduced.getClass();
66  
67          try {
68  
69              Method m = klass.getDeclaredMethod("NOT_IN_MIXIN_INTF", new Class[0]);
70  
71              fail("should not have introduced : " + m);
72  
73          } catch (NoSuchMethodException e) {
74  
75              ;//ok
76  
77          }
78  
79      }
80  
81  
82  
83      public void testInterfaceIntroduction() {
84  
85          assertTrue(m_toBeIntroduced instanceof java.io.Serializable);
86  
87          assertTrue(m_toBeIntroduced instanceof test.Introductions);
88  
89      }
90  
91  
92  
93      public void testReturnVoid() {
94  
95          try {
96  
97              ((Introductions) m_toBeIntroduced).getVoid();
98  
99          } catch (RuntimeException e) {
100 
101             fail(e.getMessage());
102 
103         }
104 
105     }
106 
107 
108 
109     public void testReturnLong() {
110 
111         assertEquals(1L, ((Introductions) m_toBeIntroduced).getLong());
112 
113     }
114 
115 
116 
117     public void testReturnInt() {
118 
119         assertEquals(1, ((Introductions) m_toBeIntroduced).getInt());
120 
121     }
122 
123 
124 
125     public void testReturnShort() {
126 
127         assertEquals(1, ((Introductions) m_toBeIntroduced).getShort());
128 
129     }
130 
131 
132 
133     public void testReturnDouble() {
134 
135         assertEquals(new Double(1.1D), new Double(((Introductions) m_toBeIntroduced).getDouble()));
136 
137     }
138 
139 
140 
141     public void testReturnFloat() {
142 
143         assertEquals(new Float(1.1F), new Float(((Introductions) m_toBeIntroduced).getFloat()));
144 
145     }
146 
147 
148 
149     public void testReturnByte() {
150 
151         assertEquals(Byte.parseByte("1"), ((Introductions) m_toBeIntroduced).getByte());
152 
153     }
154 
155 
156 
157     public void testReturnChar() {
158 
159         assertEquals('A', ((Introductions) m_toBeIntroduced).getChar());
160 
161     }
162 
163 
164 
165     public void testReturnBoolean() {
166 
167         assertEquals(true, ((Introductions) m_toBeIntroduced).getBoolean());
168 
169     }
170 
171 
172 
173     public void testNoArgs() {
174 
175         try {
176 
177             ((Introductions) m_toBeIntroduced).noArgs();
178 
179         } catch (Exception e) {
180 
181             fail();
182 
183         }
184 
185     }
186 
187 
188 
189     public void testIntArg() {
190 
191         assertEquals(12, ((Introductions) m_toBeIntroduced).intArg(12));
192 
193     }
194 
195 
196 
197     public void testLongArg() {
198 
199         long result = ((Introductions) m_toBeIntroduced).longArg(12L);
200 
201         assertEquals(12L, result);
202 
203     }
204 
205 
206 
207     public void testShortArg() {
208 
209         assertEquals((short) 3, ((Introductions) m_toBeIntroduced).shortArg((short) 3));
210 
211     }
212 
213 
214 
215     public void testDoubleArg() {
216 
217         assertEquals(new Double(2.3D), new Double(((Introductions) m_toBeIntroduced)
218 
219                 .doubleArg(2.3D)));
220 
221     }
222 
223 
224 
225     public void testFloatArg() {
226 
227         assertEquals(new Float(2.3F), new Float(((Introductions) m_toBeIntroduced).floatArg(2.3F)));
228 
229     }
230 
231 
232 
233     public void testByteArg() {
234 
235         assertEquals(Byte.parseByte("1"), ((Introductions) m_toBeIntroduced).byteArg(Byte
236 
237                 .parseByte("1")));
238 
239     }
240 
241 
242 
243     public void testCharArg() {
244 
245         assertEquals('B', ((Introductions) m_toBeIntroduced).charArg('B'));
246 
247     }
248 
249 
250 
251     public void testBooleanArg() {
252 
253         assertTrue(!((Introductions) m_toBeIntroduced).booleanArg(false));
254 
255     }
256 
257 
258 
259     public void testObjectArg() {
260 
261         assertEquals("test", ((Introductions) m_toBeIntroduced).objectArg("test"));
262 
263     }
264 
265 
266 
267     public void testArrayArg() {
268 
269         String[] strings = new String[0];
270 
271         try {
272 
273             strings = ((Introductions) m_toBeIntroduced).arrayArg(new String[] {
274 
275                 "test1", "test2"
276 
277             });
278 
279         } catch (Throwable e) {
280 
281             System.out.println("e = " + e);
282 
283         }
284 
285         assertEquals("test1", strings[0]);
286 
287         assertEquals("test2", strings[1]);
288 
289     }
290 
291 
292 
293     public void testVariousArguments1() {
294 
295         assertEquals(
296 
297             "dummy".hashCode() + 1 + (int) 2.3F,
298 
299             this.hashCode() + (int) 34L,
300 
301             ((Introductions) m_toBeIntroduced).variousArguments1("dummy", 1, 2.3F, this, 34L));
302 
303     }
304 
305 
306 
307     public void testVariousArguments2() {
308 
309         assertEquals((int) 2.3F
310 
311             + 1
312 
313             + "dummy".hashCode()
314 
315             + this.hashCode()
316 
317             + (int) 34L
318 
319             + "test".hashCode(), ((Introductions) m_toBeIntroduced).variousArguments2(
320 
321             2.3F,
322 
323             1,
324 
325             "dummy",
326 
327             this,
328 
329             34L,
330 
331             "test"));
332 
333     }
334 
335 
336 
337     public void testThrowException() {
338 
339         try {
340 
341             ((Introductions) m_toBeIntroduced).exceptionThrower();
342 
343         } catch (Throwable e) {
344 
345             assertTrue(e instanceof UnsupportedOperationException);
346 
347             return;
348 
349         }
350 
351         fail("this point should never be reached");
352 
353     }
354 
355 
356 
357     public void testThrowExceptionChecked() {
358 
359         try {
360 
361             ((Introductions) m_toBeIntroduced).exceptionThrowerChecked();
362 
363         } catch (Throwable e) {
364 
365             assertTrue(e instanceof Introductions.CheckedException);
366 
367             return;
368 
369         }
370 
371         fail("this point should never be reached");
372 
373     }
374 
375 
376 
377     public void testReplaceImplementation() {
378 
379         assertEquals("test.aspect.IntroductionTestAspect$MyImpl", SystemLoader.getSystem(this)
380 
381                 .getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
382 
383                 .getImplementationClassName());
384 
385         assertEquals(1, ((Introductions) m_toBeIntroduced).intArg(1));
386 
387 
388 
389         // swap with an inner class
390 
391         SystemLoader.getSystem(this).getAspectManager("tests").getMixin(
392 
393             "test.aspect.IntroductionTestAspect$MyImpl").swapImplementation(
394 
395             "test.aspect.IntroductionTestAspect$MyOtherImpl");
396 
397         assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
398 
399         assertEquals("test.aspect.IntroductionTestAspect$MyOtherImpl", SystemLoader.getSystem(this)
400 
401                 .getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
402 
403                 .getImplementationClassName());
404 
405     }
406 
407 
408 
409     public void testReplaceImplementationToAutonomousOne() {
410 
411         assertEquals("test.aspect.IntroductionTestAspect$MyOtherImpl", SystemLoader.getSystem(this)
412 
413                 .getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
414 
415                 .getImplementationClassName());
416 
417         assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
418 
419 
420 
421         // swap with an outer class
422 
423         SystemLoader.getSystem(this).getAspectManager("tests").getMixin(
424 
425             "test.aspect.IntroductionTestAspect$MyImpl").swapImplementation(
426 
427             "test.aspect.IntroductionTestAspectMyImplReplacement");
428 
429         assertEquals(-2, ((Introductions) m_toBeIntroduced).intArg(1));
430 
431         assertEquals("test.aspect.IntroductionTestAspectMyImplReplacement", SystemLoader.getSystem(
432 
433             this).getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
434 
435                 .getImplementationClassName());
436 
437     }
438 
439     
440 
441     public void testIntroductionUsingHasMethod() {
442 
443        assertTrue(m_toBeIntroducedUsingHasMethod instanceof Serializable);
444 
445        assertFalse(m_toBeIntroducedUsingHasMethod instanceof Introductions);
446 
447        assertFalse(m_toBeIntroducedUsingHasMethod instanceof Cloneable);
448 
449     }
450 
451     
452 
453     public void testIntroductionUsingHasField() {
454 
455        assertTrue(m_toBeIntroducedUsingHasField instanceof Serializable);
456 
457        assertTrue(m_toBeIntroducedUsingHasField instanceof Introductions);
458 
459        assertTrue(m_toBeIntroducedUsingHasField instanceof Cloneable);
460 
461     }
462 
463 
464 
465     public static void main(String[] args) {
466 
467         junit.textui.TestRunner.run(suite());
468 
469     }
470 
471 
472 
473     public static junit.framework.Test suite() {
474 
475         //TODO: on IBM JRE, test method order is changed, and thus mixin replacement is done first
476 
477         // leading to some test
478 
479         // failure.
480 
481         return new junit.framework.TestSuite(IntroductionTest.class);
482 
483     }
484 
485 
486 
487     public String ___AW_getUuid() {
488 
489         return "ZZZZZZZZZZZZZZZZZZZZZZZZZZ";
490 
491     }
492 
493 }