From 0a17be3c4f61713cae02741f3a51200db16782d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 15 Oct 2015 19:12:38 +0200 Subject: [PATCH] =?UTF-8?q?Abstrakter=20Test=20f=C3=BCr=20BytecodeTests=20?= =?UTF-8?q?TypedVectorTest=20angelegt:=20funktoniert=20schon=20Overloading?= =?UTF-8?q?Test=20angelegt:=20N=C3=A4chstes=20Ziel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/bytecode/BytecodeTest.java | 57 ++++++++++++++++++++++++++++ test/bytecode/Overloading.jav | 12 ++++++ test/bytecode/OverloadingTest.java | 56 +++++++++++++++++++++++++++ test/bytecode/SingleClassTester.java | 1 + test/bytecode/TypedVector.jav | 7 ++++ test/bytecode/TypedVectorTest.java | 36 ++++++++++++++++++ 6 files changed, 169 insertions(+) create mode 100644 test/bytecode/BytecodeTest.java create mode 100644 test/bytecode/Overloading.jav create mode 100644 test/bytecode/OverloadingTest.java create mode 100644 test/bytecode/TypedVector.jav create mode 100644 test/bytecode/TypedVectorTest.java diff --git a/test/bytecode/BytecodeTest.java b/test/bytecode/BytecodeTest.java new file mode 100644 index 000000000..8fac8161a --- /dev/null +++ b/test/bytecode/BytecodeTest.java @@ -0,0 +1,57 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; + +import junit.framework.TestCase; + +import org.junit.BeforeClass; +import org.junit.Test; + +public abstract class BytecodeTest extends TestCase{ + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public static String testFile; + public static String outputFile; + + protected String testName; + + public BytecodeTest(){ + init(); + + if(testName != null){ + + testFile = testName+".jav"; + outputFile = testName+".class"; + + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + }else{ + throw new RuntimeException("rootDirectory, testFile or outputFile is null."); + } + } + + protected abstract void init(); + + protected Class getClassToTest(){ + Class classToTest = null; + try { + File file = new File(rootDirectory); + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + + ClassLoader classLoader = new URLClassLoader(urls); + + classToTest = classLoader.loadClass(testName); + + + } catch (Exception e) { + throw new RuntimeException(e); + } + + return classToTest; + } +} diff --git a/test/bytecode/Overloading.jav b/test/bytecode/Overloading.jav new file mode 100644 index 000000000..ce94fb7db --- /dev/null +++ b/test/bytecode/Overloading.jav @@ -0,0 +1,12 @@ +import java.util.Vector; + +class Overloading{ + + void method(Vector v) { + + } + + void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/OverloadingTest.java b/test/bytecode/OverloadingTest.java new file mode 100644 index 000000000..4b348971f --- /dev/null +++ b/test/bytecode/OverloadingTest.java @@ -0,0 +1,56 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.Vector; + +import org.junit.Test; + +public class OverloadingTest extends BytecodeTest{ + @Override + protected void init() { + testName = "Overloading"; + } + + @Test + public void testString() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + + @Test + public void testInteger() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index 34ff10a9f..323aaf535 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -27,6 +27,7 @@ public class SingleClassTester { //System.out.println(bytecode); bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); } catch (IOException | yyException e) { + Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN); e.printStackTrace(); TestCase.fail(); }finally{ diff --git a/test/bytecode/TypedVector.jav b/test/bytecode/TypedVector.jav new file mode 100644 index 000000000..a19b45bd5 --- /dev/null +++ b/test/bytecode/TypedVector.jav @@ -0,0 +1,7 @@ +import java.util.Stack; + +class TypedVector{ + public void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/TypedVectorTest.java b/test/bytecode/TypedVectorTest.java new file mode 100644 index 000000000..f5c2a9e43 --- /dev/null +++ b/test/bytecode/TypedVectorTest.java @@ -0,0 +1,36 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.Stack; +import java.util.Vector; + +import org.junit.Test; + +public class TypedVectorTest extends BytecodeTest{ + @Override + protected void init() { + testName = "TypedVector"; + } + + @Test + public void test() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } +}