changeset 83:2096dd888607

(no commit message)Convert the tests of the old testframe to the new testframe with DexifyingRunner
author volker
date Sat, 26 Mar 2011 16:48:03 +0100
parents 474e8b694138
children bd1ebca7cdec
files src/test/java/org/icedrobot/daneel/junit/TestUtils.java src/test/java/org/icedrobot/daneel/rewriter/DexRewriter_MethodRewriterTest.java src/test/java/org/icedrobot/daneel/rewriter/ReturnTest.java src/test/java/resources/classes.dex
diffstat 4 files changed, 142 insertions(+), 282 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/java/org/icedrobot/daneel/junit/TestUtils.java	Sat Mar 26 01:09:49 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,171 +0,0 @@
-/*
- * 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 static org.junit.Assert.assertTrue;
-
-import org.junit.Test;
-
-import java.io.File;
-import java.lang.reflect.Method;
-
-import org.icedrobot.daneel.loader.DaneelClassLoader;
-
-public class TestUtils {
-
-    private static DaneelClassLoader daneelClassLoader;
-
-    @Test
-    public void TestUtils_getClassLoader() {
-        DaneelClassLoader loader = getClassLoader();
-        assertTrue(loader!=null);
-    }
-
-    /**
-     * Create a ClassLoader from the IcedRobot test Android Package file
-     * (*.apk). The source are currently at http://icedtea.classpath.org/people/volker/IcedRobotAndroidTests/
-     * 
-     * @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()) {
-                file[0] = new File("../IcedRobotAndroidTests/bin/classes.dex");
-            }
-            if (!file[0].exists()) {
-                //temporary until the maven build is finish
-                file[0] = new File("src/test/java/resources/classes.dex");
-            }
-            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);
-    }
-}
--- a/src/test/java/org/icedrobot/daneel/rewriter/DexRewriter_MethodRewriterTest.java	Sat Mar 26 01:09:49 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,111 +0,0 @@
-/*
- * 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 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 returnLong() throws Exception {
-        assertEquals(-1L, TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnInt() throws Exception {
-        assertEquals(25, TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnShort() throws Exception {
-        assertEquals((short)-1, TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnByte() throws Exception {
-        assertEquals((byte)-25, TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnChar() throws Exception {
-        assertEquals('a', TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnDouble() throws Exception {
-        assertEquals(Double.valueOf(44), TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnFloat() throws Exception {
-        assertEquals(Float.valueOf(37), TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnThis() throws Exception {
-        assertEquals(instance, TestUtils.invokeDexTestMethod(instance));
-    }
-
-    //@Test
-    public void returnNull() throws Exception {
-        assertEquals(null, TestUtils.invokeDexTestMethod(instance));
-    }
-
-    @Test
-    public void returnString() throws Exception {
-        assertEquals("any string", TestUtils.invokeDexTestMethod(instance));
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/org/icedrobot/daneel/rewriter/ReturnTest.java	Sat Mar 26 16:48:03 2011 +0100
@@ -0,0 +1,142 @@
+/*
+ * 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 org.icedrobot.daneel.DexifyingRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(DexifyingRunner.class)
+public class ReturnTest {
+
+    @Test
+    public void returnLong() throws Exception {
+        assertEquals(-1L, new DEXCode().returnLong());
+    }
+
+    @Test
+    public void returnInt() throws Exception {
+        assertEquals(25, new DEXCode().returnInt());
+    }
+
+    @Test
+    public void returnShort() throws Exception {
+        assertEquals((short) -1, new DEXCode().returnShort());
+    }
+
+    @Test
+    public void returnByte() throws Exception {
+        assertEquals((byte) -25, new DEXCode().returnByte());
+    }
+
+    @Test
+    public void returnChar() throws Exception {
+        assertEquals('a', new DEXCode().returnChar());
+    }
+
+    @Test
+    public void returnDouble() throws Exception {
+        assertEquals(44.0, new DEXCode().returnDouble(), 0);
+    }
+
+    @Test
+    public void returnFloat() throws Exception {
+        assertEquals(37.0F, new DEXCode().returnFloat(), 0);
+    }
+
+    @Test
+    public void returnThis() throws Exception {
+        DEXCode dex = new DEXCode();
+        assertEquals(dex, dex.returnThis());
+    }
+
+    @Test
+    public void returnNull() throws Exception {
+        assertEquals(null, new DEXCode().returnNull());
+    }
+
+    @Test
+    public void returnString() throws Exception {
+        assertEquals("any string", new DEXCode().returnString());
+    }
+
+    // Keep this class named "DEXCode" to push it through Daneel.
+    private static class DEXCode {
+        public long returnLong() {
+            return -1;
+        }
+
+        public int returnInt() {
+            return 25;
+        }
+
+        public short returnShort() {
+            return -1;
+        }
+
+        public byte returnByte() {
+            return -25;
+        }
+
+        public char returnChar() {
+            return 'a';
+        }
+
+        public double returnDouble() {
+            return 44;
+        }
+
+        public float returnFloat() {
+            return 37;
+        }
+
+        public Object returnThis() {
+            return this;
+        }
+
+        public Object returnNull() {
+            return null;
+        }
+
+        public String returnString() {
+            return "any string";
+        }
+    }
+}
Binary file src/test/java/resources/classes.dex has changed