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.container.tck.test.util;
027:
028: import junit.framework.TestCase;
029: import org.jicarilla.container.tck.util.MemberInvocation;
030:
031: import java.lang.reflect.Member;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: *
038: *
039: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
040: * @version $Id: InvocationMemberTestCase.java,v 1.1 2004/01/18 19:32:05 lsimons Exp $
041: */
042: public class InvocationMemberTestCase extends TestCase {
043: private static class MockTarget {
044: public static Member CALL = null;
045:
046: static {
047: try {
048: CALL = MockTarget.class.getMethod("call", new Class[0]);
049: } catch (NoSuchMethodException e) {
050: System.exit(1);
051: } catch (SecurityException e) {
052: System.exit(1);
053: }
054: }
055:
056: public void call() {
057: }
058: }
059:
060: public void testConstructors() throws Exception {
061: assertNotNull(new MemberInvocation());
062:
063: Object target = new MockTarget();
064: Object proxy = new MockTarget();
065: Member method = MockTarget.CALL;
066: Object[] args = new Object[0];
067:
068: assertNotNull(new MemberInvocation(target, proxy, method, args));
069: assertNotNull(new MemberInvocation(null, proxy, method, args));
070: assertNotNull(new MemberInvocation(null, null, method, args));
071: assertNotNull(new MemberInvocation(null, null, null, args));
072: assertNotNull(new MemberInvocation(null, null, null, null));
073: assertNotNull(new MemberInvocation(target, proxy, method, null));
074: assertNotNull(new MemberInvocation(target, proxy, null, null));
075: assertNotNull(new MemberInvocation(target, null, null, null));
076: }
077:
078: // ----------------------------------------------------------------------
079: // Members
080: // ----------------------------------------------------------------------
081:
082: public void testGetProxy() {
083: Object o = new MockTarget();
084: MemberInvocation i = new MemberInvocation(null, o, null, null);
085: Object r = i.getProxy();
086: assertEquals(o, r);
087:
088: i = new MemberInvocation();
089: assertNull(i.getProxy());
090: }
091:
092: public void testSetProxy() {
093: Object o = new MockTarget();
094: MemberInvocation i = new MemberInvocation();
095:
096: i.setProxy(o);
097: assertEquals(o, i.getProxy());
098: i.setProxy(null);
099: assertNull(i.getProxy());
100: }
101:
102: public void testGetTarget() {
103: Object o = new MockTarget();
104: MemberInvocation i = new MemberInvocation(o, null, null, null);
105: Object r = i.getTarget();
106: assertEquals(o, r);
107:
108: i = new MemberInvocation();
109: assertNull(i.getTarget());
110: }
111:
112: public void testSetTarget() {
113: Object o = new MockTarget();
114: MemberInvocation i = new MemberInvocation();
115:
116: i.setTarget(o);
117: assertEquals(o, i.getTarget());
118: i.setTarget(null);
119: assertNull(i.getTarget());
120: }
121:
122: public void testGetMember() {
123: Member o = MockTarget.CALL;
124: MemberInvocation i = new MemberInvocation(null, null, o, null);
125: Object r = i.getMember();
126: assertEquals(o, r);
127:
128: i = new MemberInvocation();
129: assertNull(i.getMember());
130: }
131:
132: public void testSetMember() {
133: Member o = MockTarget.CALL;
134: MemberInvocation i = new MemberInvocation();
135:
136: i.setMember(o);
137: assertEquals(o, i.getMember());
138: i.setMember(null);
139: assertNull(i.getMember());
140: }
141:
142: public void testGetArgs() {
143: Object[] o = new MockTarget[] { new MockTarget(),
144: new MockTarget() };
145: MemberInvocation i = new MemberInvocation(null, null, null, o);
146: Object r = i.getArgs();
147: assertEquals(o, r);
148:
149: i = new MemberInvocation();
150: assertNull(i.getArgs());
151: }
152:
153: public void testSetArgs() {
154: Object[] o = new MockTarget[] { new MockTarget(),
155: new MockTarget() };
156: MemberInvocation i = new MemberInvocation();
157:
158: i.setArgs(o);
159: assertEquals(o, i.getArgs());
160: i.setArgs(null);
161: assertNull(i.getArgs());
162: i.setArgs(new Object[0]);
163: assertEquals(0, i.getArgs().length);
164: }
165:
166: public void testGetAndSetResult() {
167: MemberInvocation i = new MemberInvocation();
168: Object r = i.getResult();
169: assertNull(r);
170:
171: i.setResult(r);
172: assertNull(i.getResult());
173:
174: r = new MockTarget();
175: i.setResult(r);
176: assertEquals(r, i.getResult());
177: }
178:
179: public void testGetAndSetThrowable() {
180: MemberInvocation i = new MemberInvocation();
181: Object r = i.getThrowable();
182: assertNull(r);
183:
184: i.setThrowable(null);
185: assertNull(r);
186:
187: Throwable re = new RuntimeException();
188: i.setThrowable(re);
189: assertEquals(re, i.getThrowable());
190: }
191:
192: public void testGetAndSetMemberInvocationContext() {
193: MemberInvocation i = new MemberInvocation();
194: Map r = i.getInvocationContext();
195: assertNotNull(r);
196:
197: assertNull(r.put("blah", "diblah"));
198:
199: i.setInvocationContext(null);
200: assertNull(i.getInvocationContext());
201:
202: HashMap h = new HashMap();
203: i.setInvocationContext(h);
204: assertEquals(h, i.getInvocationContext());
205: }
206:
207: public void testEquals() {
208: Object target = new MockTarget();
209: Object proxy = new MockTarget();
210: Member method = MockTarget.CALL;
211: Object[] args = new Object[0];
212:
213: MemberInvocation i1 = new MemberInvocation(target, proxy,
214: method, args);
215: i1.setResult(target);
216: MemberInvocation i2 = new MemberInvocation(target, proxy,
217: 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 MemberInvocation();
241: i2 = new MemberInvocation();
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: Member method = MockTarget.CALL;
253: Object[] args = new Object[0];
254: RuntimeException re = new RuntimeException();
255:
256: MemberInvocation i1 = new MemberInvocation(target, proxy,
257: method, args);
258: i1.setResult(target);
259: i1.setThrowable(re);
260:
261: MemberInvocation i2 = new MemberInvocation();
262:
263: assertFalse(i1.hashCode() == i2.hashCode());
264: }
265:
266: public void testRecycle() {
267: Object target = new MockTarget();
268: Object proxy = new MockTarget();
269: Member method = MockTarget.CALL;
270: Object[] args = new Object[0];
271:
272: MemberInvocation i = new MemberInvocation(target, proxy,
273: method, args);
274: i.setResult(target);
275: i.setThrowable(new RuntimeException());
276:
277: i.recycle();
278:
279: assertNull(i.getArgs());
280: assertNotNull(i.getInvocationContext());
281: assertEquals(0, i.getInvocationContext().size());
282: assertNull(i.getMember());
283: assertNull(i.getProxy());
284: assertNull(i.getResult());
285: assertNull(i.getTarget());
286: assertNull(i.getThrowable());
287:
288: Map map = new HashMap();
289: map.put("blah", "blah");
290: map = Collections.unmodifiableMap(map);
291: i.setInvocationContext(map);
292: i.recycle();
293: assertNotNull(i.getInvocationContext());
294:
295: i.setInvocationContext(null);
296: i.recycle();
297: assertNotNull(i.getInvocationContext());
298: }
299: }
|