From cd12de2a24713a494393912e2277b67770eb79fe Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Mon, 10 Aug 2015 11:07:49 +0300 Subject: [PATCH] Extended messages for CustomImportOrder #1648 --- .../CustomImportOrderTest.java | 52 +++--- .../imports/CustomImportOrderCheck.java | 50 ++++-- .../checks/imports/messages.properties | 7 +- .../imports/CustomImportOrderCheckTest.java | 160 +++++++++--------- 4 files changed, 144 insertions(+), 125 deletions(-) diff --git a/src/it/java/com/google/checkstyle/test/chapter3filestructure/rule333orderingandsoacing/CustomImportOrderTest.java b/src/it/java/com/google/checkstyle/test/chapter3filestructure/rule333orderingandsoacing/CustomImportOrderTest.java index 4bed7d6b3..37b2f4b1a 100644 --- a/src/it/java/com/google/checkstyle/test/chapter3filestructure/rule333orderingandsoacing/CustomImportOrderTest.java +++ b/src/it/java/com/google/checkstyle/test/chapter3filestructure/rule333orderingandsoacing/CustomImportOrderTest.java @@ -21,6 +21,10 @@ public class CustomImportOrderTest extends BaseCheckTestSupport{ String msgOrder = "custom.import.order"; String msgNongroup = "custom.import.order.nongroup.import"; + /** Shortcuts to make code more compact */ + private static final String STD = com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.STANDARD_JAVA_PACKAGE_RULE_GROUP; + private static final String SPECIAL = com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.SPECIAL_IMPORTS_RULE_GROUP; + @BeforeClass public static void setConfigurationBuilder() throws CheckstyleException, IOException { builder = new ConfigurationBuilder(new File("src/it/")); @@ -30,17 +34,17 @@ public class CustomImportOrderTest extends BaseCheckTestSupport{ public void customImportTest_1() throws IOException, Exception { final String[] expected = { - "4: " + getCheckMessage(clazz, msgLex, "java.awt.Button.ABORT"), - "7: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "8: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "9: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "10: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "11: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "12: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "13: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "14: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "15: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "16: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), + "4: " + getCheckMessage(clazz, msgLex, "java.awt.Button.ABORT", "java.io.File.createTempFile"), + "7: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.awt.Button"), + "8: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.awt.Frame"), + "9: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.awt.Dialog"), + "10: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.awt.event.ActionEvent"), + "11: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "javax.swing.JComponent"), + "12: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "javax.swing.JTable"), + "13: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.io.File"), + "14: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.io.IOException"), + "15: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.io.InputStream"), + "16: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.io.Reader"), }; Configuration checkConfig = builder.getCheckConfig("CustomImportOrder"); @@ -54,14 +58,14 @@ public class CustomImportOrderTest extends BaseCheckTestSupport{ public void customImportTest_2() throws IOException, Exception { final String[] expected = { - "4: " + getCheckMessage(clazz, msgLex, "java.awt.Button.ABORT"), - "7: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "8: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "9: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "10: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "11: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), + "4: " + getCheckMessage(clazz, msgLex, "java.awt.Button.ABORT", "java.io.File.createTempFile"), + "7: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.List"), + "8: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.StringTokenizer"), + "9: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.*"), + "10: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.concurrent.AbstractExecutorService"), + "11: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.concurrent.*"), "14: " + getCheckMessage(clazz, msgSeparator, "com.sun.xml.internal.xsom.impl.scd.Iterators"), - "16: " + getCheckMessage(clazz, msgOrder, "SPECIAL_IMPORTS"), + "16: " + getCheckMessage(clazz, msgOrder, SPECIAL, STD, "com.google.common.reflect.*"), }; Configuration checkConfig = builder.getCheckConfig("CustomImportOrder"); @@ -75,13 +79,13 @@ public class CustomImportOrderTest extends BaseCheckTestSupport{ public void customImportTest_3() throws IOException, Exception { final String[] expected = { - "4: " + getCheckMessage(clazz, msgLex, "java.awt.Button.ABORT"), - "8: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "9: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "10: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), - "11: " + getCheckMessage(clazz, msgOrder, "STANDARD_JAVA_PACKAGE"), + "4: " + getCheckMessage(clazz, msgLex, "java.awt.Button.ABORT", "java.io.File.createTempFile"), + "8: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.StringTokenizer"), + "9: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.*"), + "10: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.concurrent.AbstractExecutorService"), + "11: " + getCheckMessage(clazz, msgOrder, STD, SPECIAL, "java.util.concurrent.*"), "14: " + getCheckMessage(clazz, msgSeparator, "com.sun.xml.internal.xsom.impl.scd.Iterators"), - "16: " + getCheckMessage(clazz, msgOrder, "SPECIAL_IMPORTS"), + "16: " + getCheckMessage(clazz, msgOrder, SPECIAL, STD, "com.google.common.reflect.*"), }; Configuration checkConfig = builder.getCheckConfig("CustomImportOrder"); diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java index 063ccffc1..cca8cc27e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java @@ -283,29 +283,35 @@ public class CustomImportOrderCheck extends Check { */ public static final String MSG_NONGROUP_IMPORT = "custom.import.order.nongroup.import"; + /** + * A key is pointing to the warning message text in "messages.properties" + * file. + */ + public static final String MSG_NONGROUP_EXPECTED = "custom.import.order.nongroup.expected"; + /** * A key is pointing to the warning message text in "messages.properties" * file. */ public static final String MSG_ORDER = "custom.import.order"; - /** STATIC group name */ - private static final String STATIC_RULE_GROUP = "STATIC"; + /** STATIC group name. */ + public static final String STATIC_RULE_GROUP = "STATIC"; - /** SAME_PACKAGE group name */ - private static final String SAME_PACKAGE_RULE_GROUP = "SAME_PACKAGE"; + /** SAME_PACKAGE group name. */ + public static final String SAME_PACKAGE_RULE_GROUP = "SAME_PACKAGE"; - /** THIRD_PARTY_PACKAGE group name */ - private static final String THIRD_PARTY_PACKAGE_RULE_GROUP = "THIRD_PARTY_PACKAGE"; + /** THIRD_PARTY_PACKAGE group name. */ + public static final String THIRD_PARTY_PACKAGE_RULE_GROUP = "THIRD_PARTY_PACKAGE"; - /** STANDARD_JAVA_PACKAGE group name */ - private static final String STANDARD_JAVA_PACKAGE_RULE_GROUP = "STANDARD_JAVA_PACKAGE"; + /** STANDARD_JAVA_PACKAGE group name. */ + public static final String STANDARD_JAVA_PACKAGE_RULE_GROUP = "STANDARD_JAVA_PACKAGE"; - /** NON_GROUP group name */ - private static final String SPECIAL_IMPORTS_RULE_GROUP = "SPECIAL_IMPORTS"; + /** SPECIAL_IMPORTS group name. */ + public static final String SPECIAL_IMPORTS_RULE_GROUP = "SPECIAL_IMPORTS"; - /** NON_GROUP group name */ - private static final String NON_GROUP_RULE_GROUP = "NON_GROUP"; + /** NON_GROUP group name. */ + private static final String NON_GROUP_RULE_GROUP = "NOT_ASSIGNED_TO_ANY_GROUP"; /** RegExp for SAME_PACKAGE group imports */ private String samePackageDomainsRegExp = ""; @@ -472,12 +478,12 @@ public class CustomImportOrderCheck extends Check { } else { logWrongImportGroupOrder(importObject.getLineNumber(), - importGroup); + importGroup, nextGroup, fullImportIdent); } } else { logWrongImportGroupOrder(importObject.getLineNumber(), - importGroup); + importGroup, currentGroup, fullImportIdent); } } else if (sortImportsInGroupAlphabetically @@ -485,7 +491,7 @@ public class CustomImportOrderCheck extends Check { && matchesImportGroup(importObject.isStaticImport(), fullImportIdent, currentGroup) && compareImports(fullImportIdent, previousImport) < 0) { - log(importObject.getLineNumber(), MSG_LEX, fullImportIdent); + log(importObject.getLineNumber(), MSG_LEX, fullImportIdent, previousImport); } previousImport = fullImportIdent; } @@ -497,13 +503,21 @@ public class CustomImportOrderCheck extends Check { * line number of current import current import. * @param importGroup * import group. + * @param currentGroupNumber + * current group number we are checking. + * @param fullImportIdent + * full import name. */ - private void logWrongImportGroupOrder(int currentImportLine, String importGroup) { + private void logWrongImportGroupOrder(int currentImportLine, String importGroup, + String currentGroupNumber, String fullImportIdent) { if (NON_GROUP_RULE_GROUP.equals(importGroup)) { - log(currentImportLine, MSG_NONGROUP_IMPORT); + log(currentImportLine, MSG_NONGROUP_IMPORT, fullImportIdent); + } + else if (NON_GROUP_RULE_GROUP.equals(currentGroupNumber)) { + log(currentImportLine, MSG_NONGROUP_EXPECTED, importGroup, fullImportIdent); } else { - log(currentImportLine, MSG_ORDER, importGroup); + log(currentImportLine, MSG_ORDER, importGroup, currentGroupNumber, fullImportIdent); } } diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/imports/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/imports/messages.properties index 4eb8e24fe..39b0ba575 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/imports/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/imports/messages.properties @@ -10,7 +10,8 @@ import.separation=''{0}'' should be separated from previous imports. import.control.missing.file=Missing an import control file. import.control.disallowed=Disallowed import - {0}. import.control.unknown.pkg=Import control file does not handle this package. -custom.import.order=Import statement is in the wrong order. Should be in the ''{0}'' group. +custom.import.order=Import statement for ''{2}'' is in the wrong order. Should be in the ''{0}'' group, expecting group ''{1}'' on this line. custom.import.order.line.separator=''{0}'' should be separated from previous import group. -custom.import.order.lex=Wrong lexicographical order for ''{0}'' import. -custom.import.order.nongroup.import=Imports without groups should be placed at the end of the import list. +custom.import.order.lex=Wrong lexicographical order for ''{0}'' import. Should be before ''{1}''. +custom.import.order.nongroup.import=Imports without groups should be placed at the end of the import list: ''{0}''. +custom.import.order.nongroup.expected=Import statement for ''{2}'' is in the wrong order. Should be in the ''{0}'' group, expecting not assigned imports on this line. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheckTest.java index 8ec62f3d1..154e108b5 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheckTest.java @@ -21,6 +21,7 @@ package com.puppycrawl.tools.checkstyle.checks.imports; import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_LEX; import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_LINE_SEPARATOR; +import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_NONGROUP_EXPECTED; import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_NONGROUP_IMPORT; import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_ORDER; import static org.junit.Assert.assertArrayEquals; @@ -39,29 +40,35 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TokenTypes; public class CustomImportOrderCheckTest extends BaseCheckTestSupport { + /** Shortcuts to make code more compact */ + private static final String STATIC = com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.STATIC_RULE_GROUP; + private static final String SAME = com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.SAME_PACKAGE_RULE_GROUP; + private static final String THIRD = com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.THIRD_PARTY_PACKAGE_RULE_GROUP; + private static final String STD = com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.STANDARD_JAVA_PACKAGE_RULE_GROUP; + private static final String SPECIAL = com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.SPECIAL_IMPORTS_RULE_GROUP; + @Test public void testCustom() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class); checkConfig.addAttribute("standardPackageRegExp", "^(java|javax)\\."); checkConfig.addAttribute("thirdPartyPackageRegExp", "com|org"); - checkConfig - .addAttribute("customImportOrderRules", - "STATIC###SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE"); + checkConfig.addAttribute("customImportOrderRules", + "STATIC###SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE"); checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT"), - "8: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "9: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "10: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "11: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "12: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "13: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "14: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "15: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "16: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "17: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "18: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), + "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT", "java.io.File.createTempFile"), + "8: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.Button"), + "9: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.Frame"), + "10: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.Dialog"), + "11: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.color.ColorSpace"), + "12: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.event.ActionEvent"), + "13: " + getCheckMessage(MSG_ORDER, STD, SAME, "javax.swing.JComponent"), + "14: " + getCheckMessage(MSG_ORDER, STD, SAME, "javax.swing.JTable"), + "15: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.File"), + "16: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.IOException"), + "17: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.InputStream"), + "18: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.Reader"), }; verify(checkConfig, getPath("imports" + File.separator @@ -73,7 +80,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { * specified in the configuration. */ @Test - public void testGoogleStyleguideConfiguraiton() throws Exception { + public void testStaticStandardThird() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class); checkConfig.addAttribute("thirdPartyPackageRegExp", "com.|org."); @@ -81,10 +88,10 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { "STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE"); checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT"), - "10: " + getCheckMessage(MSG_LEX, "java.awt.Dialog"), - "15: " + getCheckMessage(MSG_LEX, "java.io.File"), - "22: " + getCheckMessage(MSG_LEX, "com.google.common.collect.*"), + "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT", "java.io.File.createTempFile"), + "10: " + getCheckMessage(MSG_LEX, "java.awt.Dialog", "java.awt.Frame"), + "15: " + getCheckMessage(MSG_LEX, "java.io.File", "javax.swing.JTable"), + "22: " + getCheckMessage(MSG_LEX, "com.google.common.collect.*", "com.puppycrawl.tools.*"), }; verify(checkConfig, getPath("imports" + File.separator @@ -99,16 +106,15 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class); checkConfig.addAttribute("thirdPartyPackageRegExp", "org."); - checkConfig - .addAttribute("customImportOrderRules", - "STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE###SAME_PACKAGE(3)"); + checkConfig.addAttribute("customImportOrderRules", + "STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE###SAME_PACKAGE(3)"); checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT"), - "10: " + getCheckMessage(MSG_LEX, "java.awt.Dialog"), - "15: " + getCheckMessage(MSG_LEX, "java.io.File"), - "20: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "22: " + getCheckMessage(MSG_NONGROUP_IMPORT), + "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT", "java.io.File.createTempFile"), + "10: " + getCheckMessage(MSG_LEX, "java.awt.Dialog", "java.awt.Frame"), + "15: " + getCheckMessage(MSG_LEX, "java.io.File", "javax.swing.JTable"), + "20: " + getCheckMessage(MSG_ORDER, SAME, THIRD, "com.puppycrawl.tools.*"), + "22: " + getCheckMessage(MSG_NONGROUP_IMPORT, "com.google.common.collect.*"), "23: " + getCheckMessage(MSG_LINE_SEPARATOR, "org.junit.*"), }; @@ -125,11 +131,11 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { "STANDARD_JAVA_PACKAGE"); checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "7: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "8: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "9: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "10: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "11: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), + "7: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.util.List"), + "8: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.util.StringTokenizer"), + "9: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.util.*"), + "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.util.concurrent.AbstractExecutorService"), + "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.util.concurrent.*"), }; verify(checkConfig, getPath("imports" + File.separator @@ -137,7 +143,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { } @Test - public void testSamePackageAndStatic() throws Exception { + public void testStaticSamePackage() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class); checkConfig.addAttribute("thirdPartyPackageRegExp", "org."); @@ -145,11 +151,11 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { "STATIC###SAME_PACKAGE(3)"); checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "6: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "7: " + getCheckMessage(MSG_ORDER, "STATIC"), - "8: " + getCheckMessage(MSG_ORDER, "STATIC"), - "10: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "11: " + getCheckMessage(MSG_ORDER, "STATIC"), + "6: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"), + "7: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.awt.Button.ABORT"), + "8: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "javax.swing.WindowConstants.*"), + "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.AbstractExecutorService"), + "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.io.File.createTempFile"), }; verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" @@ -167,11 +173,11 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { "STATIC###SAME_PACKAGE(3)"); checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "6: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "7: " + getCheckMessage(MSG_ORDER, "STATIC"), - "8: " + getCheckMessage(MSG_ORDER, "STATIC"), - "10: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "11: " + getCheckMessage(MSG_ORDER, "STATIC"), + "6: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"), + "7: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.awt.Button.ABORT"), + "8: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "javax.swing.WindowConstants.*"), + "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.AbstractExecutorService"), + "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.io.File.createTempFile"), }; verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" @@ -188,7 +194,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { "STATIC###STANDARD_JAVA_PACKAGE"); checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "4: " + getCheckMessage(MSG_LEX, "java.io.File.createTempFile"), + "4: " + getCheckMessage(MSG_LEX, "java.io.File.createTempFile", "javax.swing.WindowConstants.*"), }; verify(checkConfig, getPath("imports" + File.separator @@ -220,7 +226,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { checkConfig.addAttribute("customImportOrderRules", "STATIC###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE"); final String[] expected = { - "5: " + getCheckMessage(MSG_ORDER, "THIRD_PARTY_PACKAGE"), + "5: " + getCheckMessage(MSG_NONGROUP_EXPECTED, THIRD, "org.w3c.dom.Node"), }; verify(checkConfig, getPath("imports" + File.separator @@ -237,18 +243,18 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); final String[] expected = { - "7: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT"), - "10: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "11: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "12: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "13: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "14: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "15: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "16: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "17: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "18: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "19: " + getCheckMessage(MSG_ORDER, "STANDARD_JAVA_PACKAGE"), - "23: " + getCheckMessage(MSG_LEX, "com.google.common.*"), + "7: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT", "java.io.File.createTempFile"), + "10: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Button"), + "11: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Frame"), + "12: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Dialog"), + "13: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.event.ActionEvent"), + "14: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JComponent"), + "15: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JTable"), + "16: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.File"), + "17: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.IOException"), + "18: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.InputStream"), + "19: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.Reader"), + "23: " + getCheckMessage(MSG_LEX, "com.google.common.*", "com.puppycrawl.tools.*"), }; verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" @@ -277,10 +283,9 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { createCheckConfig(CustomImportOrderCheck.class); checkConfig.addAttribute("specialImportsRegExp", "antlr.*"); checkConfig.addAttribute("customImportOrderRules", - "SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STATIC###" - + "SPECIAL_IMPORTS"); + "SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STATIC###SPECIAL_IMPORTS"); final String[] expected = { - "11: " + getCheckMessage(MSG_ORDER, "THIRD_PARTY_PACKAGE"), + "11: " + getCheckMessage(MSG_ORDER, THIRD, SPECIAL, "com.google.common.annotations.GwtCompatible"), }; verify(checkConfig, getPath("imports" + File.separator @@ -337,6 +342,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { assertEquals(expected, (String) actual); } + @Test public void testSamePackageDepth2() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(CustomImportOrderCheck.class); @@ -345,14 +351,14 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(2)"); final String[] expected = { - "7: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "8: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "9: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "10: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "11: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "12: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "13: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "14: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), + "7: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.*"), + "8: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.List"), + "9: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.StringTokenizer"), + "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"), + "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.AbstractExecutorService"), + "12: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.locks.LockSupport"), + "13: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.regex.Pattern"), + "14: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.regex.Matcher"), }; verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" @@ -369,9 +375,9 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(3)"); final String[] expected = { - "10: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "11: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "12: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), + "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"), + "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.AbstractExecutorService"), + "12: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.locks.LockSupport"), }; verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" @@ -388,7 +394,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(4)"); final String[] expected = { - "12: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), + "12: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.locks.LockSupport"), }; verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" @@ -445,13 +451,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport { createCheckConfig(CustomImportOrderCheck.class); checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(3)###UNSUPPORTED_RULE"); //#AAA##BBBB###CCCC####DDDD checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true"); - final String[] expected = { - "4: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "6: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "7: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "8: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - "9: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"), - }; + final String[] expected = {}; verify(checkConfig, getPath("imports" + File.separator + "InputCustomImportOrder.java"), expected);