Mercurial > hg > icedrobot > daneel
changeset 67:d1f8574aede4
First step of using the Android test project
author | volker |
---|---|
date | Sat, 19 Mar 2011 21:10:34 +0100 |
parents | 7ce7ce3d84fc |
children | ba55a434c9c0 |
files | src/test/java/org/icedrobot/daneel/junit/TestUtils.java src/test/java/org/icedrobot/daneel/rewriter/DexRewriter_MethodRewriterTest.java |
diffstat | 2 files changed, 231 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/org/icedrobot/daneel/junit/TestUtils.java Sat Mar 19 21:10:34 2011 +0100 @@ -0,0 +1,157 @@ +/* + * Daneel - Dalvik to Java bytecode compiler + * Copyright (C) 2011 IcedRobot team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * This file is subject to the "Classpath" exception: + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give you + * permission to link this library with independent modules to produce an + * executable, regardless of the license terms of these independent + * modules, and to copy and distribute the resulting executable under terms + * of your choice, provided that you also meet, for each linked independent + * module, the terms and conditions of the license of that module. An + * independent module is a module which is not derived from or based on + * this library. If you modify this library, you may extend this exception + * to your version of the library, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. + */ + +package org.icedrobot.daneel.junit; + +import java.io.File; +import java.lang.reflect.Method; + +import org.icedrobot.daneel.loader.DaneelClassLoader; + +public class TestUtils { + + private static DaneelClassLoader daneelClassLoader; + + /** + * Create a ClassLoader from the IcedRobot test Android Package file + * (*.apk). + * + * @return a singleton instance + */ + public static DaneelClassLoader getClassLoader() { + if (daneelClassLoader == null) { + File[] file = new File[1]; + // Position of Eclipse build directory + file[0] = new File( + "../IcedRobotAndroidTests/bin/IcedRobotAndroidTests.apk"); + if (!file[0].exists()) { + // search on another location + } + daneelClassLoader = new DaneelClassLoader( + ClassLoader.getSystemClassLoader(), file); + } + return daneelClassLoader; + } + + /** + * Load a class from the IcedRobot test Android Package file (*.apk). The + * class name is build from the test clas name with suffix 'Dex'. After the + * loading it create an instance of the class. + * + * @param junitTestClass + * The class of the test case. + * @return a instance of the loaded dex class + * @throws Exception + * if there are any wrong on loading + */ + public static Object getDexTestInstance(Class junitTestClass) + throws Exception { + String className = junitTestClass.getName(); + Class clazz = getClassLoader().loadClass(className + "Dex"); + return clazz.newInstance(); + } + + /** + * Request a method from the given Object. The method name is identical to + * the caller method name. (stack deep is 2) + * + * @param instance + * the object from the dex class + * @param paramTypes + * the parameters of the method if there any + * @return the method of the dex class + * @throws Exception + * if there are any wrong on loading + */ + private static Method getDexMethodImpl(Object instance, Class... paramTypes) + throws Exception { + StackTraceElement ste = new Throwable().getStackTrace()[2]; + Class clazz; + if (instance == null) { + clazz = getClassLoader().loadClass(ste.getClassName() + "Dex"); + } else { + clazz = instance.getClass(); + } + return clazz.getDeclaredMethod(ste.getMethodName(), paramTypes); + } + + /** + * Request a method from the given Object. The method name is identical to + * the caller method name. If the object is null the class name is build + * from the caller class. + * + * @param instance + * the object from the dex class, can be null + * @param paramTypes + * the parameters of the method if there any + * @return the method of the dex class + * @throws Exception + * if there are any wrong on loading + */ + public static Method getDexTestMethod(Object instance, Class... paramTypes) + throws Exception { + return getDexMethodImpl(instance, paramTypes); + } + + /** + * Invoke a method from the given Object. The method name is identical to + * the caller method name. If the instance is null (static methods) the + * class name is build from the caller class. + * + * @param instance + * the object from the dex class, can be null for static methods + * @param params + * the parameters of the method if there any + * @return the return value of the method + * @throws Exception + * if there are any wrong on loading + */ + public static Object invokeDexTestMethod(Object instance, Object... params) + throws Exception { + Class[] paramTypes; + if (params != null && params.length > 0) { + paramTypes = new Class[params.length]; + for (int i = 0; i < params.length; i++) { + paramTypes[i] = params[i].getClass(); + } + } else { + paramTypes = null; + } + Method method = getDexMethodImpl(instance, paramTypes); + return method.invoke(instance, params); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/org/icedrobot/daneel/rewriter/DexRewriter_MethodRewriterTest.java Sat Mar 19 21:10:34 2011 +0100 @@ -0,0 +1,74 @@ +/* + * Daneel - Dalvik to Java bytecode compiler + * Copyright (C) 2011 IcedRobot team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * This file is subject to the "Classpath" exception: + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give you + * permission to link this library with independent modules to produce an + * executable, regardless of the license terms of these independent + * modules, and to copy and distribute the resulting executable under terms + * of your choice, provided that you also meet, for each linked independent + * module, the terms and conditions of the license of that module. An + * independent module is a module which is not derived from or based on + * this library. If you modify this library, you may extend this exception + * to your version of the library, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. + */ + +package org.icedrobot.daneel.rewriter; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.icedrobot.daneel.junit.TestUtils; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DexRewriter_MethodRewriterTest { + + private static Object instance; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + instance = TestUtils + .getDexTestInstance(DexRewriter_MethodRewriterTest.class); + } + + @AfterClass + public static void tearDownAfterClass() { + instance = null; + } + + @Test + public void returnInt() throws Exception { + assertEquals(25, TestUtils.invokeDexTestMethod(instance)); + } + + @Test + public void returnFloat() throws Exception { + assertEquals(Float.valueOf(37), TestUtils.invokeDexTestMethod(instance)); + } +}