use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class EqualTest method testEqualsEmptyException.
@Test
public void testEqualsEmptyException() throws Exception {
Factory factory = new Launcher().createFactory();
String realParam1 = "\"\"";
String content = "" + "class X {" + "public Object foo() {" + " Integer.getInteger(" + realParam1 + ");" + " return \"\";" + "}};";
SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
try {
builder.build();
} catch (Exception e) {
e.printStackTrace();
fail("Unable create model");
}
CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
CtMethod<?> method = (CtMethod<?>) clazz1.getMethods().toArray()[0];
CtInvocation<?> invo = (CtInvocation<?>) method.getBody().getStatement(0);
CtLiteral<?> argument1 = (CtLiteral<?>) invo.getArguments().get(0);
assertEquals(realParam1, argument1.toString());
CtReturn<?> returnStatement = (CtReturn<?>) method.getBody().getStatement(1);
CtLiteral<?> returnExp = (CtLiteral<?>) returnStatement.getReturnedExpression();
assertEquals(realParam1, returnExp.toString());
try {
assertEquals(argument1, returnExp);
} catch (Exception e) {
fail(e.getMessage());
}
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class CompilationTest method testCompilationInEmptyDir.
@Test
public void testCompilationInEmptyDir() throws Exception {
// Contract: Spoon can be launched in an empty folder as a working directory
// See: https://github.com/INRIA/spoon/pull/1208 and https://github.com/INRIA/spoon/issues/1246
// This test does not fail (it's not enough to change user.dir we should launch process inside that dir) but it explains the problem
String userDir = System.getProperty("user.dir");
File testFile = new File("src/test/resources/compilation/compilation-tests/IBar.java");
String absoluteTestPath = testFile.getAbsolutePath();
Path tempDirPath = Files.createTempDirectory("test_compilation");
System.setProperty("user.dir", tempDirPath.toFile().getAbsolutePath());
SpoonModelBuilder compiler = new Launcher().createCompiler();
compiler.addInputSource(new File(absoluteTestPath));
compiler.setBinaryOutputDirectory(tempDirPath.toFile());
compiler.compile(SpoonModelBuilder.InputType.FILES);
System.setProperty("user.dir", userDir);
assertThat(tempDirPath.toFile().listFiles().length, not(0));
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ModelUtils method buildNoClasspath.
/**
* Utility method for testing: creates the noclasspath model of the given `classesToBuild` from src/test/java and returns the factory
*/
public static Factory buildNoClasspath(Class<?>... classesToBuild) throws Exception {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
for (Class<?> classToBuild : classesToBuild) {
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + classToBuild.getName().replace('.', '/') + ".java"));
}
comp.build();
return comp.getFactory();
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ModelUtils method build.
/**
* Builds the Spoon mode of the `filesToBuild` given as parameter
*/
public static Factory build(File... filesToBuild) {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
for (File fileToBuild : filesToBuild) {
try {
comp.addInputSource(SpoonResourceHelper.createResource(fileToBuild));
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found", e);
}
}
comp.build();
return comp.getFactory();
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ModelUtils method build.
/**
* Utility method for testing: creates the model of `packageName` from src/test/java and returns the CtType corresponding to `className`
*/
public static <T extends CtType<?>> T build(String packageName, String className) throws Exception {
SpoonModelBuilder comp = new Launcher().createCompiler();
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + packageName.replace('.', '/') + "/" + className + ".java"));
comp.build();
return comp.getFactory().Package().get(packageName).getType(className);
}
Aggregations