use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class ImportBuilderTest method testWithNoImport.
@Test
public void testWithNoImport() {
// contract: when the source code has no import, none is created when building model
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/A.java");
spoon.getEnvironment().setAutoImports(true);
spoon.buildModel();
CtClass classA = spoon.getFactory().Class().get(A.class);
CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
assertTrue(unitA.getImports().isEmpty());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class ImportBuilderTest method testWithStaticStarredImportFromInterface.
@Test
public void testWithStaticStarredImportFromInterface() {
// contract: when a starred import is used with a target package, all classes of the package should be imported
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StarredImport.java");
spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/fullpack/");
spoon.getEnvironment().setAutoImports(true);
spoon.buildModel();
CtClass classA = spoon.getFactory().Class().get(StarredImport.class);
CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
Collection<CtImport> imports = unitA.getImports();
assertEquals(1, imports.size());
Iterator<CtImport> iterator = imports.iterator();
CtImport ctImport = iterator.next();
assertEquals(CtImportKind.ALL_TYPES, ctImport.getImportKind());
assertTrue(ctImport.getReference() instanceof CtPackageReference);
CtPackageReference ref = (CtPackageReference) ctImport.getReference();
assertEquals("spoon.test.jdtimportbuilder.testclasses.fullpack", ref.getQualifiedName());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class ImportBuilderTest method testWithImportFromItf.
@Test
public void testWithImportFromItf() {
// contract: When using starred static import of an interface, it imports a starred type
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/resources/jdtimportbuilder/");
spoon.getEnvironment().setAutoImports(true);
spoon.getEnvironment().setShouldCompile(true);
spoon.setSourceOutputDirectory("./target/spoon-jdtimport-itfimport");
spoon.run();
CtClass classStatic = spoon.getFactory().Class().get("jdtimportbuilder.ItfImport");
CompilationUnit unitStatic = spoon.getFactory().CompilationUnit().getMap().get(classStatic.getPosition().getFile().getPath());
Collection<CtImport> imports = unitStatic.getImports();
assertEquals(1, imports.size());
CtImport ctImport = imports.iterator().next();
assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
assertEquals("import static jdtimportbuilder.itf.DumbItf.*;\n", ctImport.toString());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class ImportBuilderTest method testWithSimpleImportNoAutoimport.
@Test
public void testWithSimpleImportNoAutoimport() {
// contract: when the source has one import, nothing is imported when not in autoimport
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/ClassWithInvocation.java");
spoon.getEnvironment().setAutoImports(false);
spoon.buildModel();
CtClass classA = spoon.getFactory().Class().get(ClassWithInvocation.class);
CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
assertTrue(unitA.getImports().isEmpty());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class ImportBuilderTest method testWithStaticInheritedImport.
@Test
public void testWithStaticInheritedImport() {
// contract: When using starred static import of a type, it imports a starred type
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StaticImportWithInheritance.java");
spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/staticimport");
spoon.getEnvironment().setAutoImports(true);
spoon.getEnvironment().setShouldCompile(true);
spoon.setSourceOutputDirectory("./target/spoon-jdtimport-inheritedstatic");
spoon.run();
CtClass classStatic = spoon.getFactory().Class().get(StaticImportWithInheritance.class);
CompilationUnit unitStatic = spoon.getFactory().CompilationUnit().getMap().get(classStatic.getPosition().getFile().getPath());
Collection<CtImport> imports = unitStatic.getImports();
assertEquals(1, imports.size());
CtImport ctImport = imports.iterator().next();
assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
assertEquals("import static spoon.test.jdtimportbuilder.testclasses.staticimport.DependencySubClass.*;\n", ctImport.toString());
}
Aggregations