aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/puppycrawl/tools/checkstyle
diff options
context:
space:
mode:
authorAndrew Kuchev <0coming.soon@gmail.com>2017-03-05 11:33:06 +0500
committerRoman Ivanov <romani@users.noreply.github.com>2017-04-10 06:54:19 -0700
commit57785f3aa7d71b67a3c8adc8b60a008f921c33c8 (patch)
treea8832a0f6fbc882876b9d1a9bef67760e827e6d9 /src/test/java/com/puppycrawl/tools/checkstyle
parentd876c652c5fcb43522f49747a63844b49c92c89a (diff)
downloadcheckstyle-57785f3aa7d71b67a3c8adc8b60a008f921c33c8.tar.gz
Issue #3309: Added excludedPackages to class coupling checks
Diffstat (limited to 'src/test/java/com/puppycrawl/tools/checkstyle')
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassDataAbstractionCouplingCheckTest.java77
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassFanOutComplexityCheckTest.java80
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java50
3 files changed, 207 insertions, 0 deletions
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassDataAbstractionCouplingCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassDataAbstractionCouplingCheckTest.java
index 557076ccf..8cc9d137d 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassDataAbstractionCouplingCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassDataAbstractionCouplingCheckTest.java
@@ -21,6 +21,7 @@ package com.puppycrawl.tools.checkstyle.checks.metrics;
import static com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck.MSG_KEY;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
@@ -31,6 +32,7 @@ import org.junit.Test;
import antlr.CommonHiddenStreamToken;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
+import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
@@ -60,6 +62,81 @@ public class ClassDataAbstractionCouplingCheckTest extends BaseCheckTestSupport
}
@Test
+ public void testExludedPackageDirectPackages() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassDataAbstractionCouplingCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.c,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.b");
+
+ final String[] expected = {
+ "8:1: " + getCheckMessage(MSG_KEY, 2, 0, "[AAClass, ABClass]"),
+ };
+
+ verify(checkConfig,
+ getPath("InputClassCouplingExcludedPackagesDirectPackages.java"), expected);
+ }
+
+ @Test
+ public void testExludedPackageCommonPackages() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassDataAbstractionCouplingCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a");
+
+ final String[] expected = {
+ "8:1: " + getCheckMessage(MSG_KEY, 2, 0, "[AAClass, ABClass]"),
+ "12:5: " + getCheckMessage(MSG_KEY, 2, 0, "[BClass, CClass]"),
+ "18:1: " + getCheckMessage(MSG_KEY, 1, 0, "[CClass]"),
+ };
+ verify(checkConfig,
+ getPath("InputClassCouplingExcludedPackagesCommonPackage.java"), expected);
+ }
+
+ @Test
+ public void testExludedPackageWithEndingDot() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassDataAbstractionCouplingCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.");
+
+ try {
+ createChecker(checkConfig);
+ fail("exception expected");
+ }
+ catch (CheckstyleException ex) {
+ assertTrue(ex.getMessage().startsWith(
+ "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
+ + "Cannot set property 'excludedPackages' to "
+ + "'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.' in module "
+ + "com.puppycrawl.tools.checkstyle.checks.metrics."
+ + "ClassDataAbstractionCouplingCheck"));
+ }
+ }
+
+ @Test
+ public void testExludedPackageCommonPackagesAllIgnored() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassDataAbstractionCouplingCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.aa,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.ab,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.b,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.c");
+
+ final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
+ verify(checkConfig, getPath("InputClassCouplingExcludedPackagesAllIgnored.java"), expected);
+ }
+
+ @Test
public void testDefaultConfiguration() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(ClassDataAbstractionCouplingCheck.class);
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassFanOutComplexityCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassFanOutComplexityCheckTest.java
index d8fec41da..a6ac4656f 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassFanOutComplexityCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/metrics/ClassFanOutComplexityCheckTest.java
@@ -20,6 +20,8 @@
package com.puppycrawl.tools.checkstyle.checks.metrics;
import static com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck.MSG_KEY;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
@@ -29,6 +31,7 @@ import org.junit.Test;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
+import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
@@ -55,6 +58,82 @@ public class ClassFanOutComplexityCheckTest extends BaseCheckTestSupport {
}
@Test
+ public void testExcludedPackagesDirectPackages() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassFanOutComplexityCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.c,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.b");
+
+ final String[] expected = {
+ "8:1: " + getCheckMessage(MSG_KEY, 2, 0),
+ };
+
+ verify(checkConfig,
+ getPath("InputClassFanOutComplexityExcludedPackagesDirectPackages.java"), expected);
+ }
+
+ @Test
+ public void testExcludedPackagesCommonPackages() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassFanOutComplexityCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a");
+
+ final String[] expected = {
+ "8:1: " + getCheckMessage(MSG_KEY, 2, 0),
+ "12:5: " + getCheckMessage(MSG_KEY, 2, 0),
+ "18:1: " + getCheckMessage(MSG_KEY, 1, 0),
+ };
+ verify(checkConfig,
+ getPath("InputClassFanOutComplexityExcludedPackagesCommonPackage.java"), expected);
+ }
+
+ @Test
+ public void testExcludedPackagesCommonPackagesWithEndingDot() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassFanOutComplexityCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.");
+
+ try {
+ createChecker(checkConfig);
+ fail("exception expected");
+ }
+ catch (CheckstyleException ex) {
+ assertTrue(ex.getMessage().startsWith(
+ "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
+ + "Cannot set property 'excludedPackages' to "
+ + "'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.' in module "
+ + "com.puppycrawl.tools.checkstyle.checks.metrics."
+ + "ClassFanOutComplexityCheck"));
+ }
+ }
+
+ @Test
+ public void testExcludedPackagesAllIgnored() throws Exception {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(ClassFanOutComplexityCheck.class);
+
+ checkConfig.addAttribute("max", "0");
+ checkConfig.addAttribute("excludedPackages",
+ "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.aa,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.ab,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.b,"
+ + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.c");
+
+ final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
+ verify(checkConfig,
+ getPath("InputClassFanOutComplexityExcludedPackagesAllIgnored.java"), expected);
+ }
+
+ @Test
public void test15() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(ClassFanOutComplexityCheck.class);
@@ -83,6 +162,7 @@ public class ClassFanOutComplexityCheckTest extends BaseCheckTestSupport {
final int[] actual = classFanOutComplexityCheckObj.getAcceptableTokens();
final int[] expected = {
TokenTypes.PACKAGE_DEF,
+ TokenTypes.IMPORT,
TokenTypes.CLASS_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.ENUM_DEF,
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java
index 966f8f053..73b4f0105 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java
@@ -228,6 +228,56 @@ public class CommonUtilsTest {
}
@Test
+ public void testIsIdentifier() throws Exception {
+ assertTrue(CommonUtils.isIdentifier("aValidIdentifier"));
+ }
+
+ @Test
+ public void testIsIdentifierEmptyString() throws Exception {
+ assertFalse(CommonUtils.isIdentifier(""));
+ }
+
+ @Test
+ public void testIsIdentifierInvalidFirstSymbol() throws Exception {
+ assertFalse(CommonUtils.isIdentifier("1InvalidIdentifier"));
+ }
+
+ @Test
+ public void testIsIdentifierInvalidSymbols() throws Exception {
+ assertFalse(CommonUtils.isIdentifier("invalid#Identifier"));
+ }
+
+ @Test
+ public void testIsName() throws Exception {
+ assertTrue(CommonUtils.isName("a.valid.Nam3"));
+ }
+
+ @Test
+ public void testIsNameEmptyString() throws Exception {
+ assertFalse(CommonUtils.isName(""));
+ }
+
+ @Test
+ public void testIsNameInvalidFirstSymbol() throws Exception {
+ assertFalse(CommonUtils.isName("1.invalid.name"));
+ }
+
+ @Test
+ public void testIsNameEmptyPart() throws Exception {
+ assertFalse(CommonUtils.isName("invalid..name"));
+ }
+
+ @Test
+ public void testIsNameEmptyLastPart() throws Exception {
+ assertFalse(CommonUtils.isName("invalid.name."));
+ }
+
+ @Test
+ public void testIsNameInvalidSymbol() throws Exception {
+ assertFalse(CommonUtils.isName("invalid.name#42"));
+ }
+
+ @Test
@PrepareForTest({ CommonUtils.class, CommonUtilsTest.class })
@SuppressWarnings("unchecked")
public void testLoadSuppressionsUriSyntaxException() throws Exception {