Abstrakter Test für BytecodeTests
TypedVectorTest angelegt: funktoniert schon OverloadingTest angelegt: Nächstes Ziel
This commit is contained in:
parent
4f1c02834d
commit
0a17be3c4f
57
test/bytecode/BytecodeTest.java
Normal file
57
test/bytecode/BytecodeTest.java
Normal 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;
|
||||
}
|
||||
}
|
12
test/bytecode/Overloading.jav
Normal file
12
test/bytecode/Overloading.jav
Normal file
@ -0,0 +1,12 @@
|
||||
import java.util.Vector;
|
||||
|
||||
class Overloading{
|
||||
|
||||
void method(Vector<String> v) {
|
||||
|
||||
}
|
||||
|
||||
void method(Vector<Integer> v) {
|
||||
|
||||
}
|
||||
}
|
56
test/bytecode/OverloadingTest.java
Normal file
56
test/bytecode/OverloadingTest.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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{
|
||||
|
7
test/bytecode/TypedVector.jav
Normal file
7
test/bytecode/TypedVector.jav
Normal file
@ -0,0 +1,7 @@
|
||||
import java.util.Stack;
|
||||
|
||||
class TypedVector{
|
||||
public void method(Vector<String> v) {
|
||||
|
||||
}
|
||||
}
|
36
test/bytecode/TypedVectorTest.java
Normal file
36
test/bytecode/TypedVectorTest.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user