ClassTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.lang; 19 20import java.io.File; 21import java.io.FileInputStream; 22import java.io.FileOutputStream; 23import java.io.InputStream; 24import java.io.Serializable; 25import java.lang.reflect.Constructor; 26import java.lang.reflect.Field; 27import java.lang.reflect.Member; 28import java.lang.reflect.Method; 29import java.lang.reflect.Modifier; 30import java.net.URL; 31import java.net.URLClassLoader; 32import java.security.AccessControlContext; 33import java.security.AccessController; 34import java.security.BasicPermission; 35import java.security.DomainCombiner; 36import java.security.Permission; 37import java.security.PrivilegedAction; 38import java.security.ProtectionDomain; 39import java.security.Security; 40import java.util.Arrays; 41import java.util.List; 42import java.util.Vector; 43 44import tests.support.resource.Support_Resources; 45 46public class ClassTest extends junit.framework.TestCase { 47 48 public static final String FILENAME = ClassTest.class.getPackage().getName().replace('.', '/')+"/test#.properties"; 49 50 static class StaticMember$Class { 51 class Member2$A { 52 } 53 } 54 55 class Member$Class { 56 class Member3$B { 57 } 58 } 59 60 public static class TestClass { 61 @SuppressWarnings("unused") 62 private int privField = 1; 63 64 public int pubField = 2; 65 66 private Object cValue = null; 67 68 public Object ack = new Object(); 69 70 @SuppressWarnings("unused") 71 private int privMethod() { 72 return 1; 73 } 74 75 public int pubMethod() { 76 return 2; 77 } 78 79 public Object cValue() { 80 return cValue; 81 } 82 83 public TestClass() { 84 } 85 86 @SuppressWarnings("unused") 87 private TestClass(Object o) { 88 } 89 } 90 91 public static class SubTestClass extends TestClass { 92 } 93 94 /** 95 * @tests java.lang.Class#forName(java.lang.String) 96 */ 97 public void test_forNameLjava_lang_String() throws Exception { 98 assertSame("Class for name failed for java.lang.Object", 99 Object.class, Class.forName("java.lang.Object")); 100 assertSame("Class for name failed for [[Ljava.lang.Object;", 101 Object[][].class, Class.forName("[[Ljava.lang.Object;")); 102 103 assertSame("Class for name failed for [I", 104 int[].class, Class.forName("[I")); 105 106 try { 107 Class.forName("int"); 108 fail(); 109 } catch (ClassNotFoundException e) { 110 } 111 112 try { 113 Class.forName("byte"); 114 fail(); 115 } catch (ClassNotFoundException e) { 116 } 117 try { 118 Class.forName("char"); 119 fail(); 120 } catch (ClassNotFoundException e) { 121 } 122 123 try { 124 Class.forName("void"); 125 fail(); 126 } catch (ClassNotFoundException e) { 127 } 128 129 try { 130 Class.forName("short"); 131 fail(); 132 } catch (ClassNotFoundException e) { 133 } 134 try { 135 Class.forName("long"); 136 fail(); 137 } catch (ClassNotFoundException e) { 138 } 139 140 try { 141 Class.forName("boolean"); 142 fail(); 143 } catch (ClassNotFoundException e) { 144 } 145 try { 146 Class.forName("float"); 147 fail(); 148 } catch (ClassNotFoundException e) { 149 } 150 try { 151 Class.forName("double"); 152 fail(); 153 } catch (ClassNotFoundException e) { 154 } 155 156 //regression test for JIRA 2162 157 try { 158 Class.forName("%"); 159 fail("should throw ClassNotFoundException."); 160 } catch (ClassNotFoundException e) { 161 } 162 163 //Regression Test for HARMONY-3332 164 String securityProviderClassName; 165 int count = 1; 166 while ((securityProviderClassName = Security 167 .getProperty("security.provider." + count++)) != null) { 168 Class.forName(securityProviderClassName); 169 } 170 } 171 172 /** 173 * @tests java.lang.Class#getClasses() 174 */ 175 public void test_getClasses() { 176 assertEquals("Incorrect class array returned", 177 2, ClassTest.class.getClasses().length); 178 } 179 180 /** 181 * @tests java.lang.Class#getClasses() 182 */ 183 public void test_getClasses_subtest0() { 184 final Permission privCheckPermission = new BasicPermission("Privilege check") { 185 private static final long serialVersionUID = 1L; 186 }; 187 188 class MyCombiner implements DomainCombiner { 189 boolean combine; 190 191 public ProtectionDomain[] combine(ProtectionDomain[] executionDomains, 192 ProtectionDomain[] parentDomains) { 193 combine = true; 194 return new ProtectionDomain[0]; 195 } 196 197 private boolean recurring = false; 198 199 public boolean isPriviledged() { 200 if (recurring) { 201 return true; 202 } 203 try { 204 recurring = true; 205 combine = false; 206 try { 207 AccessController.checkPermission(privCheckPermission); 208 } catch (SecurityException e) {} 209 return !combine; 210 } finally { 211 recurring = false; 212 } 213 } 214 } 215 216 final MyCombiner combiner = new MyCombiner(); 217 class SecurityManagerCheck extends SecurityManager { 218 String reason; 219 220 Class<?> checkClass; 221 222 int checkType; 223 224 int checkPermission; 225 226 int checkMemberAccess; 227 228 int checkPackageAccess; 229 230 public void setExpected(String reason, Class<?> cls, int type) { 231 this.reason = reason; 232 checkClass = cls; 233 checkType = type; 234 checkPermission = 0; 235 checkMemberAccess = 0; 236 checkPackageAccess = 0; 237 } 238 239 @Override 240 public void checkPermission(Permission perm) { 241 if (combiner.isPriviledged()) 242 return; 243 checkPermission++; 244 } 245 246 @Override 247 public void checkMemberAccess(Class<?> cls, int type) { 248 if (combiner.isPriviledged()) 249 return; 250 checkMemberAccess++; 251 assertEquals(reason + " unexpected class", checkClass, cls); 252 assertEquals(reason + "unexpected type", checkType, type); 253 } 254 255 @Override 256 public void checkPackageAccess(String packageName) { 257 if (combiner.isPriviledged()) 258 return; 259 checkPackageAccess++; 260 String name = checkClass.getName(); 261 int index = name.lastIndexOf('.'); 262 String checkPackage = name.substring(0, index); 263 assertEquals(reason + " unexpected package", 264 checkPackage, packageName); 265 } 266 267 public void assertProperCalls() { 268 assertEquals(reason + " unexpected checkPermission count", 269 0, checkPermission); 270 assertEquals(reason + " unexpected checkMemberAccess count", 271 1, checkMemberAccess); 272 assertEquals(reason + " unexpected checkPackageAccess count", 273 1, checkPackageAccess); 274 } 275 } 276 277 AccessControlContext acc = new AccessControlContext(new ProtectionDomain[0]); 278 AccessControlContext acc2 = new AccessControlContext(acc, combiner); 279 280 PrivilegedAction<?> action = new PrivilegedAction<Object>() { 281 public Object run() { 282 File resources = Support_Resources.createTempFolder(); 283 try { 284 Support_Resources.copyFile(resources, null, "hyts_security.jar"); 285 File file = new File(resources.toString() + "/hyts_security.jar"); 286 URL url = new URL("file:" + file.getPath()); 287 ClassLoader loader = new URLClassLoader(new URL[] { url }, null); 288 Class<?> cls = Class.forName("packB.SecurityTestSub", false, loader); 289 SecurityManagerCheck sm = new SecurityManagerCheck(); 290 System.setSecurityManager(sm); 291 try { 292 sm.setExpected("getClasses", cls, Member.PUBLIC); 293 cls.getClasses(); 294 sm.assertProperCalls(); 295 296 sm.setExpected("getDeclaredClasses", cls, Member.DECLARED); 297 cls.getDeclaredClasses(); 298 sm.assertProperCalls(); 299 300 sm.setExpected("getConstructor", cls, Member.PUBLIC); 301 cls.getConstructor(new Class[0]); 302 sm.assertProperCalls(); 303 304 sm.setExpected("getConstructors", cls, Member.PUBLIC); 305 cls.getConstructors(); 306 sm.assertProperCalls(); 307 308 sm.setExpected("getDeclaredConstructor", cls, Member.DECLARED); 309 cls.getDeclaredConstructor(new Class[0]); 310 sm.assertProperCalls(); 311 312 sm.setExpected("getDeclaredConstructors", cls, Member.DECLARED); 313 cls.getDeclaredConstructors(); 314 sm.assertProperCalls(); 315 316 sm.setExpected("getField", cls, Member.PUBLIC); 317 cls.getField("publicField"); 318 sm.assertProperCalls(); 319 320 sm.setExpected("getFields", cls, Member.PUBLIC); 321 cls.getFields(); 322 sm.assertProperCalls(); 323 324 sm.setExpected("getDeclaredField", cls, Member.DECLARED); 325 cls.getDeclaredField("publicField"); 326 sm.assertProperCalls(); 327 328 sm.setExpected("getDeclaredFields", cls, Member.DECLARED); 329 cls.getDeclaredFields(); 330 sm.assertProperCalls(); 331 332 sm.setExpected("getDeclaredMethod", cls, Member.DECLARED); 333 cls.getDeclaredMethod("publicMethod", new Class[0]); 334 sm.assertProperCalls(); 335 336 sm.setExpected("getDeclaredMethods", cls, Member.DECLARED); 337 cls.getDeclaredMethods(); 338 sm.assertProperCalls(); 339 340 sm.setExpected("getMethod", cls, Member.PUBLIC); 341 cls.getMethod("publicMethod", new Class[0]); 342 sm.assertProperCalls(); 343 344 sm.setExpected("getMethods", cls, Member.PUBLIC); 345 cls.getMethods(); 346 sm.assertProperCalls(); 347 348 sm.setExpected("newInstance", cls, Member.PUBLIC); 349 cls.newInstance(); 350 sm.assertProperCalls(); 351 } finally { 352 System.setSecurityManager(null); 353 } 354 } catch (Exception e) { 355 if (e instanceof RuntimeException) 356 throw (RuntimeException) e; 357 fail("unexpected exception: " + e); 358 } 359 return null; 360 } 361 }; 362 AccessController.doPrivileged(action, acc2); 363 } 364 365 /** 366 * @tests java.lang.Class#getComponentType() 367 */ 368 public void test_getComponentType() { 369 assertSame("int array does not have int component type", int.class, int[].class 370 .getComponentType()); 371 assertSame("Object array does not have Object component type", Object.class, 372 Object[].class.getComponentType()); 373 assertNull("Object has non-null component type", Object.class.getComponentType()); 374 } 375 376 /** 377 * @tests java.lang.Class#getConstructor(java.lang.Class[]) 378 */ 379 public void test_getConstructor$Ljava_lang_Class() 380 throws NoSuchMethodException { 381 TestClass.class.getConstructor(new Class[0]); 382 try { 383 TestClass.class.getConstructor(Object.class); 384 fail("Found private constructor"); 385 } catch (NoSuchMethodException e) { 386 // Correct - constructor with obj is private 387 } 388 } 389 390 /** 391 * @tests java.lang.Class#getConstructors() 392 */ 393 public void test_getConstructors() throws Exception { 394 Constructor[] c = TestClass.class.getConstructors(); 395 assertEquals("Incorrect number of constructors returned", 1, c.length); 396 } 397 398 /** 399 * @tests java.lang.Class#getDeclaredClasses() 400 */ 401 public void test_getDeclaredClasses() { 402 assertEquals("Incorrect class array returned", 2, ClassTest.class.getClasses().length); 403 } 404 405 /** 406 * @tests java.lang.Class#getDeclaredConstructor(java.lang.Class[]) 407 */ 408 public void test_getDeclaredConstructor$Ljava_lang_Class() throws Exception { 409 Constructor<TestClass> c = TestClass.class.getDeclaredConstructor(new Class[0]); 410 assertNull("Incorrect constructor returned", c.newInstance().cValue()); 411 c = TestClass.class.getDeclaredConstructor(Object.class); 412 } 413 414 /** 415 * @tests java.lang.Class#getDeclaredConstructors() 416 */ 417 public void test_getDeclaredConstructors() throws Exception { 418 Constructor[] c = TestClass.class.getDeclaredConstructors(); 419 assertEquals("Incorrect number of constructors returned", 2, c.length); 420 } 421 422 /** 423 * @tests java.lang.Class#getDeclaredField(java.lang.String) 424 */ 425 public void test_getDeclaredFieldLjava_lang_String() throws Exception { 426 Field f = TestClass.class.getDeclaredField("pubField"); 427 assertEquals("Returned incorrect field", 2, f.getInt(new TestClass())); 428 } 429 430 /** 431 * @tests java.lang.Class#getDeclaredFields() 432 */ 433 public void test_getDeclaredFields() throws Exception { 434 Field[] f = TestClass.class.getDeclaredFields(); 435 assertEquals("Returned incorrect number of fields", 4, f.length); 436 f = SubTestClass.class.getDeclaredFields(); 437 // Declared fields do not include inherited 438 assertEquals("Returned incorrect number of fields", 0, f.length); 439 } 440 441 /** 442 * @tests java.lang.Class#getDeclaredMethod(java.lang.String, 443 * java.lang.Class[]) 444 */ 445 public void test_getDeclaredMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 446 Method m = TestClass.class.getDeclaredMethod("pubMethod", new Class[0]); 447 assertEquals("Returned incorrect method", 2, ((Integer) (m.invoke(new TestClass()))) 448 .intValue()); 449 m = TestClass.class.getDeclaredMethod("privMethod", new Class[0]); 450 } 451 452 /** 453 * @tests java.lang.Class#getDeclaredMethods() 454 */ 455 public void test_getDeclaredMethods() throws Exception { 456 Method[] m = TestClass.class.getDeclaredMethods(); 457 assertEquals("Returned incorrect number of methods", 3, m.length); 458 m = SubTestClass.class.getDeclaredMethods(); 459 assertEquals("Returned incorrect number of methods", 0, m.length); 460 } 461 462 /** 463 * @tests java.lang.Class#getDeclaringClass() 464 */ 465 public void test_getDeclaringClass() { 466 assertEquals(ClassTest.class, TestClass.class.getDeclaringClass()); 467 } 468 469 /** 470 * @tests java.lang.Class#getField(java.lang.String) 471 */ 472 public void test_getFieldLjava_lang_String() throws Exception { 473 Field f = TestClass.class.getField("pubField"); 474 assertEquals("Returned incorrect field", 2, f.getInt(new TestClass())); 475 try { 476 f = TestClass.class.getField("privField"); 477 fail("Private field access failed to throw exception"); 478 } catch (NoSuchFieldException e) { 479 // Correct 480 } 481 } 482 483 /** 484 * @tests java.lang.Class#getFields() 485 */ 486 public void test_getFields() throws Exception { 487 Field[] f = TestClass.class.getFields(); 488 assertEquals("Incorrect number of fields", 2, f.length); 489 f = SubTestClass.class.getFields(); 490 // Check inheritance of pub fields 491 assertEquals("Incorrect number of fields", 2, f.length); 492 } 493 494 /** 495 * @tests java.lang.Class#getInterfaces() 496 */ 497 public void test_getInterfaces() { 498 Class[] interfaces; 499 List<?> interfaceList; 500 interfaces = Object.class.getInterfaces(); 501 assertEquals("Incorrect interface list for Object", 0, interfaces.length); 502 interfaceList = Arrays.asList(Vector.class.getInterfaces()); 503 assertTrue("Incorrect interface list for Vector", interfaceList 504 .contains(Cloneable.class) 505 && interfaceList.contains(Serializable.class) 506 && interfaceList.contains(List.class)); 507 } 508 509 /** 510 * @tests java.lang.Class#getMethod(java.lang.String, java.lang.Class[]) 511 */ 512 public void test_getMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 513 Method m = TestClass.class.getMethod("pubMethod", new Class[0]); 514 assertEquals("Returned incorrect method", 2, ((Integer) (m.invoke(new TestClass()))) 515 .intValue()); 516 try { 517 m = TestClass.class.getMethod("privMethod", new Class[0]); 518 fail("Failed to throw exception accessing private method"); 519 } catch (NoSuchMethodException e) { 520 // Correct 521 return; 522 } 523 } 524 525 /** 526 * @tests java.lang.Class#getMethods() 527 */ 528 public void test_getMethods() throws Exception { 529 Method[] m = TestClass.class.getMethods(); 530 assertEquals("Returned incorrect number of methods", 531 2 + Object.class.getMethods().length, m.length); 532 m = SubTestClass.class.getMethods(); 533 assertEquals("Returned incorrect number of sub-class methods", 534 2 + Object.class.getMethods().length, m.length); 535 // Number of inherited methods 536 } 537 538 private static final class PrivateClass { 539 } 540 /** 541 * @tests java.lang.Class#getModifiers() 542 */ 543 public void test_getModifiers() { 544 int dcm = PrivateClass.class.getModifiers(); 545 assertFalse("default class is public", Modifier.isPublic(dcm)); 546 assertFalse("default class is protected", Modifier.isProtected(dcm)); 547 assertTrue("default class is not private", Modifier.isPrivate(dcm)); 548 549 int ocm = Object.class.getModifiers(); 550 assertTrue("public class is not public", Modifier.isPublic(ocm)); 551 assertFalse("public class is protected", Modifier.isProtected(ocm)); 552 assertFalse("public class is private", Modifier.isPrivate(ocm)); 553 } 554 555 /** 556 * @tests java.lang.Class#getName() 557 */ 558 public void test_getName() throws Exception { 559 String className = Class.forName("java.lang.Object").getName(); 560 assertNotNull(className); 561 562 assertEquals("Class getName printed wrong value", "java.lang.Object", className); 563 assertEquals("Class getName printed wrong value", "int", int.class.getName()); 564 className = Class.forName("[I").getName(); 565 assertNotNull(className); 566 assertEquals("Class getName printed wrong value", "[I", className); 567 568 className = Class.forName("[Ljava.lang.Object;").getName(); 569 assertNotNull(className); 570 571 assertEquals("Class getName printed wrong value", "[Ljava.lang.Object;", className); 572 } 573 574 /** 575 * @tests java.lang.Class#getResource(java.lang.String) 576 */ 577 public void test_getResourceLjava_lang_String() { 578 final String name = "/org/apache/harmony/luni/tests/test_resource.txt"; 579 URL res = getClass().getResource(name); 580 assertNotNull(res); 581 } 582 583 /** 584 * @tests java.lang.Class#getResourceAsStream(java.lang.String) 585 */ 586 public void test_getResourceAsStreamLjava_lang_String() throws Exception { 587 final String name = "/org/apache/harmony/luni/tests/test_resource.txt"; 588 assertNotNull("the file " + name + " can not be found in this directory", getClass() 589 .getResourceAsStream(name)); 590 591 final String nameBadURI = "org/apache/harmony/luni/tests/test_resource.txt"; 592 assertNull("the file " + nameBadURI + " should not be found in this directory", 593 getClass().getResourceAsStream(nameBadURI)); 594 595 InputStream str = Object.class.getResourceAsStream("Class.class"); 596 assertNotNull("java.lang.Object couldn't find its class with getResource...", str); 597 598 assertTrue("Cannot read single byte", str.read() != -1); 599 assertEquals("Cannot read multiple bytes", 5, str.read(new byte[5])); 600 str.close(); 601 602 InputStream str2 = getClass().getResourceAsStream("ClassTest.class"); 603 assertNotNull("Can't find resource", str2); 604 assertTrue("Cannot read single byte", str2.read() != -1); 605 assertEquals("Cannot read multiple bytes", 5, str2.read(new byte[5])); 606 str2.close(); 607 } 608 609 /** 610 * @tests java.lang.Class#getSuperclass() 611 */ 612 public void test_getSuperclass() { 613 assertNull("Object has a superclass???", Object.class.getSuperclass()); 614 assertSame("Normal class has bogus superclass", InputStream.class, 615 FileInputStream.class.getSuperclass()); 616 assertSame("Array class has bogus superclass", Object.class, FileInputStream[].class 617 .getSuperclass()); 618 assertNull("Base class has a superclass", int.class.getSuperclass()); 619 assertNull("Interface class has a superclass", Cloneable.class.getSuperclass()); 620 } 621 622 /** 623 * @tests java.lang.Class#isArray() 624 */ 625 public void test_isArray() throws ClassNotFoundException { 626 assertTrue("Non-array type claims to be.", !int.class.isArray()); 627 Class<?> clazz = null; 628 clazz = Class.forName("[I"); 629 assertTrue("int Array type claims not to be.", clazz.isArray()); 630 631 clazz = Class.forName("[Ljava.lang.Object;"); 632 assertTrue("Object Array type claims not to be.", clazz.isArray()); 633 634 clazz = Class.forName("java.lang.Object"); 635 assertTrue("Non-array Object type claims to be.", !clazz.isArray()); 636 } 637 638 /** 639 * @tests java.lang.Class#isAssignableFrom(java.lang.Class) 640 */ 641 public void test_isAssignableFromLjava_lang_Class() { 642 Class<?> clazz1 = null; 643 Class<?> clazz2 = null; 644 645 clazz1 = Object.class; 646 clazz2 = Class.class; 647 assertTrue("returned false for superclass", clazz1.isAssignableFrom(clazz2)); 648 649 clazz1 = TestClass.class; 650 assertTrue("returned false for same class", clazz1.isAssignableFrom(clazz1)); 651 652 clazz1 = Runnable.class; 653 clazz2 = Thread.class; 654 assertTrue("returned false for implemented interface", clazz1.isAssignableFrom(clazz2)); 655 } 656 657 /** 658 * @tests java.lang.Class#isInterface() 659 */ 660 public void test_isInterface() throws ClassNotFoundException { 661 assertTrue("Prim type claims to be interface.", !int.class.isInterface()); 662 Class<?> clazz = null; 663 clazz = Class.forName("[I"); 664 assertTrue("Prim Array type claims to be interface.", !clazz.isInterface()); 665 666 clazz = Class.forName("java.lang.Runnable"); 667 assertTrue("Interface type claims not to be interface.", clazz.isInterface()); 668 clazz = Class.forName("java.lang.Object"); 669 assertTrue("Object type claims to be interface.", !clazz.isInterface()); 670 671 clazz = Class.forName("[Ljava.lang.Object;"); 672 assertTrue("Array type claims to be interface.", !clazz.isInterface()); 673 } 674 675 /** 676 * @tests java.lang.Class#isPrimitive() 677 */ 678 public void test_isPrimitive() { 679 assertFalse("Interface type claims to be primitive.", Runnable.class.isPrimitive()); 680 assertFalse("Object type claims to be primitive.", Object.class.isPrimitive()); 681 assertFalse("Prim Array type claims to be primitive.", int[].class.isPrimitive()); 682 assertFalse("Array type claims to be primitive.", Object[].class.isPrimitive()); 683 assertTrue("Prim type claims not to be primitive.", int.class.isPrimitive()); 684 assertFalse("Object type claims to be primitive.", Object.class.isPrimitive()); 685 } 686 687 /** 688 * @tests java.lang.Class#newInstance() 689 */ 690 public void test_newInstance() throws Exception { 691 Class<?> clazz = null; 692 clazz = Class.forName("java.lang.Object"); 693 assertNotNull("new object instance was null", clazz.newInstance()); 694 695 clazz = Class.forName("java.lang.Throwable"); 696 assertSame("new Throwable instance was not a throwable", 697 clazz, clazz.newInstance().getClass()); 698 699 clazz = Class.forName("java.lang.Integer"); 700 try { 701 clazz.newInstance(); 702 fail("Exception for instantiating a newInstance with no default constructor is not thrown"); 703 } catch (InstantiationException e) { 704 // expected 705 } 706 } 707 708 /** 709 * @tests java.lang.Class#toString() 710 */ 711 public void test_toString() throws ClassNotFoundException { 712 assertEquals("Class toString printed wrong value", 713 "int", int.class.toString()); 714 Class<?> clazz = null; 715 clazz = Class.forName("[I"); 716 assertEquals("Class toString printed wrong value", 717 "class [I", clazz.toString()); 718 719 clazz = Class.forName("java.lang.Object"); 720 assertEquals("Class toString printed wrong value", 721 "class java.lang.Object", clazz.toString()); 722 723 clazz = Class.forName("[Ljava.lang.Object;"); 724 assertEquals("Class toString printed wrong value", 725 "class [Ljava.lang.Object;", clazz.toString()); 726 } 727 728 729 // Regression Test for JIRA-2047 730 public void test_getResourceAsStream_withSharpChar() throws Exception{ 731 InputStream in = getClass().getResourceAsStream("/"+FILENAME); 732 assertNotNull(in); 733 in.close(); 734 735 in = getClass().getResourceAsStream(FILENAME); 736 assertNull(in); 737 738 in = this.getClass().getClassLoader().getResourceAsStream( 739 FILENAME); 740 assertNotNull(in); 741 in.close(); 742 } 743 744 /* 745 * Regression test for HARMONY-2644: 746 * Load system and non-system array classes via Class.forName() 747 */ 748 public void test_forName_arrays() throws Exception { 749 Class c1 = getClass(); 750 String s = c1.getName(); 751 Class a1 = Class.forName("[L" + s + ";"); 752 Class a2 = Class.forName("[[L" + s + ";"); 753 assertSame(c1, a1.getComponentType()); 754 assertSame(a1, a2.getComponentType()); 755 Class l4 = Class.forName("[[[[[J"); 756 assertSame(long[][][][][].class, l4); 757 758 try{ 759 System.out.println(Class.forName("[;")); 760 fail("1"); 761 } catch (ClassNotFoundException ok) {} 762 try{ 763 System.out.println(Class.forName("[[")); 764 fail("2"); 765 } catch (ClassNotFoundException ok) {} 766 try{ 767 System.out.println(Class.forName("[L")); 768 fail("3"); 769 } catch (ClassNotFoundException ok) {} 770 try{ 771 System.out.println(Class.forName("[L;")); 772 fail("4"); 773 } catch (ClassNotFoundException ok) {} 774 try{ 775 System.out.println(Class.forName(";")); 776 fail("5"); 777 } catch (ClassNotFoundException ok) {} 778 try{ 779 System.out.println(Class.forName("")); 780 fail("6"); 781 } catch (ClassNotFoundException ok) {} 782 } 783} 784