forked from JavaTX/JavaCompilerCore
67 lines
2.4 KiB
Java
67 lines
2.4 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.syntaxtree.SourceFile;
|
|
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 {
|
|
SourceFile parsed = compiler.parse(new File(rootDirectory + sourceFileToInfere));Menge<SourceFile> sourceFiles = new Menge<>();
|
|
sourceFiles.add(parsed);
|
|
Menge<TypeinferenceResultSet> results = compiler.typeReconstruction(sourceFiles);
|
|
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();
|
|
}
|
|
}
|