JavaCompilerCore/test/bytecode/BytecodeTester.java
2014-03-13 01:21:02 +01:00

46 lines
1.4 KiB
Java

package bytecode;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Vector;
import junit.framework.TestCase;
import mycompiler.MyCompiler;
import mycompiler.MyCompilerAPI;
import mycompiler.mybytecode.ClassFile;
import mycompiler.myexception.JVMCodeException;
public class BytecodeTester{
//Web-Disassembler: http://hakurai.github.io/javap.js/web/
public static final String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public static Vector<ClassFile> testCodegen(String sourceCode){
MyCompilerAPI compiler = MyCompiler.getAPI();
compiler.parse(sourceCode);
Vector<ClassFile> ret = null;
try {
ret = compiler.codeGeneration(null);
} catch (NullPointerException | JVMCodeException e) {
e.printStackTrace();
TestCase.fail();
}
TestCase.assertTrue("Es wurden keine ClassFiles generiert",ret != null && ret.size()>0);
return ret;
}
public static Vector<ClassFile> readFileAndTestCodegen(String fileName) throws IOException{
return testCodegen(getFileContent(rootDirectory + fileName));
}
private static String getFileContent(String path)throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
}
}