diff --git a/src/test/java/spoon/test/imports/ImportTest.java b/src/test/java/spoon/test/imports/ImportTest.java index d731cff3bbd..40b35f1c86f 100644 --- a/src/test/java/spoon/test/imports/ImportTest.java +++ b/src/test/java/spoon/test/imports/ImportTest.java @@ -41,6 +41,7 @@ import spoon.test.imports.testclasses.Mole; import spoon.test.imports.testclasses.NotImportExecutableType; import spoon.test.imports.testclasses.Pozole; +import spoon.test.imports.testclasses.StaticNoOrdered; import spoon.test.imports.testclasses.SubClass; import spoon.test.imports.testclasses.Tacos; import spoon.test.imports.testclasses.internal.ChildClass; @@ -1162,4 +1163,53 @@ public void testSortingOfImports() { } assertTrue(countOfImports>10); } + + @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> 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); + } } diff --git a/src/test/java/spoon/test/imports/testclasses/StaticNoOrdered.java b/src/test/java/spoon/test/imports/testclasses/StaticNoOrdered.java new file mode 100644 index 00000000000..9f84b8f1faa --- /dev/null +++ b/src/test/java/spoon/test/imports/testclasses/StaticNoOrdered.java @@ -0,0 +1,40 @@ +package spoon.test.imports.testclasses; + + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import static java.nio.charset.Charset.forName; +import java.lang.annotation.Annotation; +import org.junit.Assert; +import java.nio.charset.Charset; + + +/** + * Created by urli on 04/10/2017. + */ +public class StaticNoOrdered { + + public void testMachin() { + assertEquals("bla","truc"); + Test test = new Test() { + @Override + public Class annotationType() { + return null; + } + + @Override + public Class expected() { + return null; + } + + @Override + public long timeout() { + return 0; + } + }; + } + + public void anotherStaticImoport() { + Charset charset = forName("utf-8"); + } +}