use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.
the class ResourceTest method testFileSystemFolder.
@Test
public void testFileSystemFolder() throws Exception {
String dir = "src/test/resources/spoon/test/api/";
FileSystemFolder fileSystemFolder = new FileSystemFolder(new File(dir));
// there is one file in api
assertEquals(2, fileSystemFolder.getAllFiles().size());
assertEquals(2, fileSystemFolder.getAllJavaFiles().size());
String entry = "src/test/resources/spoon/test/api/Foo.java";
FileSystemFile file = new FileSystemFile(new File(entry));
String entry1 = "src/test/resources/spoon/test/api/CommentedClass.java";
FileSystemFile file1 = new FileSystemFile(new File(entry1));
assertThat(fileSystemFolder.getAllFiles().contains(file), is(true));
assertThat(fileSystemFolder.getAllFiles().contains(file1), is(true));
}
use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.
the class TemplateClassAccessTest method testClassAccessTest.
@Test
public void testClassAccessTest() throws Exception {
// contract: the template engine supports class access substitution
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ClassAccessTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> resultKlass = factory.Class().create("Result");
CtStatement result = new ClassAccessTemplate(String.class).apply(resultKlass);
assertEquals("java.lang.String.class.getName()", result.toString());
// I do not know if it makes sense to use null. But this kind of null handling is probably the best
CtStatement result2 = new ClassAccessTemplate(null).apply(resultKlass);
assertEquals("null.getName()", result2.toString());
}
use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.
the class TemplateEnumAccessTest method testEnumAccessTest.
@Test
public void testEnumAccessTest() throws Exception {
// contract: the template engine supports enum value access substitution
Launcher launcher = new Launcher();
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/EnumAccessTemplate.java"));
launcher.buildModel();
Factory factory = launcher.getFactory();
CtClass<?> resultKlass = factory.Class().create(factory.Package().getOrCreate("spoon.test.template"), "EnumAccessResult");
new EnumAccessTemplate(ElementType.FIELD, launcher.getFactory()).apply(resultKlass);
assertEquals("java.lang.annotation.ElementType.FIELD.name()", resultKlass.getMethod("method").getBody().getStatement(0).toString());
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/EnumAccessResult.java"), 8);
}
use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.
the class TemplateReplaceReturnTest method testNoReturnReplaceTemplate.
@Test
public void testNoReturnReplaceTemplate() throws Exception {
// contract: the template engine supports replace of return expression by `<CtExpression>`
Launcher launcher = new Launcher();
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ReturnReplaceTemplate.java"));
launcher.buildModel();
Factory factory = launcher.getFactory();
CtExpression<String> model = factory.createLiteral("AStringLiteral");
CtClass<?> resultKlass = factory.Class().create(factory.Package().getOrCreate("spoon.test.template"), "ReturnReplaceResult");
new ReturnReplaceTemplate(model).apply(resultKlass);
assertEquals("{ return \"AStringLiteral\";}", getOptimizedString(resultKlass.getMethod("method").getBody()));
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/ReturnReplaceResult.java"), 8);
}
use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.
the class TemplateReplaceReturnTest method testReturnReplaceTemplate.
@Test
public void testReturnReplaceTemplate() throws Exception {
// contract: the template engine supports replace of `return _param_.S()` by `<CtBlock>`
Launcher launcher = new Launcher();
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ReturnReplaceTemplate.java"));
launcher.buildModel();
Factory factory = launcher.getFactory();
CtBlock<String> model = (CtBlock) factory.Class().get(ReturnReplaceTemplate.class).getMethod("sample").getBody();
CtClass<?> resultKlass = factory.Class().create(factory.Package().getOrCreate("spoon.test.template"), "ReturnReplaceResult");
new ReturnReplaceTemplate(model).apply(resultKlass);
assertEquals("{ if (((java.lang.System.currentTimeMillis()) % 2L) == 0) { return \"Panna\"; }else { return \"Orel\"; }}", getOptimizedString(resultKlass.getMethod("method").getBody()));
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/ReturnReplaceResult.java"), 8);
}
Aggregations