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.mixin.perinstance;
008:
009: import java.lang.reflect.Method;
010: import java.io.Serializable;
011:
012: import junit.framework.TestCase;
013:
014: /**
015: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
016: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
017: */
018: public class IntroductionTest extends TestCase {
019: private ToBeIntroduced m_toBeIntroduced;
020: private ToBeIntroducedUsingHasMethod m_toBeIntroducedUsingHasMethod;
021: private ToBeIntroducedUsingHasField m_toBeIntroducedUsingHasField;
022:
023: public IntroductionTest(String name) {
024: super (name);
025: try {
026: m_toBeIntroduced = new ToBeIntroduced();
027: } catch (Exception e) {
028: e.printStackTrace();
029: }
030: m_toBeIntroducedUsingHasMethod = new ToBeIntroducedUsingHasMethod();
031: m_toBeIntroducedUsingHasField = new ToBeIntroducedUsingHasField();
032: }
033:
034: public void testInterfaceIntroduction() {
035: assertTrue(m_toBeIntroduced instanceof java.io.Serializable);
036: }
037:
038: public void testMixinInterfaceIntroduction() {
039: assertTrue(m_toBeIntroduced instanceof test.mixin.perinstance.Introductions);
040: assertTrue(m_toBeIntroduced instanceof Cloneable);
041: }
042:
043: public void testIntroducedComesFromInterfaces() {
044: Class klass = m_toBeIntroduced.getClass();
045: try {
046: Method m = klass.getDeclaredMethod("NOT_IN_MIXIN_INTF",
047: new Class[0]);
048: fail("should not have introduced : " + m);
049: } catch (NoSuchMethodException e) {
050: ;//ok
051: }
052: }
053:
054: public void testReturnVoid() {
055: try {
056: ((Introductions) m_toBeIntroduced).getVoid();
057: } catch (RuntimeException e) {
058: fail(e.getMessage());
059: }
060: }
061:
062: public void testParent() {
063: ToBeIntroducedParent parent = new ToBeIntroducedParent();
064: assertTrue(parent instanceof Serializable);
065: }
066:
067: public void testReturnLong() {
068: assertEquals(1L, ((Introductions) m_toBeIntroduced).getLong());
069: }
070:
071: public void testReturnInt() {
072: assertEquals(1, ((Introductions) m_toBeIntroduced).getInt());
073: }
074:
075: public void testReturnShort() {
076: assertEquals(1, ((Introductions) m_toBeIntroduced).getShort());
077: }
078:
079: public void testReturnDouble() {
080: assertEquals(new Double(1.1D), new Double(
081: ((Introductions) m_toBeIntroduced).getDouble()));
082: }
083:
084: public void testReturnFloat() {
085: assertEquals(new Float(1.1F), new Float(
086: ((Introductions) m_toBeIntroduced).getFloat()));
087: }
088:
089: public void testReturnByte() {
090: assertEquals(Byte.parseByte("1"),
091: ((Introductions) m_toBeIntroduced).getByte());
092: }
093:
094: public void testReturnChar() {
095: assertEquals('A', ((Introductions) m_toBeIntroduced).getChar());
096: }
097:
098: public void testReturnBoolean() {
099: assertEquals(true, ((Introductions) m_toBeIntroduced)
100: .getBoolean());
101: }
102:
103: public void testNoArgs() {
104: try {
105: ((Introductions) m_toBeIntroduced).noArgs();
106: } catch (Exception e) {
107: fail();
108: }
109: }
110:
111: public void testIntArg() {
112: assertEquals(12, ((Introductions) m_toBeIntroduced).intArg(12));
113: }
114:
115: public void testLongArg() {
116: long result = ((Introductions) m_toBeIntroduced).longArg(12L);
117: assertEquals(12L, result);
118: }
119:
120: public void testShortArg() {
121: assertEquals((short) 3, ((Introductions) m_toBeIntroduced)
122: .shortArg((short) 3));
123: }
124:
125: public void testDoubleArg() {
126: assertEquals(new Double(2.3D), new Double(
127: ((Introductions) m_toBeIntroduced).doubleArg(2.3D)));
128: }
129:
130: public void testFloatArg() {
131: assertEquals(new Float(2.3F), new Float(
132: ((Introductions) m_toBeIntroduced).floatArg(2.3F)));
133: }
134:
135: public void testByteArg() {
136: assertEquals(Byte.parseByte("1"),
137: ((Introductions) m_toBeIntroduced).byteArg(Byte
138: .parseByte("1")));
139: }
140:
141: public void testCharArg() {
142: assertEquals('B', ((Introductions) m_toBeIntroduced)
143: .charArg('B'));
144: }
145:
146: public void testBooleanArg() {
147: assertTrue(!((Introductions) m_toBeIntroduced)
148: .booleanArg(false));
149: }
150:
151: public void testObjectArg() {
152: assertEquals("test", ((Introductions) m_toBeIntroduced)
153: .objectArg("test"));
154: }
155:
156: public void testArrayArg() {
157: String[] strings = new String[0];
158: try {
159: strings = ((Introductions) m_toBeIntroduced)
160: .arrayArg(new String[] { "test1", "test2" });
161: } catch (Throwable e) {
162: System.out.println("e = " + e);
163: }
164: assertEquals("test1", strings[0]);
165: assertEquals("test2", strings[1]);
166: }
167:
168: public void testVariousArguments1() {
169: assertEquals("dummy".hashCode() + 1 + (int) 2.3F, this
170: .hashCode()
171: + (int) 34L, ((Introductions) m_toBeIntroduced)
172: .variousArguments1("dummy", 1, 2.3F, this , 34L));
173: }
174:
175: public void testVariousArguments2() {
176: assertEquals((int) 2.3F + 1 + "dummy".hashCode()
177: + this .hashCode() + (int) 34L + "test".hashCode(),
178: ((Introductions) m_toBeIntroduced).variousArguments2(
179: 2.3F, 1, "dummy", this , 34L, "test"));
180: }
181:
182: public void testThrowException() {
183: try {
184: ((Introductions) m_toBeIntroduced).exceptionThrower();
185: } catch (Throwable e) {
186: assertTrue(e instanceof UnsupportedOperationException);
187: return;
188: }
189: fail("this point should never be reached");
190: }
191:
192: public void testThrowExceptionChecked() {
193: try {
194: ((Introductions) m_toBeIntroduced)
195: .exceptionThrowerChecked();
196: } catch (Throwable e) {
197: assertTrue(e instanceof Introductions.CheckedException);
198: return;
199: }
200: fail("this point should never be reached");
201: }
202:
203: // FIXME XXX implement mixin and comment out tests
204:
205: // public void testReplaceImplementation() {
206: // assertEquals("test.mixin.perinstance.IntroductionTestAspect$MyImpl", SystemLoader.getCflowStack(this)
207: // .getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
208: // .getImplementationClassName());
209: // assertEquals(1, ((Introductions) m_toBeIntroduced).intArg(1));
210: //
211: // // swap with an inner class
212: // SystemLoader.getCflowStack(this).getAspectManager("tests").getMixin(
213: // "test.mixin.perinstance.IntroductionTestAspect$MyImpl").swapImplementation(
214: // "test.mixin.perinstance.IntroductionTestAspect$MyOtherImpl");
215: // assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
216: // assertEquals("test.mixin.perinstance.IntroductionTestAspect$MyOtherImpl", SystemLoader.getCflowStack(this)
217: // .getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
218: // .getImplementationClassName());
219: // }
220: //
221: // public void testReplaceImplementationToAutonomousOne() {
222: // assertEquals("test.mixin.perinstance.IntroductionTestAspect$MyOtherImpl", SystemLoader.getCflowStack(this)
223: // .getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
224: // .getImplementationClassName());
225: // assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
226: //
227: // // swap with an outer class
228: // SystemLoader.getCflowStack(this).getAspectManager("tests").getMixin(
229: // "test.mixin.perinstance.IntroductionTestAspect$MyImpl").swapImplementation(
230: // "test.mixin.IntroductionTestAspectMyImplReplacement");
231: // assertEquals(-2, ((Introductions) m_toBeIntroduced).intArg(1));
232: // assertEquals("test.mixin.IntroductionTestAspectMyImplReplacement", SystemLoader.getCflowStack(
233: // this).getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
234: // .getImplementationClassName());
235: // }
236: public void testIntroductionUsingHasMethod() {
237: assertTrue(m_toBeIntroducedUsingHasMethod instanceof Introductions);
238: assertTrue(m_toBeIntroducedUsingHasMethod instanceof Cloneable);
239: }
240:
241: public void testIntroductionUsingHasField() {
242: assertTrue(m_toBeIntroducedUsingHasField instanceof Introductions);
243: assertTrue(m_toBeIntroducedUsingHasField instanceof Cloneable);
244: }
245:
246: public static void main(String[] args) {
247: junit.textui.TestRunner.run(suite());
248: }
249:
250: public static junit.framework.Test suite() {
251: //TODO: on IBM JRE, test method order is changed, and thus mixin replacement is done first
252: // leading to some test
253: // failure.
254: return new junit.framework.TestSuite(IntroductionTest.class);
255: }
256: }
|