From 10e6bfe44834bd38bb001058e775377141c86678 Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Mon, 15 Jun 2015 21:54:39 +0300 Subject: [PATCH] UT coverage for RedundantImport check. #1128 --- pom.xml | 1 - .../checks/imports/RedundantImportCheck.java | 21 ++++++++----------- .../imports/RedundantImportCheckTest.java | 12 +++++++++++ .../imports/InputRedundantImportCheck.java | 5 ++++- ...utRedundantImportCheck_UnnamedPackage.java | 11 ++++++++++ 5 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck_UnnamedPackage.java diff --git a/pom.xml b/pom.xml index de316cff1..b6a42577b 100644 --- a/pom.xml +++ b/pom.xml @@ -819,7 +819,6 @@ .*.checks.imports.ImportControlLoader7288 .*.checks.imports.ImportOrderCheck9199 .*.checks.imports.PkgControl80100 - .*.checks.imports.RedundantImportCheck8197 .*.checks.imports.UnusedImportsCheck95100 diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java index 0f2eb7dc8..56c42c51f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java @@ -113,7 +113,9 @@ public class RedundantImportCheck log(ast.getLineNo(), ast.getColumnNo(), MSG_LANG, imp.getText()); } - else if (fromPackage(imp.getText(), pkgName)) { + // imports from unnamed package are not allowed, + // so we are checking SAME rule only for named packages + else if (pkgName != null && fromPackage(imp.getText(), pkgName)) { log(ast.getLineNo(), ast.getColumnNo(), MSG_SAME, imp.getText()); } @@ -152,17 +154,12 @@ public class RedundantImportCheck */ private static boolean fromPackage(String importName, String pkg) { boolean retVal = false; - if (pkg == null) { - // If not package, then check for no package in the import. - retVal = importName.indexOf('.') == -1; - } - else { - final int index = importName.lastIndexOf('.'); - if (index != -1) { - final String front = importName.substring(0, index); - retVal = front.equals(pkg); - } - } + // imports from unnamed package are not allowed: + // http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5 + // So '.' must be present in member name and we are not checking for it + final int index = importName.lastIndexOf('.'); + final String front = importName.substring(0, index); + retVal = front.equals(pkg); return retVal; } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheckTest.java index aae58c200..a383ecf41 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheckTest.java @@ -49,6 +49,18 @@ public class RedundantImportCheckTest verify(checkConfig, getPath("imports" + File.separator + "InputRedundantImportCheck.java"), expected); } + @Test + public void testUnnamedPackage() + throws Exception { + final DefaultConfiguration checkConfig = + createCheckConfig(RedundantImportCheck.class); + final String[] expected = { + "2:1: " + getCheckMessage(MSG_DUPLICATE, 1, "java.util.List"), + "4:1: " + getCheckMessage(MSG_LANG, "java.lang.String"), + }; + verify(checkConfig, getPath("imports" + File.separator + "InputRedundantImportCheck_UnnamedPackage.java"), expected); + } + @Test public void testGetAcceptableTokens() { RedundantImportCheck testCheckObject = diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck.java b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck.java index 83cf0dae9..1f8d8a5aa 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck.java @@ -45,6 +45,9 @@ import com.puppycrawl.tools.checkstyle.PackageNamesLoader; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; import com.puppycrawl.tools.checkstyle.DefaultLogger; +import static java.lang.Math.PI; +import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_SAME; + /** * Test case for imports * Here's an import used only by javadoc: {@link Date}. @@ -108,4 +111,4 @@ class InputRedundantImportCheck * @deprecated in 1 for removal in 2. Use {@link TestClass8} */ public void aMethodWithManyLinks() {} -} \ No newline at end of file +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck_UnnamedPackage.java b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck_UnnamedPackage.java new file mode 100644 index 000000000..eca701104 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputRedundantImportCheck_UnnamedPackage.java @@ -0,0 +1,11 @@ +import java.util.List; +import java.util.List; +import java.util.Arrays; +import java.lang.String; +import static java.lang.Math.PI; + +public class InputRedundantImportCheck_UnnamedPackage +{ + public static double pi=PI; + public List myList; +}