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