JavaCompilerCore/test/bytecode/BytecodeTester.java
2014-09-04 16:35:44 +02: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 de.dhbwstuttgart.bytecode.ClassFile;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.myexception.JVMCodeException;
import junit.framework.TestCase;
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();
}
}