Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * 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 android.os.cts;
     18 
     19 import java.io.*;
     20 import java.lang.reflect.*;
     21 
     22 import android.platform.test.annotations.Presubmit;
     23 import android.test.AndroidTestCase;
     24 
     25 public class CustomClassLoaderTest extends AndroidTestCase {
     26     File tf;
     27 
     28     @Override
     29     public void setUp() throws Exception {
     30         /*
     31          * dex1.bytes is a jar file with a classes.dex in it.
     32          * The classes.dex has been javac'ed and dx'ed
     33          * with the following java file:
     34          *
     35          * package android.os.cts;
     36          *   public class TestClass {
     37          *     public static final String MESSAGE = "expected_field";
     38          *   }
     39          *
     40          */
     41 
     42         super.setUp();
     43         // Extract the packaged dex/jar file to a temporary file so we
     44         // can use it with our classloader.
     45         tf = File.createTempFile("CustomClassLoaderTest_TestClass", ".dex");
     46         tf.deleteOnExit();
     47         InputStream is = mContext.getAssets().open("dex1.bytes");
     48         assertNotNull(is);
     49         OutputStream fos = new FileOutputStream(tf);
     50         byte[] buffer = new byte[8192];
     51         int len = is.read(buffer);
     52         while (len != -1) {
     53             fos.write(buffer, 0, len);
     54             len = is.read(buffer);
     55         }
     56         fos.flush();
     57         fos.close();
     58     }
     59 
     60     /* Test a custom class loader based on the DexClassLoader.
     61      */
     62     public void testCustomDexClassLoader() throws Exception {
     63         // Try to load the TestClass class by the CustomDexClassLoader.
     64         try {
     65             CustomDexClassLoader cl = new CustomDexClassLoader(
     66                     tf.getAbsolutePath(),
     67                     mContext.getCodeCacheDir().getAbsolutePath(),
     68                     ClassLoader.getSystemClassLoader().getParent());
     69             // Load the class and get the field 'MESSAGE' and
     70             // check that it is from the dex1.bytes .jar file.
     71             Field field = cl.loadClass("android.os.cts.TestClass").getField("MESSAGE");
     72             assertTrue(((String)field.get(null)).equals("expected_field"));
     73         } catch (Exception e) {
     74             throw new RuntimeException("test exception", e);
     75         }
     76     }
     77 
     78     /* Test a custom class loader based on the PathClassLoader.
     79      */
     80     @Presubmit
     81     public void testCustomPathClassLoader() throws Exception {
     82         // Try to load the TestClass class by the CustomPathClassLoader.
     83         try {
     84             CustomPathClassLoader cl = new CustomPathClassLoader(
     85                     tf.getAbsolutePath(),
     86                     ClassLoader.getSystemClassLoader().getParent());
     87             // Load the class and get the field 'MESSAGE' and
     88             // check that it is from the dex1.bytes .jar file.
     89             Field field = cl.loadClass("android.os.cts.TestClass").getField("MESSAGE");
     90             assertTrue(((String)field.get(null)).equals("expected_field"));
     91         } catch (Exception e) {
     92             throw new RuntimeException("test exception", e);
     93         }
     94     }
     95 }
     96