001: // $Id: MethodCallTest.java,v 1.11 2005/08/18 09:09:15 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.blocks.MethodCall;
009: import org.jgroups.util.Util;
010:
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.ObjectInputStream;
014: import java.io.ObjectOutputStream;
015: import java.lang.reflect.Method;
016:
017: /**
018: * @author Bela Ban belaban@yahoo.com
019: * @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
020: * @version $Revision: 1.11 $
021: **/
022:
023: public class MethodCallTest extends TestCase {
024:
025: Class cl = MethodCallTest.class;
026:
027: public MethodCallTest(String name) {
028: super (name);
029: }
030:
031: public boolean foo(int a, String b) {
032: System.out.println("test(" + a + ", " + b + ')');
033: return true;
034: }
035:
036: public void bar(String[] a, String b) {
037: if (a != null) {
038: for (int i = 0; i < a.length; i++) {
039: String s = a[i];
040: System.out.print(s + ' ');
041: }
042: } else
043: System.out.println("a=null");
044: if (b != null)
045: System.out.println("b=" + b);
046: else
047: System.out.println("b=null");
048: }
049:
050: public void foobar() {
051: System.out.println("foobar()");
052: }
053:
054: public void testOld() {
055: try {
056: MethodCall mc = new MethodCall("foo", new Object[] {
057: new Integer(22), "Bela" });
058: assertEquals(mc.invoke(this ), Boolean.TRUE);
059: } catch (Throwable t) {
060: fail(t.toString());
061: }
062: }
063:
064: public void testOld2() {
065: try {
066: MethodCall mc = new MethodCall("bar", new Object[] {
067: new String[] { "one", "two", "three" }, "Bela" });
068: mc.invoke(this );
069: } catch (Throwable t) {
070: fail(t.toString());
071: }
072: }
073:
074: public void testWithNull() {
075: try {
076: MethodCall mc = new MethodCall("foobar", null,
077: (Class[]) null);
078: System.out.println("mc: " + mc);
079: mc.invoke(this );
080: } catch (Throwable t) {
081: fail(t.toString());
082: }
083: }
084:
085: public void testOldWithNull() {
086: try {
087: MethodCall mc = new MethodCall("bar", new Object[] {
088: new String[] { "one", "two", "three" }, null });
089: mc.invoke(this );
090: } catch (Throwable t) {
091: fail(t.toString());
092: }
093: }
094:
095: public void testOldWithNull2() {
096: try {
097: MethodCall mc = new MethodCall("bar", new Object[] { null,
098: "Bela" });
099: mc.invoke(this );
100: } catch (Throwable t) {
101: fail(t.toString());
102: }
103: }
104:
105: public void testOldWithNull3() {
106: try {
107: MethodCall mc = new MethodCall("foobar", null);
108: mc.invoke(this );
109: } catch (Throwable t) {
110: fail(t.toString());
111: }
112: }
113:
114: public void testOldWithNull4() {
115: try {
116: MethodCall mc = new MethodCall("foobar", new Object[0]);
117: mc.invoke(this );
118: } catch (Throwable t) {
119: fail(t.toString());
120: }
121: }
122:
123: public void testMethod() {
124: Method m;
125: try {
126: m = cl.getMethod("foo", new Class[] { int.class,
127: String.class });
128: MethodCall mc = new MethodCall(m, new Object[] {
129: new Integer(22), "Bela" });
130: assertEquals(mc.invoke(this ), Boolean.TRUE);
131: } catch (Throwable t) {
132: fail(t.toString());
133: }
134: }
135:
136: public void testTypes() {
137: MethodCall mc;
138: mc = new MethodCall("foo", new Object[] { new Integer(35),
139: "Bela" }, new Class[] { int.class, String.class });
140: try {
141: assertEquals(mc.invoke(this ), Boolean.TRUE);
142: } catch (Throwable t) {
143: fail(t.toString());
144: }
145: }
146:
147: public void testTypesWithArray() {
148: MethodCall mc;
149: mc = new MethodCall("bar", new Object[] {
150: new String[] { "one", "two", "three" }, "Bela" },
151: new Class[] { String[].class, String.class });
152: try {
153: mc.invoke(this );
154: } catch (Throwable t) {
155: fail(t.toString());
156: }
157: }
158:
159: public void testTypesWithNullArgument() {
160: MethodCall mc;
161: mc = new MethodCall("bar", new Object[] {
162: new String[] { "one", "two", "three" }, null },
163: new Class[] { String[].class, String.class });
164: try {
165: mc.invoke(this );
166: } catch (Throwable t) {
167: fail(t.toString());
168: }
169: }
170:
171: public void testTypesWithNullArgument2() {
172: MethodCall mc;
173: mc = new MethodCall("bar",
174: new Object[] { new String[] { "one", "two", "three" },
175: new Object[] {} }, new Class[] {
176: String[].class, String.class });
177: try {
178: mc.invoke(this );
179: } catch (IllegalArgumentException ex) {
180: assertTrue("this was expected", true);
181: } catch (Throwable t) {
182: fail(t.toString());
183: }
184: }
185:
186: public void testTypesWithNullArgument3() {
187: MethodCall mc;
188: mc = new MethodCall("foobar", new Object[] {}, new Class[] {});
189: try {
190: mc.invoke(this );
191: } catch (IllegalArgumentException ex) {
192: assertTrue("this was expected", true);
193: } catch (Throwable t) {
194: fail(t.toString());
195: }
196: }
197:
198: public void testTypesWithNullArgument4() {
199: MethodCall mc;
200: mc = new MethodCall("foobar", (Object[]) null, (Class[]) null);
201: try {
202: mc.invoke(this );
203: } catch (IllegalArgumentException ex) {
204: assertTrue("this was expected", true);
205: } catch (Throwable t) {
206: fail(t.toString());
207: }
208: }
209:
210: public void testTypesWithNullArgument5() {
211: MethodCall mc;
212: mc = new MethodCall("foobar", new Object[0], new Class[0]);
213: try {
214: mc.invoke(this );
215: } catch (IllegalArgumentException ex) {
216: assertTrue("this was expected", true);
217: } catch (Throwable t) {
218: fail(t.toString());
219: }
220: }
221:
222: public void testSignature() {
223: MethodCall mc;
224: mc = new MethodCall("foo", new Object[] { new Integer(35),
225: "Bela" }, new String[] { int.class.getName(),
226: String.class.getName() });
227: try {
228: assertEquals(mc.invoke(this ), Boolean.TRUE);
229: } catch (Throwable t) {
230: fail(t.toString());
231: }
232: }
233:
234: public void testBufferSize() throws Exception {
235: int a = 10;
236: String b = "Bela";
237: MethodCall m = new MethodCall("foo", new Object[] {
238: new Integer(a), b }, new Class[] { int.class,
239: String.class });
240: ByteArrayOutputStream msg_data = new ByteArrayOutputStream();
241: ObjectOutputStream msg_out = new ObjectOutputStream(msg_data);
242: m.writeExternal(msg_out);
243: msg_out.flush();
244: msg_out.close();
245: byte[] data = msg_data.toByteArray();
246: ByteArrayInputStream msg_in_data = new ByteArrayInputStream(
247: data);
248: ObjectInputStream msg_in = new ObjectInputStream(msg_in_data);
249: MethodCall m2 = new MethodCall();
250: m2.readExternal(msg_in);
251: System.out.println(m2.getName());
252: System.out.println(m2.getArgs().length);
253: }
254:
255: //
256: // OLD
257: //
258:
259: public void testOLD() throws Throwable {
260:
261: MethodCall methodCall = new MethodCall("someMethod",
262: new Object[] { "abc" });
263:
264: Target target = new Target();
265: Object result = methodCall.invoke(target);
266: assertEquals("ABC", result);
267: }
268:
269: public void testInheritanceOLD() throws Throwable {
270:
271: MethodCall methodCall = new MethodCall("someMethod",
272: new Object[] { "abc" });
273:
274: TargetSubclass target = new TargetSubclass();
275: Object result = methodCall.invoke(target);
276: assertEquals("ABC", result);
277: }
278:
279: //
280: // METHOD
281: //
282:
283: public void testMETHOD() throws Throwable {
284:
285: Method method = Target.class.getMethod("someMethod",
286: new Class[] { String.class });
287: MethodCall methodCall = new MethodCall(method,
288: new Object[] { "abc" });
289:
290: Target target = new Target();
291: Object result = methodCall.invoke(target);
292: assertEquals("ABC", result);
293: }
294:
295: public void testInheritanceMETHOD() throws Throwable {
296:
297: Method method = Target.class.getMethod("someMethod",
298: new Class[] { String.class });
299: MethodCall methodCall = new MethodCall(method,
300: new Object[] { "abc" });
301:
302: TargetSubclass target = new TargetSubclass();
303: Object result = methodCall.invoke(target);
304: assertEquals("ABC", result);
305: }
306:
307: //
308: // TYPES
309: //
310:
311: public void testTYPES() throws Throwable {
312:
313: MethodCall methodCall = new MethodCall("someMethod",
314: new Object[] { "abc" }, new Class[] { String.class });
315:
316: Target target = new Target();
317: Object result = methodCall.invoke(target);
318: assertEquals("ABC", result);
319: }
320:
321: public void testInheritanceTYPES() throws Throwable {
322:
323: MethodCall methodCall = new MethodCall("someMethod",
324: new Object[] { "abc" }, new Class[] { String.class });
325:
326: TargetSubclass target = new TargetSubclass();
327: Object result = methodCall.invoke(target);
328: assertEquals("ABC", result);
329: }
330:
331: /**
332: * This tests whether overriden methods are correctly identified and invoked.
333: */
334: public void testOverriddenForTYPES() throws Throwable {
335:
336: MethodCall methodCall = new MethodCall("overriddenMethod",
337: new Object[] { "abc" }, new Class[] { String.class });
338:
339: TargetSubclass target = new TargetSubclass();
340: Object result = methodCall.invoke(target);
341: assertEquals("TargetSubclassABC", result);
342:
343: }
344:
345: public void testNoArgumentMethodForTYPES() throws Throwable {
346:
347: MethodCall methodCall = new MethodCall("noArgumentMethod",
348: new Object[0], new Class[0]);
349:
350: TargetSubclass target = new TargetSubclass();
351: Object result = methodCall.invoke(target);
352: assertEquals("noArgumentMethodResult", result);
353:
354: }
355:
356: //
357: // SIGNATURE
358: //
359:
360: public void testSIGNATURE() throws Throwable {
361:
362: MethodCall methodCall = new MethodCall("someMethod",
363: new Object[] { "abc" },
364: new String[] { "java.lang.String" });
365:
366: Target target = new Target();
367: Object result = methodCall.invoke(target);
368: assertEquals("ABC", result);
369: }
370:
371: public void testInheritanceSIGNATURE() throws Throwable {
372:
373: MethodCall methodCall = new MethodCall("someMethod",
374: new Object[] { "abc" },
375: new String[] { "java.lang.String" });
376:
377: TargetSubclass target = new TargetSubclass();
378: Object result = methodCall.invoke(target);
379: assertEquals("ABC", result);
380: }
381:
382: public void testMarshalling() throws Exception {
383: MethodCall methodCall = new MethodCall("someMethod",
384: new Object[] { "abc" },
385: new String[] { "java.lang.String" });
386: methodCall.put("name", "Bela");
387: methodCall.put("id", new Integer(322649));
388:
389: System.out.println("methodCall: " + methodCall);
390:
391: MethodCall m = marshalAndUnmarshal(methodCall);
392: System.out.println("m: " + m);
393: assertEquals(m.get("name"), "Bela");
394: assertEquals(m.get("id"), new Integer(322649));
395: }
396:
397: private MethodCall marshalAndUnmarshal(MethodCall m)
398: throws Exception {
399: byte[] buf = Util.objectToByteBuffer(m);
400: MethodCall retval = (MethodCall) Util.objectFromByteBuffer(buf);
401: return retval;
402: }
403:
404: public static Test suite() {
405: TestSuite s = new TestSuite(MethodCallTest.class);
406: return s;
407: }
408:
409: public static void main(String[] args) {
410: junit.textui.TestRunner.run(suite());
411: }
412:
413: public class Target {
414:
415: public String someMethod(String arg) {
416: return arg.toUpperCase();
417: }
418:
419: public String overriddenMethod(String arg) {
420: return "Target" + arg.toUpperCase();
421: }
422:
423: public String noArgumentMethod() {
424: return "noArgumentMethodResult";
425: }
426: }
427:
428: public class TargetSubclass extends Target {
429:
430: public String overriddenMethod(String arg) {
431: return "TargetSubclass" + arg.toUpperCase();
432: }
433:
434: }
435:
436: }
|