use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class ImportTest method testStaticMethodWithDifferentClassSameNameCollision.
@Test
public void testStaticMethodWithDifferentClassSameNameCollision() {
// contract: when using static method, if there is a collision between class name AND between method names,
// we can only use the fully qualified name of the class to call the static method
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned-apache";
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticcollision/");
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.staticcollision.AllLangTestSuite");
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 ", !output.contains("import static spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite;"));
assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite());"));
canBeBuilt(outputDir, 3);
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class ImportTest method testImportWithGenerics.
@Test
public void testImportWithGenerics() throws IOException {
// contract: in noclasspath autoimport, we should be able to use generic type
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/import-with-generics/TestWithGenerics.java");
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setShouldCompile(true);
launcher.getEnvironment().setNoClasspath(true);
launcher.setSourceOutputDirectory("./target/import-with-generics");
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses.TestWithGenerics");
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
assertTrue(output.contains("import spoon.test.imports.testclasses.withgenerics.Target;"));
}
use of spoon.reflect.visitor.PrettyPrinter 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.reflect.visitor.PrettyPrinter 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);
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class ImportTest method testShouldNotCreateAutoreference.
@Test
public void testShouldNotCreateAutoreference() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(false);
String outputDir = "./target/spooned-autoref";
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/ShouldNotAutoreference.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 for NOFOLLOW_LINKS", !output.contains("import static java.nio.file.LinkOption.NOFOLLOW_LINKS;"));
canBeBuilt(outputDir, 7);
}
Aggregations