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