JavaTXCompilerInJavaTX/test/plugindevelopment/InsertSingleTypeTest.java
JanUlrich 6fc412d949 Revert "Merge branch 'bytecode'"
This reverts commit 711c0d2f79, reversing
changes made to 2b47b8e5bf.
2015-05-12 19:54:34 +02:00

65 lines
2.2 KiB
Java

package plugindevelopment;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import de.dhbwstuttgart.typeinference.Menge;
import org.junit.Test;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
import junit.framework.TestCase;
public class InsertSingleTypeTest {
private static final String testFile = "SingleTypeInsertTest.jav";
@Test
public void test1(){
TypeinferenceResultSet res = test(testFile);
TypeInsertPoint p = res.getTypeInsertionPoints().points.firstElement();
try {
System.out.println(res.getTypeInsertionPoints().insertType(p, this.getFileContent(rootDirectory + testFile)));
} catch (IOException e) {
TestCase.fail();
e.printStackTrace();
}
}
static final String rootDirectory = System.getProperty("user.dir")+"/test/plugindevelopment/";
public static TypeinferenceResultSet test(String sourceFileToInfere){
String inferedSource = "";
MyCompilerAPI compiler = MyCompiler.getAPI(new LoggerConfiguration().setOutput(Section.TYPEINFERENCE, System.out));
try {
compiler.parse(new File(rootDirectory + sourceFileToInfere));
Menge<TypeinferenceResultSet> results = compiler.typeReconstruction();
TestCase.assertTrue("Es darf nicht mehr als eine L�sungsm�glichkeit geben und nicht "+results.size(), results.size()==1);
return results.firstElement();
} catch (IOException | yyException e) {
e.printStackTrace();
TestCase.fail();
return null;
}
}
//Source: https://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
//PS: ben�tigt Java 7
public static String getFileContent(String path)throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
}
}