001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.lang.test;
027:
028: import org.jicarilla.lang.Invocation;
029: import junit.framework.TestCase;
030:
031: import java.lang.reflect.Method;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
038: * Invocation.
039: *
040: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
041: * @version $Id: InvocationTestCase.java,v 1.2 2003/11/10 22:58:00 lsimons Exp
042: * $
043: */
044: public class InvocationTestCase extends TestCase {
045: private static class MockTarget {
046: public static Method CALL = null;
047:
048: static {
049: try {
050: CALL = MockTarget.class.getMethod("call", new Class[0]);
051: } catch (NoSuchMethodException e) {
052: System.exit(1);
053: } catch (SecurityException e) {
054: System.exit(1);
055: }
056: }
057:
058: public void call() {
059: }
060: }
061:
062: public void testConstructors() throws Exception {
063: assertNotNull(new Invocation());
064:
065: Object target = new MockTarget();
066: Object proxy = new MockTarget();
067: Method method = MockTarget.CALL;
068: Object[] args = new Object[0];
069:
070: assertNotNull(new Invocation(target, proxy, method, args));
071: assertNotNull(new Invocation(null, proxy, method, args));
072: assertNotNull(new Invocation(null, null, method, args));
073: assertNotNull(new Invocation(null, null, null, args));
074: assertNotNull(new Invocation(null, null, null, null));
075: assertNotNull(new Invocation(target, proxy, method, null));
076: assertNotNull(new Invocation(target, proxy, null, null));
077: assertNotNull(new Invocation(target, null, null, null));
078: }
079:
080: // ----------------------------------------------------------------------
081: // Methods
082: // ----------------------------------------------------------------------
083:
084: public void testGetProxy() {
085: Object o = new MockTarget();
086: Invocation i = new Invocation(null, o, null, null);
087: Object r = i.getProxy();
088: assertEquals(o, r);
089:
090: i = new Invocation();
091: assertNull(i.getProxy());
092: }
093:
094: public void testSetProxy() {
095: Object o = new MockTarget();
096: Invocation i = new Invocation();
097:
098: i.setProxy(o);
099: assertEquals(o, i.getProxy());
100: i.setProxy(null);
101: assertNull(i.getProxy());
102: }
103:
104: public void testGetTarget() {
105: Object o = new MockTarget();
106: Invocation i = new Invocation(o, null, null, null);
107: Object r = i.getTarget();
108: assertEquals(o, r);
109:
110: i = new Invocation();
111: assertNull(i.getTarget());
112: }
113:
114: public void testSetTarget() {
115: Object o = new MockTarget();
116: Invocation i = new Invocation();
117:
118: i.setTarget(o);
119: assertEquals(o, i.getTarget());
120: i.setTarget(null);
121: assertNull(i.getTarget());
122: }
123:
124: public void testGetMethod() {
125: Method o = MockTarget.CALL;
126: Invocation i = new Invocation(null, null, o, null);
127: Object r = i.getMethod();
128: assertEquals(o, r);
129:
130: i = new Invocation();
131: assertNull(i.getMethod());
132: }
133:
134: public void testSetMethod() {
135: Method o = MockTarget.CALL;
136: Invocation i = new Invocation();
137:
138: i.setMethod(o);
139: assertEquals(o, i.getMethod());
140: i.setMethod(null);
141: assertNull(i.getMethod());
142: }
143:
144: public void testGetArgs() {
145: Object[] o = new MockTarget[] { new MockTarget(),
146: new MockTarget() };
147: Invocation i = new Invocation(null, null, null, o);
148: Object r = i.getArgs();
149: assertEquals(o, r);
150:
151: i = new Invocation();
152: assertNull(i.getArgs());
153: }
154:
155: public void testSetArgs() {
156: Object[] o = new MockTarget[] { new MockTarget(),
157: new MockTarget() };
158: Invocation i = new Invocation();
159:
160: i.setArgs(o);
161: assertEquals(o, i.getArgs());
162: i.setArgs(null);
163: assertNull(i.getArgs());
164: i.setArgs(new Object[0]);
165: assertEquals(0, i.getArgs().length);
166: }
167:
168: public void testGetAndSetResult() {
169: Invocation i = new Invocation();
170: Object r = i.getResult();
171: assertNull(r);
172:
173: i.setResult(r);
174: assertNull(i.getResult());
175:
176: r = new MockTarget();
177: i.setResult(r);
178: assertEquals(r, i.getResult());
179: }
180:
181: public void testGetAndSetThrowable() {
182: Invocation i = new Invocation();
183: Object r = i.getThrowable();
184: assertNull(r);
185:
186: i.setThrowable(null);
187: assertNull(r);
188:
189: Throwable re = new RuntimeException();
190: i.setThrowable(re);
191: assertEquals(re, i.getThrowable());
192: }
193:
194: public void testGetAndSetInvocationContext() {
195: Invocation i = new Invocation();
196: Map r = i.getInvocationContext();
197: assertNotNull(r);
198:
199: assertNull(r.put("blah", "diblah"));
200:
201: i.setInvocationContext(null);
202: assertNull(i.getInvocationContext());
203:
204: HashMap h = new HashMap();
205: i.setInvocationContext(h);
206: assertEquals(h, i.getInvocationContext());
207: }
208:
209: public void testEquals() {
210: Object target = new MockTarget();
211: Object proxy = new MockTarget();
212: Method method = MockTarget.CALL;
213: Object[] args = new Object[0];
214:
215: Invocation i1 = new Invocation(target, proxy, method, args);
216: i1.setResult(target);
217: Invocation i2 = new Invocation(target, proxy, method, args);
218: i2.setResult(target);
219:
220: assertFalse(i1.equals(null));
221: assertTrue(i1.equals(i1));
222: assertFalse(i1.equals(new Object()));
223:
224: assertEquals(i1, i2);
225:
226: RuntimeException re = new RuntimeException();
227: i1.setThrowable(re);
228: i2.setThrowable(re);
229: assertEquals(i1, i2);
230: i2.setThrowable(null);
231: assertFalse(i1.equals(i2));
232: assertFalse(i2.equals(i1));
233:
234: i1.setThrowable(null);
235: i2.setResult(null);
236: assertFalse(i1.equals(i2));
237:
238: i1.setResult(null);
239:
240: i1 = new Invocation();
241: i2 = new Invocation();
242: assertTrue(i1.equals(i2));
243: i2.setArgs(new Object[0]);
244: assertFalse(i1.equals(i2));
245: i1.setArgs(new Object[1]);
246: assertFalse(i1.equals(i2));
247: }
248:
249: public void testHashCode() {
250: Object target = new MockTarget();
251: Object proxy = new MockTarget();
252: Method method = MockTarget.CALL;
253: Object[] args = new Object[0];
254: RuntimeException re = new RuntimeException();
255:
256: Invocation i1 = new Invocation(target, proxy, method, args);
257: i1.setResult(target);
258: i1.setThrowable(re);
259:
260: Invocation i2 = new Invocation();
261:
262: assertFalse(i1.hashCode() == i2.hashCode());
263: }
264:
265: public void testRecycle() {
266: Object target = new MockTarget();
267: Object proxy = new MockTarget();
268: Method method = MockTarget.CALL;
269: Object[] args = new Object[0];
270:
271: Invocation i = new Invocation(target, proxy, method, args);
272: i.setResult(target);
273: i.setThrowable(new RuntimeException());
274:
275: i.recycle();
276:
277: assertNull(i.getArgs());
278: assertNotNull(i.getInvocationContext());
279: assertEquals(0, i.getInvocationContext().size());
280: assertNull(i.getMethod());
281: assertNull(i.getProxy());
282: assertNull(i.getResult());
283: assertNull(i.getTarget());
284: assertNull(i.getThrowable());
285:
286: Map map = new HashMap();
287: map.put("blah", "blah");
288: map = Collections.unmodifiableMap(map);
289: i.setInvocationContext(map);
290: i.recycle();
291: assertNotNull(i.getInvocationContext());
292:
293: i.setInvocationContext(null);
294: i.recycle();
295: assertNotNull(i.getInvocationContext());
296: }
297: }
|