JavaTXCompilerInJavaTX/test/bytecode/SourceFileBytecodeTest.java
2016-06-17 13:55:14 +02:00

64 lines
1.4 KiB
Java

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 SourceFileBytecodeTest extends TestCase{
public String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public String testFile;
public String outputDirectory;
protected String testName;
public SourceFileBytecodeTest(){
super("Bytecode Test");
init();
if(testName != null){
testFile = testName+".jav";
outputDirectory = "";
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputDirectory);
System.out.println("Test");
}else{
throw new RuntimeException("rootDirectory, testFile or outputFile is null.");
}
}
protected abstract void init();
protected Class getClassToTest(){
Class classToTest = null;
try {
ClassLoader classLoader = getClassLoader();
classToTest = classLoader.loadClass(testName);
} catch (Exception e) {
throw new RuntimeException(e);
}
return classToTest;
}
protected ClassLoader getClassLoader() throws Exception{
File file = new File(rootDirectory);
URL url = file.toURL();
URL[] urls = new URL[]{url};
return new URLClassLoader(urls);
}
}