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.perclass;
008:
009: import java.io.Serializable;
010: import java.lang.reflect.Method;
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 Serializable);
036: }
037:
038: public void testMixinInterfaceIntroduction() {
039: assertTrue(m_toBeIntroduced instanceof 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 testReturnLong() {
063: assertEquals(1L, ((Introductions) m_toBeIntroduced).getLong());
064: }
065:
066: public void testReturnInt() {
067: assertEquals(1, ((Introductions) m_toBeIntroduced).getInt());
068: }
069:
070: public void testReturnShort() {
071: assertEquals(1, ((Introductions) m_toBeIntroduced).getShort());
072: }
073:
074: public void testReturnDouble() {
075: assertEquals(new Double(1.1D), new Double(
076: ((Introductions) m_toBeIntroduced).getDouble()));
077: }
078:
079: public void testReturnFloat() {
080: assertEquals(new Float(1.1F), new Float(
081: ((Introductions) m_toBeIntroduced).getFloat()));
082: }
083:
084: public void testReturnByte() {
085: assertEquals(Byte.parseByte("1"),
086: ((Introductions) m_toBeIntroduced).getByte());
087: }
088:
089: public void testReturnChar() {
090: assertEquals('A', ((Introductions) m_toBeIntroduced).getChar());
091: }
092:
093: public void testReturnBoolean() {
094: assertEquals(true, ((Introductions) m_toBeIntroduced)
095: .getBoolean());
096: }
097:
098: public void testNoArgs() {
099: try {
100: ((Introductions) m_toBeIntroduced).noArgs();
101: } catch (Exception e) {
102: fail();
103: }
104: }
105:
106: public void testIntArg() {
107: assertEquals(12, ((Introductions) m_toBeIntroduced).intArg(12));
108: }
109:
110: public void testLongArg() {
111: long result = ((Introductions) m_toBeIntroduced).longArg(12L);
112: assertEquals(12L, result);
113: }
114:
115: public void testShortArg() {
116: assertEquals((short) 3, ((Introductions) m_toBeIntroduced)
117: .shortArg((short) 3));
118: }
119:
120: public void testDoubleArg() {
121: assertEquals(new Double(2.3D), new Double(
122: ((Introductions) m_toBeIntroduced).doubleArg(2.3D)));
123: }
124:
125: public void testFloatArg() {
126: assertEquals(new Float(2.3F), new Float(
127: ((Introductions) m_toBeIntroduced).floatArg(2.3F)));
128: }
129:
130: public void testByteArg() {
131: assertEquals(Byte.parseByte("1"),
132: ((Introductions) m_toBeIntroduced).byteArg(Byte
133: .parseByte("1")));
134: }
135:
136: public void testCharArg() {
137: assertEquals('B', ((Introductions) m_toBeIntroduced)
138: .charArg('B'));
139: }
140:
141: public void testBooleanArg() {
142: assertTrue(!((Introductions) m_toBeIntroduced)
143: .booleanArg(false));
144: }
145:
146: public void testObjectArg() {
147: assertEquals("test", ((Introductions) m_toBeIntroduced)
148: .objectArg("test"));
149: }
150:
151: public void testArrayArg() {
152: String[] strings = new String[0];
153: try {
154: strings = ((Introductions) m_toBeIntroduced)
155: .arrayArg(new String[] { "test1", "test2" });
156: } catch (Throwable e) {
157: System.out.println("e = " + e);
158: }
159: assertEquals("test1", strings[0]);
160: assertEquals("test2", strings[1]);
161: }
162:
163: public void testVariousArguments1() {
164: assertEquals("dummy".hashCode() + 1 + (int) 2.3F, this
165: .hashCode()
166: + (int) 34L, ((Introductions) m_toBeIntroduced)
167: .variousArguments1("dummy", 1, 2.3F, this , 34L));
168: }
169:
170: public void testVariousArguments2() {
171: assertEquals((int) 2.3F + 1 + "dummy".hashCode()
172: + this .hashCode() + (int) 34L + "test".hashCode(),
173: ((Introductions) m_toBeIntroduced).variousArguments2(
174: 2.3F, 1, "dummy", this , 34L, "test"));
175: }
176:
177: public void testThrowException() {
178: try {
179: ((Introductions) m_toBeIntroduced).exceptionThrower();
180: } catch (Throwable e) {
181: assertTrue(e instanceof UnsupportedOperationException);
182: return;
183: }
184: fail("this point should never be reached");
185: }
186:
187: public void testThrowExceptionChecked() {
188: try {
189: ((Introductions) m_toBeIntroduced)
190: .exceptionThrowerChecked();
191: } catch (Throwable e) {
192: assertTrue(e instanceof Introductions.CheckedException);
193: return;
194: }
195: fail("this point should never be reached");
196: }
197:
198: // FIXME XXX implement mixin and comment out tests
199:
200: // public void testReplaceImplementation() {
201: // assertEquals("test.mixin.perinstance.IntroductionTestAspect$MyImpl", SystemLoader.getCflowStack(this)
202: // .getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
203: // .getImplementationClassName());
204: // assertEquals(1, ((Introductions) m_toBeIntroduced).intArg(1));
205: //
206: // // swap with an inner class
207: // SystemLoader.getCflowStack(this).getAspectManager("tests").getMixin(
208: // "test.mixin.perinstance.IntroductionTestAspect$MyImpl").swapImplementation(
209: // "test.mixin.perinstance.IntroductionTestAspect$MyOtherImpl");
210: // assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
211: // assertEquals("test.mixin.perinstance.IntroductionTestAspect$MyOtherImpl", SystemLoader.getCflowStack(this)
212: // .getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
213: // .getImplementationClassName());
214: // }
215: //
216: // public void testReplaceImplementationToAutonomousOne() {
217: // assertEquals("test.mixin.perinstance.IntroductionTestAspect$MyOtherImpl", SystemLoader.getCflowStack(this)
218: // .getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
219: // .getImplementationClassName());
220: // assertEquals(-1, ((Introductions) m_toBeIntroduced).intArg(1));
221: //
222: // // swap with an outer class
223: // SystemLoader.getCflowStack(this).getAspectManager("tests").getMixin(
224: // "test.mixin.perinstance.IntroductionTestAspect$MyImpl").swapImplementation(
225: // "test.mixin.IntroductionTestAspectMyImplReplacement");
226: // assertEquals(-2, ((Introductions) m_toBeIntroduced).intArg(1));
227: // assertEquals("test.mixin.IntroductionTestAspectMyImplReplacement", SystemLoader.getCflowStack(
228: // this).getAspectManager("tests").getMixin("test.mixin.perinstance.IntroductionTestAspect$MyImpl")
229: // .getImplementationClassName());
230: // }
231: public void testIntroductionUsingHasMethod() {
232: assertTrue(m_toBeIntroducedUsingHasMethod instanceof Cloneable);
233: assertTrue(m_toBeIntroducedUsingHasMethod instanceof Introductions);
234: }
235:
236: public void testIntroductionUsingHasField() {
237: assertTrue(m_toBeIntroducedUsingHasField instanceof Cloneable);
238: assertTrue(m_toBeIntroducedUsingHasField instanceof Introductions);
239: }
240:
241: public static void main(String[] args) {
242: junit.textui.TestRunner.run(suite());
243: }
244:
245: public static junit.framework.Test suite() {
246: //TODO: on IBM JRE, test method order is changed, and thus mixin replacement is done first
247: // leading to some test
248: // failure.
249: return new junit.framework.TestSuite(IntroductionTest.class);
250: }
251: }
|