Search in sources :

Example 11 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class AccessFullyQualifiedFieldTest method testStaticImportWithAutoImport.

@Test
public void testStaticImportWithAutoImport() throws Exception {
    String output = "target/spooned-" + this.getClass().getSimpleName() + "-MultiAutoImport/";
    String pathResource = "src/test/java/spoon/test/variable/testclasses/MultiBurritos.java";
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--with-imports" });
    spoon.addInputResource(pathResource);
    spoon.setSourceOutputDirectory(output);
    spoon.run();
    PrettyPrinter prettyPrinter = spoon.createPrettyPrinter();
    CtType element = spoon.getFactory().Class().getAll().get(0);
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String result = prettyPrinter.getResult();
    assertTrue("The result should contain a static import for spoon.Launcher.SPOONED_CLASSES", result.contains("import static spoon.Launcher.SPOONED_CLASSES;"));
    assertTrue("The variable x should be assigned with only SPOONED_CLASSES", result.contains("Object x = SPOONED_CLASSES;"));
    assertTrue("The result should not contain a static import for spoon.test.variable.testclasses.ForStaticVariables.foo as it is in the same package", !result.contains("import static spoon.test.variable.testclasses.ForStaticVariables.foo;"));
    assertTrue("The result should not contain a import static for spoon.test.variable.testclasses.MultiBurritos.toto as it is in the same class", !result.contains("import static spoon.test.variable.testclasses.MultiBurritos.toto;"));
    assertTrue("The result should not contain a FQN for toto", !result.contains("spoon.test.variable.testclasses.MultiBurritos.toto();"));
    assertTrue("The result should not contain a FQN for spoon access", !result.contains("spoon.test.variable.testclasses.MultiBurritos.spoon = \"truc\";"));
    assertTrue("The result should not contain a FQN for foo", !result.contains("spoon.test.variable.testclasses.ForStaticVariables.foo();"));
    canBeBuilt(output, 7);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 12 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class ImportTest method testStaticMethodWithDifferentClassSameNameJava7NoCollision.

@Test
public void testStaticMethodWithDifferentClassSameNameJava7NoCollision() {
    // contract: when there is a collision between class names when using static method, we should create a static import for the method
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned-staticmethod";
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticmethod/");
    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(7);
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.apachetestsuite.staticmethod.AllLangTestSuiteStaticMethod");
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    assertTrue("The file should contain a static import ", output.contains("import static spoon.test.imports.testclasses2.apachetestsuite.enums.EnumTestSuite.suite;"));
    assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(suite());"));
    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 13 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class ImportTest method testSortingOfImports.

@Test
public void testSortingOfImports() {
    // contract: imports are sorted alphabetically
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned";
    launcher.addInputResource("./src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java");
    launcher.setSourceOutputDirectory(outputDir);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get(DefaultJavaPrettyPrinter.class);
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    StringTokenizer st = new StringTokenizer(output, System.getProperty("line.separator"));
    String lastImport = null;
    int countOfImports = 0;
    while (st.hasMoreTokens()) {
        String line = st.nextToken();
        if (line.startsWith("import")) {
            countOfImports++;
            if (lastImport != null) {
                // check that next import is alphabetically higher then last import
                assertTrue(lastImport + " should be before " + line, lastImport.compareTo(line) < 0);
            }
            lastImport = line;
        } else {
            if (lastImport != null) {
                // there are no more imports. Finish
                break;
            }
        // no import found yet. Continue with next line
        }
    }
    assertTrue(countOfImports > 10);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) StringTokenizer(java.util.StringTokenizer) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 14 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class ImportTest method testSortImportPutStaticImportAfterTypeImport.

@Test
public void testSortImportPutStaticImportAfterTypeImport() {
    // contract: static import should be after import
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    launcher.getEnvironment().setShouldCompile(true);
    String outputDir = "./target/spoon-sort-import";
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/StaticNoOrdered.java");
    launcher.setSourceOutputDirectory(outputDir);
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get(StaticNoOrdered.class);
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    StringTokenizer st = new StringTokenizer(output, System.getProperty("line.separator"));
    int countImports = 0;
    int nbStaticImports = 2;
    int nbStandardImports = 4;
    boolean startStatic = false;
    while (st.hasMoreTokens()) {
        String line = st.nextToken();
        if (line.startsWith("import static")) {
            if (!startStatic) {
                assertEquals("Static import should start after exactly " + nbStandardImports + " standard imports", nbStandardImports, countImports);
            } else {
                assertTrue("It will normally have only " + nbStaticImports + " static imports", countImports <= nbStandardImports + nbStaticImports);
            }
            startStatic = true;
            assertTrue("Static import should be after normal import", countImports >= nbStandardImports);
        }
        if (line.startsWith("import")) {
            countImports++;
        }
    }
    assertEquals("Exactly " + nbStandardImports + nbStaticImports + " should have been counted.", (nbStandardImports + nbStaticImports), countImports);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) StringTokenizer(java.util.StringTokenizer) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 15 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class ImportTest method testAccessPath.

@Test
public void testAccessPath() {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/TransportIndicesShardStoresAction.java");
    String outputDir = "./target/spooned-accessPath";
    launcher.setSourceOutputDirectory(outputDir);
    launcher.run();
    CtType element = launcher.getFactory().Class().getAll().get(0);
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    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