2015-10-15 17:12:38 +00:00
|
|
|
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;
|
|
|
|
|
2015-11-10 17:26:29 +00:00
|
|
|
public abstract class SourceFileBytecodeTest extends TestCase{
|
2015-10-15 20:16:18 +00:00
|
|
|
public String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
|
|
|
|
public String testFile;
|
2015-10-16 08:39:34 +00:00
|
|
|
public String outputDirectory;
|
2015-10-15 17:12:38 +00:00
|
|
|
|
|
|
|
protected String testName;
|
|
|
|
|
2015-11-10 17:26:29 +00:00
|
|
|
public SourceFileBytecodeTest(){
|
2015-10-15 17:12:38 +00:00
|
|
|
init();
|
|
|
|
|
|
|
|
if(testName != null){
|
|
|
|
|
|
|
|
testFile = testName+".jav";
|
2015-10-16 08:39:34 +00:00
|
|
|
outputDirectory = "";
|
2015-10-15 17:12:38 +00:00
|
|
|
|
2015-10-16 08:39:34 +00:00
|
|
|
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputDirectory);
|
2015-10-15 17:12:38 +00:00
|
|
|
}else{
|
|
|
|
throw new RuntimeException("rootDirectory, testFile or outputFile is null.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void init();
|
|
|
|
|
|
|
|
protected Class getClassToTest(){
|
|
|
|
Class classToTest = null;
|
|
|
|
try {
|
2015-10-27 14:14:52 +00:00
|
|
|
ClassLoader classLoader = getClassLoader();
|
2015-10-15 17:12:38 +00:00
|
|
|
|
|
|
|
classToTest = classLoader.loadClass(testName);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return classToTest;
|
|
|
|
}
|
2015-10-27 14:14:52 +00:00
|
|
|
|
|
|
|
protected ClassLoader getClassLoader() throws Exception{
|
2015-10-31 17:48:46 +00:00
|
|
|
File file = new File(rootDirectory);
|
2015-10-27 14:14:52 +00:00
|
|
|
URL url = file.toURL();
|
|
|
|
URL[] urls = new URL[]{url};
|
|
|
|
|
|
|
|
return new URLClassLoader(urls);
|
|
|
|
}
|
2015-10-15 17:12:38 +00:00
|
|
|
}
|