MethodTest.java revision af12a6637be4cbb9366cd88cecd22cfc82087086
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.lang.reflect; 19 20import java.lang.annotation.Annotation; 21import java.lang.annotation.ElementType; 22import java.lang.annotation.Retention; 23import java.lang.annotation.RetentionPolicy; 24import java.lang.annotation.Target; 25import java.lang.reflect.InvocationTargetException; 26import java.lang.reflect.Method; 27import java.lang.reflect.Modifier; 28import java.lang.reflect.Type; 29import java.lang.reflect.TypeVariable; 30import java.util.HashSet; 31import java.util.Set; 32 33import dalvik.annotation.TestLevel; 34import dalvik.annotation.TestTargetClass; 35import dalvik.annotation.TestTargetNew; 36 37@TestTargetClass(Method.class) 38public class MethodTest extends junit.framework.TestCase { 39 40 static class TestMethod { 41 public TestMethod() { 42 } 43 44 public void voidMethod() throws IllegalArgumentException { 45 } 46 47 public void parmTest(int x, short y, String s, boolean bool, Object o, 48 long l, byte b, char c, double d, float f) { 49 } 50 51 public int intMethod() { 52 return 1; 53 } 54 55 public static final void printTest(int x, short y, String s, 56 boolean bool, Object o, long l, byte b, char c, double d, 57 float f) { 58 } 59 60 public double doubleMethod() { 61 return 1.0; 62 } 63 64 public short shortMethod() { 65 return (short) 1; 66 } 67 68 public byte byteMethod() { 69 return (byte) 1; 70 } 71 72 public float floatMethod() { 73 return 1.0f; 74 } 75 76 public long longMethod() { 77 return 1l; 78 } 79 80 public char charMethod() { 81 return 'T'; 82 } 83 84 public Object objectMethod() { 85 return new Object(); 86 } 87 88 private static void prstatic() { 89 } 90 91 public static void pustatic() { 92 } 93 94 public static synchronized void pustatsynch() { 95 } 96 97 public static int invokeStaticTest() { 98 return 1; 99 } 100 101 public int invokeInstanceTest() { 102 return 1; 103 } 104 105 private int privateInvokeTest() { 106 return 1; 107 } 108 109 public int invokeExceptionTest() throws NullPointerException { 110 throw new NullPointerException(); 111 } 112 113 public static synchronized native void pustatsynchnat(); 114 115 public void publicVoidVarargs(Object... param){} 116 public void publicVoidArray(Object[] param){} 117 118 public void annotatedParameter(@TestAnno @Deprecated int a, 119 @Deprecated int b, int c) { 120 } 121 122 @Deprecated 123 @TestAnno 124 public void annotatedMethod(){} 125 126 public void hashCodeTest(int i){} 127 public void hashCodeTest(String s){} 128 129 public void invokeCastTest1(byte param) { 130 } 131 132 public void invokeCastTest1(short param) { 133 } 134 135 public void invokeCastTest1(int param) { 136 } 137 138 public void invokeCastTest1(long param) { 139 } 140 141 public void invokeCastTest1(float param) { 142 } 143 144 public void invokeCastTest1(double param) { 145 } 146 147 public void invokeCastTest1(char param) { 148 } 149 150 public void invokeCastTest1(boolean param) { 151 } 152 } 153 154 @Retention(RetentionPolicy.RUNTIME) 155 @Target({ElementType.PARAMETER, ElementType.METHOD}) 156 public static @interface TestAnno{ 157 public static final String DEFAULT_VALUE = "DEFAULT_VALUE"; 158 159 String value() default DEFAULT_VALUE; 160 } 161 162 abstract class AbstractTestMethod { 163 public abstract void puabs(); 164 } 165 166 class TestMethodSub extends TestMethod { 167 public int invokeInstanceTest() { 168 return 0; 169 } 170 } 171 172 static interface IBrigeTest<T>{ 173 T m(); 174 } 175 176 static class BrigeTest implements IBrigeTest<String> { 177 public String m(){ return null; } 178 } 179 180 static class ExceptionTest<T extends Exception>{ 181 @SuppressWarnings("unused") 182 void exceptionTest() throws T{} 183 } 184 185 static class GenericReturnType<T> { 186 T returnGeneric(){return null;} 187 } 188 189 static class GenericString<T> { 190 public static final String GENERIC = 191 "T tests.api.java.lang.reflect.MethodTest$GenericString.genericString(T)"; 192 T genericString(T t) { 193 return null; 194 } 195 } 196 197 /** 198 * @tests java.lang.reflect.Method#equals(java.lang.Object) 199 */ 200 @TestTargetNew( 201 level = TestLevel.COMPLETE, 202 notes = "", 203 method = "equals", 204 args = {java.lang.Object.class} 205 ) 206 public void test_equalsLjava_lang_Object() { 207 // Test for method boolean 208 // java.lang.reflect.Method.equals(java.lang.Object) 209 210 Method m1 = null, m2 = null; 211 try { 212 m1 = TestMethod.class.getMethod("invokeInstanceTest", new Class[0]); 213 m2 = TestMethodSub.class.getMethod("invokeInstanceTest", 214 new Class[0]); 215 } catch (Exception e) { 216 fail("Exception during equals test : " + e.getMessage()); 217 } 218 assertTrue("Overriden method returned equal", !m1.equals(m2)); 219 assertTrue("Same method returned not-equal", m1.equals(m1)); 220 try { 221 m1 = TestMethod.class.getMethod("invokeStaticTest", new Class[0]); 222 m2 = TestMethodSub.class 223 .getMethod("invokeStaticTest", new Class[0]); 224 } catch (Exception e) { 225 fail("Exception during equals test : " + e.getMessage()); 226 } 227 assertTrue("Inherited method returned not-equal", m1.equals(m2)); 228 } 229 230 /** 231 * @tests java.lang.Class#getMethod(java.lang.String, java.lang.Class[]) 232 */ 233 @TestTargetNew( 234 level = TestLevel.COMPLETE, 235 notes = "", 236 method = "getMethod", 237 args = {java.lang.String.class, java.lang.Class[].class}, 238 clazz = java.lang.Class.class 239 ) 240 public void test_getMethod() throws NoSuchMethodException, SecurityException { 241 // Check that getMethod treats null parameterTypes the same as an empty array. 242 Method m1 = TestMethod.class.getMethod("invokeInstanceTest", new Class[0]); 243 Method m2 = TestMethod.class.getMethod("invokeInstanceTest", (Class[]) null); 244 assertEquals(m1, m2); 245 } 246 247 /** 248 * @tests java.lang.Class#getDeclaredMethod(java.lang.String, java.lang.Class[]) 249 */ 250 @TestTargetNew( 251 level = TestLevel.COMPLETE, 252 notes = "", 253 method = "getDeclaredMethod", 254 args = {java.lang.String.class, java.lang.Class[].class}, 255 clazz = java.lang.Class.class 256 ) 257 public void test_getDeclaredMethod() throws NoSuchMethodException, SecurityException { 258 // Check that getDeclaredMethod treats null parameterTypes the same as an empty array. 259 Method m1 = TestMethod.class.getDeclaredMethod("invokeInstanceTest", new Class[0]); 260 Method m2 = TestMethod.class.getDeclaredMethod("invokeInstanceTest", (Class[]) null); 261 assertEquals(m1, m2); 262 } 263 264 /** 265 * @tests java.lang.reflect.Method#getDeclaringClass() 266 */ 267 @TestTargetNew( 268 level = TestLevel.COMPLETE, 269 notes = "", 270 method = "getDeclaringClass", 271 args = {} 272 ) 273 public void test_getDeclaringClass() { 274 // Test for method java.lang.Class 275 // java.lang.reflect.Method.getDeclaringClass() 276 277 Method[] mths; 278 279 try { 280 mths = TestMethod.class.getDeclaredMethods(); 281 assertTrue("Returned incorrect declaring class: " 282 + mths[0].getDeclaringClass().toString(), mths[0] 283 .getDeclaringClass().equals(TestMethod.class)); 284 } catch (Exception e) { 285 fail("Exception during getDeclaringClass test: " 286 + e.toString()); 287 } 288 } 289 290 /** 291 * @tests java.lang.reflect.Method#getExceptionTypes() 292 */ 293 @TestTargetNew( 294 level = TestLevel.COMPLETE, 295 notes = "", 296 method = "getExceptionTypes", 297 args = {} 298 ) 299 public void test_getExceptionTypes() { 300 // Test for method java.lang.Class [] 301 // java.lang.reflect.Method.getExceptionTypes() 302 303 try { 304 Method mth = TestMethod.class.getMethod("voidMethod", new Class[0]); 305 Class[] ex = mth.getExceptionTypes(); 306 assertEquals("Returned incorrect number of exceptions", 307 1, ex.length); 308 assertTrue("Returned incorrect exception type", ex[0] 309 .equals(IllegalArgumentException.class)); 310 mth = TestMethod.class.getMethod("intMethod", new Class[0]); 311 ex = mth.getExceptionTypes(); 312 assertEquals("Returned incorrect number of exceptions", 313 0, ex.length); 314 } catch (Exception e) { 315 fail("Exception during getExceptionTypes: " + e.toString()); 316 } 317 318 } 319 320 /** 321 * @tests java.lang.reflect.Method#getModifiers() 322 */ 323 @TestTargetNew( 324 level = TestLevel.COMPLETE, 325 notes = "", 326 method = "getModifiers", 327 args = {} 328 ) 329 public void test_getModifiers() { 330 // Test for method int java.lang.reflect.Method.getModifiers() 331 332 Class cl = TestMethod.class; 333 int mods = 0; 334 Method mth = null; 335 int mask = 0; 336 try { 337 mth = cl.getMethod("pustatic", new Class[0]); 338 mods = mth.getModifiers(); 339 } catch (Exception e) { 340 fail("Exception during getModfiers test: " + e.toString()); 341 } 342 mask = Modifier.PUBLIC | Modifier.STATIC; 343 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 344 try { 345 mth = cl.getDeclaredMethod("prstatic", new Class[0]); 346 mods = mth.getModifiers(); 347 } catch (Exception e) { 348 fail("Exception during getModfiers test: " + e.toString()); 349 } 350 mask = Modifier.PRIVATE | Modifier.STATIC; 351 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 352 try { 353 mth = cl.getDeclaredMethod("pustatsynch", new Class[0]); 354 mods = mth.getModifiers(); 355 } catch (Exception e) { 356 fail("Exception during getModfiers test: " + e.toString()); 357 } 358 mask = (Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED; 359 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 360 try { 361 mth = cl.getDeclaredMethod("pustatsynchnat", new Class[0]); 362 mods = mth.getModifiers(); 363 } catch (Exception e) { 364 fail("Exception during getModfiers test: " + e.toString()); 365 } 366 mask = ((Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED) 367 | Modifier.NATIVE; 368 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 369 cl = AbstractTestMethod.class; 370 try { 371 mth = cl.getDeclaredMethod("puabs", new Class[0]); 372 mods = mth.getModifiers(); 373 } catch (Exception e) { 374 fail("Exception during getModfiers test: " + e.toString()); 375 } 376 mask = Modifier.PUBLIC | Modifier.ABSTRACT; 377 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 378 } 379 380 /** 381 * @tests java.lang.reflect.Method#getName() 382 */ 383 @TestTargetNew( 384 level = TestLevel.COMPLETE, 385 notes = "", 386 method = "getName", 387 args = {} 388 ) 389 public void test_getName() { 390 // Test for method java.lang.String java.lang.reflect.Method.getName() 391 Method mth = null; 392 try { 393 mth = TestMethod.class.getMethod("voidMethod", new Class[0]); 394 } catch (Exception e) { 395 fail("Exception during getMethodName(): " + e.toString()); 396 } 397 assertEquals("Returned incorrect method name", 398 "voidMethod", mth.getName()); 399 } 400 401 /** 402 * @tests java.lang.reflect.Method#isVarArgs() 403 */ 404 @TestTargetNew( 405 level = TestLevel.COMPLETE, 406 notes = "", 407 method = "isVarArgs", 408 args = {} 409 ) 410 public void test_isVarArgs() throws Exception { 411 Method mth = TestMethod.class.getMethod("publicVoidVarargs", 412 Object[].class); 413 assertTrue("Varargs method stated as non vararg.", mth.isVarArgs()); 414 415 mth = TestMethod.class.getDeclaredMethod("publicVoidArray", 416 Object[].class); 417 assertFalse("Non varargs method stated as vararg.", mth.isVarArgs()); 418 } 419 420 /** 421 * @tests java.lang.reflect.Method#isBridge() 422 */ 423 @TestTargetNew( 424 level = TestLevel.COMPLETE, 425 notes = "", 426 method = "isBridge", 427 args = {} 428 ) 429 public void test_isBridge() throws Exception { 430 Method[] declaredMethods = BrigeTest.class.getDeclaredMethods(); 431 assertEquals("Bridge method not generated.", 2, declaredMethods.length); 432 boolean foundBridgeMethod = false; 433 for (Method method : declaredMethods) { 434 if (method.getReturnType().equals(Object.class)) { 435 assertTrue("Bridge method not stated as bridge.", method 436 .isBridge()); 437 foundBridgeMethod = true; 438 } 439 } 440 assertTrue("Bridge method not found.", foundBridgeMethod); 441 } 442 443 /** 444 * @tests java.lang.reflect.Method#isSynthetic() 445 */ 446 @TestTargetNew( 447 level = TestLevel.COMPLETE, 448 notes = "", 449 method = "isSynthetic", 450 args = {} 451 ) 452 public void test_isSynthetic() throws Exception { 453 Method[] declaredMethods = BrigeTest.class.getDeclaredMethods(); 454 assertEquals("Synthetic method not generated.", 2, 455 declaredMethods.length); 456 boolean foundSyntheticMethod = false; 457 for (Method method : declaredMethods) { 458 if (method.getReturnType().equals(Object.class)) { 459 assertTrue("Synthetic method not stated as synthetic.", method 460 .isSynthetic()); 461 foundSyntheticMethod = true; 462 } 463 } 464 assertTrue("Synthetic method not found.", foundSyntheticMethod); 465 } 466 /** 467 * @tests java.lang.reflect.Method#getParameterAnnotations() 468 */ 469 @TestTargetNew( 470 level = TestLevel.COMPLETE, 471 notes = "", 472 method = "getParameterAnnotations", 473 args = {} 474 ) 475 public void test_getParameterAnnotations() throws Exception { 476 Method method = TestMethod.class.getDeclaredMethod( 477 "annotatedParameter", new Class[] { 478 int.class, int.class, int.class}); 479 Annotation[][] annotations = method.getParameterAnnotations(); 480 assertEquals(3, annotations.length); 481 assertEquals( 482 "Wrong number of annotations returned for first parameter", 2, 483 annotations[0].length); 484 Set<Class<?>> annotationSet = new HashSet<Class<?>>(); 485 annotationSet.add(annotations[0][0].annotationType()); 486 annotationSet.add(annotations[0][1].annotationType()); 487 assertTrue("Missing TestAnno annotation", annotationSet 488 .contains(TestAnno.class)); 489 assertTrue("Missing Deprecated annotation", annotationSet 490 .contains(Deprecated.class)); 491 492 assertEquals( 493 "Wrong number of annotations returned for second parameter", 494 1, annotations[1].length); 495 annotationSet = new HashSet<Class<?>>(); 496 annotationSet.add(annotations[1][0].annotationType()); 497 assertTrue("Missing Deprecated annotation", annotationSet 498 .contains(Deprecated.class)); 499 assertEquals( 500 "Wrong number of annotations returned for third parameter", 0, 501 annotations[2].length); 502 } 503 504 /** 505 * @tests java.lang.reflect.Method#getDeclaredAnnotations() 506 */ 507 @TestTargetNew( 508 level = TestLevel.COMPLETE, 509 notes = "", 510 method = "getDeclaredAnnotations", 511 args = {} 512 ) 513 public void test_getDeclaredAnnotations() throws Exception { 514 Method method = TestMethod.class.getDeclaredMethod("annotatedMethod"); 515 Annotation[] declaredAnnotations = method.getDeclaredAnnotations(); 516 assertEquals(2, declaredAnnotations.length); 517 518 Set<Class<?>> annotationSet = new HashSet<Class<?>>(); 519 annotationSet.add(declaredAnnotations[0].annotationType()); 520 annotationSet.add(declaredAnnotations[1].annotationType()); 521 assertTrue("Missing TestAnno annotation", annotationSet 522 .contains(TestAnno.class)); 523 assertTrue("Missing Deprecated annotation", annotationSet 524 .contains(Deprecated.class)); 525 } 526 527 /** 528 * @tests java.lang.reflect.Method#getDefaultValue() 529 */ 530 @TestTargetNew( 531 level = TestLevel.SUFFICIENT, 532 notes = "Missing tests for TypeNotPresentException", 533 method = "getDefaultValue", 534 args = {} 535 ) 536 public void test_getDefaultValue() throws Exception { 537 Method method = TestAnno.class.getDeclaredMethod("value"); 538 assertEquals("Wrong default value returned", TestAnno.DEFAULT_VALUE, 539 method.getDefaultValue()); 540 } 541 542 /** 543 * @tests java.lang.reflect.Method#getDefaultValue() 544 */ 545 @TestTargetNew( 546 level = TestLevel.SUFFICIENT, 547 notes = "Missing tests for GenericSignatureFormatError,TypeNotPresentException, MalformedParameterizedTypeException", 548 method = "getGenericExceptionTypes", 549 args = {} 550 ) 551 public void test_getGenericExceptionTypes() throws Exception { 552 Method method = ExceptionTest.class.getDeclaredMethod("exceptionTest"); 553 Type[] genericExceptionTypes = method.getGenericExceptionTypes(); 554 assertEquals(1, genericExceptionTypes.length); 555 assertTrue(genericExceptionTypes[0] instanceof TypeVariable<?>); 556 @SuppressWarnings("unchecked") 557 TypeVariable<Class<ExceptionTest<?>>> tv = 558 (TypeVariable<Class<ExceptionTest<?>>>) genericExceptionTypes[0]; 559 assertEquals("T", tv.getName()); 560 } 561 562 /** 563 * @tests java.lang.reflect.Method#getGenericReturnType() 564 */ 565 @TestTargetNew( 566 level = TestLevel.SUFFICIENT, 567 notes = "Missing tests for GenericSignatureFormatError,TypeNotPresentException, MalformedParameterizedTypeException", 568 method = "getGenericReturnType", 569 args = {} 570 ) 571 public void test_getGenericReturnType() throws Exception { 572 Method method = GenericReturnType.class 573 .getDeclaredMethod("returnGeneric"); 574 Type returnType = method.getGenericReturnType(); 575 assertNotNull("getGenericReturnType returned null", returnType); 576 assertTrue(returnType instanceof TypeVariable<?>); 577 @SuppressWarnings("unchecked") 578 TypeVariable<Class<ExceptionTest<?>>> tv = 579 (TypeVariable<Class<ExceptionTest<?>>>) returnType; 580 assertEquals("T", tv.getName()); 581 } 582 583 584 /** 585 * @tests java.lang.reflect.Method#toGenericString() 586 */ 587 @TestTargetNew( 588 level = TestLevel.COMPLETE, 589 notes = "", 590 method = "toGenericString", 591 args = {} 592 ) 593 public void test_toGenericString() throws Exception { 594 Method method = GenericString.class.getDeclaredMethod("genericString", 595 Object.class); 596 assertEquals("Wrong generic String returned", GenericString.GENERIC, 597 method.toGenericString()); 598 } 599 600 601 602 603 604 605 /** 606 * @tests java.lang.reflect.Method#hashCode() 607 */ 608 @TestTargetNew( 609 level = TestLevel.COMPLETE, 610 notes = "", 611 method = "hashCode", 612 args = {} 613 ) 614 public void test_hashCode() throws Exception { 615 Method mth0 = TestMethod.class.getMethod("hashCodeTest", String.class); 616 Method mth1 = TestMethod.class.getDeclaredMethod("hashCodeTest", 617 int.class); 618 assertEquals("Methods with same name did not return same hashCode.", 619 mth0.hashCode(), mth1.hashCode()); 620 } 621 622 /** 623 * @tests java.lang.reflect.Method#getParameterTypes() 624 */ 625 @TestTargetNew( 626 level = TestLevel.COMPLETE, 627 notes = "", 628 method = "getParameterTypes", 629 args = {} 630 ) 631 public void test_getParameterTypes() { 632 // Test for method java.lang.Class [] 633 // java.lang.reflect.Method.getParameterTypes() 634 Class cl = TestMethod.class; 635 Method mth = null; 636 Class[] parms = null; 637 Method[] methods = null; 638 Class[] plist = { int.class, short.class, String.class, boolean.class, 639 Object.class, long.class, byte.class, char.class, double.class, 640 float.class }; 641 try { 642 mth = cl.getMethod("voidMethod", new Class[0]); 643 parms = mth.getParameterTypes(); 644 } catch (Exception e) { 645 fail("Exception during getParameterTypes test: " 646 + e.toString()); 647 } 648 assertEquals("Returned incorrect parameterTypes", 0, parms.length); 649 try { 650 mth = cl.getMethod("parmTest", plist); 651 parms = mth.getParameterTypes(); 652 } catch (Exception e) { 653 fail("Exception during getParameterTypes test: " 654 + e.toString()); 655 } 656 assertTrue("Invalid number of parameters returned", 657 plist.length == parms.length); 658 for (int i = 0; i < plist.length; i++) 659 assertTrue("Incorrect parameter returned", plist[i] 660 .equals(parms[i])); 661 662 // Test same method. but this time pull it from the list of methods 663 // rather than asking for it explicitly 664 methods = cl.getDeclaredMethods(); 665 666 int i; 667 for (i = 0; i < methods.length; i++) 668 if (methods[i].getName().equals("parmTest")) { 669 mth = methods[i]; 670 i = methods.length + 1; 671 } 672 if (i < methods.length) { 673 parms = mth.getParameterTypes(); 674 assertTrue("Incorrect number of parameters returned", 675 parms.length == plist.length); 676 for (i = 0; i < plist.length; i++) 677 assertTrue("Incorrect parameter returned", plist[i] 678 .equals(parms[i])); 679 } 680 } 681 682 /** 683 * @tests java.lang.reflect.Method#getReturnType() 684 */ 685 @TestTargetNew( 686 level = TestLevel.COMPLETE, 687 notes = "", 688 method = "getReturnType", 689 args = {} 690 ) 691 public void test_getReturnType() { 692 // Test for method java.lang.Class 693 // java.lang.reflect.Method.getReturnType() 694 Class cl = TestMethod.class; 695 Method mth = null; 696 try { 697 mth = cl.getMethod("charMethod", new Class[0]); 698 } catch (Exception e) { 699 fail("Exception during getReturnType test : " + e.getMessage()); 700 } 701 assertTrue("Gave incorrect returne type, wanted char", mth 702 .getReturnType().equals(char.class)); 703 try { 704 mth = cl.getMethod("longMethod", new Class[0]); 705 } catch (Exception e) { 706 fail("Exception during getReturnType test : " + e.getMessage()); 707 } 708 assertTrue("Gave incorrect returne type, wanted long", mth 709 .getReturnType().equals(long.class)); 710 try { 711 mth = cl.getMethod("shortMethod", new Class[0]); 712 } catch (Exception e) { 713 fail("Exception during getReturnType test : " + e.getMessage()); 714 } 715 assertTrue("Gave incorrect returne type, wanted short", mth 716 .getReturnType().equals(short.class)); 717 try { 718 mth = cl.getMethod("intMethod", new Class[0]); 719 } catch (Exception e) { 720 fail("Exception during getReturnType test : " + e.getMessage()); 721 } 722 assertTrue("Gave incorrect returne type, wanted int: " 723 + mth.getReturnType(), mth.getReturnType().equals(int.class)); 724 try { 725 mth = cl.getMethod("doubleMethod", new Class[0]); 726 } catch (Exception e) { 727 fail("Exception during getReturnType test : " + e.getMessage()); 728 } 729 assertTrue("Gave incorrect returne type, wanted double", mth 730 .getReturnType().equals(double.class)); 731 try { 732 mth = cl.getMethod("byteMethod", new Class[0]); 733 } catch (Exception e) { 734 fail("Exception during getReturnType test : " + e.getMessage()); 735 } 736 assertTrue("Gave incorrect returne type, wanted byte", mth 737 .getReturnType().equals(byte.class)); 738 try { 739 mth = cl.getMethod("byteMethod", new Class[0]); 740 } catch (Exception e) { 741 fail("Exception during getReturnType test:" + e.toString()); 742 } 743 assertTrue("Gave incorrect returne type, wanted byte", mth 744 .getReturnType().equals(byte.class)); 745 try { 746 mth = cl.getMethod("objectMethod", new Class[0]); 747 } catch (Exception e) { 748 fail("Exception during getReturnType test : " + e.getMessage()); 749 } 750 assertTrue("Gave incorrect returne type, wanted Object", mth 751 .getReturnType().equals(Object.class)); 752 753 try { 754 mth = cl.getMethod("voidMethod", new Class[0]); 755 } catch (Exception e) { 756 fail("Exception during getReturnType test : " + e.getMessage()); 757 } 758 assertTrue("Gave incorrect returne type, wanted void", mth 759 .getReturnType().equals(void.class)); 760 } 761 762 /** 763 * @tests java.lang.reflect.Method#invoke(java.lang.Object, 764 * java.lang.Object[]) 765 */ 766 @TestTargetNew( 767 level = TestLevel.COMPLETE, 768 notes = "", 769 method = "invoke", 770 args = {java.lang.Object.class, java.lang.Object[].class} 771 ) 772 public void test_invokeLjava_lang_Object$Ljava_lang_Object() throws Exception{ 773 // Test for method java.lang.Object 774 // java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object 775 // []) 776 Class cl = TestMethod.class; 777 Class[] dcl = new Class[0]; 778 779 // Get and invoke a static method 780 Method mth = cl.getDeclaredMethod("invokeStaticTest", dcl); 781 Object ret = mth.invoke(null, new Object[0]); 782 assertEquals("Invoke returned incorrect value", 1, ((Integer) ret) 783 .intValue()); 784 785 // Get and invoke an instance method 786 mth = cl.getDeclaredMethod("invokeInstanceTest", dcl); 787 ret = mth.invoke(new TestMethod(), new Object[0]); 788 assertEquals("Invoke returned incorrect value", 1, ((Integer) ret) 789 .intValue()); 790 791 // Get and attempt to invoke a private method 792 mth = cl.getDeclaredMethod("privateInvokeTest", dcl); 793 try { 794 ret = mth.invoke(new TestMethod(), new Object[0]); 795 } catch (IllegalAccessException e) { 796 // Correct behaviour 797 } catch (Exception e) { 798 fail("Exception during invoke test : " + e.getMessage()); 799 } 800 // Generate an IllegalArgumentException 801 mth = cl.getDeclaredMethod("invokeInstanceTest", dcl); 802 803 try { 804 Object[] args = { Object.class }; 805 ret = mth.invoke(new TestMethod(), args); 806 } catch (IllegalArgumentException e) { 807 // Correct behaviour 808 } catch (Exception e) { 809 fail("Exception during invoke test : " + e.getMessage()); 810 } 811 812 // Generate a NullPointerException 813 mth = cl.getDeclaredMethod("invokeInstanceTest", dcl); 814 815 try { 816 ret = mth.invoke(null, new Object[0]); 817 } catch (NullPointerException e) { 818 // Correct behaviour 819 } catch (Exception e) { 820 fail("Exception during invoke test : " + e.getMessage()); 821 } 822 823 // Generate an InvocationTargetException 824 mth = cl.getDeclaredMethod("invokeExceptionTest", dcl); 825 try { 826 ret = mth.invoke(new TestMethod(), new Object[0]); 827 } catch (InvocationTargetException e) { 828 // Correct behaviour 829 } catch (Exception e) { 830 fail("Exception during invoke test : " + e.getMessage()); 831 } 832 833 TestMethod testMethod = new TestMethod(); 834 Method methods[] = cl.getMethods(); 835 for (int i = 0; i < methods.length; i++) { 836 if (methods[i].getName().startsWith("invokeCastTest1")) { 837 Class param = methods[i].getParameterTypes()[0]; 838 839 try { 840 methods[i].invoke(testMethod, new Object[] { new Byte( 841 (byte) 1) }); 842 assertTrue("invalid invoke with Byte: " + methods[i], 843 param == Byte.TYPE || param == Short.TYPE 844 || param == Integer.TYPE 845 || param == Long.TYPE 846 || param == Float.TYPE 847 || param == Double.TYPE); 848 } catch (Exception e) { 849 assertTrue("Byte invalid exception: " + e, 850 e instanceof IllegalArgumentException); 851 assertTrue("Byte invalid failure: " + methods[i], 852 param == Boolean.TYPE || param == Character.TYPE); 853 } 854 855 try { 856 methods[i].invoke(testMethod, new Object[] { new Short( 857 (short) 1) }); 858 assertTrue("invalid invoke with Short: " + methods[i], 859 param == Short.TYPE || param == Integer.TYPE 860 || param == Long.TYPE 861 || param == Float.TYPE 862 || param == Double.TYPE); 863 } catch (Exception e) { 864 assertTrue("Short invalid exception: " + e, 865 e instanceof IllegalArgumentException); 866 assertTrue("Short invalid failure: " + methods[i], 867 param == Byte.TYPE || param == Boolean.TYPE 868 || param == Character.TYPE); 869 } 870 871 try { 872 methods[i].invoke(testMethod, 873 new Object[] { new Integer(1) }); 874 assertTrue("invalid invoke with Integer: " + methods[i], 875 param == Integer.TYPE || param == Long.TYPE 876 || param == Float.TYPE 877 || param == Double.TYPE); 878 } catch (Exception e) { 879 assertTrue("Integer invalid exception: " + e, 880 e instanceof IllegalArgumentException); 881 assertTrue("Integer invalid failure: " + methods[i], 882 param == Byte.TYPE || param == Short.TYPE 883 || param == Boolean.TYPE 884 || param == Character.TYPE); 885 } 886 887 try { 888 methods[i].invoke(testMethod, new Object[] { new Long(1) }); 889 assertTrue("invalid invoke with Long: " + methods[i], 890 param == Long.TYPE || param == Float.TYPE 891 || param == Double.TYPE); 892 } catch (Exception e) { 893 assertTrue("Long invalid exception: " + e, 894 e instanceof IllegalArgumentException); 895 assertTrue("Long invalid failure: " + methods[i], 896 param == Byte.TYPE || param == Short.TYPE 897 || param == Integer.TYPE 898 || param == Boolean.TYPE 899 || param == Character.TYPE); 900 } 901 902 try { 903 methods[i].invoke(testMethod, new Object[] { new Character( 904 'a') }); 905 assertTrue("invalid invoke with Character: " + methods[i], 906 param == Character.TYPE || param == Integer.TYPE 907 || param == Long.TYPE 908 || param == Float.TYPE 909 || param == Double.TYPE); 910 } catch (Exception e) { 911 assertTrue("Character invalid exception: " + e, 912 e instanceof IllegalArgumentException); 913 assertTrue("Character invalid failure: " + methods[i], 914 param == Byte.TYPE || param == Short.TYPE 915 || param == Boolean.TYPE); 916 } 917 918 try { 919 methods[i] 920 .invoke(testMethod, new Object[] { new Float(1) }); 921 assertTrue("invalid invoke with Float: " + methods[i], 922 param == Float.TYPE || param == Double.TYPE); 923 } catch (Exception e) { 924 assertTrue("Float invalid exception: " + e, 925 e instanceof IllegalArgumentException); 926 assertTrue("Float invalid failure: " + methods[i], 927 param == Byte.TYPE || param == Short.TYPE 928 || param == Integer.TYPE 929 || param == Long.TYPE 930 || param == Boolean.TYPE 931 || param == Character.TYPE); 932 } 933 934 try { 935 methods[i].invoke(testMethod, 936 new Object[] { new Double(1) }); 937 assertTrue("invalid invoke with Double: " + methods[i], 938 param == Double.TYPE); 939 } catch (Exception e) { 940 assertTrue("Double invalid exception: " + e, 941 e instanceof IllegalArgumentException); 942 assertTrue("Double invalid failure: " + methods[i], 943 param == Byte.TYPE || param == Short.TYPE 944 || param == Integer.TYPE 945 || param == Long.TYPE 946 || param == Boolean.TYPE 947 || param == Character.TYPE 948 || param == Float.TYPE); 949 } 950 951 try { 952 methods[i].invoke(testMethod, new Object[] { new Boolean( 953 true) }); 954 assertTrue("invalid invoke with Boolean: " + methods[i], 955 param == Boolean.TYPE); 956 } catch (Exception e) { 957 assertTrue("Boolean invalid exception: " + e, 958 e instanceof IllegalArgumentException); 959 assertTrue("Boolean invalid failure: " + methods[i], 960 param == Byte.TYPE || param == Short.TYPE 961 || param == Integer.TYPE 962 || param == Long.TYPE 963 || param == Character.TYPE 964 || param == Float.TYPE 965 || param == Double.TYPE); 966 } 967 } 968 } 969 } 970 971 /** 972 * @tests java.lang.reflect.Method#toString() 973 */ 974 @TestTargetNew( 975 level = TestLevel.COMPLETE, 976 notes = "", 977 method = "toString", 978 args = {} 979 ) 980 public void test_toString() { 981 // Test for method java.lang.String java.lang.reflect.Method.toString() 982 Method mth = null; 983 Class[] parms = { int.class, short.class, String.class, boolean.class, 984 Object.class, long.class, byte.class, char.class, double.class, 985 float.class }; 986 try { 987 988 mth = TestMethod.class.getDeclaredMethod("printTest", parms); 989 } catch (Exception e) { 990 fail("Exception during toString test : " + e.getMessage()); 991 } 992 993 assertTrue( 994 "Returned incorrect string for method: " + mth.toString(), 995 mth 996 .toString() 997 .equals( 998 "public static final void tests.api.java.lang.reflect.MethodTest$TestMethod.printTest(int,short,java.lang.String,boolean,java.lang.Object,long,byte,char,double,float)")); 999 } 1000 1001 /** 1002 * Sets up the fixture, for example, open a network connection. This method 1003 * is called before a test is executed. 1004 */ 1005 protected void setUp() { 1006 } 1007 1008 /** 1009 * Tears down the fixture, for example, close a network connection. This 1010 * method is called after a test is executed. 1011 */ 1012 protected void tearDown() { 1013 } 1014} 1015