use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testNestedStaticPathWithTypedParameterWithImports.
@Test
public void testNestedStaticPathWithTypedParameterWithImports() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "-i", "./src/test/resources/spoon/test/imports/testclasses2/Interners.java", "--with-imports" });
launcher.buildModel();
launcher.prettyprint();
try {
launcher.getModelBuilder().compile();
} catch (Exception e) {
fail(e.getMessage());
}
CtClass<?> mm = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.Interners");
assertTrue(mm.toString().indexOf("List<Interners.WeakInterner.Dummy> list;") >= 0);
}
use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testStaticMethodWithDifferentClassSameNameJava3NoCollision.
@Test
public void testStaticMethodWithDifferentClassSameNameJava3NoCollision() {
// contract: when there is a collision between class names when using static method, we could not create a static import
// as it is not compliant with java < 1.5, so we should use fully qualified name of the class
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned-staticjava3";
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticjava3/");
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enums/");
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enum2/");
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/LangTestSuite.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.getEnvironment().setComplianceLevel(3);
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.apachetestsuite.staticjava3.AllLangTestJava3");
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
assertFalse("The file should not contain a static import ", output.contains("import static"));
assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(spoon.test.imports.testclasses2.apachetestsuite.enums.EnumTestSuite.suite());"));
canBeBuilt(outputDir, 3);
}
use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testGetImportKindReturnRightValue.
@Test
public void testGetImportKindReturnRightValue() {
// contract: the importKind is computed based on the reference class type and the boolean isImportAllStaticTypeMembers
final Launcher spoon = new Launcher();
CtType aType = spoon.getFactory().Type().get(Reflection.class);
CtImport ctImport = spoon.getFactory().createImport(aType.getReference());
assertEquals(CtImportKind.TYPE, ctImport.getImportKind());
ctImport = spoon.getFactory().createImport(spoon.getFactory().Type().createWildcardStaticTypeMemberReference(aType.getReference()));
assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
ctImport = spoon.getFactory().createImport(((CtMethod) aType.getAllMethods().iterator().next()).getReference());
assertEquals(CtImportKind.METHOD, ctImport.getImportKind());
ctImport = spoon.getFactory().createImport(((CtField) aType.getFields().get(0)).getReference());
assertEquals(CtImportKind.FIELD, ctImport.getImportKind());
ctImport = spoon.getFactory().createImport(aType.getPackage().getReference());
assertEquals(CtImportKind.ALL_TYPES, ctImport.getImportKind());
}
use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testMultipleCU.
@Test
public void testMultipleCU() throws IOException {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned-multiplecu";
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/multiplecu/");
launcher.setSourceOutputDirectory(outputDir);
launcher.run();
canBeBuilt(outputDir, 7);
String pathA = "spoon/test/imports/testclasses/multiplecu/A.java";
String pathB = "spoon/test/imports/testclasses/multiplecu/B.java";
File outputA = new File(outputDir + "/" + pathA);
String codeA = IOUtils.toString(new FileReader(outputA));
assertThat(codeA, containsString("import java.util.List;"));
File outputB = new File(outputDir + "/" + pathB);
String codeB = IOUtils.toString(new FileReader(outputB));
assertThat(codeB, containsString("import java.awt.List;"));
}
use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testWithInnerEnumDoesNotImportStaticInnerMethods.
@Test
public void testWithInnerEnumDoesNotImportStaticInnerMethods() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned-innerenum";
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/StaticImportsFromEnum.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().getAll().get(0);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
assertTrue("The file should not contain a static import to the inner enum method values", !output.contains("import static spoon.test.imports.testclasses.StaticImportsFromEnum$DataElement.values;"));
assertTrue("The file should not contain a static import to the inner enum method values of a distinct interface", !output.contains("import static spoon.test.imports.testclasses.ItfWithEnum$Bar.values;"));
assertTrue("The file should not contain a static import to the inner enum value", !output.contains("import static spoon.test.imports.testclasses.ItfWithEnum$Bar.Lip;"));
canBeBuilt(outputDir, 7);
}
Aggregations