ClassTest.java revision 5d709784bbf5001012d7f25172927d46f6c1abe1
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 17package org.apache.harmony.luni.tests.java.lang; 18 19import java.io.File; 20import java.io.FileInputStream; 21import java.io.InputStream; 22import java.io.Serializable; 23import java.lang.annotation.Annotation; 24import java.lang.reflect.Constructor; 25import java.lang.reflect.Field; 26import java.lang.reflect.Member; 27import java.lang.reflect.Method; 28import java.lang.reflect.Modifier; 29import java.lang.reflect.ParameterizedType; 30import java.lang.reflect.Type; 31import java.lang.reflect.TypeVariable; 32import java.net.URL; 33import java.net.URLClassLoader; 34import java.security.AccessControlContext; 35import java.security.AccessController; 36import java.security.BasicPermission; 37import java.security.DomainCombiner; 38import java.security.Permission; 39import java.security.PrivilegedAction; 40import java.security.ProtectionDomain; 41import java.security.Security; 42import java.util.AbstractList; 43import java.util.Arrays; 44import java.util.Collection; 45import java.util.List; 46import java.util.Vector; 47 48import tests.support.resource.Support_Resources; 49import dalvik.annotation.AndroidOnly; 50import dalvik.annotation.BrokenTest; 51import dalvik.annotation.KnownFailure; 52import dalvik.annotation.TestLevel; 53import dalvik.annotation.TestTargetClass; 54import dalvik.annotation.TestTargetNew; 55 56@SuppressWarnings("deprecation") 57@TestTargetClass(Class.class) 58public class ClassTest extends junit.framework.TestCase { 59 60 public static final String FILENAME = 61 ClassTest.class.getPackage().getName().replace('.', '/') + 62 "/test#.properties"; 63 64 final String packageName = getClass().getPackage().getName(); 65 final String classNameInitError1 = packageName + ".TestClass1"; 66 final String classNameInitError2 = packageName + ".TestClass1B"; 67 final String classNameLinkageError = packageName + ".TestClass"; 68 final String sourceJARfile = "illegalClasses.jar"; 69 final String illegalClassName = "illegalClass"; 70 71 static class StaticMember$Class { 72 class Member2$A { 73 } 74 } 75 76 class Member$Class { 77 class Member3$B { 78 } 79 } 80 81 public static class TestClass { 82 @SuppressWarnings("unused") 83 private int privField = 1; 84 85 public int pubField = 2; 86 87 private Object cValue = null; 88 89 public Object ack = new Object(); 90 91 @SuppressWarnings("unused") 92 private int privMethod() { 93 return 1; 94 } 95 96 public int pubMethod() { 97 return 2; 98 } 99 100 public Object cValue() { 101 return cValue; 102 } 103 104 public TestClass() { 105 } 106 107 @SuppressWarnings("unused") 108 private TestClass(Object o) { 109 } 110 } 111 112 public static class SubTestClass extends TestClass { 113 } 114 115 interface Intf1 { 116 public int field1 = 1; 117 public int field2 = 1; 118 void test(); 119 } 120 121 interface Intf2 { 122 public int field1 = 1; 123 void test(); 124 } 125 126 interface Intf3 extends Intf1 { 127 public int field1 = 1; 128 } 129 130 interface Intf4 extends Intf1, Intf2 { 131 public int field1 = 1; 132 void test2(int a, Object b); 133 } 134 135 interface Intf5 extends Intf1 { 136 } 137 138 class Cls1 implements Intf2 { 139 public int field1 = 2; 140 public int field2 = 2; 141 public void test() { 142 } 143 } 144 145 class Cls2 extends Cls1 implements Intf1 { 146 public int field1 = 2; 147 @Override 148 public void test() { 149 } 150 } 151 152 class Cls3 implements Intf3, Intf4 { 153 public void test() { 154 } 155 public void test2(int a, Object b) { 156 } 157 } 158 159 static class Cls4 { 160 161 } 162 163 @TestTargetNew( 164 level = TestLevel.COMPLETE, 165 notes = "", 166 method = "getAnnotations", 167 args = {} 168 ) 169 public void test_getAnnotations() { 170 Annotation [] annotations = PublicTestClass.class.getAnnotations(); 171 assertEquals(1, annotations.length); 172 assertEquals(TestAnnotation.class, annotations[0].annotationType()); 173 174 annotations = ExtendTestClass.class.getAnnotations(); 175 assertEquals(2, annotations.length); 176 177 for(int i = 0; i < annotations.length; i++) { 178 Class<? extends Annotation> type = annotations[i].annotationType(); 179 assertTrue("Annotation's type " + i + ": " + type, 180 type.equals(Deprecated.class) || 181 type.equals(TestAnnotation.class)); 182 } 183 } 184 185 /** 186 * @tests java.lang.Class#forName(java.lang.String) 187 */ 188 @TestTargetNew( 189 level = TestLevel.SUFFICIENT, 190 notes = "java.lang.LinkageError can't be checked.", 191 method = "forName", 192 args = {java.lang.String.class} 193 ) 194 @AndroidOnly("harmony specific: test with " + 195 "'org.apache.harmony.luni.tests.java.lang.TestClass1'") 196 public void test_forNameLjava_lang_String() throws Exception { 197 198 assertSame("Class for name failed for java.lang.Object", 199 Object.class, Class.forName("java.lang.Object")); 200 assertSame("Class for name failed for [[Ljava.lang.Object;", 201 Object[][].class, Class.forName("[[Ljava.lang.Object;")); 202 203 assertSame("Class for name failed for [I", 204 int[].class, Class.forName("[I")); 205 206 try { 207 Class.forName("int"); 208 fail(); 209 } catch (ClassNotFoundException e) { 210 } 211 212 try { 213 Class.forName("byte"); 214 fail(); 215 } catch (ClassNotFoundException e) { 216 } 217 try { 218 Class.forName("char"); 219 fail(); 220 } catch (ClassNotFoundException e) { 221 } 222 223 try { 224 Class.forName("void"); 225 fail(); 226 } catch (ClassNotFoundException e) { 227 } 228 229 try { 230 Class.forName("short"); 231 fail(); 232 } catch (ClassNotFoundException e) { 233 } 234 try { 235 Class.forName("long"); 236 fail(); 237 } catch (ClassNotFoundException e) { 238 } 239 240 try { 241 Class.forName("boolean"); 242 fail(); 243 } catch (ClassNotFoundException e) { 244 } 245 try { 246 Class.forName("float"); 247 fail(); 248 } catch (ClassNotFoundException e) { 249 } 250 try { 251 Class.forName("double"); 252 fail(); 253 } catch (ClassNotFoundException e) { 254 } 255 256 //regression test for JIRA 2162 257 try { 258 Class.forName("%"); 259 fail("should throw ClassNotFoundException."); 260 } catch (ClassNotFoundException e) { 261 } 262 263 //Regression Test for HARMONY-3332 264 String securityProviderClassName; 265 int count = 1; 266 while ((securityProviderClassName = Security 267 .getProperty("security.provider." + count++)) != null) { 268 Class.forName(securityProviderClassName); 269 } 270 271 try { 272 Class.forName(classNameInitError1); 273 fail("ExceptionInInitializerError or ClassNotFoundException " + 274 "expected."); 275 } catch (java.lang.ExceptionInInitializerError ie) { 276 // Expected for the RI. 277 } catch (java.lang.ClassNotFoundException ce) { 278 // Expected for Android. 279 } 280 } 281 282 @TestTargetNew( 283 level = TestLevel.SUFFICIENT, 284 notes = "", 285 method = "forName", 286 args = {java.lang.String.class, boolean.class, java.lang.ClassLoader.class} 287 ) 288 public void test_forNameLjava_lang_StringLbooleanLClassLoader() throws Exception { 289 290 ClassLoader pcl = getClass().getClassLoader(); 291 292 Class<?> [] classes = {PublicTestClass.class, ExtendTestClass.class, 293 ExtendTestClass1.class, TestInterface.class, String.class}; 294 295 for(int i = 0; i < classes.length; i++) { 296 Class<?> clazz = Class.forName(classes[i].getName(), true, pcl); 297 assertEquals(classes[i], clazz); 298 299 clazz = Class.forName(classes[i].getName(), false, pcl); 300 assertEquals(classes[i], clazz); 301 } 302 303 for(int i = 0; i < classes.length; i++) { 304 Class<?> clazz = Class.forName(classes[i].getName(), true, 305 ClassLoader.getSystemClassLoader()); 306 assertEquals(classes[i], clazz); 307 308 clazz = Class.forName(classes[i].getName(), false, 309 ClassLoader.getSystemClassLoader()); 310 assertEquals(classes[i], clazz); 311 } 312 313 try { 314 Class.forName(null, true, pcl); 315 fail("NullPointerException is not thrown."); 316 } catch(NullPointerException npe) { 317 //expected 318 } 319 320 try { 321 Class.forName("NotExistClass", true, pcl); 322 fail("ClassNotFoundException is not thrown for non existent class."); 323 } catch(ClassNotFoundException cnfe) { 324 //expected 325 } 326 327 try { 328 Class.forName("String", false, pcl); 329 fail("ClassNotFoundException is not thrown for non existent class."); 330 } catch(ClassNotFoundException cnfe) { 331 //expected 332 } 333 334 try { 335 Class.forName("org.apache.harmony.luni.tests.java.PublicTestClass", 336 false, pcl); 337 fail("ClassNotFoundException is not thrown for non existent class."); 338 } catch(ClassNotFoundException cnfe) { 339 //expected 340 } 341 } 342 343 @TestTargetNew( 344 level = TestLevel.SUFFICIENT, 345 notes = "", 346 method = "forName", 347 args = {java.lang.String.class, boolean.class, java.lang.ClassLoader.class} 348 ) 349 @AndroidOnly("Class.forName method throws ClassNotFoundException on " + 350 "Android.") 351 public void test_forNameLjava_lang_StringLbooleanLClassLoader_AndroidOnly() throws Exception { 352 353 // Android doesn't support loading class files from a jar. 354 try { 355 356 URL url = getClass().getClassLoader().getResource( 357 packageName.replace(".", "/") + "/" + sourceJARfile); 358 359 ClassLoader loader = new URLClassLoader(new URL[] { url }, 360 getClass().getClassLoader()); 361 try { 362 Class.forName(classNameLinkageError, true, loader); 363 fail("LinkageError or ClassNotFoundException expected."); 364 } catch (java.lang.LinkageError le) { 365 // Expected for the RI. 366 } catch (java.lang.ClassNotFoundException ce) { 367 // Expected for Android. 368 } 369 } catch(Exception e) { 370 fail("Unexpected exception was thrown: " + e.toString()); 371 } 372 373 try { 374 Class.forName(classNameInitError2, 375 true, getClass().getClassLoader()); 376 fail("ExceptionInInitializerError or ClassNotFoundException " + 377 "should be thrown."); 378 } catch (java.lang.ExceptionInInitializerError ie) { 379 // Expected for the RI. 380 // Remove this comment to let the test pass on Android. 381 } catch (java.lang.ClassNotFoundException ce) { 382 // Expected for Android. 383 } 384 } 385 386 @TestTargetNew( 387 level = TestLevel.COMPLETE, 388 notes = "", 389 method = "getAnnotation", 390 args = {java.lang.Class.class} 391 ) 392 public void test_getAnnotation() { 393 TestAnnotation target = PublicTestClass.class.getAnnotation(TestAnnotation.class); 394 assertEquals(target.value(), PublicTestClass.class.getName()); 395 396 assertNull(PublicTestClass.class.getAnnotation(Deprecated.class)); 397 398 Deprecated target2 = ExtendTestClass.class.getAnnotation(Deprecated.class); 399 assertNotNull(target2); 400 } 401 402 @TestTargetNew( 403 level = TestLevel.COMPLETE, 404 notes = "", 405 method = "getDeclaredAnnotations", 406 args = {} 407 ) 408 public void test_getDeclaredAnnotations() { 409 Annotation [] annotations = PublicTestClass.class.getDeclaredAnnotations(); 410 assertEquals(1, annotations.length); 411 412 annotations = ExtendTestClass.class.getDeclaredAnnotations(); 413 assertEquals(2, annotations.length); 414 415 annotations = TestInterface.class.getDeclaredAnnotations(); 416 assertEquals(0, annotations.length); 417 418 annotations = String.class.getDeclaredAnnotations(); 419 assertEquals(0, annotations.length); 420 } 421 422 @TestTargetNew( 423 level = TestLevel.COMPLETE, 424 notes = "", 425 method = "getEnclosingClass", 426 args = {} 427 ) 428 public void test_getEnclosingClass() { 429 Class clazz = ExtendTestClass.class.getEnclosingClass(); 430 assertNull(clazz); 431 432 assertEquals(getClass(), Cls1.class.getEnclosingClass()); 433 assertEquals(getClass(), Intf1.class.getEnclosingClass()); 434 assertEquals(getClass(), Cls4.class.getEnclosingClass()); 435 } 436 437 438 @TestTargetNew( 439 level = TestLevel.COMPLETE, 440 notes = "", 441 method = "getEnclosingMethod", 442 args = {} 443 ) 444 public void test_getEnclosingMethod() { 445 Method clazz = ExtendTestClass.class.getEnclosingMethod(); 446 assertNull(clazz); 447 448 PublicTestClass ptc = new PublicTestClass(); 449 try { 450 assertEquals("getEnclosingMethod returns incorrect method.", 451 PublicTestClass.class.getMethod("getLocalClass", 452 (Class []) null), 453 ptc.getLocalClass().getClass().getEnclosingMethod()); 454 } catch(NoSuchMethodException nsme) { 455 fail("NoSuchMethodException was thrown."); 456 } 457 } 458 459 @TestTargetNew( 460 level = TestLevel.COMPLETE, 461 notes = "", 462 method = "getEnclosingConstructor", 463 args = {} 464 ) 465 public void test_getEnclosingConstructor() { 466 467 PublicTestClass ptc = new PublicTestClass(); 468 469 assertEquals("getEnclosingConstructor method returns incorrect class.", 470 PublicTestClass.class.getConstructors()[0], 471 ptc.clazz.getClass().getEnclosingConstructor()); 472 473 assertNull("getEnclosingConstructor should return null for local " + 474 "class declared in method.", 475 ptc.getLocalClass().getClass().getEnclosingConstructor()); 476 477 assertNull("getEnclosingConstructor should return null for local " + 478 "class declared in method.", 479 ExtendTestClass.class.getEnclosingConstructor()); 480 } 481 482 483 @TestTargetNew( 484 level = TestLevel.COMPLETE, 485 notes = "", 486 method = "getEnumConstants", 487 args = {} 488 ) 489 public void test_getEnumConstants() { 490 Object [] clazz = ExtendTestClass.class.getEnumConstants(); 491 assertNull(clazz); 492 Object [] constants = TestEnum.class.getEnumConstants(); 493 assertEquals(TestEnum.values().length, constants.length); 494 for(int i = 0; i < constants.length; i++) { 495 assertEquals(TestEnum.values()[i], constants[i]); 496 } 497 assertEquals(0, TestEmptyEnum.class.getEnumConstants().length); 498 } 499 public enum TestEnum { 500 ONE, TWO, THREE 501 } 502 public enum TestEmptyEnum { 503 } 504 @TestTargetNew( 505 level = TestLevel.SUFFICIENT, 506 notes = "GenericSignatureFormatError, TypeNotPresentException, " + 507 "MalformedParameterizedTypeException are not verified.", 508 method = "getGenericInterfaces", 509 args = {} 510 ) 511 public void test_getGenericInterfaces() { 512 Type [] types = ExtendTestClass1.class.getGenericInterfaces(); 513 assertEquals(0, types.length); 514 515 Class [] interfaces = {TestInterface.class, Serializable.class, 516 Cloneable.class}; 517 types = PublicTestClass.class.getGenericInterfaces(); 518 assertEquals(interfaces.length, types.length); 519 for(int i = 0; i < types.length; i++) { 520 assertEquals(interfaces[i], types[i]); 521 } 522 523 types = TestInterface.class.getGenericInterfaces(); 524 assertEquals(0, types.length); 525 526 types = List.class.getGenericInterfaces(); 527 assertEquals(1, types.length); 528 assertEquals(Collection.class, ((ParameterizedType)types[0]).getRawType()); 529 530 assertEquals(0, int.class.getGenericInterfaces().length); 531 assertEquals(0, void.class.getGenericInterfaces().length); 532 } 533 534 @TestTargetNew( 535 level = TestLevel.SUFFICIENT, 536 notes = "GenericSignatureFormatError, TypeNotPresentException, MalformedParameterizedTypeException are not verified.", 537 method = "getGenericSuperclass", 538 args = {} 539 ) 540 public void test_getGenericSuperclass () { 541 assertEquals(PublicTestClass.class, 542 ExtendTestClass.class.getGenericSuperclass()); 543 assertEquals(ExtendTestClass.class, 544 ExtendTestClass1.class.getGenericSuperclass()); 545 assertEquals(Object.class, PublicTestClass.class.getGenericSuperclass()); 546 assertEquals(Object.class, String.class.getGenericSuperclass()); 547 assertEquals(null, TestInterface.class.getGenericSuperclass()); 548 549 ParameterizedType type = (ParameterizedType) Vector.class.getGenericSuperclass(); 550 assertEquals(AbstractList.class, type.getRawType()); 551 } 552 553 @TestTargetNew( 554 level = TestLevel.SUFFICIENT, 555 method = "getPackage", 556 args = {} 557 ) 558 @AndroidOnly("Uses dalvik.system.PathClassLoader.") 559 public void test_getPackage() { 560 561 Package thisPackage = getClass().getPackage(); 562 assertEquals("org.apache.harmony.luni.tests.java.lang", 563 thisPackage.getName()); 564 565 Package stringPackage = String.class.getPackage(); 566 assertNotNull("java.lang", stringPackage.getName()); 567 568 String hyts_package_name = "hyts_package_dex.jar"; 569 File resources = Support_Resources.createTempFolder(); 570 Support_Resources.copyFile(resources, "Package", hyts_package_name); 571 572 String resPath = resources.toString(); 573 if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') 574 resPath = resPath.substring(1); 575 576 try { 577 578 URL resourceURL = new URL("file:/" + resPath + "/Package/" 579 + hyts_package_name); 580 581 ClassLoader cl = new dalvik.system.PathClassLoader( 582 resourceURL.getPath(), getClass().getClassLoader()); 583 584 Class clazz = cl.loadClass("C"); 585 assertNull("getPackage for C.class should return null", 586 clazz.getPackage()); 587 588 clazz = cl.loadClass("a.b.C"); 589 Package cPackage = clazz.getPackage(); 590 assertNotNull("getPackage for a.b.C.class should not return null", 591 cPackage); 592 593 /* 594 * URLClassLoader doesn't work on Android for jar files 595 * 596 * URL url = getClass().getClassLoader().getResource( 597 * packageName.replace(".", "/") + "/" + sourceJARfile); 598 * 599 * ClassLoader loader = new URLClassLoader(new URL[] { url }, null); 600 * 601 * try { 602 * Class<?> clazz = loader.loadClass(illegalClassName); 603 * Package pack = clazz.getPackage(); 604 * assertNull(pack); 605 * } catch(ClassNotFoundException cne) { 606 * fail("ClassNotFoundException was thrown for " + illegalClassName); 607 * } 608 */ 609 } catch(Exception e) { 610 fail("Unexpected exception was thrown: " + e.toString()); 611 } 612 } 613 614 @TestTargetNew( 615 level = TestLevel.COMPLETE, 616 method = "getProtectionDomain", 617 args = {} 618 ) 619 @BrokenTest("There is no protection domain set in Android.") 620 public void test_getProtectionDomain() { 621 ProtectionDomain pd = PublicTestClass.class.getProtectionDomain(); 622 assertNotNull("Test 1: Protection domain expected to be set.", pd); 623 624 SecurityManager sm = new SecurityManager() { 625 626 public void checkPermission(Permission perm) { 627 if (perm.getName().equals("getProtectionDomain")) { 628 throw new SecurityException(); 629 } 630 } 631 }; 632 633 SecurityManager oldSm = System.getSecurityManager(); 634 System.setSecurityManager(sm); 635 try { 636 PublicTestClass.class.getProtectionDomain(); 637 fail("Test 2: SecurityException expected."); 638 } catch (SecurityException e) { 639 // expected 640 } finally { 641 System.setSecurityManager(oldSm); 642 } 643 } 644 @TestTargetNew( 645 level = TestLevel.SUFFICIENT, 646 notes = "", 647 method = "getSigners", 648 args = {} 649 ) 650 public void test_getSigners() { 651 assertNull(void.class.getSigners()); 652 assertNull(PublicTestClass.class.getSigners()); 653 654 } 655 656 @TestTargetNew( 657 level = TestLevel.COMPLETE, 658 notes = "", 659 method = "getSimpleName", 660 args = {} 661 ) 662 public void test_getSimpleName() { 663 assertEquals("PublicTestClass", PublicTestClass.class.getSimpleName()); 664 assertEquals("void", void.class.getSimpleName()); 665 assertEquals("int[]", int[].class.getSimpleName()); 666 } 667 668 @TestTargetNew( 669 level = TestLevel.COMPLETE, 670 notes = "", 671 method = "getTypeParameters", 672 args = {} 673 ) 674 public void test_getTypeParameters() { 675 assertEquals(0, PublicTestClass.class.getTypeParameters().length); 676 TypeVariable [] tv = TempTestClass1.class.getTypeParameters(); 677 assertEquals(1, tv.length); 678 assertEquals(Object.class, tv[0].getBounds()[0]); 679 680 TempTestClass2<String> tc = new TempTestClass2<String>(); 681 tv = tc.getClass().getTypeParameters(); 682 assertEquals(1, tv.length); 683 assertEquals(String.class, tv[0].getBounds()[0]); 684 } 685 686 class TempTestClass1<T> { 687 } 688 689 class TempTestClass2<S extends String> extends TempTestClass1<S> { 690 } 691 692 @TestTargetNew( 693 level = TestLevel.COMPLETE, 694 notes = "", 695 method = "isAnnotation", 696 args = {} 697 ) 698 public void test_isAnnotation() { 699 assertTrue(Deprecated.class.isAnnotation()); 700 assertTrue(TestAnnotation.class.isAnnotation()); 701 assertFalse(PublicTestClass.class.isAnnotation()); 702 assertFalse(String.class.isAnnotation()); 703 } 704 705 @TestTargetNew( 706 level = TestLevel.COMPLETE, 707 notes = "", 708 method = "isAnnotationPresent", 709 args = {java.lang.Class.class} 710 ) 711 public void test_isAnnotationPresent() { 712 assertTrue(PublicTestClass.class.isAnnotationPresent(TestAnnotation.class)); 713 assertFalse(ExtendTestClass1.class.isAnnotationPresent(TestAnnotation.class)); 714 assertFalse(String.class.isAnnotationPresent(Deprecated.class)); 715 assertTrue(ExtendTestClass.class.isAnnotationPresent(TestAnnotation.class)); 716 assertTrue(ExtendTestClass.class.isAnnotationPresent(Deprecated.class)); 717 } 718 719 @TestTargetNew( 720 level = TestLevel.COMPLETE, 721 notes = "", 722 method = "isAnonymousClass", 723 args = {} 724 ) 725 public void test_isAnonymousClass() { 726 assertFalse(PublicTestClass.class.isAnonymousClass()); 727 assertTrue((new Thread() {}).getClass().isAnonymousClass()); 728 } 729 730 @TestTargetNew( 731 level = TestLevel.COMPLETE, 732 notes = "", 733 method = "isEnum", 734 args = {} 735 ) 736 public void test_isEnum() { 737 assertFalse(PublicTestClass.class.isEnum()); 738 assertFalse(ExtendTestClass.class.isEnum()); 739 assertTrue(TestEnum.ONE.getClass().isEnum()); 740 assertTrue(TestEnum.class.isEnum()); 741 } 742 743 @TestTargetNew( 744 level = TestLevel.COMPLETE, 745 notes = "", 746 method = "isLocalClass", 747 args = {} 748 ) 749 public void test_isLocalClass() { 750 assertFalse(ExtendTestClass.class.isLocalClass()); 751 assertFalse(TestInterface.class.isLocalClass()); 752 assertFalse(TestEnum.class.isLocalClass()); 753 class InternalClass {} 754 assertTrue(InternalClass.class.isLocalClass()); 755 } 756 757 @TestTargetNew( 758 level = TestLevel.COMPLETE, 759 notes = "", 760 method = "isMemberClass", 761 args = {} 762 ) 763 public void test_isMemberClass() { 764 assertFalse(ExtendTestClass.class.isMemberClass()); 765 assertFalse(TestInterface.class.isMemberClass()); 766 assertFalse(String.class.isMemberClass()); 767 assertTrue(TestEnum.class.isMemberClass()); 768 assertTrue(StaticMember$Class.class.isMemberClass()); 769 } 770 771 @TestTargetNew( 772 level = TestLevel.COMPLETE, 773 notes = "", 774 method = "isSynthetic", 775 args = {} 776 ) 777 public void test_isSynthetic() { 778 assertFalse("Returned true for non synthetic class.", 779 ExtendTestClass.class.isSynthetic()); 780 assertFalse("Returned true for non synthetic class.", 781 TestInterface.class.isSynthetic()); 782 assertFalse("Returned true for non synthetic class.", 783 String.class.isSynthetic()); 784 785 String className = "org.apache.harmony.luni.tests.java.lang.ClassLoaderTest$1"; 786 787 /* 788 *try { 789 * assertTrue("Returned false for synthetic class.", 790 * getClass().getClassLoader().loadClass(className). 791 * isSynthetic()); 792 *} catch(ClassNotFoundException cnfe) { 793 * fail("Class " + className + " can't be found."); 794 *} 795 */ 796 797 } 798 799 @TestTargetNew( 800 level = TestLevel.COMPLETE, 801 notes = "", 802 method = "isInstance", 803 args = {java.lang.Object.class} 804 ) 805 public void test_isInstance() { 806 } 807 808 @TestTargetNew( 809 level = TestLevel.COMPLETE, 810 notes = "", 811 method = "getCanonicalName", 812 args = {} 813 ) 814 public void test_getCanonicalName() { 815 String name = int[].class.getCanonicalName(); 816 Class [] classArray = { int.class, int[].class, String.class, 817 PublicTestClass.class, TestInterface.class, 818 ExtendTestClass.class }; 819 String [] classNames = {"int", "int[]", "java.lang.String", 820 "org.apache.harmony.luni.tests.java.lang.PublicTestClass", 821 "org.apache.harmony.luni.tests.java.lang.TestInterface", 822 "org.apache.harmony.luni.tests.java.lang.ExtendTestClass"}; 823 824 for(int i = 0; i < classArray.length; i++) { 825 assertEquals(classNames[i], classArray[i].getCanonicalName()); 826 } 827 } 828 829 @TestTargetNew( 830 level = TestLevel.COMPLETE, 831 notes = "", 832 method = "getClassLoader", 833 args = {} 834 ) 835 public void test_getClassLoader() { 836 837 assertEquals(ExtendTestClass.class.getClassLoader(), 838 PublicTestClass.class.getClassLoader()); 839 840 assertNull(int.class.getClassLoader()); 841 assertNull(void.class.getClassLoader()); 842 843 SecurityManager sm = new SecurityManager() { 844 845 public void checkPermission(Permission perm) { 846 if ((perm instanceof RuntimePermission) && 847 perm.getName().equals("getClassLoader")) { 848 throw new SecurityException(); 849 } 850 } 851 }; 852 853 SecurityManager oldSm = System.getSecurityManager(); 854 System.setSecurityManager(sm); 855 try { 856 System.class.getClassLoader(); 857 } catch (SecurityException e) { 858 fail("SecurityException should not be thrown."); 859 } finally { 860 System.setSecurityManager(oldSm); 861 } 862 } 863 864 /** 865 * @tests java.lang.Class#getClasses() 866 */ 867 @TestTargetNew( 868 level = TestLevel.COMPLETE, 869 notes = "", 870 method = "getClasses", 871 args = {} 872 ) 873 public void test_getClasses() { 874 assertEquals("Incorrect class array returned", 875 4, ClassTest.class.getClasses().length); 876 } 877 878 /** 879 * @tests java.lang.Class#getClasses() 880 */ 881 @TestTargetNew( 882 level = TestLevel.NOT_FEASIBLE, 883 method = "getClasses", 884 args = {} 885 ) 886 @BrokenTest("Class.forName does not work with an URLClassLoader; " + 887 "the VMClassLoader does not support loading classes from a " + 888 "(jar) byte array.") 889 public void test_getClasses_subtest0() { 890 final Permission privCheckPermission = new BasicPermission("Privilege check") { 891 private static final long serialVersionUID = 1L; 892 }; 893 894 class MyCombiner implements DomainCombiner { 895 boolean combine; 896 897 public ProtectionDomain[] combine(ProtectionDomain[] executionDomains, 898 ProtectionDomain[] parentDomains) { 899 combine = true; 900 return new ProtectionDomain[0]; 901 } 902 903 private boolean recurring = false; 904 905 public boolean isPriviledged() { 906 if (recurring) { 907 return true; 908 } 909 try { 910 recurring = true; 911 combine = false; 912 try { 913 AccessController.checkPermission(privCheckPermission); 914 } catch (SecurityException e) {} 915 return !combine; 916 } finally { 917 recurring = false; 918 } 919 } 920 } 921 922 final MyCombiner combiner = new MyCombiner(); 923 class SecurityManagerCheck extends SecurityManager { 924 String reason; 925 926 Class<?> checkClass; 927 928 int checkType; 929 930 int checkPermission; 931 932 int checkMemberAccess; 933 934 int checkPackageAccess; 935 936 public void setExpected(String reason, Class<?> cls, int type) { 937 this.reason = reason; 938 checkClass = cls; 939 checkType = type; 940 checkPermission = 0; 941 checkMemberAccess = 0; 942 checkPackageAccess = 0; 943 } 944 945 @Override 946 public void checkPermission(Permission perm) { 947 if (combiner.isPriviledged()) 948 return; 949 checkPermission++; 950 } 951 952 @Override 953 public void checkMemberAccess(Class<?> cls, int type) { 954 if (combiner.isPriviledged()) 955 return; 956 checkMemberAccess++; 957 assertEquals(reason + " unexpected class", checkClass, cls); 958 assertEquals(reason + "unexpected type", checkType, type); 959 } 960 961 @Override 962 public void checkPackageAccess(String packageName) { 963 if (combiner.isPriviledged()) 964 return; 965 checkPackageAccess++; 966 String name = checkClass.getName(); 967 int index = name.lastIndexOf('.'); 968 String checkPackage = name.substring(0, index); 969 assertEquals(reason + " unexpected package", 970 checkPackage, packageName); 971 } 972 973 public void assertProperCalls() { 974 assertEquals(reason + " unexpected checkPermission count", 975 0, checkPermission); 976 assertEquals(reason + " unexpected checkMemberAccess count", 977 1, checkMemberAccess); 978 assertEquals(reason + " unexpected checkPackageAccess count", 979 1, checkPackageAccess); 980 } 981 } 982 983 AccessControlContext acc = new AccessControlContext(new ProtectionDomain[0]); 984 AccessControlContext acc2 = new AccessControlContext(acc, combiner); 985 986 PrivilegedAction<?> action = new PrivilegedAction<Object>() { 987 public Object run() { 988 File resources = Support_Resources.createTempFolder(); 989 try { 990 Support_Resources.copyFile(resources, null, "hyts_security.jar"); 991 File file = new File(resources.toString() + "/hyts_security.jar"); 992 URL url = new URL("file:" + file.getPath()); 993 ClassLoader loader = new URLClassLoader(new URL[] { url }, null); 994 Class<?> cls = Class.forName("packB.SecurityTestSub", false, loader); 995 SecurityManagerCheck sm = new SecurityManagerCheck(); 996 System.setSecurityManager(sm); 997 try { 998 sm.setExpected("getClasses", cls, Member.PUBLIC); 999 cls.getClasses(); 1000 sm.assertProperCalls(); 1001 1002 sm.setExpected("getDeclaredClasses", cls, Member.DECLARED); 1003 cls.getDeclaredClasses(); 1004 sm.assertProperCalls(); 1005 1006 sm.setExpected("getConstructor", cls, Member.PUBLIC); 1007 cls.getConstructor(new Class[0]); 1008 sm.assertProperCalls(); 1009 1010 sm.setExpected("getConstructors", cls, Member.PUBLIC); 1011 cls.getConstructors(); 1012 sm.assertProperCalls(); 1013 1014 sm.setExpected("getDeclaredConstructor", cls, Member.DECLARED); 1015 cls.getDeclaredConstructor(new Class[0]); 1016 sm.assertProperCalls(); 1017 1018 sm.setExpected("getDeclaredConstructors", cls, Member.DECLARED); 1019 cls.getDeclaredConstructors(); 1020 sm.assertProperCalls(); 1021 1022 sm.setExpected("getField", cls, Member.PUBLIC); 1023 cls.getField("publicField"); 1024 sm.assertProperCalls(); 1025 1026 sm.setExpected("getFields", cls, Member.PUBLIC); 1027 cls.getFields(); 1028 sm.assertProperCalls(); 1029 1030 sm.setExpected("getDeclaredField", cls, Member.DECLARED); 1031 cls.getDeclaredField("publicField"); 1032 sm.assertProperCalls(); 1033 1034 sm.setExpected("getDeclaredFields", cls, Member.DECLARED); 1035 cls.getDeclaredFields(); 1036 sm.assertProperCalls(); 1037 1038 sm.setExpected("getDeclaredMethod", cls, Member.DECLARED); 1039 cls.getDeclaredMethod("publicMethod", new Class[0]); 1040 sm.assertProperCalls(); 1041 1042 sm.setExpected("getDeclaredMethods", cls, Member.DECLARED); 1043 cls.getDeclaredMethods(); 1044 sm.assertProperCalls(); 1045 1046 sm.setExpected("getMethod", cls, Member.PUBLIC); 1047 cls.getMethod("publicMethod", new Class[0]); 1048 sm.assertProperCalls(); 1049 1050 sm.setExpected("getMethods", cls, Member.PUBLIC); 1051 cls.getMethods(); 1052 sm.assertProperCalls(); 1053 1054 sm.setExpected("newInstance", cls, Member.PUBLIC); 1055 cls.newInstance(); 1056 sm.assertProperCalls(); 1057 } finally { 1058 System.setSecurityManager(null); 1059 } 1060/* Remove this comment to let the test pass on Android. 1061 } catch (java.lang.ClassNotFoundException ce) { 1062 // Expected for Android. 1063*/ 1064 } catch (Exception e) { 1065 if (e instanceof RuntimeException) 1066 throw (RuntimeException) e; 1067 fail("unexpected exception: " + e); 1068 } 1069 return null; 1070 } 1071 }; 1072 AccessController.doPrivileged(action, acc2); 1073 } 1074 1075 /** 1076 * @tests java.lang.Class#getComponentType() 1077 */ 1078 @TestTargetNew( 1079 level = TestLevel.COMPLETE, 1080 notes = "", 1081 method = "getComponentType", 1082 args = {} 1083 ) 1084 public void test_getComponentType() { 1085 assertSame("int array does not have int component type", int.class, int[].class 1086 .getComponentType()); 1087 assertSame("Object array does not have Object component type", Object.class, 1088 Object[].class.getComponentType()); 1089 assertNull("Object has non-null component type", Object.class.getComponentType()); 1090 } 1091 1092 /** 1093 * @tests java.lang.Class#getConstructor(java.lang.Class[]) 1094 */ 1095 @TestTargetNew( 1096 level = TestLevel.COMPLETE, 1097 notes = "", 1098 method = "getConstructor", 1099 args = {java.lang.Class[].class} 1100 ) 1101 public void test_getConstructor$Ljava_lang_Class() 1102 throws NoSuchMethodException { 1103 Constructor constr = TestClass.class.getConstructor(new Class[0]); 1104 assertNotNull(constr); 1105 assertEquals("org.apache.harmony.luni.tests.java.lang.ClassTest$TestClass", 1106 constr.getName()); 1107 try { 1108 TestClass.class.getConstructor(Object.class); 1109 fail("Found private constructor"); 1110 } catch (NoSuchMethodException e) { 1111 // Correct - constructor with obj is private 1112 } 1113 1114 SecurityManager oldSm = System.getSecurityManager(); 1115 System.setSecurityManager(sm); 1116 try { 1117 TestClass.class.getConstructor(new Class[0]); 1118 fail("Should throw SecurityException"); 1119 } catch (SecurityException e) { 1120 // expected 1121 } finally { 1122 System.setSecurityManager(oldSm); 1123 } 1124 } 1125 1126 /** 1127 * @tests java.lang.Class#getConstructors() 1128 */ 1129 @TestTargetNew( 1130 level = TestLevel.COMPLETE, 1131 notes = "", 1132 method = "getConstructors", 1133 args = {} 1134 ) 1135 public void test_getConstructors() throws Exception { 1136 Constructor[] c = TestClass.class.getConstructors(); 1137 assertEquals("Incorrect number of constructors returned", 1, c.length); 1138 1139 SecurityManager oldSm = System.getSecurityManager(); 1140 System.setSecurityManager(sm); 1141 try { 1142 TestClass.class.getConstructors(); 1143 fail("Should throw SecurityException"); 1144 } catch (SecurityException e) { 1145 // expected 1146 } finally { 1147 System.setSecurityManager(oldSm); 1148 } 1149 } 1150 1151 /** 1152 * @tests java.lang.Class#getDeclaredClasses() 1153 */ 1154 @TestTargetNew( 1155 level = TestLevel.COMPLETE, 1156 notes = "", 1157 method = "getDeclaredClasses", 1158 args = {} 1159 ) 1160 public void test_getDeclaredClasses() { 1161 1162 Class [] declClasses = Object.class.getDeclaredClasses(); 1163 assertEquals("Incorrect length of declared classes array is returned " + 1164 "for Object.", 0, declClasses.length); 1165 1166 declClasses = PublicTestClass.class.getDeclaredClasses(); 1167 assertEquals(2, declClasses.length); 1168 1169 assertEquals(0, int.class.getDeclaredClasses().length); 1170 assertEquals(0, void.class.getDeclaredClasses().length); 1171 1172 for(int i = 0; i < declClasses.length; i++) { 1173 Constructor<?> constr = declClasses[i].getDeclaredConstructors()[0]; 1174 constr.setAccessible(true); 1175 PublicTestClass publicClazz = new PublicTestClass(); 1176 try { 1177 Object o = constr.newInstance(publicClazz); 1178 assertTrue("Returned incorrect class: " + o.toString(), 1179 o.toString().startsWith("PrivateClass")); 1180 } catch(Exception e) { 1181 fail("Unexpected exception was thrown: " + e.toString()); 1182 } 1183 } 1184 1185 1186 declClasses = TestInterface.class.getDeclaredClasses(); 1187 assertEquals(0, declClasses.length); 1188 1189 SecurityManager sm = new SecurityManager() { 1190 1191 final String forbidenPermissionName = "user.dir"; 1192 1193 public void checkPermission(Permission perm) { 1194 if (perm.getName().equals(forbidenPermissionName)) { 1195 throw new SecurityException(); 1196 } 1197 } 1198 1199 public void checkMemberAccess(Class<?> clazz, 1200 int which) { 1201 if(clazz.equals(TestInterface.class)) { 1202 throw new SecurityException(); 1203 } 1204 } 1205 1206 public void checkPackageAccess(String pkg) { 1207 if(pkg.equals(PublicTestClass.class.getPackage())) { 1208 throw new SecurityException(); 1209 } 1210 } 1211 1212 }; 1213 1214 SecurityManager oldSm = System.getSecurityManager(); 1215 System.setSecurityManager(sm); 1216 try { 1217 TestInterface.class.getDeclaredClasses(); 1218 fail("Should throw SecurityException"); 1219 } catch (SecurityException e) { 1220 // expected 1221 } finally { 1222 System.setSecurityManager(oldSm); 1223 } 1224 1225 } 1226 1227 1228 /** 1229 * @tests java.lang.Class#getDeclaredConstructor(java.lang.Class[]) 1230 */ 1231 @TestTargetNew( 1232 level = TestLevel.COMPLETE, 1233 notes = "", 1234 method = "getDeclaredConstructor", 1235 args = {java.lang.Class[].class} 1236 ) 1237 public void test_getDeclaredConstructor$Ljava_lang_Class() throws Exception { 1238 Constructor<TestClass> c = TestClass.class.getDeclaredConstructor(new Class[0]); 1239 assertNull("Incorrect constructor returned", c.newInstance().cValue()); 1240 c = TestClass.class.getDeclaredConstructor(Object.class); 1241 1242 try { 1243 TestClass.class.getDeclaredConstructor(String.class); 1244 fail("NoSuchMethodException should be thrown."); 1245 } catch(NoSuchMethodException nsme) { 1246 //expected 1247 } 1248 1249 SecurityManager oldSm = System.getSecurityManager(); 1250 System.setSecurityManager(sm); 1251 try { 1252 TestClass.class.getDeclaredConstructor(Object.class); 1253 fail("Should throw SecurityException"); 1254 } catch (SecurityException e) { 1255 // expected 1256 } finally { 1257 System.setSecurityManager(oldSm); 1258 } 1259 } 1260 1261 /** 1262 * @tests java.lang.Class#getDeclaredConstructors() 1263 */ 1264 @TestTargetNew( 1265 level = TestLevel.COMPLETE, 1266 notes = "", 1267 method = "getDeclaredConstructors", 1268 args = {} 1269 ) 1270 public void test_getDeclaredConstructors() throws Exception { 1271 Constructor[] c = TestClass.class.getDeclaredConstructors(); 1272 assertEquals("Incorrect number of constructors returned", 2, c.length); 1273 1274 SecurityManager oldSm = System.getSecurityManager(); 1275 System.setSecurityManager(sm); 1276 try { 1277 TestClass.class.getDeclaredConstructors(); 1278 fail("Should throw SecurityException"); 1279 } catch (SecurityException e) { 1280 // expected 1281 } finally { 1282 System.setSecurityManager(oldSm); 1283 } 1284 } 1285 1286 /** 1287 * @tests java.lang.Class#getDeclaredField(java.lang.String) 1288 */ 1289 @TestTargetNew( 1290 level = TestLevel.COMPLETE, 1291 notes = "", 1292 method = "getDeclaredField", 1293 args = {java.lang.String.class} 1294 ) 1295 public void test_getDeclaredFieldLjava_lang_String() throws Exception { 1296 Field f = TestClass.class.getDeclaredField("pubField"); 1297 assertEquals("Returned incorrect field", 2, f.getInt(new TestClass())); 1298 1299 try { 1300 TestClass.class.getDeclaredField(null); 1301 fail("NullPointerException is not thrown."); 1302 } catch(NullPointerException npe) { 1303 //expected 1304 } 1305 1306 try { 1307 TestClass.class.getDeclaredField("NonExistentField"); 1308 fail("NoSuchFieldException is not thrown."); 1309 } catch(NoSuchFieldException nsfe) { 1310 //expected 1311 } 1312 1313 SecurityManager oldSm = System.getSecurityManager(); 1314 System.setSecurityManager(sm); 1315 try { 1316 TestClass.class.getDeclaredField("pubField"); 1317 fail("Should throw SecurityException"); 1318 } catch (SecurityException e) { 1319 // expected 1320 } finally { 1321 System.setSecurityManager(oldSm); 1322 } 1323 } 1324 1325 /** 1326 * @tests java.lang.Class#getDeclaredFields() 1327 */ 1328 @TestTargetNew( 1329 level = TestLevel.COMPLETE, 1330 notes = "", 1331 method = "getDeclaredFields", 1332 args = {} 1333 ) 1334 public void test_getDeclaredFields() throws Exception { 1335 Field[] f = TestClass.class.getDeclaredFields(); 1336 assertEquals("Returned incorrect number of fields", 4, f.length); 1337 f = SubTestClass.class.getDeclaredFields(); 1338 // Declared fields do not include inherited 1339 assertEquals("Returned incorrect number of fields", 0, f.length); 1340 1341 SecurityManager oldSm = System.getSecurityManager(); 1342 System.setSecurityManager(sm); 1343 try { 1344 TestClass.class.getDeclaredFields(); 1345 fail("Should throw SecurityException"); 1346 } catch (SecurityException e) { 1347 // expected 1348 } finally { 1349 System.setSecurityManager(oldSm); 1350 } 1351 } 1352 1353 /** 1354 * @tests java.lang.Class#getDeclaredMethod(java.lang.String, 1355 * java.lang.Class[]) 1356 */ 1357 @TestTargetNew( 1358 level = TestLevel.COMPLETE, 1359 notes = "", 1360 method = "getDeclaredMethod", 1361 args = {java.lang.String.class, java.lang.Class[].class} 1362 ) 1363 public void test_getDeclaredMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 1364 Method m = TestClass.class.getDeclaredMethod("pubMethod", new Class[0]); 1365 assertEquals("Returned incorrect method", 2, ((Integer) (m.invoke(new TestClass()))) 1366 .intValue()); 1367 m = TestClass.class.getDeclaredMethod("privMethod", new Class[0]); 1368 1369 try { 1370 TestClass.class.getDeclaredMethod(null, new Class[0]); 1371 fail("NullPointerException is not thrown."); 1372 } catch(NullPointerException npe) { 1373 //expected 1374 } 1375 1376 try { 1377 TestClass.class.getDeclaredMethod("NonExistentMethod", new Class[0]); 1378 fail("NoSuchMethodException is not thrown."); 1379 } catch(NoSuchMethodException nsme) { 1380 //expected 1381 } 1382 1383 SecurityManager oldSm = System.getSecurityManager(); 1384 System.setSecurityManager(sm); 1385 try { 1386 TestClass.class.getDeclaredMethod("pubMethod", new Class[0]); 1387 fail("Should throw SecurityException"); 1388 } catch (SecurityException e) { 1389 // expected 1390 } finally { 1391 System.setSecurityManager(oldSm); 1392 } 1393 } 1394 1395 /** 1396 * @tests java.lang.Class#getDeclaredMethods() 1397 */ 1398 @TestTargetNew( 1399 level = TestLevel.COMPLETE, 1400 notes = "", 1401 method = "getDeclaredMethods", 1402 args = {} 1403 ) 1404 public void test_getDeclaredMethods() throws Exception { 1405 Method[] m = TestClass.class.getDeclaredMethods(); 1406 assertEquals("Returned incorrect number of methods", 3, m.length); 1407 m = SubTestClass.class.getDeclaredMethods(); 1408 assertEquals("Returned incorrect number of methods", 0, m.length); 1409 1410 SecurityManager oldSm = System.getSecurityManager(); 1411 System.setSecurityManager(sm); 1412 try { 1413 TestClass.class.getDeclaredMethods(); 1414 fail("Should throw SecurityException"); 1415 } catch (SecurityException e) { 1416 // expected 1417 } finally { 1418 System.setSecurityManager(oldSm); 1419 } 1420 } 1421 1422 /** 1423 * @tests java.lang.Class#getDeclaringClass() 1424 */ 1425 @TestTargetNew( 1426 level = TestLevel.COMPLETE, 1427 notes = "", 1428 method = "getDeclaringClass", 1429 args = {} 1430 ) 1431 public void test_getDeclaringClass() { 1432 assertEquals(ClassTest.class, TestClass.class.getDeclaringClass()); 1433 assertNull(PublicTestClass.class.getDeclaringClass()); 1434 } 1435 1436 /** 1437 * @tests java.lang.Class#getField(java.lang.String) 1438 */ 1439 @TestTargetNew( 1440 level = TestLevel.COMPLETE, 1441 notes = "", 1442 method = "getField", 1443 args = {java.lang.String.class} 1444 ) 1445 public void test_getFieldLjava_lang_String() throws Exception { 1446 Field f = TestClass.class.getField("pubField"); 1447 assertEquals("Returned incorrect field", 2, f.getInt(new TestClass())); 1448 1449 f = PublicTestClass.class.getField("TEST_FIELD"); 1450 assertEquals("Returned incorrect field", "test field", 1451 f.get(new PublicTestClass())); 1452 1453 f = PublicTestClass.class.getField("TEST_INTERFACE_FIELD"); 1454 assertEquals("Returned incorrect field", 0, 1455 f.getInt(new PublicTestClass())); 1456 1457 try { 1458 f = TestClass.class.getField("privField"); 1459 fail("Private field access failed to throw exception"); 1460 } catch (NoSuchFieldException e) { 1461 // Correct 1462 } 1463 1464 try { 1465 TestClass.class.getField(null); 1466 fail("NullPointerException is thrown."); 1467 } catch(NullPointerException npe) { 1468 //expected 1469 } 1470 1471 SecurityManager sm = new SecurityManager() { 1472 1473 final String forbidenPermissionName = "user.dir"; 1474 1475 public void checkPermission(Permission perm) { 1476 if (perm.getName().equals(forbidenPermissionName)) { 1477 throw new SecurityException(); 1478 } 1479 } 1480 1481 public void checkMemberAccess(Class<?> clazz, 1482 int which) { 1483 if(clazz.equals(TestClass.class)) { 1484 throw new SecurityException(); 1485 } 1486 } 1487 1488 public void checkPackageAccess(String pkg) { 1489 if(pkg.equals(TestClass.class.getPackage())) { 1490 throw new SecurityException(); 1491 } 1492 } 1493 1494 }; 1495 1496 SecurityManager oldSm = System.getSecurityManager(); 1497 System.setSecurityManager(sm); 1498 try { 1499 TestClass.class.getField("pubField"); 1500 fail("Should throw SecurityException"); 1501 } catch (SecurityException e) { 1502 // expected 1503 } finally { 1504 System.setSecurityManager(oldSm); 1505 } 1506 } 1507 1508 /** 1509 * @tests java.lang.Class#getFields() 1510 */ 1511 @TestTargetNew( 1512 level = TestLevel.PARTIAL_COMPLETE, 1513 notes = "", 1514 method = "getFields", 1515 args = {} 1516 ) 1517 public void test_getFields2() throws Exception { 1518 Field[] f; 1519 Field expected = null; 1520 1521 f = PublicTestClass.class.getFields(); 1522 assertEquals("Test 1: Incorrect number of fields;", 2, f.length); 1523 1524 f = Cls2.class.getFields(); 1525 assertEquals("Test 2: Incorrect number of fields;", 6, f.length); 1526 1527 f = Cls3.class.getFields(); 1528 assertEquals("Test 2: Incorrect number of fields;", 5, f.length); 1529 1530 for (Field field : f) { 1531 if (field.toString().equals("public static final int org.apache" + 1532 ".harmony.luni.tests.java.lang.ClassTest$Intf3.field1")) { 1533 expected = field; 1534 break; 1535 } 1536 } 1537 if (expected == null) { 1538 fail("Test 3: getFields() did not return all fields."); 1539 } 1540 assertEquals("Test 4: Incorrect field;", expected, 1541 Cls3.class.getField("field1")); 1542 1543 expected = null; 1544 for (Field field : f) { 1545 if(field.toString().equals("public static final int org.apache" + 1546 ".harmony.luni.tests.java.lang.ClassTest$Intf1.field2")) { 1547 expected = field; 1548 break; 1549 } 1550 } 1551 if (expected == null) { 1552 fail("Test 5: getFields() did not return all fields."); 1553 } 1554 assertEquals("Test 6: Incorrect field;", expected, 1555 Cls3.class.getField("field2")); 1556 } 1557 1558 /** 1559 * @tests java.lang.Class#getFields() 1560 */ 1561 @TestTargetNew( 1562 level = TestLevel.PARTIAL_COMPLETE, 1563 notes = "", 1564 method = "getFields", 1565 args = {} 1566 ) 1567 public void test_getFields() throws Exception { 1568 Field[] f = TestClass.class.getFields(); 1569 assertEquals("Test 1: Incorrect number of fields;", 2, f.length); 1570 f = SubTestClass.class.getFields(); 1571 // Check inheritance of pub fields 1572 assertEquals("Test 2: Incorrect number of fields;", 2, f.length); 1573 1574 SecurityManager oldSm = System.getSecurityManager(); 1575 System.setSecurityManager(sm); 1576 try { 1577 TestClass.class.getFields(); 1578 fail("Should throw SecurityException"); 1579 } catch (SecurityException e) { 1580 // expected 1581 } finally { 1582 System.setSecurityManager(oldSm); 1583 } 1584 1585 Field expected = null; 1586 Field[] fields = Cls2.class.getFields(); 1587 for (Field field : fields) { 1588 if(field.toString().equals("public int org.apache.harmony.luni" + 1589 ".tests.java.lang.ClassTest$Cls2.field1")) { 1590 expected = field; 1591 break; 1592 } 1593 } 1594 if (expected == null) { 1595 fail("getFields() did not return all fields"); 1596 } 1597 assertEquals(expected, Cls2.class.getField("field1")); 1598 } 1599 1600 /** 1601 * @tests java.lang.Class#getInterfaces() 1602 */ 1603 @TestTargetNew( 1604 level = TestLevel.COMPLETE, 1605 notes = "", 1606 method = "getInterfaces", 1607 args = {} 1608 ) 1609 public void test_getInterfaces() { 1610 Class[] interfaces; 1611 List<?> interfaceList; 1612 interfaces = Object.class.getInterfaces(); 1613 assertEquals("Incorrect interface list for Object", 0, interfaces.length); 1614 interfaceList = Arrays.asList(Vector.class.getInterfaces()); 1615 assertTrue("Incorrect interface list for Vector", interfaceList 1616 .contains(Cloneable.class) 1617 && interfaceList.contains(Serializable.class) 1618 && interfaceList.contains(List.class)); 1619 1620 Class [] interfaces1 = Cls1.class.getInterfaces(); 1621 assertEquals(1, interfaces1.length); 1622 assertEquals(Intf2.class, interfaces1[0]); 1623 1624 Class [] interfaces2 = Cls2.class.getInterfaces(); 1625 assertEquals(1, interfaces2.length); 1626 assertEquals(Intf1.class, interfaces2[0]); 1627 1628 Class [] interfaces3 = Cls3.class.getInterfaces(); 1629 assertEquals(2, interfaces3.length); 1630 assertEquals(Intf3.class, interfaces3[0]); 1631 assertEquals(Intf4.class, interfaces3[1]); 1632 1633 Class [] interfaces4 = Cls4.class.getInterfaces(); 1634 assertEquals(0, interfaces4.length); 1635 } 1636 1637 /** 1638 * @tests java.lang.Class#getMethod(java.lang.String, java.lang.Class[]) 1639 */ 1640 @TestTargetNew( 1641 level = TestLevel.COMPLETE, 1642 notes = "", 1643 method = "getMethod", 1644 args = {java.lang.String.class, java.lang.Class[].class} 1645 ) 1646 public void test_getMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 1647 Method m = TestClass.class.getMethod("pubMethod", new Class[0]); 1648 assertEquals("Returned incorrect method", 2, ((Integer) (m.invoke(new TestClass()))) 1649 .intValue()); 1650 1651 m = ExtendTestClass1.class.getMethod("getCount", new Class[0]); 1652 assertEquals("Returned incorrect method", 0, ((Integer) (m.invoke(new ExtendTestClass1()))) 1653 .intValue()); 1654 1655 try { 1656 m = TestClass.class.getMethod("privMethod", new Class[0]); 1657 fail("Failed to throw exception accessing private method"); 1658 } catch (NoSuchMethodException e) { 1659 // Correct 1660 return; 1661 } 1662 1663 try { 1664 m = TestClass.class.getMethod("init", new Class[0]); 1665 fail("Failed to throw exception accessing to init method"); 1666 } catch (NoSuchMethodException e) { 1667 // Correct 1668 return; 1669 } 1670 1671 try { 1672 TestClass.class.getMethod("pubMethod", new Class[0]); 1673 fail("NullPointerException is not thrown."); 1674 } catch(NullPointerException npe) { 1675 //expected 1676 } 1677 1678 SecurityManager oldSm = System.getSecurityManager(); 1679 System.setSecurityManager(sm); 1680 try { 1681 TestClass.class.getMethod("pubMethod", new Class[0]); 1682 fail("Should throw SecurityException"); 1683 } catch (SecurityException e) { 1684 // expected 1685 } finally { 1686 System.setSecurityManager(oldSm); 1687 } 1688 } 1689 1690 /** 1691 * @tests java.lang.Class#getMethods() 1692 */ 1693 @TestTargetNew( 1694 level = TestLevel.COMPLETE, 1695 notes = "", 1696 method = "getMethods", 1697 args = {} 1698 ) 1699 public void test_getMethods() throws Exception { 1700 Method[] m = TestClass.class.getMethods(); 1701 assertEquals("Returned incorrect number of methods", 1702 2 + Object.class.getMethods().length, m.length); 1703 m = SubTestClass.class.getMethods(); 1704 assertEquals("Returned incorrect number of sub-class methods", 1705 2 + Object.class.getMethods().length, m.length); 1706 // Number of inherited methods 1707 1708 SecurityManager oldSm = System.getSecurityManager(); 1709 System.setSecurityManager(sm); 1710 try { 1711 TestClass.class.getMethods(); 1712 fail("Should throw SecurityException"); 1713 } catch (SecurityException e) { 1714 // expected 1715 } finally { 1716 System.setSecurityManager(oldSm); 1717 } 1718 1719 assertEquals("Incorrect number of methods", 10, 1720 Cls2.class.getMethods().length); 1721 assertEquals("Incorrect number of methods", 11, 1722 Cls3.class.getMethods().length); 1723 1724 Method expected = null; 1725 Method[] methods = Cls2.class.getMethods(); 1726 for (Method method : methods) { 1727 if(method.toString().equals("public void org.apache.harmony.luni" + 1728 ".tests.java.lang.ClassTest$Cls2.test()")) { 1729 expected = method; 1730 break; 1731 } 1732 } 1733 if (expected == null) { 1734 fail("getMethods() did not return all methods"); 1735 } 1736 assertEquals(expected, Cls2.class.getMethod("test")); 1737 1738 expected = null; 1739 methods = Cls3.class.getMethods(); 1740 for (Method method : methods) { 1741 if(method.toString().equals("public void org.apache.harmony.luni" + 1742 ".tests.java.lang.ClassTest$Cls3.test()")) { 1743 expected = method; 1744 break; 1745 } 1746 } 1747 if (expected == null) { 1748 fail("getMethods() did not return all methods"); 1749 } 1750 assertEquals(expected, Cls3.class.getMethod("test")); 1751 1752 expected = null; 1753 methods = Cls3.class.getMethods(); 1754 for (Method method : methods) { 1755 if(method.toString().equals("public void org.apache.harmony.luni" + 1756 ".tests.java.lang.ClassTest$Cls3.test2(int," + 1757 "java.lang.Object)")) { 1758 expected = method; 1759 break; 1760 } 1761 } 1762 if (expected == null) { 1763 fail("getMethods() did not return all methods"); 1764 } 1765 1766 assertEquals(expected, Cls3.class.getMethod("test2", int.class, 1767 Object.class)); 1768 1769 assertEquals("Incorrect number of methods", 1, 1770 Intf5.class.getMethods().length); 1771 } 1772 1773 private static final class PrivateClass { 1774 } 1775 /** 1776 * @tests java.lang.Class#getModifiers() 1777 */ 1778 @TestTargetNew( 1779 level = TestLevel.COMPLETE, 1780 notes = "", 1781 method = "getModifiers", 1782 args = {} 1783 ) 1784 public void test_getModifiers() { 1785 int dcm = PrivateClass.class.getModifiers(); 1786 assertFalse("default class is public", Modifier.isPublic(dcm)); 1787 assertFalse("default class is protected", Modifier.isProtected(dcm)); 1788 assertTrue("default class is not private", Modifier.isPrivate(dcm)); 1789 1790 int ocm = Object.class.getModifiers(); 1791 assertTrue("public class is not public", Modifier.isPublic(ocm)); 1792 assertFalse("public class is protected", Modifier.isProtected(ocm)); 1793 assertFalse("public class is private", Modifier.isPrivate(ocm)); 1794 } 1795 1796 /** 1797 * @tests java.lang.Class#getName() 1798 */ 1799 @TestTargetNew( 1800 level = TestLevel.COMPLETE, 1801 notes = "", 1802 method = "getName", 1803 args = {} 1804 ) 1805 public void test_getName() throws Exception { 1806 String className = Class.forName("java.lang.Object").getName(); 1807 assertNotNull(className); 1808 1809 assertEquals("Class getName printed wrong value", "java.lang.Object", className); 1810 assertEquals("Class getName printed wrong value", "int", int.class.getName()); 1811 className = Class.forName("[I").getName(); 1812 assertNotNull(className); 1813 assertEquals("Class getName printed wrong value", "[I", className); 1814 1815 className = Class.forName("[Ljava.lang.Object;").getName(); 1816 assertNotNull(className); 1817 1818 assertEquals("Class getName printed wrong value", "[Ljava.lang.Object;", className); 1819 } 1820 1821 /** 1822 * @tests java.lang.Class#getResource(java.lang.String) 1823 */ 1824 @TestTargetNew( 1825 level = TestLevel.COMPLETE, 1826 notes = "", 1827 method = "getResource", 1828 args = {java.lang.String.class} 1829 ) 1830 public void test_getResourceLjava_lang_String() { 1831 final String name = "/"; 1832 URL res = getClass().getResource(name + "HelloWorld.txt"); 1833 assertNotNull(res); 1834 assertNull(getClass().getResource( 1835 "org/apache/harmony/luni/tests/java/lang/NonExistentResource")); 1836 assertNull(getClass().getResource(name + "NonExistentResource")); 1837 } 1838 1839 /** 1840 * @tests java.lang.Class#getResourceAsStream(java.lang.String) 1841 */ 1842 @TestTargetNew( 1843 level = TestLevel.PARTIAL_COMPLETE, 1844 notes = "", 1845 method = "getResourceAsStream", 1846 args = {java.lang.String.class} 1847 ) 1848 public void test_getResourceAsStreamLjava_lang_String() throws Exception { 1849 String name = "/HelloWorld.txt"; 1850 assertNotNull("the file " + name + " can not be found in this " + 1851 "directory", getClass().getResourceAsStream(name)); 1852 1853 final String nameBadURI = "org/apache/harmony/luni/tests/" + 1854 "test_resource.txt"; 1855 assertNull("the file " + nameBadURI + " should not be found in this " + 1856 "directory", 1857 getClass().getResourceAsStream(nameBadURI)); 1858 1859 ClassLoader pcl = getClass().getClassLoader(); 1860 Class<?> clazz = pcl.loadClass("org.apache.harmony.luni.tests.java.lang.ClassTest"); 1861 assertNotNull(clazz.getResourceAsStream("HelloWorld1.txt")); 1862 1863 try { 1864 getClass().getResourceAsStream(null); 1865 fail("NullPointerException is not thrown."); 1866 } catch(NullPointerException npe) { 1867 //expected 1868 } 1869 } 1870 1871 /** 1872 * @tests java.lang.Class#getSuperclass() 1873 */ 1874 @TestTargetNew( 1875 level = TestLevel.COMPLETE, 1876 notes = "", 1877 method = "getSuperclass", 1878 args = {} 1879 ) 1880 public void test_getSuperclass() { 1881 assertNull("Object has a superclass???", Object.class.getSuperclass()); 1882 assertSame("Normal class has bogus superclass", InputStream.class, 1883 FileInputStream.class.getSuperclass()); 1884 assertSame("Array class has bogus superclass", Object.class, FileInputStream[].class 1885 .getSuperclass()); 1886 assertNull("Base class has a superclass", int.class.getSuperclass()); 1887 assertNull("Interface class has a superclass", Cloneable.class.getSuperclass()); 1888 } 1889 1890 /** 1891 * @tests java.lang.Class#isArray() 1892 */ 1893 @TestTargetNew( 1894 level = TestLevel.COMPLETE, 1895 notes = "", 1896 method = "isArray", 1897 args = {} 1898 ) 1899 public void test_isArray() throws ClassNotFoundException { 1900 assertTrue("Non-array type claims to be.", !int.class.isArray()); 1901 Class<?> clazz = null; 1902 clazz = Class.forName("[I"); 1903 assertTrue("int Array type claims not to be.", clazz.isArray()); 1904 1905 clazz = Class.forName("[Ljava.lang.Object;"); 1906 assertTrue("Object Array type claims not to be.", clazz.isArray()); 1907 1908 clazz = Class.forName("java.lang.Object"); 1909 assertTrue("Non-array Object type claims to be.", !clazz.isArray()); 1910 } 1911 1912 /** 1913 * @tests java.lang.Class#isAssignableFrom(java.lang.Class) 1914 */ 1915 @TestTargetNew( 1916 level = TestLevel.COMPLETE, 1917 notes = "", 1918 method = "isAssignableFrom", 1919 args = {java.lang.Class.class} 1920 ) 1921 public void test_isAssignableFromLjava_lang_Class() { 1922 Class<?> clazz1 = null; 1923 Class<?> clazz2 = null; 1924 1925 clazz1 = Object.class; 1926 clazz2 = Class.class; 1927 assertTrue("returned false for superclass", 1928 clazz1.isAssignableFrom(clazz2)); 1929 1930 clazz1 = TestClass.class; 1931 assertTrue("returned false for same class", 1932 clazz1.isAssignableFrom(clazz1)); 1933 1934 clazz1 = Runnable.class; 1935 clazz2 = Thread.class; 1936 assertTrue("returned false for implemented interface", 1937 clazz1.isAssignableFrom(clazz2)); 1938 1939 assertFalse("returned true not assignable classes", 1940 Integer.class.isAssignableFrom(String.class)); 1941 1942 try { 1943 clazz1.isAssignableFrom(null); 1944 fail("NullPointerException is not thrown."); 1945 } catch(NullPointerException npe) { 1946 //expected 1947 } 1948 } 1949 1950 /** 1951 * @tests java.lang.Class#isInterface() 1952 */ 1953 @TestTargetNew( 1954 level = TestLevel.COMPLETE, 1955 notes = "", 1956 method = "isInterface", 1957 args = {} 1958 ) 1959 public void test_isInterface() throws ClassNotFoundException { 1960 assertTrue("Prim type claims to be interface.", !int.class.isInterface()); 1961 Class<?> clazz = null; 1962 clazz = Class.forName("[I"); 1963 assertTrue("Prim Array type claims to be interface.", !clazz.isInterface()); 1964 1965 clazz = Class.forName("java.lang.Runnable"); 1966 assertTrue("Interface type claims not to be interface.", clazz.isInterface()); 1967 clazz = Class.forName("java.lang.Object"); 1968 assertTrue("Object type claims to be interface.", !clazz.isInterface()); 1969 1970 clazz = Class.forName("[Ljava.lang.Object;"); 1971 assertTrue("Array type claims to be interface.", !clazz.isInterface()); 1972 } 1973 1974 /** 1975 * @tests java.lang.Class#isPrimitive() 1976 */ 1977 @TestTargetNew( 1978 level = TestLevel.COMPLETE, 1979 notes = "", 1980 method = "isPrimitive", 1981 args = {} 1982 ) 1983 public void test_isPrimitive() { 1984 assertFalse("Interface type claims to be primitive.", 1985 Runnable.class.isPrimitive()); 1986 assertFalse("Object type claims to be primitive.", 1987 Object.class.isPrimitive()); 1988 assertFalse("Prim Array type claims to be primitive.", 1989 int[].class.isPrimitive()); 1990 assertFalse("Array type claims to be primitive.", 1991 Object[].class.isPrimitive()); 1992 assertTrue("Prim type claims not to be primitive.", 1993 int.class.isPrimitive()); 1994 assertFalse("Object type claims to be primitive.", 1995 Object.class.isPrimitive()); 1996 } 1997 1998 /** 1999 * @tests java.lang.Class#newInstance() 2000 */ 2001 @TestTargetNew( 2002 level = TestLevel.PARTIAL_COMPLETE, 2003 notes = "", 2004 method = "newInstance", 2005 args = {} 2006 ) 2007 public void test_newInstance() throws Exception { 2008 Class<?> clazz = null; 2009 clazz = Class.forName("java.lang.Object"); 2010 assertNotNull("new object instance was null", clazz.newInstance()); 2011 2012 clazz = Class.forName("java.lang.Throwable"); 2013 assertSame("new Throwable instance was not a throwable", 2014 clazz, clazz.newInstance().getClass()); 2015 2016 clazz = Class.forName("java.lang.Integer"); 2017 try { 2018 clazz.newInstance(); 2019 fail("Exception for instantiating a newInstance with no default " + 2020 " constructor is not thrown"); 2021 } catch (InstantiationException e) { 2022 // expected 2023 } 2024 2025 try { 2026 TestClass3.class.newInstance(); 2027 fail("IllegalAccessException is not thrown."); 2028 } catch(IllegalAccessException iae) { 2029 //expected 2030 } 2031 2032 try { 2033 TestClass1C.class.newInstance(); 2034 fail("ExceptionInInitializerError should be thrown."); 2035 } catch (java.lang.ExceptionInInitializerError ie) { 2036 //expected 2037 } 2038 } 2039 2040 /** 2041 * @tests java.lang.Class#newInstance() 2042 */ 2043 @TestTargetNew( 2044 level = TestLevel.PARTIAL_COMPLETE, 2045 notes = "", 2046 method = "newInstance", 2047 args = {} 2048 ) 2049 public void test_newInstance2() throws Exception { 2050 SecurityManager oldSm = System.getSecurityManager(); 2051 System.setSecurityManager(sm); 2052 try { 2053 TestClass.class.newInstance(); 2054 fail("Test 1: SecurityException expected."); 2055 } catch (SecurityException e) { 2056 // expected 2057 } finally { 2058 System.setSecurityManager(oldSm); 2059 } 2060 } 2061 2062 /** 2063 * @tests java.lang.Class#toString() 2064 */ 2065 @TestTargetNew( 2066 level = TestLevel.COMPLETE, 2067 notes = "", 2068 method = "toString", 2069 args = {} 2070 ) 2071 public void test_toString() throws ClassNotFoundException { 2072 assertEquals("Class toString printed wrong value", 2073 "int", int.class.toString()); 2074 Class<?> clazz = null; 2075 clazz = Class.forName("[I"); 2076 assertEquals("Class toString printed wrong value", 2077 "class [I", clazz.toString()); 2078 2079 clazz = Class.forName("java.lang.Object"); 2080 assertEquals("Class toString printed wrong value", 2081 "class java.lang.Object", clazz.toString()); 2082 2083 clazz = Class.forName("[Ljava.lang.Object;"); 2084 assertEquals("Class toString printed wrong value", 2085 "class [Ljava.lang.Object;", clazz.toString()); 2086 } 2087 2088 @TestTargetNew( 2089 level = TestLevel.PARTIAL_COMPLETE, 2090 notes = "", 2091 method = "getResourceAsStream", 2092 args = {java.lang.String.class} 2093 ) 2094 // Regression Test for JIRA-2047 2095 public void test_getResourceAsStream_withSharpChar() throws Exception{ 2096 InputStream in = getClass().getResourceAsStream("/" + FILENAME); 2097 assertNotNull(in); 2098 in.close(); 2099 2100 in = getClass().getResourceAsStream(FILENAME); 2101 assertNull(in); 2102 2103 in = this.getClass().getClassLoader().getResourceAsStream( 2104 FILENAME); 2105 assertNotNull(in); 2106 in.close(); 2107 } 2108 2109 /* 2110 * Regression test for HARMONY-2644: 2111 * Load system and non-system array classes via Class.forName() 2112 */ 2113 @TestTargetNew( 2114 level = TestLevel.PARTIAL_COMPLETE, 2115 notes = "Verifies ClassNotFoundException.", 2116 method = "forName", 2117 args = {java.lang.String.class} 2118 ) 2119 public void test_forName_arrays() throws Exception { 2120 Class<?> c1 = getClass(); 2121 String s = c1.getName(); 2122 Class<?> a1 = Class.forName("[L" + s + ";"); 2123 Class<?> a2 = Class.forName("[[L" + s + ";"); 2124 assertSame(c1, a1.getComponentType()); 2125 assertSame(a1, a2.getComponentType()); 2126 Class<?> l4 = Class.forName("[[[[[J"); 2127 assertSame(long[][][][][].class, l4); 2128 2129 try{ 2130 Class<?> clazz = Class.forName("[;"); 2131 fail("1: " + clazz); 2132 } catch (ClassNotFoundException ok) {} 2133 try{ 2134 Class<?> clazz = Class.forName("[["); 2135 fail("2:" + clazz); 2136 } catch (ClassNotFoundException ok) {} 2137 try{ 2138 Class<?> clazz = Class.forName("[L"); 2139 fail("3:" + clazz); 2140 } catch (ClassNotFoundException ok) {} 2141 try{ 2142 Class<?> clazz = Class.forName("[L;"); 2143 fail("4:" + clazz); 2144 } catch (ClassNotFoundException ok) {} 2145 try{ 2146 Class<?> clazz = Class.forName(";"); 2147 fail("5:" + clazz); 2148 } catch (ClassNotFoundException ok) {} 2149 try{ 2150 Class<?> clazz = Class.forName(""); 2151 fail("6:" + clazz); 2152 } catch (ClassNotFoundException ok) {} 2153 } 2154 2155 @TestTargetNew( 2156 level = TestLevel.PARTIAL_COMPLETE, 2157 notes = "", 2158 method = "asSubclass", 2159 args = {java.lang.Class.class} 2160 ) 2161 public void test_asSubclass1() { 2162 assertEquals(ExtendTestClass.class, 2163 ExtendTestClass.class.asSubclass(PublicTestClass.class)); 2164 2165 assertEquals(PublicTestClass.class, 2166 PublicTestClass.class.asSubclass(TestInterface.class)); 2167 2168 assertEquals(ExtendTestClass1.class, 2169 ExtendTestClass1.class.asSubclass(PublicTestClass.class)); 2170 2171 assertEquals(PublicTestClass.class, 2172 PublicTestClass.class.asSubclass(PublicTestClass.class)); 2173 } 2174 2175 @TestTargetNew( 2176 level = TestLevel.PARTIAL_COMPLETE, 2177 notes = "", 2178 method = "asSubclass", 2179 args = {java.lang.Class.class} 2180 ) 2181 public void test_asSubclass2() { 2182 try { 2183 PublicTestClass.class.asSubclass(ExtendTestClass.class); 2184 fail("Test 1: ClassCastException expected."); 2185 } catch(ClassCastException cce) { 2186 // Expected. 2187 } 2188 2189 try { 2190 PublicTestClass.class.asSubclass(String.class); 2191 fail("Test 2: ClassCastException expected."); 2192 } catch(ClassCastException cce) { 2193 // Expected. 2194 } 2195 } 2196 2197 @TestTargetNew( 2198 level = TestLevel.COMPLETE, 2199 notes = "", 2200 method = "cast", 2201 args = {java.lang.Object.class} 2202 ) 2203 public void test_cast() { 2204 Object o = PublicTestClass.class.cast(new ExtendTestClass()); 2205 assertTrue(o instanceof ExtendTestClass); 2206 2207 try { 2208 ExtendTestClass.class.cast(new PublicTestClass()); 2209 fail("Test 1: ClassCastException expected."); 2210 } catch(ClassCastException cce) { 2211 //expected 2212 } 2213 2214 try { 2215 ExtendTestClass.class.cast(new String()); 2216 fail("ClassCastException is not thrown."); 2217 } catch(ClassCastException cce) { 2218 //expected 2219 } 2220 } 2221 2222 @TestTargetNew( 2223 level = TestLevel.COMPLETE, 2224 notes = "", 2225 method = "desiredAssertionStatus", 2226 args = {} 2227 ) 2228 public void test_desiredAssertionStatus() { 2229 Class [] classArray = { Object.class, Integer.class, 2230 String.class, PublicTestClass.class, 2231 ExtendTestClass.class, ExtendTestClass1.class}; 2232 2233 for(int i = 0; i < classArray.length; i++) { 2234 assertFalse("assertion status for " + classArray[i], 2235 classArray[i].desiredAssertionStatus()); 2236 } 2237 } 2238 2239 2240 2241 SecurityManager sm = new SecurityManager() { 2242 2243 final String forbidenPermissionName = "user.dir"; 2244 2245 public void checkPermission(Permission perm) { 2246 if (perm.getName().equals(forbidenPermissionName)) { 2247 throw new SecurityException(); 2248 } 2249 } 2250 2251 public void checkMemberAccess(Class<?> clazz, 2252 int which) { 2253 if(clazz.equals(TestClass.class)) { 2254 throw new SecurityException(); 2255 } 2256 } 2257 2258 public void checkPackageAccess(String pkg) { 2259 if(pkg.equals(TestClass.class.getPackage())) { 2260 throw new SecurityException(); 2261 } 2262 } 2263 2264 }; 2265} 2266