Abstrakter Test für BytecodeTests

TypedVectorTest angelegt: funktoniert schon
OverloadingTest angelegt: Nächstes Ziel
This commit is contained in:
Enrico Schrödter 2015-10-15 19:12:38 +02:00
parent 4f1c02834d
commit 0a17be3c4f
6 changed files with 169 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,12 @@
import java.util.Vector;
class Overloading{
void method(Vector<String> v) {
}
void method(Vector<Integer> v) {
}
}

View File

@ -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<String> stringVector = new Vector<String>();
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<Integer> stringVector = new Vector<Integer>();
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();
}
}
}

View File

@ -27,6 +27,7 @@ public class SingleClassTester {
//System.out.println(bytecode); //System.out.println(bytecode);
bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile));
} catch (IOException | yyException e) { } catch (IOException | yyException e) {
Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN);
e.printStackTrace(); e.printStackTrace();
TestCase.fail(); TestCase.fail();
}finally{ }finally{

View File

@ -0,0 +1,7 @@
import java.util.Stack;
class TypedVector{
public void method(Vector<String> v) {
}
}

View File

@ -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<String> stringVector = new Vector<String>();
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();
}
}
}