diff --git a/test/bytecode/BytecodeTest.java b/test/bytecode/BytecodeTest.java new file mode 100644 index 00000000..8fac8161 --- /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 00000000..ce94fb7d --- /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 00000000..4b348971 --- /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 34ff10a9..323aaf53 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 00000000..a19b45bd --- /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 00000000..f5c2a9e4 --- /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(); + } + } +}