Search in sources :

Example 6 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder 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 7 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project spoon by INRIA.

the class ResourceTest method testVirtualFolder.

@Test
public void testVirtualFolder() throws Exception {
    String dir = "src/test/resources/spoon/test/api/";
    FileSystemFolder fileSystemFolder = new FileSystemFolder(new File(dir));
    String dir2 = "src/test/resources/spoon/test/exceptions/";
    FileSystemFolder fileSystemFolder2 = new FileSystemFolder(new File(dir2));
    SpoonFolder folder = new VirtualFolder();
    folder.addFolder(fileSystemFolder);
    folder.addFolder(fileSystemFolder2);
    assertEquals(4, folder.getAllFiles().size());
    // the README is not a Java file
    assertEquals(3, folder.getAllJavaFiles().size());
}
Also used : VirtualFolder(spoon.support.compiler.VirtualFolder) SpoonFolder(spoon.compiler.SpoonFolder) FileSystemFolder(spoon.support.compiler.FileSystemFolder) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 8 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project spoon by INRIA.

the class FileSystemFolderTest method jarFileIsNotSubfolder.

@Test
public void jarFileIsNotSubfolder() {
    String folderPath = "./src/test/resources/folderWithJar";
    FileSystemFolder folder = new FileSystemFolder(new File(folderPath));
    List<SpoonFolder> subFolders = folder.getSubFolders();
    assertTrue(subFolders.isEmpty());
}
Also used : SpoonFolder(spoon.compiler.SpoonFolder) FileSystemFolder(spoon.support.compiler.FileSystemFolder) File(java.io.File) Test(org.junit.Test) LauncherTest(spoon.LauncherTest)

Example 9 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project spoon by INRIA.

the class TemplateTest method createTypeFromTemplate.

@Test
public void createTypeFromTemplate() throws Exception {
    // contract: the Substitution API provides a method createTypeFromTemplate
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addTemplateResource(new FileSystemFolder("./src/test/java/spoon/test/template/testclasses/types"));
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    Map<String, Object> parameters = new HashMap<>();
    // replace someMethod with genMethod
    parameters.put("someMethod", "genMethod");
    // contract: we can generate interface
    final CtType<?> aIfaceModel = launcher.getFactory().Interface().get(AnIfaceModel.class);
    CtType<?> genIface = Substitution.createTypeFromTemplate("generated.GenIface", aIfaceModel, parameters);
    assertNotNull(genIface);
    assertSame(genIface, factory.Type().get("generated.GenIface"));
    CtMethod<?> generatedIfaceMethod = genIface.getMethod("genMethod");
    assertNotNull(generatedIfaceMethod);
    assertNull(genIface.getMethod("someMethod"));
    // add new substitution request - replace AnIfaceModel by GenIface
    parameters.put("AnIfaceModel", genIface.getReference());
    // contract: we can generate class
    final CtType<?> aClassModel = launcher.getFactory().Class().get(AClassModel.class);
    CtType<?> genClass = Substitution.createTypeFromTemplate("generated.GenClass", aClassModel, parameters);
    assertNotNull(genClass);
    assertSame(genClass, factory.Type().get("generated.GenClass"));
    CtMethod<?> generatedClassMethod = genClass.getMethod("genMethod");
    assertNotNull(generatedClassMethod);
    assertNull(genClass.getMethod("someMethod"));
    assertTrue(generatedIfaceMethod != generatedClassMethod);
    assertTrue(generatedClassMethod.isOverriding(generatedIfaceMethod));
    // contract: we can generate enum
    parameters.put("case1", "GOOD");
    parameters.put("case2", "BETTER");
    final CtType<?> aEnumModel = launcher.getFactory().Type().get(AnEnumModel.class);
    CtEnum<?> genEnum = (CtEnum<?>) Substitution.createTypeFromTemplate("generated.GenEnum", aEnumModel, parameters);
    assertNotNull(genEnum);
    assertSame(genEnum, factory.Type().get("generated.GenEnum"));
    assertEquals(2, genEnum.getEnumValues().size());
    assertEquals("GOOD", genEnum.getEnumValues().get(0).getSimpleName());
    assertEquals("BETTER", genEnum.getEnumValues().get(1).getSimpleName());
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtEnum(spoon.reflect.declaration.CtEnum) FileSystemFolder(spoon.support.compiler.FileSystemFolder) Test(org.junit.Test)

Aggregations

FileSystemFolder (spoon.support.compiler.FileSystemFolder)9 File (java.io.File)6 Test (org.junit.Test)6 Launcher (spoon.Launcher)4 SpoonFolder (spoon.compiler.SpoonFolder)3 FileSystemFile (spoon.support.compiler.FileSystemFile)2 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LauncherTest (spoon.LauncherTest)1 SpoonModelBuilder (spoon.SpoonModelBuilder)1 InvalidClassPathException (spoon.compiler.InvalidClassPathException)1 SpoonFile (spoon.compiler.SpoonFile)1 CtEnum (spoon.reflect.declaration.CtEnum)1 Factory (spoon.reflect.factory.Factory)1 SpoonClassNotFoundException (spoon.support.SpoonClassNotFoundException)1 FilteringFolder (spoon.support.compiler.FilteringFolder)1 VirtualFolder (spoon.support.compiler.VirtualFolder)1