Home | History | Annotate | Download | only in lang
      1 /*
      2  *  contributor license agreements.  See the NOTICE file distributed with
      3  *  this work for additional information regarding copyright ownership.
      4  *  The ASF licenses this file to You under the Apache License, Version 2.0
      5  *  (the "License"); you may not use this file except in compliance with
      6  *  the License.  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  *  Unless required by applicable law or agreed to in writing, software
     11  *  distributed under the License is distributed on an "AS IS" BASIS,
     12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  *  See the License for the specific language governing permissions and
     14  *  limitations under the License.
     15  */
     16 
     17 package libcore.java.lang;
     18 
     19 import java.io.File;
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 import java.io.Serializable;
     23 import java.lang.annotation.Annotation;
     24 import java.lang.annotation.Retention;
     25 import java.lang.annotation.RetentionPolicy;
     26 import java.lang.reflect.Constructor;
     27 import java.lang.reflect.Field;
     28 import java.lang.reflect.Method;
     29 import java.lang.reflect.ParameterizedType;
     30 import java.lang.reflect.Type;
     31 import java.lang.reflect.TypeVariable;
     32 import java.net.URL;
     33 import java.net.URLClassLoader;
     34 import java.util.AbstractList;
     35 import java.util.Collection;
     36 import java.util.List;
     37 import java.util.Vector;
     38 import tests.support.Support_ClassLoader;
     39 import tests.support.resource.Support_Resources;
     40 
     41 @SuppressWarnings("deprecation")
     42 public class OldClassTest extends junit.framework.TestCase {
     43     final String packageName = getClass().getPackage().getName();
     44     final String classNameInitError1 = packageName + ".TestClass1";
     45     final String classNameInitError2 = packageName + ".TestClass1B";
     46     final String classNameLinkageError = packageName + ".TestClass";
     47     final String sourceJARfile = "illegalClasses.jar";
     48     final String illegalClassName = "illegalClass";
     49 
     50     @Retention(RetentionPolicy.RUNTIME)
     51     public @interface TestAnnotation {
     52         String value();
     53     }
     54 
     55     public static class TestClass1C {
     56         static TestClass2 tc = new TestClass2(0);
     57 
     58         TestClass1C() {
     59         }
     60 
     61     }
     62 
     63     public static class TestClass2 {
     64 
     65         public TestClass2(int i) throws IllegalArgumentException {
     66             throw new IllegalArgumentException();
     67         }
     68     }
     69 
     70     public static class TestClass3 {
     71         private TestClass3() {}
     72     }
     73 
     74     interface TestInterface {
     75         public static int TEST_INTERFACE_FIELD = 0;
     76 
     77         int getCount();
     78         void setCount(int value);
     79     }
     80 
     81     static class StaticMember$Class {
     82         class Member2$A {
     83         }
     84     }
     85 
     86     class Member$Class {
     87         class Member3$B {
     88         }
     89     }
     90 
     91     @Deprecated
     92     @TestAnnotation("libcore.java.lang.OldClassTest$ExtendTestClass")
     93     public static class ExtendTestClass extends PublicTestClass {
     94 
     95         private static final long serialVersionUID = 1L;
     96 
     97         public enum enumExm {ONE, TWO, THREE};
     98         @Override
     99         public void setCount(int value) {
    100 
    101         }
    102     }
    103 
    104     public class ExtendTestClass1 extends ExtendTestClass {
    105 
    106     }
    107 
    108     @TestAnnotation("libcore.java.lang.OldClassTest$PublicTestClass")
    109     public static class PublicTestClass implements TestInterface, Serializable, Cloneable {
    110 
    111         private static final long serialVersionUID = 1L;
    112 
    113         public static String TEST_FIELD = "test field";
    114 
    115         Object clazz;
    116 
    117         public PublicTestClass() {
    118             class LocalClass { }
    119 
    120             clazz = new LocalClass();
    121         }
    122 
    123         public Object getLocalClass() {
    124             class LocalClass {}
    125             Object returnedObject = new LocalClass();
    126             return returnedObject;
    127         }
    128 
    129         int count = 0;
    130 
    131         public int getCount() {
    132             return count;
    133         }
    134 
    135         public void setCount(int value) {
    136             count = value;
    137         }
    138 
    139         private class PrivateClass1 {
    140 
    141             public String toString() {
    142                 return "PrivateClass0";
    143             }
    144         }
    145 
    146         public class PrivateClass2 {
    147 
    148             public String toString() {
    149                 return "PrivateClass1";
    150             }
    151         }
    152     }
    153 
    154     public static class TestClass {
    155         @SuppressWarnings("unused")
    156         private int privField = 1;
    157 
    158         public int pubField = 2;
    159 
    160         private Object cValue = null;
    161 
    162         public Object ack = new Object();
    163 
    164         @SuppressWarnings("unused")
    165         private int privMethod() {
    166             return 1;
    167         }
    168 
    169         public int pubMethod() {
    170             return 2;
    171         }
    172 
    173         public Object cValue() {
    174             return cValue;
    175         }
    176 
    177         public TestClass() {
    178         }
    179 
    180         @SuppressWarnings("unused")
    181         private TestClass(Object o) {
    182         }
    183     }
    184 
    185     public static class SubTestClass extends TestClass {
    186     }
    187 
    188     interface Intf1 {
    189         public int field1 = 1;
    190         public int field2 = 1;
    191         void test();
    192     }
    193 
    194     interface Intf2 {
    195         public int field1 = 1;
    196         void test();
    197     }
    198 
    199     interface Intf3 extends Intf1 {
    200         public int field1 = 1;
    201     }
    202 
    203     interface Intf4 extends Intf1, Intf2 {
    204         public int field1 = 1;
    205         void test2(int a, Object b);
    206     }
    207 
    208     interface Intf5 extends Intf1 {
    209     }
    210 
    211     class Cls1 implements Intf2 {
    212         public int field1 = 2;
    213         public int field2 = 2;
    214         public void test() {
    215         }
    216     }
    217 
    218     class Cls2 extends Cls1 implements Intf1 {
    219         public int field1 = 2;
    220         @Override
    221         public void test() {
    222         }
    223     }
    224 
    225     class Cls3 implements Intf3, Intf4 {
    226         public void test() {
    227         }
    228         public void test2(int a, Object b) {
    229         }
    230     }
    231 
    232     static class Cls4 {
    233 
    234     }
    235 
    236     public void test_getAnnotations() {
    237       Annotation [] annotations = PublicTestClass.class.getAnnotations();
    238       assertEquals(1, annotations.length);
    239       assertEquals(TestAnnotation.class, annotations[0].annotationType());
    240 
    241       annotations = ExtendTestClass.class.getAnnotations();
    242       assertEquals(2, annotations.length);
    243 
    244       for(int i = 0; i < annotations.length; i++) {
    245           Class<? extends Annotation> type = annotations[i].annotationType();
    246           assertTrue("Annotation's type " + i + ": " + type,
    247               type.equals(Deprecated.class) ||
    248               type.equals(TestAnnotation.class));
    249       }
    250     }
    251 
    252     public void test_forNameLjava_lang_StringLbooleanLClassLoader() throws Exception {
    253 
    254         ClassLoader pcl = getClass().getClassLoader();
    255 
    256         Class<?> [] classes = {PublicTestClass.class, ExtendTestClass.class,
    257                 ExtendTestClass1.class, TestInterface.class, String.class};
    258 
    259         for(int i = 0; i < classes.length; i++) {
    260             Class<?> clazz = Class.forName(classes[i].getName(), true, pcl);
    261             assertEquals(classes[i], clazz);
    262 
    263             clazz = Class.forName(classes[i].getName(), false, pcl);
    264             assertEquals(classes[i], clazz);
    265         }
    266 
    267         Class<?> [] systemClasses = {String.class, Integer.class, Object.class,
    268                 Object[].class};
    269 
    270         for(int i = 0; i < systemClasses.length; i++) {
    271             Class<?> clazz = Class.forName(systemClasses[i].getName(), true,
    272                                             ClassLoader.getSystemClassLoader());
    273             assertEquals(systemClasses[i], clazz);
    274 
    275             clazz = Class.forName(systemClasses[i].getName(), false,
    276                                             ClassLoader.getSystemClassLoader());
    277             assertEquals(systemClasses[i], clazz);
    278         }
    279 
    280         try  {
    281             Class.forName(null, true, pcl);
    282             fail("NullPointerException is not thrown.");
    283         } catch(NullPointerException  npe) {
    284             //expected
    285         }
    286 
    287         try {
    288             Class.forName("NotExistClass", true, pcl);
    289             fail("ClassNotFoundException is not thrown for non existent class.");
    290         } catch(ClassNotFoundException cnfe) {
    291             //expected
    292         }
    293 
    294         try {
    295             Class.forName("String", false, pcl);
    296             fail("ClassNotFoundException is not thrown for non existent class.");
    297         } catch(ClassNotFoundException cnfe) {
    298             //expected
    299         }
    300 
    301         try {
    302             Class.forName("libcore.java.lang.NonexistentClass", false, pcl);
    303             fail("ClassNotFoundException is not thrown for non existent class.");
    304         } catch(ClassNotFoundException cnfe) {
    305             //expected
    306         }
    307     }
    308 
    309     // AndroidOnly: Class.forName method throws ClassNotFoundException on Android.
    310     public void test_forNameLjava_lang_StringLbooleanLClassLoader_AndroidOnly() throws Exception {
    311 
    312         // Android doesn't support loading class files from a jar.
    313         try {
    314 
    315             URL url = getClass().getClassLoader().getResource(
    316                     packageName.replace(".", "/") + "/" + sourceJARfile);
    317 
    318             ClassLoader loader = new URLClassLoader(new URL[] { url },
    319                     getClass().getClassLoader());
    320             try {
    321                 Class.forName(classNameLinkageError, true, loader);
    322                 fail("LinkageError or ClassNotFoundException expected.");
    323             } catch (java.lang.LinkageError le) {
    324                 // Expected for the RI.
    325             } catch (java.lang.ClassNotFoundException ce) {
    326                 // Expected for Android.
    327             }
    328         } catch(Exception e) {
    329             fail("Unexpected exception was thrown: " + e.toString());
    330         }
    331 
    332         try {
    333             Class.forName(classNameInitError2,
    334                     true, getClass().getClassLoader());
    335             fail("ExceptionInInitializerError or ClassNotFoundException " +
    336             "should be thrown.");
    337         } catch (java.lang.ExceptionInInitializerError ie) {
    338             // Expected for the RI.
    339         // Remove this comment to let the test pass on Android.
    340         } catch (java.lang.ClassNotFoundException ce) {
    341             // Expected for Android.
    342         }
    343     }
    344 
    345     public void test_getAnnotation() {
    346       TestAnnotation target = PublicTestClass.class.getAnnotation(TestAnnotation.class);
    347       assertEquals(target.value(), PublicTestClass.class.getName());
    348 
    349       assertNull(PublicTestClass.class.getAnnotation(Deprecated.class));
    350 
    351       Deprecated target2 = ExtendTestClass.class.getAnnotation(Deprecated.class);
    352       assertNotNull(target2);
    353     }
    354 
    355     public void test_getDeclaredAnnotations() {
    356         Annotation [] annotations = PublicTestClass.class.getDeclaredAnnotations();
    357         assertEquals(1, annotations.length);
    358 
    359         annotations = ExtendTestClass.class.getDeclaredAnnotations();
    360         assertEquals(2, annotations.length);
    361 
    362         annotations = TestInterface.class.getDeclaredAnnotations();
    363         assertEquals(0, annotations.length);
    364 
    365         annotations = String.class.getDeclaredAnnotations();
    366         assertEquals(0, annotations.length);
    367     }
    368 
    369     public void test_getEnclosingClass() {
    370         Class clazz = OldClassTest.class.getEnclosingClass();
    371         assertNull(clazz);
    372 
    373         assertEquals(getClass(), Cls1.class.getEnclosingClass());
    374         assertEquals(getClass(), Intf1.class.getEnclosingClass());
    375         assertEquals(getClass(), Cls4.class.getEnclosingClass());
    376     }
    377 
    378     public void test_getEnclosingMethod() {
    379         Method clazz = ExtendTestClass.class.getEnclosingMethod();
    380         assertNull(clazz);
    381 
    382         PublicTestClass ptc = new PublicTestClass();
    383         try {
    384             assertEquals("getEnclosingMethod returns incorrect method.",
    385                     PublicTestClass.class.getMethod("getLocalClass",
    386                             (Class []) null),
    387                     ptc.getLocalClass().getClass().getEnclosingMethod());
    388         } catch(NoSuchMethodException nsme) {
    389             fail("NoSuchMethodException was thrown.");
    390         }
    391     }
    392 
    393     public void test_getEnclosingConstructor() {
    394 
    395         PublicTestClass ptc = new PublicTestClass();
    396 
    397         assertEquals("getEnclosingConstructor method returns incorrect class.",
    398                 PublicTestClass.class.getConstructors()[0],
    399                 ptc.clazz.getClass().getEnclosingConstructor());
    400 
    401         assertNull("getEnclosingConstructor should return null for local " +
    402                 "class declared in method.",
    403                 ptc.getLocalClass().getClass().getEnclosingConstructor());
    404 
    405         assertNull("getEnclosingConstructor should return null for local " +
    406                 "class declared in method.",
    407                 ExtendTestClass.class.getEnclosingConstructor());
    408     }
    409 
    410 
    411     public void test_getEnumConstants() {
    412         Object [] clazz = ExtendTestClass.class.getEnumConstants();
    413         assertNull(clazz);
    414         Object [] constants = TestEnum.class.getEnumConstants();
    415         assertEquals(TestEnum.values().length, constants.length);
    416         for(int i = 0; i < constants.length; i++) {
    417             assertEquals(TestEnum.values()[i], constants[i]);
    418         }
    419         assertEquals(0, TestEmptyEnum.class.getEnumConstants().length);
    420     }
    421     public enum TestEnum {
    422         ONE, TWO, THREE
    423     }
    424     public enum TestEmptyEnum {
    425     }
    426 
    427     public void test_getGenericInterfaces() {
    428         Type [] types = ExtendTestClass1.class.getGenericInterfaces();
    429         assertEquals(0, types.length);
    430 
    431         Class [] interfaces = {TestInterface.class, Serializable.class,
    432                                Cloneable.class};
    433         types = PublicTestClass.class.getGenericInterfaces();
    434         assertEquals(interfaces.length, types.length);
    435         for(int i = 0; i < types.length; i++) {
    436             assertEquals(interfaces[i], types[i]);
    437         }
    438 
    439         types = TestInterface.class.getGenericInterfaces();
    440         assertEquals(0, types.length);
    441 
    442         types = List.class.getGenericInterfaces();
    443         assertEquals(1, types.length);
    444         assertEquals(Collection.class, ((ParameterizedType)types[0]).getRawType());
    445 
    446         assertEquals(0, int.class.getGenericInterfaces().length);
    447         assertEquals(0, void.class.getGenericInterfaces().length);
    448     }
    449 
    450     public void test_getGenericSuperclass () {
    451         assertEquals(PublicTestClass.class,
    452                                   ExtendTestClass.class.getGenericSuperclass());
    453         assertEquals(ExtendTestClass.class,
    454                 ExtendTestClass1.class.getGenericSuperclass());
    455         assertEquals(Object.class, PublicTestClass.class.getGenericSuperclass());
    456         assertEquals(Object.class, String.class.getGenericSuperclass());
    457         assertEquals(null, TestInterface.class.getGenericSuperclass());
    458 
    459         ParameterizedType type = (ParameterizedType) Vector.class.getGenericSuperclass();
    460         assertEquals(AbstractList.class, type.getRawType());
    461     }
    462 
    463     // AndroidOnly: Uses dalvik.system.PathClassLoader.
    464     // Different behavior between cts host and run-core-test")
    465     public void test_getPackage() {
    466 
    467       Package thisPackage = getClass().getPackage();
    468       assertEquals("libcore.java.lang",
    469                       thisPackage.getName());
    470 
    471       Package stringPackage = String.class.getPackage();
    472       assertNotNull("java.lang", stringPackage.getName());
    473 
    474       String hyts_package_name = "hyts_package_dex.jar";
    475       File resources = Support_Resources.createTempFolder();
    476       Support_Resources.copyFile(resources, "Package", hyts_package_name);
    477 
    478       String resPath = resources.toString();
    479       if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\')
    480           resPath = resPath.substring(1);
    481 
    482       try {
    483 
    484           URL resourceURL = new URL("file:/" + resPath + "/Package/"
    485                   + hyts_package_name);
    486 
    487           ClassLoader cl = Support_ClassLoader.getInstance(resourceURL,
    488                   getClass().getClassLoader());
    489 
    490           Class clazz = cl.loadClass("C");
    491           assertNull("getPackage for C.class should return null",
    492                   clazz.getPackage());
    493 
    494           clazz = cl.loadClass("a.b.C");
    495           Package cPackage = clazz.getPackage();
    496           assertNotNull("getPackage for a.b.C.class should not return null",
    497                   cPackage);
    498 
    499         /*
    500          * URLClassLoader doesn't work on Android for jar files
    501          *
    502          * URL url = getClass().getClassLoader().getResource(
    503          *         packageName.replace(".", "/") + "/" + sourceJARfile);
    504          *
    505          * ClassLoader loader = new URLClassLoader(new URL[] { url }, null);
    506          *
    507          * try {
    508          *     Class<?> clazz = loader.loadClass(illegalClassName);
    509          *     Package pack = clazz.getPackage();
    510          *     assertNull(pack);
    511          * } catch(ClassNotFoundException cne) {
    512          *     fail("ClassNotFoundException was thrown for " + illegalClassName);
    513          * }
    514         */
    515       } catch(Exception e) {
    516           fail("Unexpected exception was thrown: " + e.toString());
    517       }
    518     }
    519 
    520     public void test_getSigners() {
    521         assertNull(void.class.getSigners());
    522         assertNull(PublicTestClass.class.getSigners());
    523     }
    524 
    525     public void test_getSimpleName() {
    526         assertEquals("PublicTestClass", PublicTestClass.class.getSimpleName());
    527         assertEquals("void", void.class.getSimpleName());
    528         assertEquals("int[]", int[].class.getSimpleName());
    529     }
    530 
    531     public void test_getTypeParameters() {
    532         assertEquals(0, PublicTestClass.class.getTypeParameters().length);
    533         TypeVariable [] tv = TempTestClass1.class.getTypeParameters();
    534         assertEquals(1, tv.length);
    535         assertEquals(Object.class, tv[0].getBounds()[0]);
    536 
    537         TempTestClass2<String> tc = new TempTestClass2<String>();
    538         tv = tc.getClass().getTypeParameters();
    539         assertEquals(1, tv.length);
    540         assertEquals(String.class, tv[0].getBounds()[0]);
    541     }
    542 
    543     class TempTestClass1<T> {
    544     }
    545 
    546     class TempTestClass2<S extends String> extends TempTestClass1<S> {
    547     }
    548 
    549     public void test_isAnnotation() {
    550         assertTrue(Deprecated.class.isAnnotation());
    551         assertTrue(TestAnnotation.class.isAnnotation());
    552         assertFalse(PublicTestClass.class.isAnnotation());
    553         assertFalse(String.class.isAnnotation());
    554     }
    555 
    556      public void test_isAnnotationPresent() {
    557         assertTrue(PublicTestClass.class.isAnnotationPresent(TestAnnotation.class));
    558         assertFalse(ExtendTestClass1.class.isAnnotationPresent(TestAnnotation.class));
    559         assertFalse(String.class.isAnnotationPresent(Deprecated.class));
    560         assertTrue(ExtendTestClass.class.isAnnotationPresent(TestAnnotation.class));
    561         assertTrue(ExtendTestClass.class.isAnnotationPresent(Deprecated.class));
    562      }
    563 
    564     public void test_isAnonymousClass() {
    565         assertFalse(PublicTestClass.class.isAnonymousClass());
    566         assertTrue((new Thread() {}).getClass().isAnonymousClass());
    567     }
    568 
    569     public void test_isEnum() {
    570       assertFalse(PublicTestClass.class.isEnum());
    571       assertFalse(ExtendTestClass.class.isEnum());
    572       assertTrue(TestEnum.ONE.getClass().isEnum());
    573       assertTrue(TestEnum.class.isEnum());
    574     }
    575 
    576     public void test_isLocalClass() {
    577         assertFalse(ExtendTestClass.class.isLocalClass());
    578         assertFalse(TestInterface.class.isLocalClass());
    579         assertFalse(TestEnum.class.isLocalClass());
    580         class InternalClass {}
    581         assertTrue(InternalClass.class.isLocalClass());
    582     }
    583 
    584     public void test_isMemberClass() {
    585         assertFalse(OldClassTest.class.isMemberClass());
    586         assertFalse(String.class.isMemberClass());
    587         assertTrue(TestEnum.class.isMemberClass());
    588         assertTrue(StaticMember$Class.class.isMemberClass());
    589     }
    590 
    591     public void test_isSynthetic() {
    592       assertFalse("Returned true for non synthetic class.",
    593               ExtendTestClass.class.isSynthetic());
    594       assertFalse("Returned true for non synthetic class.",
    595               TestInterface.class.isSynthetic());
    596       assertFalse("Returned true for non synthetic class.",
    597               String.class.isSynthetic());
    598     }
    599 
    600     public void test_getCanonicalName() {
    601         Class [] classArray = { int.class, int[].class, String.class,
    602                                 PublicTestClass.class, TestInterface.class,
    603                                 ExtendTestClass.class };
    604         String [] classNames = {"int", "int[]", "java.lang.String",
    605                       "libcore.java.lang.OldClassTest.PublicTestClass",
    606                         "libcore.java.lang.OldClassTest.TestInterface",
    607                      "libcore.java.lang.OldClassTest.ExtendTestClass"};
    608 
    609         for(int i = 0; i < classArray.length; i++) {
    610             assertEquals(classNames[i], classArray[i].getCanonicalName());
    611         }
    612     }
    613 
    614     public void test_getClassLoader() {
    615         assertEquals(ExtendTestClass.class.getClassLoader(),
    616                          PublicTestClass.class.getClassLoader());
    617 
    618         assertNull(int.class.getClassLoader());
    619         assertNull(void.class.getClassLoader());
    620     }
    621 
    622     public void test_getClasses() {
    623         assertEquals("Incorrect class array returned",
    624                      11, OldClassTest.class.getClasses().length);
    625     }
    626 
    627     public void test_getDeclaredClasses() {
    628         Class [] declClasses = Object.class.getDeclaredClasses();
    629         assertEquals("Incorrect length of declared classes array is returned " +
    630                 "for Object.", 0, declClasses.length);
    631 
    632         declClasses = PublicTestClass.class.getDeclaredClasses();
    633         assertEquals(2, declClasses.length);
    634 
    635         assertEquals(0, int.class.getDeclaredClasses().length);
    636         assertEquals(0, void.class.getDeclaredClasses().length);
    637 
    638         for(int i = 0; i < declClasses.length; i++) {
    639             Constructor<?> constr = declClasses[i].getDeclaredConstructors()[0];
    640             constr.setAccessible(true);
    641             PublicTestClass publicClazz = new PublicTestClass();
    642             try {
    643                 Object o = constr.newInstance(publicClazz);
    644                 assertTrue("Returned incorrect class: " + o.toString(),
    645                         o.toString().startsWith("PrivateClass"));
    646             } catch(Exception e) {
    647                 fail("Unexpected exception was thrown: " + e.toString());
    648             }
    649         }
    650 
    651         declClasses = TestInterface.class.getDeclaredClasses();
    652         assertEquals(0, declClasses.length);
    653     }
    654 
    655     public void test_getDeclaredConstructor$Ljava_lang_Class() throws Exception {
    656         try {
    657             TestClass.class.getDeclaredConstructor(String.class);
    658             fail("NoSuchMethodException should be thrown.");
    659         } catch(NoSuchMethodException nsme) {
    660             //expected
    661         }
    662     }
    663 
    664     public void test_getDeclaredFieldLjava_lang_String() throws Exception {
    665         try {
    666             TestClass.class.getDeclaredField(null);
    667             fail("NullPointerException is not thrown.");
    668         } catch(NullPointerException npe) {
    669             //expected
    670         }
    671 
    672         try {
    673             TestClass.class.getDeclaredField("NonExistentField");
    674             fail("NoSuchFieldException is not thrown.");
    675         } catch(NoSuchFieldException nsfe) {
    676             //expected
    677         }
    678     }
    679 
    680     public void test_getDeclaredMethodLjava_lang_String$Ljava_lang_Class() throws Exception {
    681         try {
    682             TestClass.class.getDeclaredMethod(null, new Class[0]);
    683             fail("NullPointerException is not thrown.");
    684         } catch(NullPointerException npe) {
    685             //expected
    686         }
    687 
    688         try {
    689             TestClass.class.getDeclaredMethod("NonExistentMethod", new Class[0]);
    690             fail("NoSuchMethodException is not thrown.");
    691         } catch(NoSuchMethodException nsme) {
    692             //expected
    693         }
    694     }
    695 
    696     public void test_getMethodLjava_lang_String$Ljava_lang_Class() throws Exception {
    697         Method m = ExtendTestClass1.class.getMethod("getCount", new Class[0]);
    698         assertEquals("Returned incorrect method", 0, ((Integer) (m.invoke(new ExtendTestClass1())))
    699                 .intValue());
    700 
    701         try {
    702             m = TestClass.class.getMethod("init", new Class[0]);
    703             fail("Failed to throw exception accessing to init method");
    704         } catch (NoSuchMethodException e) {
    705             // Correct
    706             return;
    707         }
    708 
    709         try {
    710             TestClass.class.getMethod("pubMethod", new Class[0]);
    711             fail("NullPointerException is not thrown.");
    712         } catch(NullPointerException npe) {
    713             //expected
    714         }
    715     }
    716 
    717     public void test_getDeclaringClass() {
    718         assertEquals(OldClassTest.class, Intf1.class.getDeclaringClass());
    719         assertEquals(null, Serializable.class.getDeclaringClass());
    720         assertEquals(null, OldClassTest.class.getDeclaringClass());
    721 
    722         assertEquals(OldClassTest.class, PublicTestClass.class.getDeclaringClass());
    723 
    724         // https://code.google.com/p/android/issues/detail?id=61003
    725         assertEquals(null, new Object() {}.getClass().getDeclaringClass());
    726         assertEquals(null, new AnonymousMemberFixture().instanceOfAnonymousClass.getClass().getDeclaringClass());
    727 
    728         // Arrays, primitive types, and void all return null.
    729         assertEquals(null, char[].class.getDeclaringClass());
    730         assertEquals(null, int.class.getDeclaringClass());
    731         assertEquals(null, void.class.getDeclaringClass());
    732     }
    733 
    734     public void test_getFieldLjava_lang_String() throws Exception {
    735         Field f = TestClass.class.getField("pubField");
    736         assertEquals("Returned incorrect field", 2, f.getInt(new TestClass()));
    737 
    738         f = PublicTestClass.class.getField("TEST_FIELD");
    739         assertEquals("Returned incorrect field", "test field",
    740                 f.get(new PublicTestClass()));
    741 
    742         f = PublicTestClass.class.getField("TEST_INTERFACE_FIELD");
    743         assertEquals("Returned incorrect field", 0,
    744                 f.getInt(new PublicTestClass()));
    745 
    746         try {
    747             TestClass.class.getField(null);
    748             fail("NullPointerException is thrown.");
    749         } catch(NullPointerException npe) {
    750             //expected
    751         }
    752     }
    753 
    754     public void test_getFields2() throws Exception {
    755         Field[] f;
    756         Field expected = null;
    757 
    758         f = PublicTestClass.class.getFields();
    759         assertEquals("Test 1: Incorrect number of fields;", 2, f.length);
    760 
    761         f = Cls2.class.getFields();
    762         assertEquals("Test 2: Incorrect number of fields;", 6, f.length);
    763 
    764         f = Cls3.class.getFields();
    765         assertEquals("Test 2: Incorrect number of fields;", 5, f.length);
    766 
    767         for (Field field : f) {
    768             if (field.toString().equals("public static final int "
    769                     + "libcore.java.lang.OldClassTest$Intf3.field1")) {
    770                 expected = field;
    771                 break;
    772             }
    773         }
    774         if (expected == null) {
    775             fail("Test 3: getFields() did not return all fields.");
    776         }
    777         assertEquals("Test 4: Incorrect field;", expected,
    778                 Cls3.class.getField("field1"));
    779 
    780         expected = null;
    781         for (Field field : f) {
    782             if(field.toString().equals("public static final int " +
    783                     "libcore.java.lang.OldClassTest$Intf1.field2")) {
    784                 expected = field;
    785                 break;
    786             }
    787         }
    788         if (expected == null) {
    789             fail("Test 5: getFields() did not return all fields.");
    790         }
    791         assertEquals("Test 6: Incorrect field;", expected,
    792                 Cls3.class.getField("field2"));
    793     }
    794 
    795     public void test_getFields() throws Exception {
    796         Field expected = null;
    797         Field[] fields = Cls2.class.getFields();
    798         for (Field field : fields) {
    799             if(field.toString().equals("public int libcore.java.lang.OldClassTest$Cls2.field1")) {
    800                 expected = field;
    801                 break;
    802             }
    803         }
    804         if (expected == null) {
    805             fail("getFields() did not return all fields");
    806         }
    807         assertEquals(expected, Cls2.class.getField("field1"));
    808     }
    809 
    810     public void test_getInterfaces() {
    811         Class [] interfaces1 = Cls1.class.getInterfaces();
    812         assertEquals(1, interfaces1.length);
    813         assertEquals(Intf2.class, interfaces1[0]);
    814 
    815         Class [] interfaces2 = Cls2.class.getInterfaces();
    816         assertEquals(1, interfaces2.length);
    817         assertEquals(Intf1.class, interfaces2[0]);
    818 
    819         Class [] interfaces3 = Cls3.class.getInterfaces();
    820         assertEquals(2, interfaces3.length);
    821         assertEquals(Intf3.class, interfaces3[0]);
    822         assertEquals(Intf4.class, interfaces3[1]);
    823 
    824         Class [] interfaces4 = Cls4.class.getInterfaces();
    825         assertEquals(0, interfaces4.length);
    826     }
    827 
    828     public void test_getMethods() throws Exception {
    829         assertEquals("Incorrect number of methods", 10,
    830                 Cls2.class.getMethods().length);
    831         assertEquals("Incorrect number of methods", 11,
    832                 Cls3.class.getMethods().length);
    833 
    834         Method expected = null;
    835         Method[] methods = Cls2.class.getMethods();
    836         for (Method method : methods) {
    837             if(method.toString().equals("public void libcore.java.lang.OldClassTest$Cls2.test()")) {
    838                 expected = method;
    839                 break;
    840             }
    841         }
    842         if (expected == null) {
    843             fail("getMethods() did not return all methods");
    844         }
    845         assertEquals(expected, Cls2.class.getMethod("test"));
    846 
    847         expected = null;
    848         methods = Cls3.class.getMethods();
    849         for (Method method : methods) {
    850             if(method.toString().equals("public void libcore.java.lang.OldClassTest$Cls3.test()")) {
    851                 expected = method;
    852                 break;
    853             }
    854         }
    855         if (expected == null) {
    856             fail("getMethods() did not return all methods");
    857         }
    858         assertEquals(expected, Cls3.class.getMethod("test"));
    859 
    860         expected = null;
    861         methods = Cls3.class.getMethods();
    862         for (Method method : methods) {
    863             if(method.toString().equals("public void libcore.java.lang.OldClassTest$Cls3.test2(int,"
    864                     + "java.lang.Object)")) {
    865                 expected = method;
    866                 break;
    867             }
    868         }
    869         if (expected == null) {
    870             fail("getMethods() did not return all methods");
    871         }
    872 
    873         assertEquals(expected, Cls3.class.getMethod("test2", int.class,
    874                 Object.class));
    875 
    876         assertEquals("Incorrect number of methods", 1,
    877                 Intf5.class.getMethods().length);
    878     }
    879 
    880     public void test_getResourceLjava_lang_String() {
    881         assertNull(getClass().getResource(
    882                 "libcore/java/lang/NonExistentResource"));
    883         assertNull(getClass().getResource(getClass().getName() + "NonExistentResource"));
    884     }
    885 
    886     public void test_getResourceAsStreamLjava_lang_String() throws Exception {
    887         String name = "/HelloWorld.txt";
    888         assertNotNull("the file " + name + " can not be found in this " +
    889                 "directory", getClass().getResourceAsStream(name));
    890 
    891         final String nameBadURI = "org/apache/harmony/luni/tests/test_resource.txt";
    892         assertNull("the file " + nameBadURI + " should not be found in this directory",
    893                 getClass().getResourceAsStream(nameBadURI));
    894 
    895         ClassLoader pcl = getClass().getClassLoader();
    896         Class<?> clazz = pcl.loadClass("libcore.java.lang.OldClassTest");
    897         assertNotNull(clazz.getResourceAsStream("HelloWorld1.txt"));
    898 
    899         try {
    900             getClass().getResourceAsStream(null);
    901             fail("NullPointerException is not thrown.");
    902         } catch(NullPointerException npe) {
    903             //expected
    904         }
    905     }
    906 
    907     public void test_isAssignableFromLjava_lang_Class() {
    908         assertFalse("returned true not assignable classes",
    909                 Integer.class.isAssignableFrom(String.class));
    910 
    911         try {
    912             Runnable.class.isAssignableFrom(null);
    913             fail("NullPointerException is not thrown.");
    914         } catch(NullPointerException npe) {
    915             //expected
    916         }
    917     }
    918 
    919     public void test_newInstance() throws Exception {
    920         try {
    921             TestClass3.class.newInstance();
    922             fail("IllegalAccessException is not thrown.");
    923         } catch(IllegalAccessException  iae) {
    924             //expected
    925         }
    926 
    927         try {
    928             TestClass1C.class.newInstance();
    929             fail("ExceptionInInitializerError should be thrown.");
    930         } catch (java.lang.ExceptionInInitializerError ie) {
    931             //expected
    932         }
    933     }
    934 
    935     public void test_asSubclass1() {
    936         assertEquals(ExtendTestClass.class,
    937                 ExtendTestClass.class.asSubclass(PublicTestClass.class));
    938 
    939         assertEquals(PublicTestClass.class,
    940                 PublicTestClass.class.asSubclass(TestInterface.class));
    941 
    942         assertEquals(ExtendTestClass1.class,
    943                 ExtendTestClass1.class.asSubclass(PublicTestClass.class));
    944 
    945         assertEquals(PublicTestClass.class,
    946                 PublicTestClass.class.asSubclass(PublicTestClass.class));
    947     }
    948 
    949     public void test_asSubclass2() {
    950         try {
    951             PublicTestClass.class.asSubclass(ExtendTestClass.class);
    952             fail("Test 1: ClassCastException expected.");
    953         } catch(ClassCastException cce) {
    954             // Expected.
    955         }
    956 
    957         try {
    958             PublicTestClass.class.asSubclass(String.class);
    959             fail("Test 2: ClassCastException expected.");
    960         } catch(ClassCastException cce) {
    961             // Expected.
    962         }
    963     }
    964 
    965     public void test_cast() {
    966         Object o = PublicTestClass.class.cast(new ExtendTestClass());
    967         assertTrue(o instanceof ExtendTestClass);
    968 
    969         try {
    970             ExtendTestClass.class.cast(new PublicTestClass());
    971             fail("Test 1: ClassCastException expected.");
    972         } catch(ClassCastException cce) {
    973             //expected
    974         }
    975 
    976         try {
    977             ExtendTestClass.class.cast(new String());
    978             fail("ClassCastException is not thrown.");
    979         } catch(ClassCastException cce) {
    980             //expected
    981         }
    982     }
    983 
    984     public void test_desiredAssertionStatus() {
    985       Class [] classArray = { Object.class, Integer.class,
    986                               String.class, PublicTestClass.class,
    987                               ExtendTestClass.class, ExtendTestClass1.class};
    988 
    989       for(int i = 0; i < classArray.length; i++) {
    990           assertFalse("assertion status for " + classArray[i],
    991                        classArray[i].desiredAssertionStatus());
    992       }
    993    }
    994 
    995     public void testGetResourceAsStream1() throws IOException {
    996         Class clazz = getClass();
    997 
    998         InputStream stream = clazz.getResourceAsStream("HelloWorld.txt");
    999         assertNotNull(stream);
   1000 
   1001         byte[] buffer = new byte[20];
   1002         int length = stream.read(buffer);
   1003         String s = new String(buffer, 0, length);
   1004         assertEquals("Hello, World.\n",  s);
   1005 
   1006         stream.close();
   1007     }
   1008 
   1009     public void testGetResourceAsStream2() throws IOException {
   1010         Class clazz = getClass();
   1011 
   1012         InputStream stream = clazz.getResourceAsStream("/libcore/java/lang/HelloWorld.txt");
   1013         assertNotNull(stream);
   1014 
   1015         byte[] buffer = new byte[20];
   1016         int length = stream.read(buffer);
   1017         String s = new String(buffer, 0, length);
   1018         assertEquals("Hello, World.\n", s);
   1019 
   1020         stream.close();
   1021 
   1022         try {
   1023             clazz.getResourceAsStream(null);
   1024             fail("NullPointerException is not thrown.");
   1025         } catch(NullPointerException npe) {
   1026             //expected
   1027         }
   1028         assertNull(clazz.getResourceAsStream("/NonExistentResource"));
   1029         assertNull(clazz.getResourceAsStream("libcore/java/lang/HelloWorld.txt"));
   1030     }
   1031 }
   1032 
   1033 class AnonymousMemberFixture {
   1034     Object instanceOfAnonymousClass = new Object() {};
   1035 }
   1036