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 18 package org.apache.harmony.tests.java.lang; 19 20 import java.io.FileInputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.io.Serializable; 24 import java.lang.reflect.Constructor; 25 import java.lang.reflect.Field; 26 import java.lang.reflect.Method; 27 import java.lang.reflect.Modifier; 28 import java.net.URL; 29 import java.security.AccessController; 30 import java.security.BasicPermission; 31 import java.security.DomainCombiner; 32 import java.security.Permission; 33 import java.security.ProtectionDomain; 34 import java.security.Security; 35 import java.util.Arrays; 36 import java.util.List; 37 import java.util.Vector; 38 39 public class ClassTest extends junit.framework.TestCase { 40 41 // Relative resource paths. 42 private static final String SHARP_RESOURCE_RELATIVE_NAME = "test#.properties"; 43 private static final String RESOURCE_RELATIVE_NAME = "test.properties"; 44 45 // Absolute resource paths. 46 private static final String ABS_PATH = 47 ClassTest.class.getPackage().getName().replace('.', '/'); 48 public static final String SHARP_RESOURCE_ABS_NAME = 49 ABS_PATH + "/" + SHARP_RESOURCE_RELATIVE_NAME; 50 public static final String RESOURCE_ABS_NAME = ABS_PATH + "/" + RESOURCE_RELATIVE_NAME; 51 52 public static class TestClass { 53 @SuppressWarnings("unused") 54 private int privField = 1; 55 56 public int pubField = 2; 57 58 private Object cValue = null; 59 60 public Object ack = new Object(); 61 62 @SuppressWarnings("unused") 63 private int privMethod() { 64 return 1; 65 } 66 67 public int pubMethod() { 68 return 2; 69 } 70 71 public Object cValue() { 72 return cValue; 73 } 74 75 public TestClass() { 76 } 77 78 @SuppressWarnings("unused") 79 private TestClass(Object o) { 80 } 81 } 82 83 public static class SubTestClass extends TestClass { 84 } 85 86 /** 87 * java.lang.Class#forName(java.lang.String) 88 */ 89 public void test_forNameLjava_lang_String() throws Exception { 90 assertSame("Class for name failed for java.lang.Object", 91 Object.class, Class.forName("java.lang.Object")); 92 assertSame("Class for name failed for [[Ljava.lang.Object;", 93 Object[][].class, Class.forName("[[Ljava.lang.Object;")); 94 95 assertSame("Class for name failed for [I", 96 int[].class, Class.forName("[I")); 97 98 try { 99 Class.forName("int"); 100 fail(); 101 } catch (ClassNotFoundException e) { 102 } 103 104 try { 105 Class.forName("byte"); 106 fail(); 107 } catch (ClassNotFoundException e) { 108 } 109 try { 110 Class.forName("char"); 111 fail(); 112 } catch (ClassNotFoundException e) { 113 } 114 115 try { 116 Class.forName("void"); 117 fail(); 118 } catch (ClassNotFoundException e) { 119 } 120 121 try { 122 Class.forName("short"); 123 fail(); 124 } catch (ClassNotFoundException e) { 125 } 126 try { 127 Class.forName("long"); 128 fail(); 129 } catch (ClassNotFoundException e) { 130 } 131 132 try { 133 Class.forName("boolean"); 134 fail(); 135 } catch (ClassNotFoundException e) { 136 } 137 try { 138 Class.forName("float"); 139 fail(); 140 } catch (ClassNotFoundException e) { 141 } 142 try { 143 Class.forName("double"); 144 fail(); 145 } catch (ClassNotFoundException e) { 146 } 147 148 //regression test for JIRA 2162 149 try { 150 Class.forName("%"); 151 fail("should throw ClassNotFoundException."); 152 } catch (ClassNotFoundException e) { 153 } 154 155 //Regression Test for HARMONY-3332 156 String securityProviderClassName; 157 int count = 1; 158 while ((securityProviderClassName = Security 159 .getProperty("security.provider." + count++)) != null) { 160 Class.forName(securityProviderClassName); 161 } 162 } 163 164 /** 165 * java.lang.Class#getClasses() 166 */ 167 public void test_getClasses() { 168 assertEquals("Incorrect class array returned", 169 2, ClassTest.class.getClasses().length); 170 } 171 172 /** 173 * java.lang.Class#getClasses() 174 */ 175 public void test_getClasses_subtest0() { 176 final Permission privCheckPermission = new BasicPermission("Privilege check") { 177 private static final long serialVersionUID = 1L; 178 }; 179 180 class MyCombiner implements DomainCombiner { 181 boolean combine; 182 183 public ProtectionDomain[] combine(ProtectionDomain[] executionDomains, 184 ProtectionDomain[] parentDomains) { 185 combine = true; 186 return new ProtectionDomain[0]; 187 } 188 189 private boolean recurring = false; 190 191 public boolean isPriviledged() { 192 if (recurring) { 193 return true; 194 } 195 try { 196 recurring = true; 197 combine = false; 198 try { 199 AccessController.checkPermission(privCheckPermission); 200 } catch (SecurityException e) { 201 } 202 return !combine; 203 } finally { 204 recurring = false; 205 } 206 } 207 } 208 } 209 210 /** 211 * java.lang.Class#getComponentType() 212 */ 213 public void test_getComponentType() { 214 assertSame("int array does not have int component type", int.class, int[].class 215 .getComponentType()); 216 assertSame("Object array does not have Object component type", Object.class, 217 Object[].class.getComponentType()); 218 assertNull("Object has non-null component type", Object.class.getComponentType()); 219 } 220 221 /** 222 * java.lang.Class#getConstructor(java.lang.Class[]) 223 */ 224 public void test_getConstructor$Ljava_lang_Class() 225 throws NoSuchMethodException { 226 TestClass.class.getConstructor(new Class[0]); 227 try { 228 TestClass.class.getConstructor(Object.class); 229 fail("Found private constructor"); 230 } catch (NoSuchMethodException e) { 231 // Correct - constructor with obj is private 232 } 233 } 234 235 /** 236 * java.lang.Class#getConstructors() 237 */ 238 public void test_getConstructors() throws Exception { 239 Constructor[] c = TestClass.class.getConstructors(); 240 assertEquals("Incorrect number of constructors returned", 1, c.length); 241 } 242 243 /** 244 * java.lang.Class#getDeclaredClasses() 245 */ 246 public void test_getDeclaredClasses() { 247 assertEquals("Incorrect class array returned", 2, ClassTest.class.getClasses().length); 248 } 249 250 /** 251 * java.lang.Class#getDeclaredConstructor(java.lang.Class[]) 252 */ 253 public void test_getDeclaredConstructor$Ljava_lang_Class() throws Exception { 254 Constructor<TestClass> c = TestClass.class.getDeclaredConstructor(new Class[0]); 255 assertNull("Incorrect constructor returned", c.newInstance().cValue()); 256 c = TestClass.class.getDeclaredConstructor(Object.class); 257 } 258 259 /** 260 * java.lang.Class#getDeclaredConstructors() 261 */ 262 public void test_getDeclaredConstructors() throws Exception { 263 Constructor[] c = TestClass.class.getDeclaredConstructors(); 264 assertEquals("Incorrect number of constructors returned", 2, c.length); 265 } 266 267 /** 268 * java.lang.Class#getDeclaredField(java.lang.String) 269 */ 270 public void test_getDeclaredFieldLjava_lang_String() throws Exception { 271 Field f = TestClass.class.getDeclaredField("pubField"); 272 assertEquals("Returned incorrect field", 2, f.getInt(new TestClass())); 273 } 274 275 /** 276 * java.lang.Class#getDeclaredFields() 277 */ 278 public void test_getDeclaredFields() throws Exception { 279 Field[] f = TestClass.class.getDeclaredFields(); 280 assertEquals("Returned incorrect number of fields", 4, f.length); 281 f = SubTestClass.class.getDeclaredFields(); 282 // Declared fields do not include inherited 283 assertEquals("Returned incorrect number of fields", 0, f.length); 284 } 285 286 /** 287 * java.lang.Class#getDeclaredMethod(java.lang.String, 288 *java.lang.Class[]) 289 */ 290 public void test_getDeclaredMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 291 Method m = TestClass.class.getDeclaredMethod("pubMethod", new Class[0]); 292 assertEquals("Returned incorrect method", 2, ((Integer) (m.invoke(new TestClass()))) 293 .intValue()); 294 m = TestClass.class.getDeclaredMethod("privMethod", new Class[0]); 295 } 296 297 /** 298 * java.lang.Class#getDeclaredMethods() 299 */ 300 public void test_getDeclaredMethods() throws Exception { 301 Method[] m = TestClass.class.getDeclaredMethods(); 302 assertEquals("Returned incorrect number of methods", 3, m.length); 303 m = SubTestClass.class.getDeclaredMethods(); 304 assertEquals("Returned incorrect number of methods", 0, m.length); 305 } 306 307 /** 308 * java.lang.Class#getDeclaringClass() 309 */ 310 public void test_getDeclaringClass() { 311 assertEquals(ClassTest.class, TestClass.class.getDeclaringClass()); 312 } 313 314 /** 315 * java.lang.Class#getField(java.lang.String) 316 */ 317 public void test_getFieldLjava_lang_String() throws Exception { 318 Field f = TestClass.class.getField("pubField"); 319 assertEquals("Returned incorrect field", 2, f.getInt(new TestClass())); 320 try { 321 f = TestClass.class.getField("privField"); 322 fail("Private field access failed to throw exception"); 323 } catch (NoSuchFieldException e) { 324 // Correct 325 } 326 } 327 328 /** 329 * java.lang.Class#getFields() 330 */ 331 public void test_getFields() throws Exception { 332 Field[] f = TestClass.class.getFields(); 333 assertEquals("Incorrect number of fields", 2, f.length); 334 f = SubTestClass.class.getFields(); 335 // Check inheritance of pub fields 336 assertEquals("Incorrect number of fields", 2, f.length); 337 } 338 339 /** 340 * java.lang.Class#getInterfaces() 341 */ 342 public void test_getInterfaces() { 343 Class[] interfaces; 344 List<?> interfaceList; 345 interfaces = Object.class.getInterfaces(); 346 assertEquals("Incorrect interface list for Object", 0, interfaces.length); 347 interfaceList = Arrays.asList(Vector.class.getInterfaces()); 348 assertTrue("Incorrect interface list for Vector", interfaceList 349 .contains(Cloneable.class) 350 && interfaceList.contains(Serializable.class) 351 && interfaceList.contains(List.class)); 352 } 353 354 /** 355 * java.lang.Class#getMethod(java.lang.String, java.lang.Class[]) 356 */ 357 public void test_getMethodLjava_lang_String$Ljava_lang_Class() throws Exception { 358 Method m = TestClass.class.getMethod("pubMethod", new Class[0]); 359 assertEquals("Returned incorrect method", 2, ((Integer) (m.invoke(new TestClass()))) 360 .intValue()); 361 try { 362 m = TestClass.class.getMethod("privMethod", new Class[0]); 363 fail("Failed to throw exception accessing private method"); 364 } catch (NoSuchMethodException e) { 365 // Correct 366 return; 367 } 368 } 369 370 /** 371 * java.lang.Class#getMethods() 372 */ 373 public void test_getMethods() throws Exception { 374 Method[] m = TestClass.class.getMethods(); 375 assertEquals("Returned incorrect number of methods", 376 2 + Object.class.getMethods().length, m.length); 377 m = SubTestClass.class.getMethods(); 378 assertEquals("Returned incorrect number of sub-class methods", 379 2 + Object.class.getMethods().length, m.length); 380 // Number of inherited methods 381 } 382 383 private static final class PrivateClass { 384 } 385 386 /** 387 * java.lang.Class#getModifiers() 388 */ 389 public void test_getModifiers() { 390 int dcm = PrivateClass.class.getModifiers(); 391 assertFalse("default class is public", Modifier.isPublic(dcm)); 392 assertFalse("default class is protected", Modifier.isProtected(dcm)); 393 assertTrue("default class is not private", Modifier.isPrivate(dcm)); 394 395 int ocm = Object.class.getModifiers(); 396 assertTrue("public class is not public", Modifier.isPublic(ocm)); 397 assertFalse("public class is protected", Modifier.isProtected(ocm)); 398 assertFalse("public class is private", Modifier.isPrivate(ocm)); 399 } 400 401 /** 402 * java.lang.Class#getName() 403 */ 404 public void test_getName() throws Exception { 405 String className = Class.forName("java.lang.Object").getName(); 406 assertNotNull(className); 407 408 assertEquals("Class getName printed wrong value", "java.lang.Object", className); 409 assertEquals("Class getName printed wrong value", "int", int.class.getName()); 410 className = Class.forName("[I").getName(); 411 assertNotNull(className); 412 assertEquals("Class getName printed wrong value", "[I", className); 413 414 className = Class.forName("[Ljava.lang.Object;").getName(); 415 assertNotNull(className); 416 417 assertEquals("Class getName printed wrong value", "[Ljava.lang.Object;", className); 418 } 419 420 /** 421 * java.lang.Class#getResource(java.lang.String) 422 */ 423 public void test_getResourceLjava_lang_String() { 424 final String name = "/resources/test_resource.txt"; 425 URL res = getClass().getResource(name); 426 assertNotNull(res); 427 } 428 429 /** 430 * java.lang.Class#getResourceAsStream(java.lang.String) 431 */ 432 public void test_getResourceAsStreamLjava_lang_String() throws Exception { 433 final String name = "/resources/test_resource.txt"; 434 InputStream str2 = getClass().getResourceAsStream(name); 435 assertNotNull("the file " + name + " can not be found in this directory", str2); 436 437 final String nameBadURI = "org/apache/harmony/luni/tests/test_resource.txt"; 438 assertNull("the file " + nameBadURI + " should not be found in this directory", 439 getClass().getResourceAsStream(nameBadURI)); 440 441 assertTrue("Cannot read single byte", str2.read() != -1); 442 assertEquals("Cannot read multiple bytes", 5, str2.read(new byte[5])); 443 str2.close(); 444 } 445 446 /** 447 * java.lang.Class#getSuperclass() 448 */ 449 public void test_getSuperclass() { 450 assertNull("Object has a superclass???", Object.class.getSuperclass()); 451 assertSame("Normal class has bogus superclass", InputStream.class, 452 FileInputStream.class.getSuperclass()); 453 assertSame("Array class has bogus superclass", Object.class, FileInputStream[].class 454 .getSuperclass()); 455 assertNull("Base class has a superclass", int.class.getSuperclass()); 456 assertNull("Interface class has a superclass", Cloneable.class.getSuperclass()); 457 } 458 459 /** 460 * java.lang.Class#isArray() 461 */ 462 public void test_isArray() throws ClassNotFoundException { 463 assertTrue("Non-array type claims to be.", !int.class.isArray()); 464 Class<?> clazz = null; 465 clazz = Class.forName("[I"); 466 assertTrue("int Array type claims not to be.", clazz.isArray()); 467 468 clazz = Class.forName("[Ljava.lang.Object;"); 469 assertTrue("Object Array type claims not to be.", clazz.isArray()); 470 471 clazz = Class.forName("java.lang.Object"); 472 assertTrue("Non-array Object type claims to be.", !clazz.isArray()); 473 } 474 475 /** 476 * java.lang.Class#isAssignableFrom(java.lang.Class) 477 */ 478 public void test_isAssignableFromLjava_lang_Class() { 479 Class<?> clazz1 = null; 480 Class<?> clazz2 = null; 481 482 clazz1 = Object.class; 483 clazz2 = Class.class; 484 assertTrue("returned false for superclass", clazz1.isAssignableFrom(clazz2)); 485 486 clazz1 = TestClass.class; 487 assertTrue("returned false for same class", clazz1.isAssignableFrom(clazz1)); 488 489 clazz1 = Runnable.class; 490 clazz2 = Thread.class; 491 assertTrue("returned false for implemented interface", clazz1.isAssignableFrom(clazz2)); 492 } 493 494 /** 495 * java.lang.Class#isInterface() 496 */ 497 public void test_isInterface() throws ClassNotFoundException { 498 assertTrue("Prim type claims to be interface.", !int.class.isInterface()); 499 Class<?> clazz = null; 500 clazz = Class.forName("[I"); 501 assertTrue("Prim Array type claims to be interface.", !clazz.isInterface()); 502 503 clazz = Class.forName("java.lang.Runnable"); 504 assertTrue("Interface type claims not to be interface.", clazz.isInterface()); 505 clazz = Class.forName("java.lang.Object"); 506 assertTrue("Object type claims to be interface.", !clazz.isInterface()); 507 508 clazz = Class.forName("[Ljava.lang.Object;"); 509 assertTrue("Array type claims to be interface.", !clazz.isInterface()); 510 } 511 512 /** 513 * java.lang.Class#isPrimitive() 514 */ 515 public void test_isPrimitive() { 516 assertFalse("Interface type claims to be primitive.", Runnable.class.isPrimitive()); 517 assertFalse("Object type claims to be primitive.", Object.class.isPrimitive()); 518 assertFalse("Prim Array type claims to be primitive.", int[].class.isPrimitive()); 519 assertFalse("Array type claims to be primitive.", Object[].class.isPrimitive()); 520 assertTrue("Prim type claims not to be primitive.", int.class.isPrimitive()); 521 assertFalse("Object type claims to be primitive.", Object.class.isPrimitive()); 522 } 523 524 /** 525 * java.lang.Class#newInstance() 526 */ 527 public void test_newInstance() throws Exception { 528 Class<?> clazz = null; 529 clazz = Class.forName("java.lang.Object"); 530 assertNotNull("new object instance was null", clazz.newInstance()); 531 532 clazz = Class.forName("java.lang.Throwable"); 533 assertSame("new Throwable instance was not a throwable", 534 clazz, clazz.newInstance().getClass()); 535 536 clazz = Class.forName("java.lang.Integer"); 537 try { 538 clazz.newInstance(); 539 fail("Exception for instantiating a newInstance with no default constructor is not thrown"); 540 } catch (InstantiationException e) { 541 // expected 542 } 543 } 544 545 /** 546 * java.lang.Class#toString() 547 */ 548 public void test_toString() throws ClassNotFoundException { 549 assertEquals("Class toString printed wrong value", 550 "int", int.class.toString()); 551 Class<?> clazz = null; 552 clazz = Class.forName("[I"); 553 assertEquals("Class toString printed wrong value", 554 "class [I", clazz.toString()); 555 556 clazz = Class.forName("java.lang.Object"); 557 assertEquals("Class toString printed wrong value", 558 "class java.lang.Object", clazz.toString()); 559 560 clazz = Class.forName("[Ljava.lang.Object;"); 561 assertEquals("Class toString printed wrong value", 562 "class [Ljava.lang.Object;", clazz.toString()); 563 } 564 565 566 // Regression Test for JIRA-2047 567 public void test_getResourceAsStream_withSharpChar() throws Exception { 568 // Class.getResourceAsStream() requires a leading "/" for absolute paths. 569 assertNull(getClass().getResourceAsStream(SHARP_RESOURCE_ABS_NAME)); 570 assertResourceExists("/" + SHARP_RESOURCE_ABS_NAME); 571 assertResourceExists(SHARP_RESOURCE_RELATIVE_NAME); 572 573 574 InputStream in = 575 this.getClass().getClassLoader().getResourceAsStream(SHARP_RESOURCE_ABS_NAME); 576 assertNotNull(in); 577 in.close(); 578 } 579 580 public void test_getResourceAsStream() throws Exception { 581 // Class.getResourceAsStream() requires a leading "/" for absolute paths. 582 assertNull(getClass().getResourceAsStream(RESOURCE_ABS_NAME)); 583 assertResourceExists("/" + RESOURCE_ABS_NAME); 584 assertResourceExists(RESOURCE_RELATIVE_NAME); 585 586 InputStream in = this.getClass().getClassLoader().getResourceAsStream(RESOURCE_ABS_NAME); 587 assertNotNull(in); 588 in.close(); 589 } 590 591 private void assertResourceExists(String resourceName) throws IOException { 592 InputStream in = getClass().getResourceAsStream(resourceName); 593 assertNotNull(in); 594 in.close(); 595 } 596 597 /* 598 * Regression test for HARMONY-2644: 599 * Load system and non-system array classes via Class.forName() 600 */ 601 public void test_forName_arrays() throws Exception { 602 Class c1 = getClass(); 603 String s = c1.getName(); 604 Class a1 = Class.forName("[L" + s + ";"); 605 Class a2 = Class.forName("[[L" + s + ";"); 606 assertSame(c1, a1.getComponentType()); 607 assertSame(a1, a2.getComponentType()); 608 Class l4 = Class.forName("[[[[[J"); 609 assertSame(long[][][][][].class, l4); 610 611 try { 612 System.out.println(Class.forName("[;")); 613 fail("1"); 614 } catch (ClassNotFoundException ok) { 615 } 616 try { 617 System.out.println(Class.forName("[[")); 618 fail("2"); 619 } catch (ClassNotFoundException ok) { 620 } 621 try { 622 System.out.println(Class.forName("[L")); 623 fail("3"); 624 } catch (ClassNotFoundException ok) { 625 } 626 try { 627 System.out.println(Class.forName("[L;")); 628 fail("4"); 629 } catch (ClassNotFoundException ok) { 630 } 631 try { 632 System.out.println(Class.forName(";")); 633 fail("5"); 634 } catch (ClassNotFoundException ok) { 635 } 636 try { 637 System.out.println(Class.forName("")); 638 fail("6"); 639 } catch (ClassNotFoundException ok) { 640 } 641 } 642 } 643