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 java.io.Serializable;
11  import java.lang.reflect.Method;
12  
13  import junit.framework.TestCase;
14  import org.codehaus.aspectwerkz.SystemLoader;
15  
16  /***
17   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
18   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
19   */
20  public class IntroductionTest extends TestCase {
21      private ToBeIntroduced m_toBeIntroduced;
22      private ToBeIntroducedUsingHasMethod m_toBeIntroducedUsingHasMethod;
23      private ToBeIntroducedUsingHasField m_toBeIntroducedUsingHasField;
24  
25      public IntroductionTest(String name) {
26          super(name);
27          m_toBeIntroduced = new ToBeIntroduced();
28          m_toBeIntroducedUsingHasMethod = new ToBeIntroducedUsingHasMethod();
29          m_toBeIntroducedUsingHasField = new ToBeIntroducedUsingHasField();
30      }
31  
32      public void testIntroducedComesFromInterfaces() {
33          Class klass = m_toBeIntroduced.getClass();
34          try {
35              Method m = klass.getDeclaredMethod("NOT_IN_MIXIN_INTF", new Class[0]);
36              fail("should not have introduced : " + m);
37          } catch (NoSuchMethodException e) {
38              ;//ok
39          }
40      }
41  
42      public void testInterfaceIntroduction() {
43          assertTrue(m_toBeIntroduced instanceof java.io.Serializable);
44          assertTrue(m_toBeIntroduced instanceof test.Introductions);
45      }
46  
47      public void testReturnVoid() {
48          try {
49              ((Introductions) m_toBeIntroduced).getVoid();
50          } catch (RuntimeException e) {
51              fail(e.getMessage());
52          }
53      }
54  
55      public void testReturnLong() {
56          assertEquals(1L, ((Introductions) m_toBeIntroduced).getLong());
57      }
58  
59      public void testReturnInt() {
60          assertEquals(1, ((Introductions) m_toBeIntroduced).getInt());
61      }
62  
63      public void testReturnShort() {
64          assertEquals(1, ((Introductions) m_toBeIntroduced).getShort());
65      }
66  
67      public void testReturnDouble() {
68          assertEquals(new Double(1.1D), new Double(((Introductions) m_toBeIntroduced).getDouble()));
69      }
70  
71      public void testReturnFloat() {
72          assertEquals(new Float(1.1F), new Float(((Introductions) m_toBeIntroduced).getFloat()));
73      }
74  
75      public void testReturnByte() {
76          assertEquals(Byte.parseByte("1"), ((Introductions) m_toBeIntroduced).getByte());
77      }
78  
79      public void testReturnChar() {
80          assertEquals('A', ((Introductions) m_toBeIntroduced).getChar());
81      }
82  
83      public void testReturnBoolean() {
84          assertEquals(true, ((Introductions) m_toBeIntroduced).getBoolean());
85      }
86  
87      public void testNoArgs() {
88          try {
89              ((Introductions) m_toBeIntroduced).noArgs();
90          } catch (Exception e) {
91              fail();
92          }
93      }
94  
95      public void testIntArg() {
96          assertEquals(12, ((Introductions) m_toBeIntroduced).intArg(12));
97      }
98  
99      public void testLongArg() {
100         long result = ((Introductions) m_toBeIntroduced).longArg(12L);
101         assertEquals(12L, result);
102     }
103 
104     public void testShortArg() {
105         assertEquals((short) 3, ((Introductions) m_toBeIntroduced).shortArg((short) 3));
106     }
107 
108     public void testDoubleArg() {
109         assertEquals(new Double(2.3D), new Double(((Introductions) m_toBeIntroduced)
110                 .doubleArg(2.3D)));
111     }
112 
113     public void testFloatArg() {
114         assertEquals(new Float(2.3F), new Float(((Introductions) m_toBeIntroduced).floatArg(2.3F)));
115     }
116 
117     public void testByteArg() {
118         assertEquals(Byte.parseByte("1"), ((Introductions) m_toBeIntroduced).byteArg(Byte
119                 .parseByte("1")));
120     }
121 
122     public void testCharArg() {
123         assertEquals('B', ((Introductions) m_toBeIntroduced).charArg('B'));
124     }
125 
126     public void testBooleanArg() {
127         assertTrue(!((Introductions) m_toBeIntroduced).booleanArg(false));
128     }
129 
130     public void testObjectArg() {
131         assertEquals("test", ((Introductions) m_toBeIntroduced).objectArg("test"));
132     }
133 
134     public void testArrayArg() {
135         String[] strings = new String[0];
136         try {
137             strings = ((Introductions) m_toBeIntroduced).arrayArg(new String[] {
138                 "test1", "test2"
139             });
140         } catch (Throwable e) {
141             System.out.println("e = " + e);
142         }
143         assertEquals("test1", strings[0]);
144         assertEquals("test2", strings[1]);
145     }
146 
147     public void testVariousArguments1() {
148         assertEquals(
149             "dummy".hashCode() + 1 + (int) 2.3F,
150             this.hashCode() + (int) 34L,
151             ((Introductions) m_toBeIntroduced).variousArguments1("dummy", 1, 2.3F, this, 34L));
152     }
153 
154     public void testVariousArguments2() {
155         assertEquals((int) 2.3F
156             + 1
157             + "dummy".hashCode()
158             + this.hashCode()
159             + (int) 34L
160             + "test".hashCode(), ((Introductions) m_toBeIntroduced).variousArguments2(
161             2.3F,
162             1,
163             "dummy",
164             this,
165             34L,
166             "test"));
167     }
168 
169     public void testThrowException() {
170         try {
171             ((Introductions) m_toBeIntroduced).exceptionThrower();
172         } catch (Throwable e) {
173             assertTrue(e instanceof UnsupportedOperationException);
174             return;
175         }
176         fail("this point should never be reached");
177     }
178 
179     public void testThrowExceptionChecked() {
180         try {
181             ((Introductions) m_toBeIntroduced).exceptionThrowerChecked();
182         } catch (Throwable e) {
183             assertTrue(e instanceof Introductions.CheckedException);
184             return;
185         }
186         fail("this point should never be reached");
187     }
188 
189     public void testReplaceImplementation() {
190         assertEquals("test.aspect.IntroductionTestAspect$MyImpl", SystemLoader.getSystem(this)
191                 .getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
192                 .getImplementationClassName());
193         assertEquals(1, ((Introductions) m_toBeIntroduced).intArg(1));
194 
195         // swap with an inner class
196         SystemLoader.getSystem(this).getAspectManager("tests").getMixin(
197             "test.aspect.IntroductionTestAspect$MyImpl").swapImplementation(
198             "test.aspect.IntroductionTestAspect$MyOtherImpl");
199         assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
200         assertEquals("test.aspect.IntroductionTestAspect$MyOtherImpl", SystemLoader.getSystem(this)
201                 .getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
202                 .getImplementationClassName());
203     }
204 
205     public void testReplaceImplementationToAutonomousOne() {
206         assertEquals("test.aspect.IntroductionTestAspect$MyOtherImpl", SystemLoader.getSystem(this)
207                 .getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
208                 .getImplementationClassName());
209         assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
210 
211         // swap with an outer class
212         SystemLoader.getSystem(this).getAspectManager("tests").getMixin(
213             "test.aspect.IntroductionTestAspect$MyImpl").swapImplementation(
214             "test.aspect.IntroductionTestAspectMyImplReplacement");
215         assertEquals(-2, ((Introductions) m_toBeIntroduced).intArg(1));
216         assertEquals("test.aspect.IntroductionTestAspectMyImplReplacement", SystemLoader.getSystem(
217             this).getAspectManager("tests").getMixin("test.aspect.IntroductionTestAspect$MyImpl")
218                 .getImplementationClassName());
219     }
220     
221     public void testIntroductionUsingHasMethod() {
222        assertTrue(m_toBeIntroducedUsingHasMethod instanceof Serializable);
223        assertFalse(m_toBeIntroducedUsingHasMethod instanceof Introductions);
224        assertFalse(m_toBeIntroducedUsingHasMethod instanceof Cloneable);
225     }
226     
227     public void testIntroductionUsingHasField() {
228        assertTrue(m_toBeIntroducedUsingHasField instanceof Serializable);
229        assertTrue(m_toBeIntroducedUsingHasField instanceof Introductions);
230        assertTrue(m_toBeIntroducedUsingHasField instanceof Cloneable);
231     }
232 
233     public static void main(String[] args) {
234         junit.textui.TestRunner.run(suite());
235     }
236 
237     public static junit.framework.Test suite() {
238         //TODO: on IBM JRE, test method order is changed, and thus mixin replacement is done first
239         // leading to some test
240         // failure.
241         return new junit.framework.TestSuite(IntroductionTest.class);
242     }
243 
244     public String ___AW_getUuid() {
245         return "ZZZZZZZZZZZZZZZZZZZZZZZZZZ";
246     }
247 }