1 3 package org.jgroups.tests; 4 5 import junit.framework.Test; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import org.jgroups.blocks.MethodCall; 9 import org.jgroups.util.Util; 10 11 import java.io.ByteArrayInputStream ; 12 import java.io.ByteArrayOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.ObjectOutputStream ; 15 import java.lang.reflect.Method ; 16 17 18 23 24 public class MethodCallTest extends TestCase { 25 26 Class cl=MethodCallTest.class; 27 28 public MethodCallTest(String name) { 29 super(name); 30 } 31 32 33 34 35 public boolean foo(int a, String b) { 36 System.out.println("test(" + a + ", " + b + ')'); 37 return true; 38 } 39 40 41 public void bar(String [] a, String b) { 42 if(a != null) { 43 for(int i=0; i < a.length; i++) { 44 String s=a[i]; 45 System.out.print(s + ' '); 46 } 47 } 48 else 49 System.out.println("a=null"); 50 if(b != null) 51 System.out.println("b=" + b); 52 else 53 System.out.println("b=null"); 54 } 55 56 57 public void foobar() { 58 System.out.println("foobar()"); 59 } 60 61 62 public void testOld() { 63 try { 64 MethodCall mc=new MethodCall("foo", new Object []{new Integer (22), "Bela"}); 65 assertEquals(mc.invoke(this), Boolean.TRUE); 66 } 67 catch(Throwable t) { 68 fail(t.toString()); 69 } 70 } 71 72 public void testOld2() { 73 try { 74 MethodCall mc=new MethodCall("bar", new Object []{new String []{"one", "two", "three"}, "Bela"}); 75 mc.invoke(this); 76 } 77 catch(Throwable t) { 78 fail(t.toString()); 79 } 80 } 81 82 public void testWithNull() { 83 try { 84 MethodCall mc=new MethodCall("foobar", null, (Class [])null); 85 System.out.println("mc: " + mc); 86 mc.invoke(this); 87 } 88 catch(Throwable t) { 89 fail(t.toString()); 90 } 91 } 92 93 public void testOldWithNull() { 94 try { 95 MethodCall mc=new MethodCall("bar", new Object []{new String []{"one", "two", "three"}, null}); 96 mc.invoke(this); 97 } 98 catch(Throwable t) { 99 fail(t.toString()); 100 } 101 } 102 103 public void testOldWithNull2() { 104 try { 105 MethodCall mc=new MethodCall("bar", new Object []{null, "Bela"}); 106 mc.invoke(this); 107 } 108 catch(Throwable t) { 109 fail(t.toString()); 110 } 111 } 112 113 public void testOldWithNull3() { 114 try { 115 MethodCall mc=new MethodCall("foobar", null); 116 mc.invoke(this); 117 } 118 catch(Throwable t) { 119 fail(t.toString()); 120 } 121 } 122 123 public void testOldWithNull4() { 124 try { 125 MethodCall mc=new MethodCall("foobar", new Object [0]); 126 mc.invoke(this); 127 } 128 catch(Throwable t) { 129 fail(t.toString()); 130 } 131 } 132 133 134 135 136 public void testMethod() { 137 Method m; 138 try { 139 m=cl.getMethod("foo", new Class []{int.class, String .class}); 140 MethodCall mc=new MethodCall(m, new Object []{new Integer (22), "Bela"}); 141 assertEquals(mc.invoke(this), Boolean.TRUE); 142 } 143 catch(Throwable t) { 144 fail(t.toString()); 145 } 146 } 147 148 public void testTypes() { 149 MethodCall mc; 150 mc=new MethodCall("foo", new Object []{new Integer (35), "Bela"}, new Class []{int.class, String .class}); 151 try { 152 assertEquals(mc.invoke(this), Boolean.TRUE); 153 } 154 catch(Throwable t) { 155 fail(t.toString()); 156 } 157 } 158 159 160 public void testTypesWithArray() { 161 MethodCall mc; 162 mc=new MethodCall("bar", new Object []{new String []{"one", "two", "three"}, "Bela"}, 163 new Class []{String [].class, String .class}); 164 try { 165 mc.invoke(this); 166 } 167 catch(Throwable t) { 168 fail(t.toString()); 169 } 170 } 171 172 public void testTypesWithNullArgument() { 173 MethodCall mc; 174 mc=new MethodCall("bar", new Object []{new String []{"one", "two", "three"}, null}, 175 new Class []{String [].class, String .class}); 176 try { 177 mc.invoke(this); 178 } 179 catch(Throwable t) { 180 fail(t.toString()); 181 } 182 } 183 184 public void testTypesWithNullArgument2() { 185 MethodCall mc; 186 mc=new MethodCall("bar", new Object []{new String []{"one", "two", "three"}, new Object []{}}, 187 new Class []{String [].class, String .class}); 188 try { 189 mc.invoke(this); 190 } 191 catch(IllegalArgumentException ex) { 192 assertTrue("this was expected", true); 193 } 194 catch(Throwable t) { 195 fail(t.toString()); 196 } 197 } 198 199 public void testTypesWithNullArgument3() { 200 MethodCall mc; 201 mc=new MethodCall("foobar", new Object []{}, new Class []{}); 202 try { 203 mc.invoke(this); 204 } 205 catch(IllegalArgumentException ex) { 206 assertTrue("this was expected", true); 207 } 208 catch(Throwable t) { 209 fail(t.toString()); 210 } 211 } 212 213 public void testTypesWithNullArgument4() { 214 MethodCall mc; 215 mc=new MethodCall("foobar", (Object [])null, (Class [])null); 216 try { 217 mc.invoke(this); 218 } 219 catch(IllegalArgumentException ex) { 220 assertTrue("this was expected", true); 221 } 222 catch(Throwable t) { 223 fail(t.toString()); 224 } 225 } 226 227 public void testTypesWithNullArgument5() { 228 MethodCall mc; 229 mc=new MethodCall("foobar", new Object [0], new Class [0]); 230 try { 231 mc.invoke(this); 232 } 233 catch(IllegalArgumentException ex) { 234 assertTrue("this was expected", true); 235 } 236 catch(Throwable t) { 237 fail(t.toString()); 238 } 239 } 240 241 242 public void testSignature() { 243 MethodCall mc; 244 mc=new MethodCall("foo", new Object []{new Integer (35), "Bela"}, 245 new String []{int.class.getName(), String .class.getName()}); 246 try { 247 assertEquals(mc.invoke(this), Boolean.TRUE); 248 } 249 catch(Throwable t) { 250 fail(t.toString()); 251 } 252 } 253 254 255 public void testBufferSize() throws Exception { 256 int a=10; 257 String b="Bela"; 258 MethodCall m=new MethodCall("foo", new Object []{new Integer (a),b}, new Class []{int.class, String .class}); 259 ByteArrayOutputStream msg_data=new ByteArrayOutputStream (); 260 ObjectOutputStream msg_out=new ObjectOutputStream (msg_data); 261 m.writeExternal(msg_out); 262 msg_out.flush(); 263 msg_out.close(); 264 byte[] data=msg_data.toByteArray(); 265 ByteArrayInputStream msg_in_data=new ByteArrayInputStream (data); 266 ObjectInputStream msg_in=new ObjectInputStream (msg_in_data); 267 MethodCall m2=new MethodCall(); 268 m2.readExternal(msg_in); 269 System.out.println(m2.getName()); 270 System.out.println(m2.getArgs().length); 271 } 272 273 277 public void testOLD() throws Throwable { 278 279 MethodCall methodCall = new MethodCall("someMethod", new Object [] {"abc"}); 280 281 Target target = new Target(); 282 Object result = methodCall.invoke(target); 283 assertEquals("ABC", result); 284 } 285 286 public void testInheritanceOLD() throws Throwable { 287 288 MethodCall methodCall = new MethodCall("someMethod", new Object [] {"abc"}); 289 290 TargetSubclass target = new TargetSubclass(); 291 Object result = methodCall.invoke(target); 292 assertEquals("ABC", result); 293 } 294 295 299 public void testMETHOD() throws Throwable { 300 301 Method method = Target.class.getMethod("someMethod", new Class [] { String .class }); 302 MethodCall methodCall = new MethodCall(method, new Object [] {"abc"}); 303 304 Target target = new Target(); 305 Object result = methodCall.invoke(target); 306 assertEquals("ABC", result); 307 } 308 309 public void testInheritanceMETHOD() throws Throwable { 310 311 Method method = Target.class.getMethod("someMethod", new Class [] { String .class }); 312 MethodCall methodCall = new MethodCall(method, new Object [] {"abc"}); 313 314 TargetSubclass target = new TargetSubclass(); 315 Object result = methodCall.invoke(target); 316 assertEquals("ABC", result); 317 } 318 319 323 public void testTYPES() throws Throwable { 324 325 MethodCall methodCall = new MethodCall("someMethod", 326 new Object [] { "abc" }, 327 new Class [] { String .class }); 328 329 Target target = new Target(); 330 Object result = methodCall.invoke(target); 331 assertEquals("ABC", result); 332 } 333 334 public void testInheritanceTYPES() throws Throwable { 335 336 MethodCall methodCall = new MethodCall("someMethod", 337 new Object [] { "abc" }, 338 new Class [] { String .class }); 339 340 TargetSubclass target = new TargetSubclass(); 341 Object result = methodCall.invoke(target); 342 assertEquals("ABC", result); 343 } 344 345 348 public void testOverriddenForTYPES() throws Throwable { 349 350 MethodCall methodCall = new MethodCall("overriddenMethod", 351 new Object [] { "abc" }, 352 new Class [] { String .class }); 353 354 TargetSubclass target = new TargetSubclass(); 355 Object result = methodCall.invoke(target); 356 assertEquals("TargetSubclassABC", result); 357 358 } 359 360 public void testNoArgumentMethodForTYPES() throws Throwable { 361 362 MethodCall methodCall = new MethodCall("noArgumentMethod", new Object [0], new Class [0]); 363 364 TargetSubclass target = new TargetSubclass(); 365 Object result = methodCall.invoke(target); 366 assertEquals("noArgumentMethodResult", result); 367 368 } 369 370 371 375 public void testSIGNATURE() throws Throwable { 376 377 MethodCall methodCall = new MethodCall("someMethod", 378 new Object [] { "abc" }, 379 new String [] { "java.lang.String" }); 380 381 Target target = new Target(); 382 Object result = methodCall.invoke(target); 383 assertEquals("ABC", result); 384 } 385 386 public void testInheritanceSIGNATURE() throws Throwable { 387 388 MethodCall methodCall = new MethodCall("someMethod", 389 new Object [] { "abc" }, 390 new String [] { "java.lang.String" }); 391 392 TargetSubclass target = new TargetSubclass(); 393 Object result = methodCall.invoke(target); 394 assertEquals("ABC", result); 395 } 396 397 398 public void testMarshalling() throws Exception { 399 MethodCall methodCall = new MethodCall("someMethod", 400 new Object [] { "abc" }, 401 new String [] { "java.lang.String" }); 402 methodCall.put("name", "Bela"); 403 methodCall.put("id", new Integer (322649)); 404 405 System.out.println("methodCall: " + methodCall); 406 407 MethodCall m=marshalAndUnmarshal(methodCall); 408 System.out.println("m: " + m); 409 assertEquals(m.get("name"), "Bela"); 410 assertEquals(m.get("id"), new Integer (322649)); 411 } 412 413 414 private MethodCall marshalAndUnmarshal(MethodCall m) throws Exception { 415 byte[] buf=Util.objectToByteBuffer(m); 416 MethodCall retval=(MethodCall)Util.objectFromByteBuffer(buf); 417 return retval; 418 } 419 420 421 public static Test suite() { 422 TestSuite s=new TestSuite(MethodCallTest.class); 423 return s; 424 } 425 426 public static void main(String [] args) { 427 junit.textui.TestRunner.run(suite()); 428 } 429 430 public class Target { 431 432 public String someMethod(String arg) { 433 return arg.toUpperCase(); 434 } 435 436 public String overriddenMethod(String arg) { 437 return "Target" + arg.toUpperCase(); 438 } 439 440 public String noArgumentMethod() { 441 return "noArgumentMethodResult"; 442 } 443 } 444 445 public class TargetSubclass extends Target { 446 447 public String overriddenMethod(String arg) { 448 return "TargetSubclass" + arg.toUpperCase(); 449 } 450 451 } 452 453 454 } 455 | Popular Tags |