Search in sources :

Example 1 with PrettyPrinter

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);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 2 with PrettyPrinter

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;"));
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 3 with PrettyPrinter

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);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with PrettyPrinter

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);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 5 with PrettyPrinter

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);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 Launcher (spoon.Launcher)18 Test (org.junit.Test)17 CtType (spoon.reflect.declaration.CtType)17 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)17 ArrayList (java.util.ArrayList)16 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 StringTokenizer (java.util.StringTokenizer)2 CtClass (spoon.reflect.declaration.CtClass)2 CtPackage (spoon.reflect.declaration.CtPackage)2 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)1 Environment (spoon.compiler.Environment)1 MainTest (spoon.test.main.MainTest)1