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