DexClassLoaderTest.java revision 0097708dae398465c3da40cc7f9a2dfde5571364
1/* 2 * Copyright (C) 2011 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 17package dalvik.system; 18 19import java.lang.reflect.InvocationTargetException; 20import java.lang.reflect.Method; 21import java.io.File; 22import java.io.FileOutputStream; 23import java.io.IOException; 24import java.io.InputStream; 25import libcore.base.Streams; 26import junit.framework.TestCase; 27 28/** 29 * Tests for the class {@link DexClassLoader}. 30 */ 31public class DexClassLoaderTest extends TestCase { 32 private static final String PACKAGE_PATH = "dalvik/system/"; 33 private static final String JAR_NAME = "loading-test.jar"; 34 private static final File TMP_DIR = 35 new File(System.getProperty("java.io.tmpdir"), "loading-test"); 36 private static final File TMP_JAR = new File(TMP_DIR, JAR_NAME); 37 38 protected void setUp() throws IOException { 39 TMP_DIR.mkdirs(); 40 41 ClassLoader cl = DexClassLoaderTest.class.getClassLoader(); 42 InputStream in = cl.getResourceAsStream(PACKAGE_PATH + JAR_NAME); 43 FileOutputStream out = new FileOutputStream(TMP_JAR); 44 45 Streams.copy(in, out); 46 in.close(); 47 out.close(); 48 } 49 50 /** 51 * Helper to construct an instance to test. 52 */ 53 static private DexClassLoader createInstance() { 54 return new DexClassLoader(TMP_JAR.getAbsolutePath(), 55 TMP_DIR.getAbsolutePath(), 56 null, 57 ClassLoader.getSystemClassLoader()); 58 } 59 60 /** 61 * Just a trivial test of construction. This one merely makes 62 * sure that a valid construction doesn't fail; it doesn't try 63 * to verify anything about the constructed instance. (Other 64 * tests will do that.) 65 */ 66 public void test_init() { 67 createInstance(); 68 } 69 70 /** 71 * Check that a class in the jar file may be used successfully. In this 72 * case, a trivial static method is called. 73 */ 74 public void test_simpleUse() 75 throws ClassNotFoundException, NoSuchMethodException, 76 IllegalAccessException, InvocationTargetException { 77 DexClassLoader dcl = createInstance(); 78 Class c = dcl.loadClass("test.Test1"); 79 Method m = c.getMethod("test", (Class[]) null); 80 String result = (String) m.invoke(null, (Object[]) null); 81 82 assertSame("blort", result); 83 } 84} 85