Search in sources :

Example 16 with FileSystemFile

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));
}
Also used : FileSystemFile(spoon.support.compiler.FileSystemFile) FileSystemFolder(spoon.support.compiler.FileSystemFolder) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 17 with FileSystemFile

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) ClassAccessTemplate(spoon.test.template.testclasses.ClassAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 18 with FileSystemFile

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);
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) FileSystemFile(spoon.support.compiler.FileSystemFile) File(java.io.File) EnumAccessTemplate(spoon.test.template.testclasses.EnumAccessTemplate) Test(org.junit.Test)

Example 19 with FileSystemFile

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);
}
Also used : ReturnReplaceTemplate(spoon.test.template.testclasses.ReturnReplaceTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) ModelUtils.getOptimizedString(spoon.testing.utils.ModelUtils.getOptimizedString) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 20 with FileSystemFile

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);
}
Also used : CtBlock(spoon.reflect.code.CtBlock) ReturnReplaceTemplate(spoon.test.template.testclasses.ReturnReplaceTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) ModelUtils.getOptimizedString(spoon.testing.utils.ModelUtils.getOptimizedString) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 FileSystemFile (spoon.support.compiler.FileSystemFile)28 Launcher (spoon.Launcher)26 Factory (spoon.reflect.factory.Factory)25 CtClass (spoon.reflect.declaration.CtClass)8 CtMethod (spoon.reflect.declaration.CtMethod)8 CtStatement (spoon.reflect.code.CtStatement)6 File (java.io.File)5 CtBlock (spoon.reflect.code.CtBlock)4 CtIf (spoon.reflect.code.CtIf)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtTry (spoon.reflect.code.CtTry)2 CtElement (spoon.reflect.declaration.CtElement)2 TemplateMatcher (spoon.template.TemplateMatcher)2 AnotherFieldAccessTemplate (spoon.test.template.testclasses.AnotherFieldAccessTemplate)2 FieldAccessOfInnerClassTemplate (spoon.test.template.testclasses.FieldAccessOfInnerClassTemplate)2 InvocationSubstitutionByExpressionTemplate (spoon.test.template.testclasses.InvocationSubstitutionByExpressionTemplate)2 ReturnReplaceTemplate (spoon.test.template.testclasses.ReturnReplaceTemplate)2 ModelUtils.getOptimizedString (spoon.testing.utils.ModelUtils.getOptimizedString)2 HashMap (java.util.HashMap)1