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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.harmony.luni.tests.java.lang;
20 import java.io.FileInputStream;
21 import java.io.InputStream;
22 import java.io.Serializable;
23 import java.lang.annotation.Annotation;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.Field;
26 import java.lang.reflect.Member;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Modifier;
29 import java.lang.reflect.ParameterizedType;
30 import java.lang.reflect.Type;
31 import java.lang.reflect.TypeVariable;
33 import java.net.URLClassLoader;
34 import java.security.AccessControlContext;
35 import java.security.AccessController;
36 import java.security.BasicPermission;
37 import java.security.DomainCombiner;
38 import java.security.Permission;
39 import java.security.PrivilegedAction;
40 import java.security.ProtectionDomain;
41 import java.security.Security;
42 import java.util.AbstractList;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.List;
46 import java.util.Vector;
48 import tests.support.Support_ClassLoader;
49 import tests.support.resource.Support_Resources;
50 import dalvik.annotation.AndroidOnly;
51 import dalvik.annotation.BrokenTest;
52 import dalvik.annotation.KnownFailure;
53 import dalvik.annotation.TestLevel;
54 import dalvik.annotation.TestTargetClass;
55 import dalvik.annotation.TestTargetNew;
57 @SuppressWarnings("deprecation")
58 @TestTargetClass(Class.class)
59 public class ClassTest extends junit.framework.TestCase {
61 public static final String FILENAME =
62 ClassTest.class.getPackage().getName().replace('.', '/') +
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";
72 static class StaticMember$Class {
82 public static class TestClass {
83 @SuppressWarnings("unused")
84 private int privField = 1;
86 public int pubField = 2;
88 private Object cValue = null;
90 public Object ack = new Object();
92 @SuppressWarnings("unused")
93 private int privMethod() {
97 public int pubMethod() {
101 public Object cValue() {
108 @SuppressWarnings("unused")
109 private TestClass(Object o) {
113 public static class SubTestClass extends TestClass {
117 public int field1 = 1;
118 public int field2 = 1;
123 public int field1 = 1;
127 interface Intf3 extends Intf1 {
128 public int field1 = 1;
131 interface Intf4 extends Intf1, Intf2 {
132 public int field1 = 1;
133 void test2(int a, Object b);
136 interface Intf5 extends Intf1 {
139 class Cls1 implements Intf2 {
140 public int field1 = 2;
141 public int field2 = 2;
146 class Cls2 extends Cls1 implements Intf1 {
147 public int field1 = 2;
153 class Cls3 implements Intf3, Intf4 {
156 public void test2(int a, Object b) {
165 level = TestLevel.COMPLETE,
167 method = "getAnnotations",
170 public void test_getAnnotations() {
171 Annotation [] annotations = PublicTestClass.class.getAnnotations();
172 assertEquals(1, annotations.length);
173 assertEquals(TestAnnotation.class, annotations[0].annotationType());
175 annotations = ExtendTestClass.class.getAnnotations();
176 assertEquals(2, annotations.length);
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));
187 * @tests java.lang.Class#forName(java.lang.String)
190 level = TestLevel.SUFFICIENT,
191 notes = "java.lang.LinkageError can't be checked.",
193 args = {java.lang.String.class}
195 @AndroidOnly("harmony specific: test with " +
196 "'org.apache.harmony.luni.tests.java.lang.TestClass1'")
197 public void test_forNameLjava_lang_String() throws Exception {
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;"));
204 assertSame("Class for name failed for [I",
205 int[].class, Class.forName("[I"));
208 Class.forName("int");
210 } catch (ClassNotFoundException e) {
214 Class.forName("byte");
216 } catch (ClassNotFoundException e) {
219 Class.forName("char");
221 } catch (ClassNotFoundException e) {
225 Class.forName("void");
227 } catch (ClassNotFoundException e) {
231 Class.forName("short");
233 } catch (ClassNotFoundException e) {
236 Class.forName("long");
238 } catch (ClassNotFoundException e) {
242 Class.forName("boolean");
244 } catch (ClassNotFoundException e) {
247 Class.forName("float");
249 } catch (ClassNotFoundException e) {
252 Class.forName("double");
254 } catch (ClassNotFoundException e) {
257 //regression test for JIRA 2162
260 fail("should throw ClassNotFoundException.");
261 } catch (ClassNotFoundException e) {
264 //Regression Test for HARMONY-3332
265 String securityProviderClassName;
267 while ((securityProviderClassName = Security
268 .getProperty("security.provider." + count++)) != null) {
269 Class.forName(securityProviderClassName);
273 Class.forName(classNameInitError1);
274 fail("ExceptionInInitializerError or ClassNotFoundException " +
276 } catch (java.lang.ExceptionInInitializerError ie) {
277 // Expected for the RI.
278 } catch (java.lang.ClassNotFoundException ce) {
279 // Expected for Android.
284 level = TestLevel.SUFFICIENT,
287 args = {java.lang.String.class, boolean.class, java.lang.ClassLoader.class}
289 public void test_forNameLjava_lang_StringLbooleanLClassLoader() throws Exception {
291 ClassLoader pcl = getClass().getClassLoader();
293 Class<?> [] classes = {PublicTestClass.class, ExtendTestClass.class,
294 ExtendTestClass1.class, TestInterface.class, String.class};
296 for(int i = 0; i < classes.length; i++) {
297 Class<?> clazz = Class.forName(classes[i].getName(), true, pcl);
298 assertEquals(classes[i], clazz);
300 clazz = Class.forName(classes[i].getName(), false, pcl);
301 assertEquals(classes[i], clazz);
304 Class<?> [] systemClasses = {String.class, Integer.class, Object.class,
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);
312 clazz = Class.forName(systemClasses[i].getName(), false,
313 ClassLoader.getSystemClassLoader());
314 assertEquals(systemClasses[i], clazz);
318 Class.forName(null, true, pcl);
319 fail("NullPointerException is not thrown.");
320 } catch(NullPointerException npe) {
325 Class.forName("NotExistClass", true, pcl);
326 fail("ClassNotFoundException is not thrown for non existent class.");
327 } catch(ClassNotFoundException cnfe) {
332 Class.forName("String", false, pcl);
333 fail("ClassNotFoundException is not thrown for non existent class.");
334 } catch(ClassNotFoundException cnfe) {
339 Class.forName("org.apache.harmony.luni.tests.java.PublicTestClass",
341 fail("ClassNotFoundException is not thrown for non existent class.");
342 } catch(ClassNotFoundException cnfe) {
348 level = TestLevel.SUFFICIENT,
351 args = {java.lang.String.class, boolean.class, java.lang.ClassLoader.class}
353 @AndroidOnly("Class.forName method throws ClassNotFoundException on " +
355 public void test_forNameLjava_lang_StringLbooleanLClassLoader_AndroidOnly() throws Exception {
357 // Android doesn't support loading class files from a jar.
360 URL url = getClass().getClassLoader().getResource(
361 packageName.replace(".", "/") + "/" + sourceJARfile);
363 ClassLoader loader = new URLClassLoader(new URL[] { url },
364 getClass().getClassLoader());
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.
373 } catch(Exception e) {
374 fail("Unexpected exception was thrown: " + e.toString());
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.
391 level = TestLevel.COMPLETE,
393 method = "getAnnotation",
394 args = {java.lang.Class.class}
396 public void test_getAnnotation() {
397 TestAnnotation target = PublicTestClass.class.getAnnotation(TestAnnotation.class);
398 assertEquals(target.value(), PublicTestClass.class.getName());
400 assertNull(PublicTestClass.class.getAnnotation(Deprecated.class));
402 Deprecated target2 = ExtendTestClass.class.getAnnotation(Deprecated.class);
403 assertNotNull(target2);
407 level = TestLevel.COMPLETE,
409 method = "getDeclaredAnnotations",
412 public void test_getDeclaredAnnotations() {
413 Annotation [] annotations = PublicTestClass.class.getDeclaredAnnotations();
414 assertEquals(1, annotations.length);
416 annotations = ExtendTestClass.class.getDeclaredAnnotations();
417 assertEquals(2, annotations.length);
419 annotations = TestInterface.class.getDeclaredAnnotations();
420 assertEquals(0, annotations.length);
422 annotations = String.class.getDeclaredAnnotations();
423 assertEquals(0, annotations.length);
427 level = TestLevel.COMPLETE,
429 method = "getEnclosingClass",
432 public void test_getEnclosingClass() {
433 Class clazz = ExtendTestClass.class.getEnclosingClass();
436 assertEquals(getClass(), Cls1.class.getEnclosingClass());
437 assertEquals(getClass(), Intf1.class.getEnclosingClass());
438 assertEquals(getClass(), Cls4.class.getEnclosingClass());
443 level = TestLevel.COMPLETE,
445 method = "getEnclosingMethod",
448 public void test_getEnclosingMethod() {
449 Method clazz = ExtendTestClass.class.getEnclosingMethod();
452 PublicTestClass ptc = new PublicTestClass();
454 assertEquals("getEnclosingMethod returns incorrect method.",
455 PublicTestClass.class.getMethod("getLocalClass",
457 ptc.getLocalClass().getClass().getEnclosingMethod());
458 } catch(NoSuchMethodException nsme) {
459 fail("NoSuchMethodException was thrown.");
464 level = TestLevel.COMPLETE,
466 method = "getEnclosingConstructor",
469 public void test_getEnclosingConstructor() {
471 PublicTestClass ptc = new PublicTestClass();
473 assertEquals("getEnclosingConstructor method returns incorrect class.",
474 PublicTestClass.class.getConstructors()[0],
475 ptc.clazz.getClass().getEnclosingConstructor());
477 assertNull("getEnclosingConstructor should return null for local " +
478 "class declared in method.",
479 ptc.getLocalClass().getClass().getEnclosingConstructor());
481 assertNull("getEnclosingConstructor should return null for local " +
482 "class declared in method.",
483 ExtendTestClass.class.getEnclosingConstructor());
488 level = TestLevel.COMPLETE,
490 method = "getEnumConstants",
493 public void test_getEnumConstants() {
494 Object [] clazz = ExtendTestClass.class.getEnumConstants();
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]);
501 assertEquals(0, TestEmptyEnum.class.getEnumConstants().length);
503 public enum TestEnum {
506 public enum TestEmptyEnum {
509 level = TestLevel.SUFFICIENT,
510 notes = "GenericSignatureFormatError, TypeNotPresentException, " +
511 "MalformedParameterizedTypeException are not verified.",
512 method = "getGenericInterfaces",
515 public void test_getGenericInterfaces() {
516 Type [] types = ExtendTestClass1.class.getGenericInterfaces();
517 assertEquals(0, types.length);
519 Class [] interfaces = {TestInterface.class, Serializable.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]);
527 types = TestInterface.class.getGenericInterfaces();
528 assertEquals(0, types.length);
530 types = List.class.getGenericInterfaces();
531 assertEquals(1, types.length);
532 assertEquals(Collection.class, ((ParameterizedType)types[0]).getRawType());
534 assertEquals(0, int.class.getGenericInterfaces().length);
535 assertEquals(0, void.class.getGenericInterfaces().length);
539 level = TestLevel.SUFFICIENT,
540 notes = "GenericSignatureFormatError, TypeNotPresentException, MalformedParameterizedTypeException are not verified.",
541 method = "getGenericSuperclass",
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());
553 ParameterizedType type = (ParameterizedType) Vector.class.getGenericSuperclass();
554 assertEquals(AbstractList.class, type.getRawType());
558 level = TestLevel.SUFFICIENT,
559 method = "getPackage",
562 @AndroidOnly("Uses dalvik.system.PathClassLoader.")
563 public void test_getPackage() {
565 Package thisPackage = getClass().getPackage();
566 assertEquals("org.apache.harmony.luni.tests.java.lang",
567 thisPackage.getName());
569 Package stringPackage = String.class.getPackage();
570 assertNotNull("java.lang", stringPackage.getName());
572 String hyts_package_name = "hyts_package_dex.jar";
573 File resources = Support_Resources.createTempFolder();
574 Support_Resources.copyFile(resources, "Package", hyts_package_name);
576 String resPath = resources.toString();
577 if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\')
578 resPath = resPath.substring(1);
582 URL resourceURL = new URL("file:/" + resPath + "/Package/"
583 + hyts_package_name);
585 ClassLoader cl = Support_ClassLoader.getInstance(resourceURL,
586 getClass().getClassLoader());
588 Class clazz = cl.loadClass("C");
589 assertNull("getPackage for C.class should return null",
592 clazz = cl.loadClass("a.b.C");
593 Package cPackage = clazz.getPackage();
594 assertNotNull("getPackage for a.b.C.class should not return null",
598 * URLClassLoader doesn't work on Android for jar files
600 * URL url = getClass().getClassLoader().getResource(
601 * packageName.replace(".", "/") + "/" + sourceJARfile);
603 * ClassLoader loader = new URLClassLoader(new URL[] { url }, null);
606 * Class<?> clazz = loader.loadClass(illegalClassName);
607 * Package pack = clazz.getPackage();
609 * } catch(ClassNotFoundException cne) {
610 * fail("ClassNotFoundException was thrown for " + illegalClassName);
613 } catch(Exception e) {
614 fail("Unexpected exception was thrown: " + e.toString());
619 level = TestLevel.COMPLETE,
620 method = "getProtectionDomain",
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);
628 SecurityManager sm = new SecurityManager() {
630 public void checkPermission(Permission perm) {
631 if (perm.getName().equals("getProtectionDomain")) {
632 throw new SecurityException();
637 SecurityManager oldSm = System.getSecurityManager();
638 System.setSecurityManager(sm);
640 PublicTestClass.class.getProtectionDomain();
641 fail("Test 2: SecurityException expected.");
642 } catch (SecurityException e) {
645 System.setSecurityManager(oldSm);
649 level = TestLevel.SUFFICIENT,
651 method = "getSigners",
654 public void test_getSigners() {
655 assertNull(void.class.getSigners());
656 assertNull(PublicTestClass.class.getSigners());
661 level = TestLevel.COMPLETE,
663 method = "getSimpleName",
666 public void test_getSimpleName() {
667 assertEquals("PublicTestClass", PublicTestClass.class.getSimpleName());
668 assertEquals("void", void.class.getSimpleName());
669 assertEquals("int[]", int[].class.getSimpleName());
673 level = TestLevel.COMPLETE,
675 method = "getTypeParameters",
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]);
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]);
690 class TempTestClass1<T> {
693 class TempTestClass2<S extends String> extends TempTestClass1<S> {
697 level = TestLevel.COMPLETE,
699 method = "isAnnotation",
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());
710 level = TestLevel.COMPLETE,
712 method = "isAnnotationPresent",
713 args = {java.lang.Class.class}
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));
724 level = TestLevel.COMPLETE,
726 method = "isAnonymousClass",
729 public void test_isAnonymousClass() {
730 assertFalse(PublicTestClass.class.isAnonymousClass());
731 assertTrue((new Thread() {}).getClass().isAnonymousClass());
735 level = TestLevel.COMPLETE,
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());
748 level = TestLevel.COMPLETE,
750 method = "isLocalClass",
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());
762 level = TestLevel.COMPLETE,
764 method = "isMemberClass",
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());
776 level = TestLevel.COMPLETE,
778 method = "isSynthetic",
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());
789 String className = "org.apache.harmony.luni.tests.java.lang.ClassLoaderTest$1";
793 * assertTrue("Returned false for synthetic class.",
794 * getClass().getClassLoader().loadClass(className).
796 *} catch(ClassNotFoundException cnfe) {
797 * fail("Class " + className + " can't be found.");
804 level = TestLevel.COMPLETE,
806 method = "isInstance",
807 args = {java.lang.Object.class}
809 public void test_isInstance() {
813 level = TestLevel.COMPLETE,
815 method = "getCanonicalName",
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"};
828 for(int i = 0; i < classArray.length; i++) {
829 assertEquals(classNames[i], classArray[i].getCanonicalName());
834 level = TestLevel.COMPLETE,
836 method = "getClassLoader",
839 public void test_getClassLoader() {
841 assertEquals(ExtendTestClass.class.getClassLoader(),
842 PublicTestClass.class.getClassLoader());
844 assertNull(int.class.getClassLoader());
845 assertNull(void.class.getClassLoader());
847 SecurityManager sm = new SecurityManager() {
849 public void checkPermission(Permission perm) {
850 if ((perm instanceof RuntimePermission) &&
851 perm.getName().equals("getClassLoader")) {
852 throw new SecurityException();
857 SecurityManager oldSm = System.getSecurityManager();
858 System.setSecurityManager(sm);
860 System.class.getClassLoader();
861 } catch (SecurityException e) {
862 fail("SecurityException should not be thrown.");
864 System.setSecurityManager(oldSm);
869 * @tests java.lang.Class#getClasses()
872 level = TestLevel.COMPLETE,
874 method = "getClasses",
877 public void test_getClasses() {
878 assertEquals("Incorrect class array returned",
879 4, ClassTest.class.getClasses().length);
883 * @tests java.lang.Class#getClasses()
886 level = TestLevel.NOT_FEASIBLE,
887 method = "getClasses",
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;
896 class MyCombiner implements DomainCombiner {
899 public ProtectionDomain[] combine(ProtectionDomain[] executionDomains,
900 ProtectionDomain[] parentDomains) {
902 return new ProtectionDomain[0];
905 private boolean recurring = false;
907 public boolean isPriviledged() {
915 AccessController.checkPermission(privCheckPermission);
916 } catch (SecurityException e) {}
924 final MyCombiner combiner = new MyCombiner();
925 class SecurityManagerCheck extends SecurityManager {
934 int checkMemberAccess;
936 int checkPackageAccess;
938 public void setExpected(String reason, Class<?> cls, int type) {
939 this.reason = reason;
943 checkMemberAccess = 0;
944 checkPackageAccess = 0;
948 public void checkPermission(Permission perm) {
949 if (combiner.isPriviledged())
955 public void checkMemberAccess(Class<?> cls, int type) {
956 if (combiner.isPriviledged())
959 assertEquals(reason + " unexpected class", checkClass, cls);
960 assertEquals(reason + "unexpected type", checkType, type);
964 public void checkPackageAccess(String packageName) {
965 if (combiner.isPriviledged())
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);
975 public void assertProperCalls() {
976 assertEquals(reason + " unexpected checkPermission count",
978 assertEquals(reason + " unexpected checkMemberAccess count",
979 1, checkMemberAccess);
980 assertEquals(reason + " unexpected checkPackageAccess count",
981 1, checkPackageAccess);
985 AccessControlContext acc = new AccessControlContext(new ProtectionDomain[0]);
986 AccessControlContext acc2 = new AccessControlContext(acc, combiner);
988 PrivilegedAction<?> action = new PrivilegedAction<Object>() {
989 public Object run() {
990 File resources = Support_Resources.createTempFolder();
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);
1000 sm.setExpected("getClasses", cls, Member.PUBLIC);
1002 sm.assertProperCalls();
1004 sm.setExpected("getDeclaredClasses", cls, Member.DECLARED);
1005 cls.getDeclaredClasses();
1006 sm.assertProperCalls();
1008 sm.setExpected("getConstructor", cls, Member.PUBLIC);
1009 cls.getConstructor(new Class[0]);
1010 sm.assertProperCalls();
1012 sm.setExpected("getConstructors", cls, Member.PUBLIC);
1013 cls.getConstructors();
1014 sm.assertProperCalls();
1016 sm.setExpected("getDeclaredConstructor", cls, Member.DECLARED);
1017 cls.getDeclaredConstructor(new Class[0]);
1018 sm.assertProperCalls();
1020 sm.setExpected("getDeclaredConstructors", cls, Member.DECLARED);
1021 cls.getDeclaredConstructors();
1022 sm.assertProperCalls();
1024 sm.setExpected("getField", cls, Member.PUBLIC);
1025 cls.getField("publicField");
1026 sm.assertProperCalls();
1028 sm.setExpected("getFields", cls, Member.PUBLIC);
1030 sm.assertProperCalls();
1032 sm.setExpected("getDeclaredField", cls, Member.DECLARED);
1033 cls.getDeclaredField("publicField");
1034 sm.assertProperCalls();
1036 sm.setExpected("getDeclaredFields", cls, Member.DECLARED);
1037 cls.getDeclaredFields();
1038 sm.assertProperCalls();
1040 sm.setExpected("getDeclaredMethod", cls, Member.DECLARED);
1041 cls.getDeclaredMethod("publicMethod", new Class[0]);
1042 sm.assertProperCalls();
1044 sm.setExpected("getDeclaredMethods", cls, Member.DECLARED);
1045 cls.getDeclaredMethods();
1046 sm.assertProperCalls();
1048 sm.setExpected("getMethod", cls, Member.PUBLIC);
1049 cls.getMethod("publicMethod", new Class[0]);
1050 sm.assertProperCalls();
1052 sm.setExpected("getMethods", cls, Member.PUBLIC);
1054 sm.assertProperCalls();
1056 sm.setExpected("newInstance", cls, Member.PUBLIC);
1058 sm.assertProperCalls();
1060 System.setSecurityManager(null);
1062 /* Remove this comment to let the test pass on Android.
1063 } catch (java.lang.ClassNotFoundException ce) {
1064 // Expected for Android.
1066 } catch (Exception e) {
1067 if (e instanceof RuntimeException)
1068 throw (RuntimeException) e;
1069 fail("unexpected exception: " + e);
1074 AccessController.doPrivileged(action, acc2);
1078 * @tests java.lang.Class#getComponentType()
1081 level = TestLevel.COMPLETE,
1083 method = "getComponentType",
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());
1095 * @tests java.lang.Class#getConstructor(java.lang.Class[])
1098 level = TestLevel.COMPLETE,
1100 method = "getConstructor",
1101 args = {java.lang.Class[].class}
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",
1110 TestClass.class.getConstructor(Object.class);
1111 fail("Found private constructor");
1112 } catch (NoSuchMethodException e) {
1113 // Correct - constructor with obj is private
1116 SecurityManager oldSm = System.getSecurityManager();
1117 System.setSecurityManager(sm);
1119 TestClass.class.getConstructor(new Class[0]);
1120 fail("Should throw SecurityException");
1121 } catch (SecurityException e) {
1124 System.setSecurityManager(oldSm);
1129 * @tests java.lang.Class#getConstructors()
1132 level = TestLevel.COMPLETE,
1134 method = "getConstructors",
1137 public void test_getConstructors() throws Exception {
1138 Constructor[] c = TestClass.class.getConstructors();
1139 assertEquals("Incorrect number of constructors returned", 1, c.length);
1141 SecurityManager oldSm = System.getSecurityManager();
1142 System.setSecurityManager(sm);
1144 TestClass.class.getConstructors();
1145 fail("Should throw SecurityException");
1146 } catch (SecurityException e) {
1149 System.setSecurityManager(oldSm);
1154 * @tests java.lang.Class#getDeclaredClasses()
1157 level = TestLevel.COMPLETE,
1159 method = "getDeclaredClasses",
1162 public void test_getDeclaredClasses() {
1164 Class [] declClasses = Object.class.getDeclaredClasses();
1165 assertEquals("Incorrect length of declared classes array is returned " +
1166 "for Object.", 0, declClasses.length);
1168 declClasses = PublicTestClass.class.getDeclaredClasses();
1169 assertEquals(2, declClasses.length);
1171 assertEquals(0, int.class.getDeclaredClasses().length);
1172 assertEquals(0, void.class.getDeclaredClasses().length);
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();
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());
1188 declClasses = TestInterface.class.getDeclaredClasses();
1189 assertEquals(0, declClasses.length);
1191 SecurityManager sm = new SecurityManager() {
1193 final String forbidenPermissionName = "user.dir";
1195 public void checkPermission(Permission perm) {
1196 if (perm.getName().equals(forbidenPermissionName)) {
1197 throw new SecurityException();
1201 public void checkMemberAccess(Class<?> clazz,
1203 if(clazz.equals(TestInterface.class)) {
1204 throw new SecurityException();
1208 public void checkPackageAccess(String pkg) {
1209 if(pkg.equals(PublicTestClass.class.getPackage())) {
1210 throw new SecurityException();
1216 SecurityManager oldSm = System.getSecurityManager();
1217 System.setSecurityManager(sm);
1219 TestInterface.class.getDeclaredClasses();
1220 fail("Should throw SecurityException");
1221 } catch (SecurityException e) {
1224 System.setSecurityManager(oldSm);
1231 * @tests java.lang.Class#getDeclaredConstructor(java.lang.Class[])
1234 level = TestLevel.COMPLETE,
1236 method = "getDeclaredConstructor",
1237 args = {java.lang.Class[].class}
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);
1245 TestClass.class.getDeclaredConstructor(String.class);
1246 fail("NoSuchMethodException should be thrown.");
1247 } catch(NoSuchMethodException nsme) {
1251 SecurityManager oldSm = System.getSecurityManager();
1252 System.setSecurityManager(sm);
1254 TestClass.class.getDeclaredConstructor(Object.class);
1255 fail("Should throw SecurityException");
1256 } catch (SecurityException e) {
1259 System.setSecurityManager(oldSm);
1264 * @tests java.lang.Class#getDeclaredConstructors()
1267 level = TestLevel.COMPLETE,
1269 method = "getDeclaredConstructors",
1272 public void test_getDeclaredConstructors() throws Exception {
1273 Constructor[] c = TestClass.class.getDeclaredConstructors();
1274 assertEquals("Incorrect number of constructors returned", 2, c.length);
1276 SecurityManager oldSm = System.getSecurityManager();
1277 System.setSecurityManager(sm);
1279 TestClass.class.getDeclaredConstructors();
1280 fail("Should throw SecurityException");
1281 } catch (SecurityException e) {
1284 System.setSecurityManager(oldSm);
1289 * @tests java.lang.Class#getDeclaredField(java.lang.String)
1292 level = TestLevel.COMPLETE,
1294 method = "getDeclaredField",
1295 args = {java.lang.String.class}
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()));
1302 TestClass.class.getDeclaredField(null);
1303 fail("NullPointerException is not thrown.");
1304 } catch(NullPointerException npe) {
1309 TestClass.class.getDeclaredField("NonExistentField");
1310 fail("NoSuchFieldException is not thrown.");
1311 } catch(NoSuchFieldException nsfe) {
1315 SecurityManager oldSm = System.getSecurityManager();
1316 System.setSecurityManager(sm);
1318 TestClass.class.getDeclaredField("pubField");
1319 fail("Should throw SecurityException");
1320 } catch (SecurityException e) {
1323 System.setSecurityManager(oldSm);
1328 * @tests java.lang.Class#getDeclaredFields()
1331 level = TestLevel.COMPLETE,
1333 method = "getDeclaredFields",
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);
1343 SecurityManager oldSm = System.getSecurityManager();
1344 System.setSecurityManager(sm);
1346 TestClass.class.getDeclaredFields();
1347 fail("Should throw SecurityException");
1348 } catch (SecurityException e) {
1351 System.setSecurityManager(oldSm);
1356 * @tests java.lang.Class#getDeclaredMethod(java.lang.String,
1357 * java.lang.Class[])
1360 level = TestLevel.COMPLETE,
1362 method = "getDeclaredMethod",
1363 args = {java.lang.String.class, java.lang.Class[].class}
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())))
1369 m = TestClass.class.getDeclaredMethod("privMethod", new Class[0]);
1372 TestClass.class.getDeclaredMethod(null, new Class[0]);
1373 fail("NullPointerException is not thrown.");
1374 } catch(NullPointerException npe) {
1379 TestClass.class.getDeclaredMethod("NonExistentMethod", new Class[0]);
1380 fail("NoSuchMethodException is not thrown.");
1381 } catch(NoSuchMethodException nsme) {
1385 SecurityManager oldSm = System.getSecurityManager();
1386 System.setSecurityManager(sm);
1388 TestClass.class.getDeclaredMethod("pubMethod", new Class[0]);
1389 fail("Should throw SecurityException");
1390 } catch (SecurityException e) {
1393 System.setSecurityManager(oldSm);
1398 * @tests java.lang.Class#getDeclaredMethods()
1401 level = TestLevel.COMPLETE,
1403 method = "getDeclaredMethods",
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);
1412 SecurityManager oldSm = System.getSecurityManager();
1413 System.setSecurityManager(sm);
1415 TestClass.class.getDeclaredMethods();
1416 fail("Should throw SecurityException");
1417 } catch (SecurityException e) {
1420 System.setSecurityManager(oldSm);
1425 * @tests java.lang.Class#getDeclaringClass()
1428 level = TestLevel.COMPLETE,
1430 method = "getDeclaringClass",
1433 public void test_getDeclaringClass() {
1434 assertEquals(ClassTest.class, TestClass.class.getDeclaringClass());
1435 assertNull(PublicTestClass.class.getDeclaringClass());
1439 * @tests java.lang.Class#getField(java.lang.String)
1442 level = TestLevel.COMPLETE,
1444 method = "getField",
1445 args = {java.lang.String.class}
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()));
1451 f = PublicTestClass.class.getField("TEST_FIELD");
1452 assertEquals("Returned incorrect field", "test field",
1453 f.get(new PublicTestClass()));
1455 f = PublicTestClass.class.getField("TEST_INTERFACE_FIELD");
1456 assertEquals("Returned incorrect field", 0,
1457 f.getInt(new PublicTestClass()));
1460 f = TestClass.class.getField("privField");
1461 fail("Private field access failed to throw exception");
1462 } catch (NoSuchFieldException e) {
1467 TestClass.class.getField(null);
1468 fail("NullPointerException is thrown.");
1469 } catch(NullPointerException npe) {
1473 SecurityManager sm = new SecurityManager() {
1475 final String forbidenPermissionName = "user.dir";
1477 public void checkPermission(Permission perm) {
1478 if (perm.getName().equals(forbidenPermissionName)) {
1479 throw new SecurityException();
1483 public void checkMemberAccess(Class<?> clazz,
1485 if(clazz.equals(TestClass.class)) {
1486 throw new SecurityException();
1490 public void checkPackageAccess(String pkg) {
1491 if(pkg.equals(TestClass.class.getPackage())) {
1492 throw new SecurityException();
1498 SecurityManager oldSm = System.getSecurityManager();
1499 System.setSecurityManager(sm);
1501 TestClass.class.getField("pubField");
1502 fail("Should throw SecurityException");
1503 } catch (SecurityException e) {
1506 System.setSecurityManager(oldSm);
1511 * @tests java.lang.Class#getFields()
1514 level = TestLevel.PARTIAL_COMPLETE,
1516 method = "getFields",
1519 public void test_getFields2() throws Exception {
1521 Field expected = null;
1523 f = PublicTestClass.class.getFields();
1524 assertEquals("Test 1: Incorrect number of fields;", 2, f.length);
1526 f = Cls2.class.getFields();
1527 assertEquals("Test 2: Incorrect number of fields;", 6, f.length);
1529 f = Cls3.class.getFields();
1530 assertEquals("Test 2: Incorrect number of fields;", 5, f.length);
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")) {
1539 if (expected == null) {
1540 fail("Test 3: getFields() did not return all fields.");
1542 assertEquals("Test 4: Incorrect field;", expected,
1543 Cls3.class.getField("field1"));
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")) {
1553 if (expected == null) {
1554 fail("Test 5: getFields() did not return all fields.");
1556 assertEquals("Test 6: Incorrect field;", expected,
1557 Cls3.class.getField("field2"));
1561 * @tests java.lang.Class#getFields()
1564 level = TestLevel.PARTIAL_COMPLETE,
1566 method = "getFields",
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);
1576 SecurityManager oldSm = System.getSecurityManager();
1577 System.setSecurityManager(sm);
1579 TestClass.class.getFields();
1580 fail("Should throw SecurityException");
1581 } catch (SecurityException e) {
1584 System.setSecurityManager(oldSm);
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")) {
1596 if (expected == null) {
1597 fail("getFields() did not return all fields");
1599 assertEquals(expected, Cls2.class.getField("field1"));
1603 * @tests java.lang.Class#getInterfaces()
1606 level = TestLevel.COMPLETE,
1608 method = "getInterfaces",
1611 public void test_getInterfaces() {
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));
1622 Class [] interfaces1 = Cls1.class.getInterfaces();
1623 assertEquals(1, interfaces1.length);
1624 assertEquals(Intf2.class, interfaces1[0]);
1626 Class [] interfaces2 = Cls2.class.getInterfaces();
1627 assertEquals(1, interfaces2.length);
1628 assertEquals(Intf1.class, interfaces2[0]);
1630 Class [] interfaces3 = Cls3.class.getInterfaces();
1631 assertEquals(2, interfaces3.length);
1632 assertEquals(Intf3.class, interfaces3[0]);
1633 assertEquals(Intf4.class, interfaces3[1]);
1635 Class [] interfaces4 = Cls4.class.getInterfaces();
1636 assertEquals(0, interfaces4.length);
1640 * @tests java.lang.Class#getMethod(java.lang.String, java.lang.Class[])
1643 level = TestLevel.COMPLETE,
1645 method = "getMethod",
1646 args = {java.lang.String.class, java.lang.Class[].class}
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())))
1653 m = ExtendTestClass1.class.getMethod("getCount", new Class[0]);
1654 assertEquals("Returned incorrect method", 0, ((Integer) (m.invoke(new ExtendTestClass1())))
1658 m = TestClass.class.getMethod("privMethod", new Class[0]);
1659 fail("Failed to throw exception accessing private method");
1660 } catch (NoSuchMethodException e) {
1666 m = TestClass.class.getMethod("init", new Class[0]);
1667 fail("Failed to throw exception accessing to init method");
1668 } catch (NoSuchMethodException e) {
1674 TestClass.class.getMethod("pubMethod", new Class[0]);
1675 fail("NullPointerException is not thrown.");
1676 } catch(NullPointerException npe) {
1680 SecurityManager oldSm = System.getSecurityManager();
1681 System.setSecurityManager(sm);
1683 TestClass.class.getMethod("pubMethod", new Class[0]);
1684 fail("Should throw SecurityException");
1685 } catch (SecurityException e) {
1688 System.setSecurityManager(oldSm);
1693 * @tests java.lang.Class#getMethods()
1696 level = TestLevel.COMPLETE,
1698 method = "getMethods",
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
1710 SecurityManager oldSm = System.getSecurityManager();
1711 System.setSecurityManager(sm);
1713 TestClass.class.getMethods();
1714 fail("Should throw SecurityException");
1715 } catch (SecurityException e) {
1718 System.setSecurityManager(oldSm);
1721 assertEquals("Incorrect number of methods", 10,
1722 Cls2.class.getMethods().length);
1723 assertEquals("Incorrect number of methods", 11,
1724 Cls3.class.getMethods().length);
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()")) {
1735 if (expected == null) {
1736 fail("getMethods() did not return all methods");
1738 assertEquals(expected, Cls2.class.getMethod("test"));
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()")) {
1749 if (expected == null) {
1750 fail("getMethods() did not return all methods");
1752 assertEquals(expected, Cls3.class.getMethod("test"));
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)")) {
1764 if (expected == null) {
1765 fail("getMethods() did not return all methods");
1768 assertEquals(expected, Cls3.class.getMethod("test2", int.class,
1771 assertEquals("Incorrect number of methods", 1,
1772 Intf5.class.getMethods().length);
1775 private static final class PrivateClass {
1778 * @tests java.lang.Class#getModifiers()
1781 level = TestLevel.COMPLETE,
1783 method = "getModifiers",
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));
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));
1799 * @tests java.lang.Class#getName()
1802 level = TestLevel.COMPLETE,
1807 public void test_getName() throws Exception {
1808 String className = Class.forName("java.lang.Object").getName();
1809 assertNotNull(className);
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);
1817 className = Class.forName("[Ljava.lang.Object;").getName();
1818 assertNotNull(className);
1820 assertEquals("Class getName printed wrong value", "[Ljava.lang.Object;", className);
1824 * @tests java.lang.Class#getResource(java.lang.String)
1827 level = TestLevel.COMPLETE,
1829 method = "getResource",
1830 args = {java.lang.String.class}
1832 public void test_getResourceLjava_lang_String() {
1833 final String name = "/";
1834 URL res = getClass().getResource(name + "HelloWorld.txt");
1836 assertNull(getClass().getResource(
1837 "org/apache/harmony/luni/tests/java/lang/NonExistentResource"));
1838 assertNull(getClass().getResource(name + "NonExistentResource"));
1842 * @tests java.lang.Class#getResourceAsStream(java.lang.String)
1845 level = TestLevel.PARTIAL_COMPLETE,
1847 method = "getResourceAsStream",
1848 args = {java.lang.String.class}
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));
1855 final String nameBadURI = "org/apache/harmony/luni/tests/" +
1856 "test_resource.txt";
1857 assertNull("the file " + nameBadURI + " should not be found in this " +
1859 getClass().getResourceAsStream(nameBadURI));
1861 ClassLoader pcl = getClass().getClassLoader();
1862 Class<?> clazz = pcl.loadClass("org.apache.harmony.luni.tests.java.lang.ClassTest");
1863 assertNotNull(clazz.getResourceAsStream("HelloWorld1.txt"));
1866 getClass().getResourceAsStream(null);
1867 fail("NullPointerException is not thrown.");
1868 } catch(NullPointerException npe) {
1874 * @tests java.lang.Class#getSuperclass()
1877 level = TestLevel.COMPLETE,
1879 method = "getSuperclass",
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
1888 assertNull("Base class has a superclass", int.class.getSuperclass());
1889 assertNull("Interface class has a superclass", Cloneable.class.getSuperclass());
1893 * @tests java.lang.Class#isArray()
1896 level = TestLevel.COMPLETE,
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());
1907 clazz = Class.forName("[Ljava.lang.Object;");
1908 assertTrue("Object Array type claims not to be.", clazz.isArray());
1910 clazz = Class.forName("java.lang.Object");
1911 assertTrue("Non-array Object type claims to be.", !clazz.isArray());
1915 * @tests java.lang.Class#isAssignableFrom(java.lang.Class)
1918 level = TestLevel.COMPLETE,
1920 method = "isAssignableFrom",
1921 args = {java.lang.Class.class}
1923 public void test_isAssignableFromLjava_lang_Class() {
1924 Class<?> clazz1 = null;
1925 Class<?> clazz2 = null;
1927 clazz1 = Object.class;
1928 clazz2 = Class.class;
1929 assertTrue("returned false for superclass",
1930 clazz1.isAssignableFrom(clazz2));
1932 clazz1 = TestClass.class;
1933 assertTrue("returned false for same class",
1934 clazz1.isAssignableFrom(clazz1));
1936 clazz1 = Runnable.class;
1937 clazz2 = Thread.class;
1938 assertTrue("returned false for implemented interface",
1939 clazz1.isAssignableFrom(clazz2));
1941 assertFalse("returned true not assignable classes",
1942 Integer.class.isAssignableFrom(String.class));
1945 clazz1.isAssignableFrom(null);
1946 fail("NullPointerException is not thrown.");
1947 } catch(NullPointerException npe) {
1953 * @tests java.lang.Class#isInterface()
1956 level = TestLevel.COMPLETE,
1958 method = "isInterface",
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());
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());
1972 clazz = Class.forName("[Ljava.lang.Object;");
1973 assertTrue("Array type claims to be interface.", !clazz.isInterface());
1977 * @tests java.lang.Class#isPrimitive()
1980 level = TestLevel.COMPLETE,
1982 method = "isPrimitive",
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());
2001 * @tests java.lang.Class#newInstance()
2004 level = TestLevel.PARTIAL_COMPLETE,
2006 method = "newInstance",
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());
2014 clazz = Class.forName("java.lang.Throwable");
2015 assertSame("new Throwable instance was not a throwable",
2016 clazz, clazz.newInstance().getClass());
2018 clazz = Class.forName("java.lang.Integer");
2020 clazz.newInstance();
2021 fail("Exception for instantiating a newInstance with no default " +
2022 " constructor is not thrown");
2023 } catch (InstantiationException e) {
2028 TestClass3.class.newInstance();
2029 fail("IllegalAccessException is not thrown.");
2030 } catch(IllegalAccessException iae) {
2035 TestClass1C.class.newInstance();
2036 fail("ExceptionInInitializerError should be thrown.");
2037 } catch (java.lang.ExceptionInInitializerError ie) {
2043 * @tests java.lang.Class#newInstance()
2046 level = TestLevel.PARTIAL_COMPLETE,
2048 method = "newInstance",
2051 public void test_newInstance2() throws Exception {
2052 SecurityManager oldSm = System.getSecurityManager();
2053 System.setSecurityManager(sm);
2055 TestClass.class.newInstance();
2056 fail("Test 1: SecurityException expected.");
2057 } catch (SecurityException e) {
2060 System.setSecurityManager(oldSm);
2065 * @tests java.lang.Class#toString()
2068 level = TestLevel.COMPLETE,
2070 method = "toString",
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());
2081 clazz = Class.forName("java.lang.Object");
2082 assertEquals("Class toString printed wrong value",
2083 "class java.lang.Object", clazz.toString());
2085 clazz = Class.forName("[Ljava.lang.Object;");
2086 assertEquals("Class toString printed wrong value",
2087 "class [Ljava.lang.Object;", clazz.toString());
2091 level = TestLevel.PARTIAL_COMPLETE,
2093 method = "getResourceAsStream",
2094 args = {java.lang.String.class}
2096 // Regression Test for JIRA-2047
2097 public void test_getResourceAsStream_withSharpChar() throws Exception{
2098 InputStream in = getClass().getResourceAsStream("/" + FILENAME);
2102 in = getClass().getResourceAsStream(FILENAME);
2105 in = this.getClass().getClassLoader().getResourceAsStream(
2112 * Regression test for HARMONY-2644:
2113 * Load system and non-system array classes via Class.forName()
2116 level = TestLevel.PARTIAL_COMPLETE,
2117 notes = "Verifies ClassNotFoundException.",
2119 args = {java.lang.String.class}
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);
2132 Class<?> clazz = Class.forName("[;");
2133 fail("1: " + clazz);
2134 } catch (ClassNotFoundException ok) {}
2136 Class<?> clazz = Class.forName("[[");
2138 } catch (ClassNotFoundException ok) {}
2140 Class<?> clazz = Class.forName("[L");
2142 } catch (ClassNotFoundException ok) {}
2144 Class<?> clazz = Class.forName("[L;");
2146 } catch (ClassNotFoundException ok) {}
2148 Class<?> clazz = Class.forName(";");
2150 } catch (ClassNotFoundException ok) {}
2152 Class<?> clazz = Class.forName("");
2154 } catch (ClassNotFoundException ok) {}
2158 level = TestLevel.PARTIAL_COMPLETE,
2160 method = "asSubclass",
2161 args = {java.lang.Class.class}
2163 public void test_asSubclass1() {
2164 assertEquals(ExtendTestClass.class,
2165 ExtendTestClass.class.asSubclass(PublicTestClass.class));
2167 assertEquals(PublicTestClass.class,
2168 PublicTestClass.class.asSubclass(TestInterface.class));
2170 assertEquals(ExtendTestClass1.class,
2171 ExtendTestClass1.class.asSubclass(PublicTestClass.class));
2173 assertEquals(PublicTestClass.class,
2174 PublicTestClass.class.asSubclass(PublicTestClass.class));
2178 level = TestLevel.PARTIAL_COMPLETE,
2180 method = "asSubclass",
2181 args = {java.lang.Class.class}
2183 public void test_asSubclass2() {
2185 PublicTestClass.class.asSubclass(ExtendTestClass.class);
2186 fail("Test 1: ClassCastException expected.");
2187 } catch(ClassCastException cce) {
2192 PublicTestClass.class.asSubclass(String.class);
2193 fail("Test 2: ClassCastException expected.");
2194 } catch(ClassCastException cce) {
2200 level = TestLevel.COMPLETE,
2203 args = {java.lang.Object.class}
2205 public void test_cast() {
2206 Object o = PublicTestClass.class.cast(new ExtendTestClass());
2207 assertTrue(o instanceof ExtendTestClass);
2210 ExtendTestClass.class.cast(new PublicTestClass());
2211 fail("Test 1: ClassCastException expected.");
2212 } catch(ClassCastException cce) {
2217 ExtendTestClass.class.cast(new String());
2218 fail("ClassCastException is not thrown.");
2219 } catch(ClassCastException cce) {
2225 level = TestLevel.COMPLETE,
2227 method = "desiredAssertionStatus",
2230 public void test_desiredAssertionStatus() {
2231 Class [] classArray = { Object.class, Integer.class,
2232 String.class, PublicTestClass.class,
2233 ExtendTestClass.class, ExtendTestClass1.class};
2235 for(int i = 0; i < classArray.length; i++) {
2236 assertFalse("assertion status for " + classArray[i],
2237 classArray[i].desiredAssertionStatus());
2243 SecurityManager sm = new SecurityManager() {
2245 final String forbidenPermissionName = "user.dir";
2247 public void checkPermission(Permission perm) {
2248 if (perm.getName().equals(forbidenPermissionName)) {
2249 throw new SecurityException();
2253 public void checkMemberAccess(Class<?> clazz,
2255 if(clazz.equals(TestClass.class)) {
2256 throw new SecurityException();
2260 public void checkPackageAccess(String pkg) {
2261 if(pkg.equals(TestClass.class.getPackage())) {
2262 throw new SecurityException();