1 /* 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.java.lang; 18 19 import java.io.File; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.Serializable; 23 import java.lang.annotation.Annotation; 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 import java.lang.reflect.Constructor; 27 import java.lang.reflect.Field; 28 import java.lang.reflect.Method; 29 import java.lang.reflect.ParameterizedType; 30 import java.lang.reflect.Type; 31 import java.lang.reflect.TypeVariable; 32 import java.net.URL; 33 import java.net.URLClassLoader; 34 import java.util.AbstractList; 35 import java.util.Collection; 36 import java.util.List; 37 import java.util.Vector; 38 import tests.support.Support_ClassLoader; 39 import tests.support.resource.Support_Resources; 40 41 @SuppressWarnings("deprecation") 42 public class OldClassTest extends junit.framework.TestCase { 43 44 public static final String FILENAME = 45 OldClassTest.class.getPackage().getName().replace('.', '/') + 46 "/test#.properties"; 47 48 final String packageName = getClass().getPackage().getName(); 49 final String classNameInitError1 = packageName + ".TestClass1"; 50 final String classNameInitError2 = packageName + ".TestClass1B"; 51 final String classNameLinkageError = packageName + ".TestClass"; 52 final String sourceJARfile = "illegalClasses.jar"; 53 final String illegalClassName = "illegalClass"; 54 55 @Retention(RetentionPolicy.RUNTIME) 56 public @interface TestAnnotation { 57 String value(); 58 } 59 60 public static class TestClass1C { 61 static TestClass2 tc = new TestClass2(0); 62 63 TestClass1C() { 64 } 65 66 } 67 68 public static class TestClass2 { 69 70 public TestClass2(int i) throws IllegalArgumentException { 71 throw new IllegalArgumentException(); 72 } 73 } 74 75 public static class TestClass3 { 76 private TestClass3() {} 77 } 78 79 interface TestInterface { 80 public static int TEST_INTERFACE_FIELD = 0; 81 82 int getCount(); 83 void setCount(int value); 84 } 85 86 static class StaticMember$Class { 87 class Member2$A { 88 } 89 } 90 91 class Member$Class { 92 class Member3$B { 93 } 94 } 95 96 @Deprecated 97 @TestAnnotation("libcore.java.lang.OldClassTest$ExtendTestClass") 98 public static class ExtendTestClass extends PublicTestClass { 99 100 private static final long serialVersionUID = 1L; 101 102 public enum enumExm {ONE, TWO, THREE}; 103 @Override 104 public void setCount(int value) { 105 106 } 107 } 108 109 public class ExtendTestClass1 extends ExtendTestClass { 110 111 } 112 113 @TestAnnotation("libcore.java.lang.OldClassTest$PublicTestClass") 114 public static class PublicTestClass implements TestInterface, Serializable, Cloneable { 115 116 private static final long serialVersionUID = 1L; 117 118 public static String TEST_FIELD = "test field"; 119 120 Object clazz; 121 122 public PublicTestClass() { 123 class LocalClass { } 124 125 clazz = new LocalClass(); 126 } 127 128 public Object getLocalClass() { 129 class LocalClass {} 130 Object returnedObject = new LocalClass(); 131 return returnedObject; 132 } 133 134 int count = 0; 135 136 public int getCount() { 137 return count; 138 } 139 140 public void setCount(int value) { 141 count = value; 142 } 143 144 private class PrivateClass1 { 145 146 public String toString() { 147 return "PrivateClass0"; 148 } 149 } 150 151 public class PrivateClass2 { 152 153 public String toString() { 154 return "PrivateClass1"; 155 } 156 } 157 } 158 159 public static class TestClass { 160 @SuppressWarnings("unused") 161 private int privField = 1; 162 163 public int pubField = 2; 164 165 private Object cValue = null; 166 167 public Object ack = new Object(); 168 169 @SuppressWarnings("unused") 170 private int privMethod() { 171 return 1; 172 } 173 174 public int pubMethod() { 175 return 2; 176 } 177 178 public Object cValue() { 179 return cValue; 180 } 181 182 public TestClass() { 183 } 184 185 @SuppressWarnings("unused") 186 private TestClass(Object o) { 187 } 188 } 189 190 public static class SubTestClass extends TestClass { 191 } 192 193 interface Intf1 { 194 public int field1 = 1; 195 public int field2 = 1; 196 void test(); 197 } 198 199 interface Intf2 { 200 public int field1 = 1; 201 void test(); 202 } 203 204 interface Intf3 extends Intf1 { 205 public int field1 = 1; 206 } 207 208 interface Intf4 extends Intf1, Intf2 { 209 public int field1 = 1; 210 void test2(int a, Object b); 211 } 212 213 interface Intf5 extends Intf1 { 214 } 215 216 class Cls1 implements Intf2 { 217 public int field1 = 2; 218 public int field2 = 2; 219 public void test() { 220 } 221 } 222 223 class Cls2 extends Cls1 implements Intf1 { 224 public int field1 = 2; 225 @Override 226 public void test() { 227 } 228 } 229 230 class Cls3 implements Intf3, Intf4 { 231 public void test() { 232 } 233 public void test2(int a, Object b) { 234 } 235 } 236 237 static class Cls4 { 238 239 } 240 241 public void test_getAnnotations() { 242 Annotation [] annotations = PublicTestClass.class.getAnnotations(); 243 assertEquals(1, annotations.length); 244 assertEquals(TestAnnotation.class, annotations[0].annotationType()); 245 246 annotations = ExtendTestClass.class.getAnnotations(); 247 assertEquals(2, annotations.length); 248 249 for(int i = 0; i < annotations.length; i++) { 250 Class<? extends Annotation> type = annotations[i].annotationType(); 251 assertTrue("Annotation's type " + i + ": " + type, 252 type.equals(Deprecated.class) || 253 type.equals(TestAnnotation.class)); 254 } 255 } 256 257 public void test_forNameLjava_lang_StringLbooleanLClassLoader() throws Exception { 258 259 ClassLoader pcl = getClass().getClassLoader(); 260 261 Class<?> [] classes = {PublicTestClass.class, ExtendTestClass.class, 262 ExtendTestClass1.class, TestInterface.class, String.class}; 263 264 for(int i = 0; i < classes.length; i++) { 265 Class<?> clazz = Class.forName(classes[i].getName(), true, pcl); 266 assertEquals(classes[i], clazz); 267 268 clazz = Class.forName(classes[i].getName(), false, pcl); 269 assertEquals(classes[i], clazz); 270 } 271 272 Class<?> [] systemClasses = {String.class, Integer.class, Object.class, 273 Object[].class}; 274 275 for(int i = 0; i < systemClasses.length; i++) { 276 Class<?> clazz = Class.forName(systemClasses[i].getName(), true, 277 ClassLoader.getSystemClassLoader()); 278 assertEquals(systemClasses[i], clazz); 279 280 clazz = Class.forName(systemClasses[i].getName(), false, 281 ClassLoader.getSystemClassLoader()); 282 assertEquals(systemClasses[i], clazz); 283 } 284 285 try { 286 Class.forName(null, true, pcl); 287 fail("NullPointerException is not thrown."); 288 } catch(NullPointerException npe) { 289 //expected 290 } 291 292 try { 293 Class.forName("NotExistClass", true, pcl); 294 fail("ClassNotFoundException is not thrown for non existent class."); 295 } catch(ClassNotFoundException cnfe) { 296 //expected 297 } 298 299 try { 300 Class.forName("String", false, pcl); 301 fail("ClassNotFoundException is not thrown for non existent class."); 302 } catch(ClassNotFoundException cnfe) { 303 //expected 304 } 305 306 try { 307 Class.forName("libcore.java.lang.NonexistentClass", false, pcl); 308 fail("ClassNotFoundException is not thrown for non existent class."); 309 } catch(ClassNotFoundException cnfe) { 310 //expected 311 } 312 } 313 314 // AndroidOnly: Class.forName method throws ClassNotFoundException on Android. 315 public void test_forNameLjava_lang_StringLbooleanLClassLoader_AndroidOnly() throws Exception { 316 317 // Android doesn't support loading class files from a jar. 318 try { 319 320 URL url = getClass().getClassLoader().getResource( 321 packageName.replace(".", "/") + "/" + sourceJARfile); 322 323 ClassLoader loader = new URLClassLoader(new URL[] { url }, 324 getClass().getClassLoader()); 325 try { 326 Class.forName(classNameLinkageError, true, loader); 327 fail("LinkageError or ClassNotFoundException expected."); 328 } catch (java.lang.LinkageError le) { 329 // Expected for the RI. 330 } catch (java.lang.ClassNotFoundException ce) { 331 // Expected for Android. 332 } 333 } catch(Exception e) { 334 fail("Unexpected exception was thrown: " + e.toString()); 335 } 336 337 try { 338 Class.forName(classNameInitError2, 339 true, getClass().getClassLoader()); 340 fail("ExceptionInInitializerError or ClassNotFoundException " + 341 "should be thrown."); 342 } catch (java.lang.ExceptionInInitializerError ie) { 343 // Expected for the RI. 344 // Remove this comment to let the test pass on Android. 345 } catch (java.lang.ClassNotFoundException ce) { 346 // Expected for Android. 347 } 348 } 349 350 public void test_getAnnotation() { 351 TestAnnotation target = PublicTestClass.class.getAnnotation(TestAnnotation.class); 352 assertEquals(target.value(), PublicTestClass.class.getName()); 353 354 assertNull(PublicTestClass.class.getAnnotation(Deprecated.class)); 355 356 Deprecated target2 = ExtendTestClass.class.getAnnotation(Deprecated.class); 357 assertNotNull(target2); 358 } 359 360 public void test_getDeclaredAnnotations() { 361 Annotation [] annotations = PublicTestClass.class.getDeclaredAnnotations(); 362 assertEquals(1, annotations.length); 363 364 annotations = ExtendTestClass.class.getDeclaredAnnotations(); 365 assertEquals(2, annotations.length); 366 367 annotations = TestInterface.class.getDeclaredAnnotations(); 368 assertEquals(0, annotations.length); 369 370 annotations = String.class.getDeclaredAnnotations(); 371 assertEquals(0, annotations.length); 372 } 373 374 public void test_getEnclosingClass() { 375 Class clazz = OldClassTest.class.getEnclosingClass(); 376 assertNull(clazz); 377 378 assertEquals(getClass(), Cls1.class.getEnclosingClass()); 379 assertEquals(getClass(), Intf1.class.getEnclosingClass()); 380 assertEquals(getClass(), Cls4.class.getEnclosingClass()); 381 } 382 383 public void test_getEnclosingMethod() { 384 Method clazz = ExtendTestClass.class.getEnclosingMethod(); 385 assertNull(clazz); 386 387 PublicTestClass ptc = new PublicTestClass(); 388 try { 389 assertEquals("getEnclosingMethod returns incorrect method.", 390 PublicTestClass.class.getMethod("getLocalClass", 391 (Class []) null), 392 ptc.getLocalClass().getClass().getEnclosingMethod()); 393 } catch(NoSuchMethodException nsme) { 394 fail("NoSuchMethodException was thrown."); 395 } 396 } 397 398 public void test_getEnclosingConstructor() { 399 400 PublicTestClass ptc = new PublicTestClass(); 401 402 assertEquals("getEnclosingConstructor method returns incorrect class.", 403 PublicTestClass.class.getConstructors()[0], 404 ptc.clazz.getClass().getEnclosingConstructor()); 405 406 assertNull("getEnclosingConstructor should return null for local " + 407 "class declared in method.", 408 ptc.getLocalClass().getClass().getEnclosingConstructor()); 409 410 assertNull("getEnclosingConstructor should return null for local " + 411 "class declared in method.", 412 ExtendTestClass.class.getEnclosingConstructor()); 413 } 414 415 416 public void test_getEnumConstants() { 417 Object [] clazz = ExtendTestClass.class.getEnumConstants(); 418 assertNull(clazz); 419 Object [] constants = TestEnum.class.getEnumConstants(); 420 assertEquals(TestEnum.values().length, constants.length); 421 for(int i = 0; i < constants.length; i++) { 422 assertEquals(TestEnum.values()[i], constants[i]); 423 } 424 assertEquals(0, TestEmptyEnum.class.getEnumConstants().length); 425 } 426 public enum TestEnum { 427 ONE, TWO, THREE 428 } 429 public enum TestEmptyEnum { 430 } 431 432 public void test_getGenericInterfaces() { 433 Type [] types = ExtendTestClass1.class.getGenericInterfaces(); 434 assertEquals(0, types.length); 435 436 Class [] interfaces = {TestInterface.class, Serializable.class, 437 Cloneable.class}; 438 types = PublicTestClass.class.getGenericInterfaces(); 439 assertEquals(interfaces.length, types.length); 440 for(int i = 0; i < types.length; i++) { 441 assertEquals(interfaces[i], types[i]); 442 } 443 444 types = TestInterface.class.getGenericInterfaces(); 445 assertEquals(0, types.length); 446 447 types = List.class.getGenericInterfaces(); 448 assertEquals(1, types.length); 449 assertEquals(Collection.class, ((ParameterizedType)types[0]).getRawType()); 450 451 assertEquals(0, int.class.getGenericInterfaces().length); 452 assertEquals(0, void.class.getGenericInterfaces().length); 453 } 454 455 public void test_getGenericSuperclass () { 456 assertEquals(PublicTestClass.class, 457 ExtendTestClass.class.getGenericSuperclass()); 458 assertEquals(ExtendTestClass.class, 459 ExtendTestClass1.class.getGenericSuperclass()); 460 assertEquals(Object.class, PublicTestClass.class.getGenericSuperclass()); 461 assertEquals(Object.class, String.class.getGenericSuperclass()); 462 assertEquals(null, TestInterface.class.getGenericSuperclass()); 463 464 ParameterizedType type = (ParameterizedType) Vector.class.getGenericSuperclass(); 465 assertEquals(AbstractList.class, type.getRawType()); 466 } 467 468 // AndroidOnly: Uses dalvik.system.PathClassLoader. 469 // Different behavior between cts host and run-core-test") 470 public void test_getPackage() { 471 472 Package thisPackage = getClass().getPackage(); 473 assertEquals("libcore.java.lang", 474 thisPackage.getName()); 475 476 Package stringPackage = String.class.getPackage(); 477 assertNotNull("java.lang", stringPackage.getName()); 478 479 String hyts_package_name = "hyts_package_dex.jar"; 480 File resources = Support_Resources.createTempFolder(); 481 Support_Resources.copyFile(resources, "Package", hyts_package_name); 482 483 String resPath = resources.toString(); 484 if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') 485 resPath = resPath.substring(1); 486 487 try { 488 489 URL resourceURL = new URL("file:/" + resPath + "/Package/" 490 + hyts_package_name); 491 492 ClassLoader cl = Support_ClassLoader.getInstance(resourceURL, 493 getClass().getClassLoader()); 494 495 Class clazz = cl.loadClass("C"); 496 assertNull("getPackage for C.class should return null", 497 clazz.getPackage()); 498 499 clazz = cl.loadClass("a.b.C"); 500 Package cPackage = clazz.getPackage(); 501 assertNotNull("getPackage for a.b.C.class should not return null", 502 cPackage); 503 504 /* 505 * URLClassLoader doesn't work on Android for jar files 506 * 507 * URL url = getClass().getClassLoader().getResource( 508 * packageName.replace(".", "/") + "/" + sourceJARfile); 509 * 510 * ClassLoader loader = new URLClassLoader(new URL[] { url }, null); 511 * 512 * try { 513 * Class<?> clazz = loader.loadClass(illegalClassName); 514 * Package pack = clazz.getPackage(); 515 * assertNull(pack); 516 * } catch(ClassNotFoundException cne) { 517 * fail("ClassNotFoundException was thrown for " + illegalClassName); 518 * } 519 */ 520 } catch(Exception e) { 521 fail("Unexpected exception was thrown: " + e.toString()); 522 } 523 } 524 525 public void test_getSigners() { 526 assertNull(void.class.getSigners()); 527 assertNull(PublicTestClass.class.getSigners()); 528 } 529 530 public void test_getSimpleName() { 531 assertEquals("PublicTestClass", PublicTestClass.class.getSimpleName()); 532 assertEquals("void", void.class.getSimpleName()); 533 assertEquals("int[]", int[].class.getSimpleName()); 534 } 535 536 public void test_getTypeParameters() { 537 assertEquals(0, PublicTestClass.class.getTypeParameters().length); 538 TypeVariable [] tv = TempTestClass1.class.getTypeParameters(); 539 assertEquals(1, tv.length); 540 assertEquals(Object.class, tv[0].getBounds()[0]); 541 542 TempTestClass2<String> tc = new TempTestClass2<String>(); 543 tv = tc.getClass().getTypeParameters(); 544 assertEquals(1, tv.length); 545 assertEquals(String.class, tv[0].getBounds()[0]); 546 } 547 548 class TempTestClass1<T> { 549 } 550 551 class TempTestClass2<S extends String> extends TempTestClass1<S> { 552 } 553 554 public void test_isAnnotation() { 555 assertTrue(Deprecated.class.isAnnotation()); 556 assertTrue(TestAnnotation.class.isAnnotation()); 557 assertFalse(PublicTestClass.class.isAnnotation()); 558 assertFalse(String.class.isAnnotation()); 559 } 560 561 public void test_isAnnotationPresent() { 562 assertTrue(PublicTestClass.class.isAnnotationPresent(TestAnnotation.class)); 563 assertFalse(ExtendTestClass1.class.isAnnotationPresent(TestAnnotation.class)); 564 assertFalse(String.class.isAnnotationPresent(Deprecated.class)); 565 assertTrue(ExtendTestClass.class.isAnnotationPresent(TestAnnotation.class)); 566 assertTrue(ExtendTestClass.class.isAnnotationPresent(Deprecated.class)); 567 } 568 569 public void test_isAnonymousClass() { 570 assertFalse(PublicTestClass.class.isAnonymousClass()); 571 assertTrue((new Thread() {}).getClass().isAnonymousClass()); 572 } 573 574 public void test_isEnum() { 575 assertFalse(PublicTestClass.class.isEnum()); 576 assertFalse(ExtendTestClass.class.isEnum()); 577 assertTrue(TestEnum.ONE.getClass().isEnum()); 578 assertTrue(TestEnum.class.isEnum()); 579 } 580 581 public void test_isLocalClass() { 582 assertFalse(ExtendTestClass.class.isLocalClass()); 583 assertFalse(TestInterface.class.isLocalClass()); 584 assertFalse(TestEnum.class.isLocalClass()); 585 class InternalClass {} 586 assertTrue(InternalClass.class.isLocalClass()); 587 } 588 589 public void test_isMemberClass() { 590 assertFalse(OldClassTest.class.isMemberClass()); 591 assertFalse(String.class.isMemberClass()); 592 assertTrue(TestEnum.class.isMemberClass()); 593 assertTrue(StaticMember$Class.class.isMemberClass()); 594 } 595 596 public void test_isSynthetic() { 597 assertFalse("Returned true for non synthetic class.", 598 ExtendTestClass.class.isSynthetic()); 599 assertFalse("Returned true for non synthetic class.", 600 TestInterface.class.isSynthetic()); 601 assertFalse("Returned true for non synthetic class.", 602 String.class.isSynthetic()); 603 } 604 605 public void test_getCanonicalName() { 606 Class [] classArray = { int.class, int[].class, String.class, 607 PublicTestClass.class, TestInterface.class, 608 ExtendTestClass.class }; 609 String [] classNames = {"int", "int[]", "java.lang.String", 610 "libcore.java.lang.OldClassTest.PublicTestClass", 611 "libcore.java.lang.OldClassTest.TestInterface", 612 "libcore.java.lang.OldClassTest.ExtendTestClass"}; 613 614 for(int i = 0; i < classArray.length; i++) { 615 assertEquals(classNames[i], classArray[i].getCanonicalName()); 616 } 617 } 618 619 public void test_getClassLoader() { 620 assertEquals(ExtendTestClass.class.getClassLoader(), 621 PublicTestClass.class.getClassLoader()); 622 623 assertNull(int.class.getClassLoader()); 624 assertNull(void.class.getClassLoader()); 625 } 626 627 public void test_getClasses() { 628 assertEquals("Incorrect class array returned", 629 11, OldClassTest.class.getClasses().length); 630 } 631 632 public void test_getDeclaredClasses() { 633 Class [] declClasses = Object.class.getDeclaredClasses(); 634 assertEquals("Incorrect length of declared classes array is returned " + 635 "for Object.", 0, declClasses.length); 636 637 declClasses = PublicTestClass.class.getDeclaredClasses(); 638 assertEquals(2, declClasses.length); 639 640 assertEquals(0, int.class.getDeclaredClasses().length); 641 assertEquals(0, void.class.getDeclaredClasses().length); 642 643 for(int i = 0; i < declClasses.length; i++) { 644 Constructor<?> constr = declClasses[i].getDeclaredConstructors()[0]; 645 constr.setAccessible(true); 646 PublicTestClass publicClazz = new PublicTestClass(); 647 try { 648 Object o = constr.newInstance(publicClazz); 649 assertTrue("Returned incorrect class: " + o.toString(), 650 o.toString().startsWith("PrivateClass")); 651 } catch(Exception e) { 652 fail("Unexpected exception was thrown: " + e.toString()); 653 } 654 } 655 656 declClasses = TestInterface.class.getDeclaredClasses(); 657 assertEquals(0, declClasses.length); 658 } 659 660 public void test_getDeclaredConstructor$Ljava_lang_Class() throws Exception { 661 try { 662 TestClass.class.getDeclaredConstructor(String.class); 663 fail("NoSuchMethodException should be thrown."); 664 } catch(NoSuchMethodException nsme) { 665 //expected 666 } 667 } 668 669 public void test_getDeclaredFieldLjava_lang_String() throws Exception { 670 try { 671 TestClass.class.getDeclaredField(null); 672 fail("NullPointerException is not thrown."); 673 } catch(NullPointerException npe) { 674 //expected 675 } 676 677 try { 678 TestClass.class.getDeclaredField("NonExistentField"); 679 fail("NoSuchFieldException is not thrown."); 680 } catch(NoSuchFieldException nsfe) { 681 //expected 682 } 683 } 684 685 public void test_getDeclaredMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 686 try { 687 TestClass.class.getDeclaredMethod(null, new Class[0]); 688 fail("NullPointerException is not thrown."); 689 } catch(NullPointerException npe) { 690 //expected 691 } 692 693 try { 694 TestClass.class.getDeclaredMethod("NonExistentMethod", new Class[0]); 695 fail("NoSuchMethodException is not thrown."); 696 } catch(NoSuchMethodException nsme) { 697 //expected 698 } 699 } 700 701 public void test_getMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 702 Method m = ExtendTestClass1.class.getMethod("getCount", new Class[0]); 703 assertEquals("Returned incorrect method", 0, ((Integer) (m.invoke(new ExtendTestClass1()))) 704 .intValue()); 705 706 try { 707 m = TestClass.class.getMethod("init", new Class[0]); 708 fail("Failed to throw exception accessing to init method"); 709 } catch (NoSuchMethodException e) { 710 // Correct 711 return; 712 } 713 714 try { 715 TestClass.class.getMethod("pubMethod", new Class[0]); 716 fail("NullPointerException is not thrown."); 717 } catch(NullPointerException npe) { 718 //expected 719 } 720 } 721 722 public void test_getDeclaringClass() { 723 assertNull(OldClassTest.class.getDeclaringClass()); 724 assertNotNull(PublicTestClass.class.getDeclaringClass()); 725 } 726 727 public void test_getFieldLjava_lang_String() throws Exception { 728 Field f = TestClass.class.getField("pubField"); 729 assertEquals("Returned incorrect field", 2, f.getInt(new TestClass())); 730 731 f = PublicTestClass.class.getField("TEST_FIELD"); 732 assertEquals("Returned incorrect field", "test field", 733 f.get(new PublicTestClass())); 734 735 f = PublicTestClass.class.getField("TEST_INTERFACE_FIELD"); 736 assertEquals("Returned incorrect field", 0, 737 f.getInt(new PublicTestClass())); 738 739 try { 740 TestClass.class.getField(null); 741 fail("NullPointerException is thrown."); 742 } catch(NullPointerException npe) { 743 //expected 744 } 745 } 746 747 public void test_getFields2() throws Exception { 748 Field[] f; 749 Field expected = null; 750 751 f = PublicTestClass.class.getFields(); 752 assertEquals("Test 1: Incorrect number of fields;", 2, f.length); 753 754 f = Cls2.class.getFields(); 755 assertEquals("Test 2: Incorrect number of fields;", 6, f.length); 756 757 f = Cls3.class.getFields(); 758 assertEquals("Test 2: Incorrect number of fields;", 5, f.length); 759 760 for (Field field : f) { 761 if (field.toString().equals("public static final int " 762 + "libcore.java.lang.OldClassTest$Intf3.field1")) { 763 expected = field; 764 break; 765 } 766 } 767 if (expected == null) { 768 fail("Test 3: getFields() did not return all fields."); 769 } 770 assertEquals("Test 4: Incorrect field;", expected, 771 Cls3.class.getField("field1")); 772 773 expected = null; 774 for (Field field : f) { 775 if(field.toString().equals("public static final int " + 776 "libcore.java.lang.OldClassTest$Intf1.field2")) { 777 expected = field; 778 break; 779 } 780 } 781 if (expected == null) { 782 fail("Test 5: getFields() did not return all fields."); 783 } 784 assertEquals("Test 6: Incorrect field;", expected, 785 Cls3.class.getField("field2")); 786 } 787 788 public void test_getFields() throws Exception { 789 Field expected = null; 790 Field[] fields = Cls2.class.getFields(); 791 for (Field field : fields) { 792 if(field.toString().equals("public int libcore.java.lang.OldClassTest$Cls2.field1")) { 793 expected = field; 794 break; 795 } 796 } 797 if (expected == null) { 798 fail("getFields() did not return all fields"); 799 } 800 assertEquals(expected, Cls2.class.getField("field1")); 801 } 802 803 public void test_getInterfaces() { 804 Class [] interfaces1 = Cls1.class.getInterfaces(); 805 assertEquals(1, interfaces1.length); 806 assertEquals(Intf2.class, interfaces1[0]); 807 808 Class [] interfaces2 = Cls2.class.getInterfaces(); 809 assertEquals(1, interfaces2.length); 810 assertEquals(Intf1.class, interfaces2[0]); 811 812 Class [] interfaces3 = Cls3.class.getInterfaces(); 813 assertEquals(2, interfaces3.length); 814 assertEquals(Intf3.class, interfaces3[0]); 815 assertEquals(Intf4.class, interfaces3[1]); 816 817 Class [] interfaces4 = Cls4.class.getInterfaces(); 818 assertEquals(0, interfaces4.length); 819 } 820 821 public void test_getMethods() throws Exception { 822 assertEquals("Incorrect number of methods", 10, 823 Cls2.class.getMethods().length); 824 assertEquals("Incorrect number of methods", 11, 825 Cls3.class.getMethods().length); 826 827 Method expected = null; 828 Method[] methods = Cls2.class.getMethods(); 829 for (Method method : methods) { 830 if(method.toString().equals("public void libcore.java.lang.OldClassTest$Cls2.test()")) { 831 expected = method; 832 break; 833 } 834 } 835 if (expected == null) { 836 fail("getMethods() did not return all methods"); 837 } 838 assertEquals(expected, Cls2.class.getMethod("test")); 839 840 expected = null; 841 methods = Cls3.class.getMethods(); 842 for (Method method : methods) { 843 if(method.toString().equals("public void libcore.java.lang.OldClassTest$Cls3.test()")) { 844 expected = method; 845 break; 846 } 847 } 848 if (expected == null) { 849 fail("getMethods() did not return all methods"); 850 } 851 assertEquals(expected, Cls3.class.getMethod("test")); 852 853 expected = null; 854 methods = Cls3.class.getMethods(); 855 for (Method method : methods) { 856 if(method.toString().equals("public void libcore.java.lang.OldClassTest$Cls3.test2(int," 857 + "java.lang.Object)")) { 858 expected = method; 859 break; 860 } 861 } 862 if (expected == null) { 863 fail("getMethods() did not return all methods"); 864 } 865 866 assertEquals(expected, Cls3.class.getMethod("test2", int.class, 867 Object.class)); 868 869 assertEquals("Incorrect number of methods", 1, 870 Intf5.class.getMethods().length); 871 } 872 873 public void test_getResourceLjava_lang_String() { 874 assertNull(getClass().getResource( 875 "libcore/java/lang/NonExistentResource")); 876 assertNull(getClass().getResource(getClass().getName() + "NonExistentResource")); 877 } 878 879 public void test_getResourceAsStreamLjava_lang_String() throws Exception { 880 String name = "/HelloWorld.txt"; 881 assertNotNull("the file " + name + " can not be found in this " + 882 "directory", getClass().getResourceAsStream(name)); 883 884 final String nameBadURI = "org/apache/harmony/luni/tests/test_resource.txt"; 885 assertNull("the file " + nameBadURI + " should not be found in this directory", 886 getClass().getResourceAsStream(nameBadURI)); 887 888 ClassLoader pcl = getClass().getClassLoader(); 889 Class<?> clazz = pcl.loadClass("libcore.java.lang.OldClassTest"); 890 assertNotNull(clazz.getResourceAsStream("HelloWorld1.txt")); 891 892 try { 893 getClass().getResourceAsStream(null); 894 fail("NullPointerException is not thrown."); 895 } catch(NullPointerException npe) { 896 //expected 897 } 898 } 899 900 public void test_isAssignableFromLjava_lang_Class() { 901 assertFalse("returned true not assignable classes", 902 Integer.class.isAssignableFrom(String.class)); 903 904 try { 905 Runnable.class.isAssignableFrom(null); 906 fail("NullPointerException is not thrown."); 907 } catch(NullPointerException npe) { 908 //expected 909 } 910 } 911 912 public void test_newInstance() throws Exception { 913 try { 914 TestClass3.class.newInstance(); 915 fail("IllegalAccessException is not thrown."); 916 } catch(IllegalAccessException iae) { 917 //expected 918 } 919 920 try { 921 TestClass1C.class.newInstance(); 922 fail("ExceptionInInitializerError should be thrown."); 923 } catch (java.lang.ExceptionInInitializerError ie) { 924 //expected 925 } 926 } 927 928 public void test_asSubclass1() { 929 assertEquals(ExtendTestClass.class, 930 ExtendTestClass.class.asSubclass(PublicTestClass.class)); 931 932 assertEquals(PublicTestClass.class, 933 PublicTestClass.class.asSubclass(TestInterface.class)); 934 935 assertEquals(ExtendTestClass1.class, 936 ExtendTestClass1.class.asSubclass(PublicTestClass.class)); 937 938 assertEquals(PublicTestClass.class, 939 PublicTestClass.class.asSubclass(PublicTestClass.class)); 940 } 941 942 public void test_asSubclass2() { 943 try { 944 PublicTestClass.class.asSubclass(ExtendTestClass.class); 945 fail("Test 1: ClassCastException expected."); 946 } catch(ClassCastException cce) { 947 // Expected. 948 } 949 950 try { 951 PublicTestClass.class.asSubclass(String.class); 952 fail("Test 2: ClassCastException expected."); 953 } catch(ClassCastException cce) { 954 // Expected. 955 } 956 } 957 958 public void test_cast() { 959 Object o = PublicTestClass.class.cast(new ExtendTestClass()); 960 assertTrue(o instanceof ExtendTestClass); 961 962 try { 963 ExtendTestClass.class.cast(new PublicTestClass()); 964 fail("Test 1: ClassCastException expected."); 965 } catch(ClassCastException cce) { 966 //expected 967 } 968 969 try { 970 ExtendTestClass.class.cast(new String()); 971 fail("ClassCastException is not thrown."); 972 } catch(ClassCastException cce) { 973 //expected 974 } 975 } 976 977 public void test_desiredAssertionStatus() { 978 Class [] classArray = { Object.class, Integer.class, 979 String.class, PublicTestClass.class, 980 ExtendTestClass.class, ExtendTestClass1.class}; 981 982 for(int i = 0; i < classArray.length; i++) { 983 assertFalse("assertion status for " + classArray[i], 984 classArray[i].desiredAssertionStatus()); 985 } 986 } 987 988 public void testGetResourceAsStream1() throws IOException { 989 Class clazz = getClass(); 990 991 InputStream stream = clazz.getResourceAsStream("HelloWorld.txt"); 992 assert(stream != null); 993 994 byte[] buffer = new byte[20]; 995 int length = stream.read(buffer); 996 String s = new String(buffer, 0, length); 997 assert("Hello, World.".equals(s)); 998 999 stream.close(); 1000 } 1001 1002 public void testGetResourceAsStream2() throws IOException { 1003 Class clazz = getClass(); 1004 1005 InputStream stream = clazz.getResourceAsStream("/libcore/java/lang/HelloWorld.txt"); 1006 assert(stream != null); 1007 1008 byte[] buffer = new byte[20]; 1009 int length = stream.read(buffer); 1010 String s = new String(buffer, 0, length); 1011 assert("Hello, World.".equals(s)); 1012 1013 stream.close(); 1014 1015 try { 1016 clazz.getResourceAsStream(null); 1017 fail("NullPointerException is not thrown."); 1018 } catch(NullPointerException npe) { 1019 //expected 1020 } 1021 assertNull(clazz.getResourceAsStream("/NonExistentResource")); 1022 assertNull(clazz.getResourceAsStream("libcore/java/lang/HelloWorld.txt")); 1023 } 1024 } 1025