JavaCompilerCore/test/bytecode/SourceFileBytecodeTest.java

62 lines
1.3 KiB
Java
Raw Permalink Normal View History

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(){
init();
if(testName != null){
testFile = testName+".jav";
outputDirectory = "";
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputDirectory);
}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);
}
}