Mercurial > hg > icedrobot > daneel
changeset 76:f25015b8ff8b
Adapted old test case for static fields.
* ClassTransformerTest.java: Moved and renamed.
* rewriter/FieldStaticTest.java: Adapted to new test runner.
author | Michael Starzinger <michi@complang.tuwien.ac.at> |
---|---|
date | Thu, 24 Mar 2011 19:05:37 +0100 |
parents | fc5234735b4b |
children | a5ab6534a6a6 |
files | src/test/java/org/icedrobot/daneel/ClassTransformerTest.java src/test/java/org/icedrobot/daneel/rewriter/FieldStaticTest.java |
diffstat | 2 files changed, 132 insertions(+), 117 deletions(-) [+] |
line wrap: on
line diff
--- a/src/test/java/org/icedrobot/daneel/ClassTransformerTest.java Thu Mar 24 18:54:03 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +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/>. - */ - -package org.icedrobot.daneel; - -import static org.junit.Assert.assertEquals; - -import org.icedrobot.daneel.dex.DexFile; -import org.icedrobot.daneel.rewriter.DexRewriter; -import org.junit.Test; - -public class ClassTransformerTest extends ClassLoader { - - @Test - public void testTransformClassStaticFields() throws Exception { - DexFile dex = DexFile.parse(TEST_STATIC_FIELDS); - byte[] bytecode = DexRewriter.rewrite("StaticFieldTest", dex); - Class<?> cls = defineClass(null, bytecode, 0, bytecode.length); - assertEquals(7, cls.getFields().length); - assertEquals("public static byte StaticFieldTest.staticByte", - cls.getField("staticByte").toString()); - assertEquals("public static short StaticFieldTest.staticShort", - cls.getField("staticShort").toString()); - assertEquals("public static int StaticFieldTest.staticInt", - cls.getField("staticInt").toString()); - assertEquals("public static long StaticFieldTest.staticLong", - cls.getField("staticLong").toString()); - assertEquals("public static float StaticFieldTest.staticFloat", - cls.getField("staticFloat").toString()); - assertEquals("public static double StaticFieldTest.staticDouble", - cls.getField("staticDouble").toString()); - assertEquals("public static java.lang.Object StaticFieldTest.staticObject", - cls.getField("staticObject").toString()); - } - - /** - * Test data containing the DEX file derived from the following source: - * <code><pre>public class StaticFieldTest { - * public static byte staticByte; - * public static short staticShort; - * public static int staticInt; - * public static long staticLong; - * public static float staticFloat; - * public static double staticDouble; - * public static Object staticObject; - * }</pre></code> - */ - private static final byte[] TEST_STATIC_FIELDS = new byte[] { - 100, 101, 120, 10, 48, 51, 53, 0, 38, 92, -84, -49, 38, 96, - 19, 12, -90,-123, -66, 81, 46, -61,-122, -38, 56, 23, 99, -23, - 42, -14, -71, -28, -52, 2, 0, 0, 112, 0, 0, 0, 120, 86, - 52, 18, 0, 0, 0, 0, 0, 0, 0, 0, 56, 2, 0, 0, - 18, 0, 0, 0, 112, 0, 0, 0, 9, 0, 0, 0, -72, 0, - 0, 0, 1, 0, 0, 0, -36, 0, 0, 0, 7, 0, 0, 0, - -24, 0, 0, 0, 2, 0, 0, 0, 32, 1, 0, 0, 1, 0, - 0, 0, 48, 1, 0, 0, 124, 1, 0, 0, 80, 1, 0, 0, - 104, 1, 0, 0, 112, 1, 0, 0, 115, 1, 0, 0, 118, 1, - 0, 0, 121, 1, 0, 0, 124, 1, 0, 0, 127, 1, 0, 0, - -110, 1, 0, 0, -90, 1, 0, 0, -87, 1, 0, 0, -65, 1, - 0, 0, -62, 1, 0, 0, -50, 1, 0, 0, -36, 1, 0, 0, - -23, 1, 0, 0, -12, 1, 0, 0, 0, 2, 0, 0, 14, 2, - 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, - 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, - 0, 0, 8, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, - 8, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 11, 0, - 0, 0, 5, 0, 1, 0, 12, 0, 0, 0, 5, 0, 2, 0, - 13, 0, 0, 0, 5, 0, 3, 0, 14, 0, 0, 0, 5, 0, - 4, 0, 15, 0, 0, 0, 5, 0, 6, 0, 16, 0, 0, 0, - 5, 0, 7, 0, 17, 0, 0, 0, 5, 0, 0, 0, 0, 0, - 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, - 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 9, 0, - 0, 0, 0, 0, 0, 0, 32, 2, 0, 0, 0, 0, 0, 0, - 1, 0, 1, 0, 1, 0, 0, 0, 27, 2, 0, 0, 4, 0, - 0, 0, 112, 16, 1, 0, 0, 0, 14, 0, 6, 60, 105, 110, - 105, 116, 62, 0, 1, 66, 0, 1, 68, 0, 1, 70, 0, 1, - 73, 0, 1, 74, 0, 17, 76, 83, 116, 97, 116, 105, 99, 70, - 105, 101, 108, 100, 84, 101, 115, 116, 59, 0, 18, 76, 106, 97, - 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, - 59, 0, 1, 83, 0, 20, 83, 116, 97, 116, 105, 99, 70, 105, - 101, 108, 100, 84, 101, 115, 116, 46, 106, 97, 118, 97, 0, 1, - 86, 0, 10, 115, 116, 97, 116, 105, 99, 66, 121, 116, 101, 0, - 12, 115, 116, 97, 116, 105, 99, 68, 111, 117, 98, 108, 101, 0, - 11, 115, 116, 97, 116, 105, 99, 70, 108, 111, 97, 116, 0, 9, - 115, 116, 97, 116, 105, 99, 73, 110, 116, 0, 10, 115, 116, 97, - 116, 105, 99, 76, 111, 110, 103, 0, 12, 115, 116, 97, 116, 105, - 99, 79, 98, 106, 101, 99, 116, 0, 11, 115, 116, 97, 116, 105, - 99, 83, 104, 111, 114, 116, 0, 1, 0, 7, 14, 0, 7, 0, - 1, 0, 0, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, - 1, 9, 0,-127,-128, 4, -48, 2, 12, 0, 0, 0, 0, 0, - 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, - 18, 0, 0, 0, 112, 0, 0, 0, 2, 0, 0, 0, 9, 0, - 0, 0, -72, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, - -36, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, -24, 0, - 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 32, 1, 0, 0, - 6, 0, 0, 0, 1, 0, 0, 0, 48, 1, 0, 0, 1, 32, - 0, 0, 1, 0, 0, 0, 80, 1, 0, 0, 2, 32, 0, 0, - 18, 0, 0, 0, 104, 1, 0, 0, 3, 32, 0, 0, 1, 0, - 0, 0, 27, 2, 0, 0, 0, 32, 0, 0, 1, 0, 0, 0, - 32, 2, 0, 0, 0, 16, 0, 0, 1, 0, 0, 0, 56, 2, - 0, 0 - }; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/org/icedrobot/daneel/rewriter/FieldStaticTest.java Thu Mar 24 19:05:37 2011 +0100 @@ -0,0 +1,132 @@ +/* + * 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 static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +import org.icedrobot.daneel.DexifyingRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(DexifyingRunner.class) +public class FieldStaticTest { + + @Test + public void testPresence() throws Exception { + Class<?> cls = DEXCode.class; + String fmt = "public static %s " + cls.getName() + ".%s"; + assertEquals(7, cls.getFields().length); + assertEquals(String.format(fmt, "byte", "staticByte"), + cls.getField("staticByte").toString()); + assertEquals(String.format(fmt, "short", "staticShort"), + cls.getField("staticShort").toString()); + assertEquals(String.format(fmt, "int", "staticInt"), + cls.getField("staticInt").toString()); + assertEquals(String.format(fmt, "long", "staticLong"), + cls.getField("staticLong").toString()); + assertEquals(String.format(fmt, "float", "staticFloat"), + cls.getField("staticFloat").toString()); + assertEquals(String.format(fmt, "double", "staticDouble"), + cls.getField("staticDouble").toString()); + assertEquals(String.format(fmt, "java.lang.Object", "staticObject"), + cls.getField("staticObject").toString()); + } + + @Test + public void testWriteByte() { + assertEquals(0, DEXCode.staticByte); + DEXCode.staticByte = 23; + assertEquals(23, DEXCode.staticByte); + } + + @Test + public void testWriteShort() { + assertEquals(0, DEXCode.staticShort); + DEXCode.staticShort = 23; + assertEquals(23, DEXCode.staticShort); + } + + @Test + public void testWriteInt() { + assertEquals(0, DEXCode.staticInt); + DEXCode.staticInt = 23; + assertEquals(23, DEXCode.staticInt); + } + + @Test + public void testWriteLong() { + assertEquals(0, DEXCode.staticLong); + DEXCode.staticLong = 23; + assertEquals(23, DEXCode.staticLong); + } + + @Test + public void testWriteFloat() { + assertEquals(0, DEXCode.staticFloat, 0); + DEXCode.staticFloat = 2.3f; + assertEquals(2.3f, DEXCode.staticFloat, 0); + } + + @Test + public void testWriteDouble() { + assertEquals(0, DEXCode.staticDouble, 0); + DEXCode.staticDouble = 2.3; + assertEquals(2.3, DEXCode.staticDouble, 0); + } + + @Test + public void testWriteObject() { + assertNull(DEXCode.staticObject); + Object obj = new String("some test object"); + DEXCode.staticObject = obj; + assertSame(obj, DEXCode.staticObject); + } + + // Keep this class named "DEXCode" to push it through Daneel. + private static class DEXCode { + public static byte staticByte; + public static short staticShort; + public static int staticInt; + public static long staticLong; + public static float staticFloat; + public static double staticDouble; + public static Object staticObject; + }; +}