diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AbstractImportRule.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AbstractImportRule.java new file mode 100644 index 00000000000..f27ab6e3c87 --- /dev/null +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AbstractImportRule.java @@ -0,0 +1,94 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks.imports; + +/** + * Base class for import rules. + * @author Jochen Van de Velde + */ +abstract class AbstractImportRule { + /** Indicates whether to allow access or not. */ + private final boolean allowed; + + /** Indicates if the guard only applies to this package. */ + private final boolean localOnly; + + /** + * Indicates if the name is to be interpreted + * as a regular expression. + */ + private final boolean regExp; + + /** + * Constructs an instance. + * @param allow whether to allow access. + * @param localOnly whether the rules is to be applied locally only. + * @param regExp whether the name is to be interpreted as a regular + * expression. + */ + protected AbstractImportRule(final boolean allow, final boolean localOnly, + final boolean regExp) { + allowed = allow; + this.localOnly = localOnly; + this.regExp = regExp; + } + + /** + * Verifies whether a package name is used. + * @param forImport the import to check. + * @return a result {@link AccessResult} indicating whether it can be used. + */ + public abstract AccessResult verifyImport(String forImport); + + /** + * @return whether the guard is to only be applied locally. + */ + public boolean isLocalOnly() { + return localOnly; + } + + /** + * @return whether the name is to be interpreted as a regular expression. + */ + protected boolean isRegExp() { + return regExp; + } + + /** + * Returns the appropriate {@link AccessResult} based on whether there + * was a match and if the rule is to allow access. + * @param matched indicates whether there was a match. + * @return An appropriate {@link AccessResult}. + */ + protected AccessResult calculateResult(final boolean matched) { + AccessResult result = AccessResult.UNKNOWN; + + if (matched) { + if (allowed) { + result = AccessResult.ALLOWED; + } + else { + result = AccessResult.DISALLOWED; + } + } + + return result; + } +} diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ClassImportRule.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ClassImportRule.java new file mode 100644 index 00000000000..8cb720b0a74 --- /dev/null +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ClassImportRule.java @@ -0,0 +1,62 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks.imports; + +/** + * Represents whether a class is allowed to be imported or not. + * @author Oliver Burn + */ +class ClassImportRule extends AbstractImportRule { + /** Package to control access to. */ + private final String className; + + /** + * Constructs an instance. + * @param allow whether to allow access. + * @param localOnly whether the rule is to be applied locally only + * @param className the class to apply the rule on. + * @param regExp whether the class name is to be interpreted as a regular + * expression. + */ + ClassImportRule(final boolean allow, final boolean localOnly, + final String className, final boolean regExp) { + super(allow, localOnly, regExp); + this.className = className; + } + + /** + * Verifies whether a class name is used. + * @param forImport the import to check. + * @return a result {@link AccessResult} indicating whether it can be used. + */ + @Override + public AccessResult verifyImport(final String forImport) { + final boolean classMatch; + + if (isRegExp()) { + classMatch = forImport.matches(className); + } + else { + classMatch = forImport.equals(className); + } + + return calculateResult(classMatch); + } +} diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/Guard.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/Guard.java deleted file mode 100644 index d66ec664ea6..00000000000 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/Guard.java +++ /dev/null @@ -1,151 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// checkstyle: Checks Java source code for adherence to a set of rules. -// Copyright (C) 2001-2016 the original author or authors. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -//////////////////////////////////////////////////////////////////////////////// - -package com.puppycrawl.tools.checkstyle.checks.imports; - -/** - * Represents whether a package is allowed to be used or not. - * @author Oliver Burn - */ -class Guard { - /** Indicates if allow access or not. */ - private final boolean allowed; - /** Package to control access to. */ - private final String pkgName; - /** Package to control access to. */ - private final String className; - - /** - * Indicates if must be an exact match. Only valid if guard using a - * package. - */ - private final boolean exactMatch; - /** Indicates if the guard only applies to this package. */ - private final boolean localOnly; - /** - * Indicates if the package and the class names are to be interpreted - * as regular expressions. - */ - private final boolean regExp; - - /** - * Constructs an instance. - * @param allow whether to allow access. - * @param localOnly whether guard is to be applied locally only - * @param pkgName the package to apply guard on. - * @param exactMatch whether the package must match exactly. - * @param regExp whether the package is to be interpreted as regular - * expression. - */ - Guard(final boolean allow, final boolean localOnly, - final String pkgName, final boolean exactMatch, final boolean regExp) { - allowed = allow; - this.localOnly = localOnly; - this.pkgName = pkgName; - this.regExp = regExp; - className = null; - this.exactMatch = exactMatch; - } - - /** - * Constructs an instance. - * @param allow whether to allow access. - * @param localOnly whether guard is to be applied locally only - * @param className the class to apply guard on. - * @param regExp whether the class is to be interpreted as regular - * expression. - */ - Guard(final boolean allow, final boolean localOnly, - final String className, final boolean regExp) { - allowed = allow; - this.localOnly = localOnly; - this.regExp = regExp; - pkgName = null; - this.className = className; - - // not used - exactMatch = true; - } - - /** - * Verifies whether a package name be used. - * @param forImport the package to check. - * @return a result {@link AccessResult} indicating whether it can be used. - */ - public AccessResult verifyImport(final String forImport) { - if (className != null) { - final boolean classMatch; - - if (regExp) { - classMatch = forImport.matches(className); - } - else { - classMatch = forImport.equals(className); - } - return calculateResult(classMatch); - } - - // Must be checking a package. First check that we actually match - // the package. Then check if matched and we must be an exact match. - // In this case, the text after the first "." must not contain - // another "." as this indicates that it is not an exact match. - boolean pkgMatch; - if (regExp) { - pkgMatch = forImport.matches(pkgName + "\\..*"); - if (pkgMatch && exactMatch) { - pkgMatch = !forImport.matches(pkgName + "\\..*\\..*"); - } - } - else { - pkgMatch = forImport.startsWith(pkgName + "."); - if (pkgMatch && exactMatch) { - pkgMatch = forImport.indexOf('.', - pkgName.length() + 1) == -1; - } - } - return calculateResult(pkgMatch); - } - - /** - * @return returns whether the guard is to only be applied locally. - */ - public boolean isLocalOnly() { - return localOnly; - } - - /** - * Returns the appropriate {@link AccessResult} based on whether there - * was a match and if the guard is to allow access. - * @param matched indicates whether there was a match. - * @return An appropriate {@link AccessResult}. - */ - private AccessResult calculateResult(final boolean matched) { - AccessResult result = AccessResult.UNKNOWN; - - if (matched) { - if (allowed) { - result = AccessResult.ALLOWED; - } - else { - result = AccessResult.DISALLOWED; - } - } - return result; - } -} diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java index 021c2a0dd1f..6cbd4ac3a0f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java @@ -112,21 +112,19 @@ else if (ALLOW_ELEMENT_NAME.equals(qName) || "disallow".equals(qName)) { final boolean isLocalOnly = attributes.getValue("local-only") != null; final String pkg = attributes.getValue(PKG_ATTRIBUTE_NAME); final boolean regex = containsRegexAttribute(attributes); - final Guard guard; + final AbstractImportRule rule; if (pkg == null) { // handle class names which can be normal class names or regular // expressions final String clazz = safeGet(attributes, "class"); - guard = new Guard(isAllow, isLocalOnly, clazz, regex); + rule = new ClassImportRule(isAllow, isLocalOnly, clazz, regex); } else { final boolean exactMatch = attributes.getValue("exact-match") != null; - guard = new Guard(isAllow, isLocalOnly, pkg, exactMatch, regex); + rule = new PkgImportRule(isAllow, isLocalOnly, pkg, exactMatch, regex); } - - final PkgControl pkgControl = stack.peek(); - pkgControl.addGuard(guard); + stack.peek().addImportRule(rule); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControl.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControl.java index 22668b3782c..6b4f7f04a45 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControl.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControl.java @@ -39,8 +39,8 @@ class PkgControl { private static final Pattern DOT_PATTERN = Pattern.compile(DOT, Pattern.LITERAL); /** The regex for the package separator: "\\.". */ private static final String DOT_REGEX = "\\."; - /** List of {@link Guard} objects to check. */ - private final Deque guards = new LinkedList<>(); + /** List of {@link AbstractImportRule} objects to check. */ + private final Deque rules = new LinkedList<>(); /** List of children {@link PkgControl} objects. */ private final List children = new ArrayList<>(); /** The parent. Null indicates we are the root node. */ @@ -180,11 +180,11 @@ private static Pattern createPatternForExactMatch(String expression) { } /** - * Adds a guard to the node. - * @param thug the guard to be added. + * Adds a {@link AbstractImportRule} to the node. + * @param rule the rule to be added. */ - protected void addGuard(final Guard thug) { - guards.addFirst(thug); + protected void addImportRule(final AbstractImportRule rule) { + rules.addFirst(rule); } /** @@ -265,20 +265,20 @@ else if (parent == null) { } /** - * Checks whether any of the guards for this node control access to - * a specified package. - * @param forImport the package to check. + * Checks whether any of the rules for this node control access to + * a specified package or class. + * @param forImport the import to check. * @param inPkg the package doing the import. * @return an {@link AccessResult}. */ private AccessResult localCheckAccess(final String forImport, final String inPkg) { - for (Guard g : guards) { - // Check if a Guard is only meant to be applied locally. - if (g.isLocalOnly() && !matchesExactly(inPkg)) { + for (AbstractImportRule r : rules) { + // Check if a PkgImportRule is only meant to be applied locally. + if (r.isLocalOnly() && !matchesExactly(inPkg)) { continue; } - final AccessResult result = g.verifyImport(forImport); + final AccessResult result = r.verifyImport(forImport); if (result != AccessResult.UNKNOWN) { return result; } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgImportRule.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgImportRule.java new file mode 100644 index 00000000000..fe9497f14f4 --- /dev/null +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgImportRule.java @@ -0,0 +1,81 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks.imports; + +/** + * Represents whether a package is allowed to be imported or not. + * @author Oliver Burn + */ +class PkgImportRule extends AbstractImportRule { + /** Package to control access to. */ + private final String pkgName; + + /** Indicates if the package name must be an exact match. */ + private final boolean exactMatch; + + /** + * Constructs an instance. + * @param allow whether to allow access. + * @param localOnly whether the rule is to be applied locally only + * @param pkgName the package to apply the rule on. + * @param exactMatch whether the package name must match exactly. + * @param regExp whether the package name is to be interpreted as a regular + * expression. + */ + PkgImportRule(final boolean allow, final boolean localOnly, + final String pkgName, final boolean exactMatch, final boolean regExp) { + super(allow, localOnly, regExp); + this.pkgName = pkgName; + this.exactMatch = exactMatch; + } + + /** + * Verifies whether a package name is used. + * @param forImport the import to check. + * @return a result {@link AccessResult} indicating whether it can be used. + */ + @Override + public AccessResult verifyImport(final String forImport) { + // First check that we actually match the package. + // Then check if matched and f we must be an exact match. + // In this case, the text after the first "." must not contain + // another "." as this indicates that it is not an exact match. + + boolean pkgMatch; + + if (isRegExp()) { + pkgMatch = forImport.matches(pkgName + "\\..*"); + + if (pkgMatch && exactMatch) { + pkgMatch = !forImport.matches(pkgName + "\\..*\\..*"); + } + } + else { + pkgMatch = forImport.startsWith(pkgName + "."); + + if (pkgMatch && exactMatch) { + pkgMatch = forImport.indexOf('.', + pkgName.length() + 1) == -1; + } + } + + return calculateResult(pkgMatch); + } +} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AccessResultTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AccessResultTest.java new file mode 100644 index 00000000000..04494a0ceb3 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AccessResultTest.java @@ -0,0 +1,53 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks.imports; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class AccessResultTest { + + /* Additional test for jacoco, since valueOf() + * is generated by javac and jacoco reports that + * valueOf() is uncovered. + */ + @Test + public void testAccessResultValueOf() { + final AccessResult result = AccessResult.valueOf("ALLOWED"); + assertEquals(AccessResult.ALLOWED, result); + } + + /* Additional test for jacoco, since values() + * is generated by javac and jacoco reports that + * values() is uncovered. + */ + @Test + public void testAccessResultValues() { + final AccessResult[] actual = AccessResult.values(); + final AccessResult[] expected = { + AccessResult.ALLOWED, + AccessResult.DISALLOWED, + AccessResult.UNKNOWN, + }; + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ClassImportRuleTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ClassImportRuleTest.java new file mode 100644 index 00000000000..840d82a295b --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ClassImportRuleTest.java @@ -0,0 +1,65 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks.imports; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class ClassImportRuleTest { + + @Test + public void testClassImportRule() { + final ClassImportRule r = new ClassImportRule(true, false, "pkg.a", false); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkga")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + } + + @Test + public void testClassImportRuleRegexpSimple() { + final ClassImportRule r = new ClassImportRule(true, false, "pkg.a", true); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkga")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + } + + @Test + public void testClassImportRuleRegexp() { + final ClassImportRule r = new ClassImportRule(true, false, "pk[gx]\\.a", true); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkga")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkx.a")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + } +} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/GuardTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/GuardTest.java deleted file mode 100644 index d277f9dd692..00000000000 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/GuardTest.java +++ /dev/null @@ -1,167 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// checkstyle: Checks Java source code for adherence to a set of rules. -// Copyright (C) 2001-2016 the original author or authors. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -//////////////////////////////////////////////////////////////////////////////// - -package com.puppycrawl.tools.checkstyle.checks.imports; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import org.junit.Test; - -public class GuardTest { - - /* Additional test for jacoco, since valueOf() - * is generated by javac and jacoco reports that - * valueOf() is uncovered. - */ - @Test - public void testAccessResultValueOf() { - final AccessResult result = AccessResult.valueOf("ALLOWED"); - assertEquals(AccessResult.ALLOWED, result); - } - - /* Additional test for jacoco, since values() - * is generated by javac and jacoco reports that - * values() is uncovered. - */ - @Test - public void testAccessResultValues() { - final AccessResult[] actual = AccessResult.values(); - final AccessResult[] expected = { - AccessResult.ALLOWED, - AccessResult.DISALLOWED, - AccessResult.UNKNOWN, - }; - assertArrayEquals(expected, actual); - } - - @Test - public void testPkgGuard1() { - final Guard g = new Guard(true, false, "pkg", false, false); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkga")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - } - - @Test - public void testPkgGuard2() { - final Guard g = new Guard(true, false, "pkg", true, false); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - } - - @Test - public void testClassGuard() { - final Guard g = new Guard(true, false, "pkg.a", false); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkga")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - } - - @Test - public void testPkgGuard1re() { - final Guard g = new Guard(true, false, "pkg", false, true); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkga")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - } - - @Test - public void testPkgGuard2re() { - final Guard g = new Guard(true, false, "pkg", true, true); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - } - - @Test - public void testClassGuardre() { - final Guard g = new Guard(true, false, "pkg.a", true); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkga")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - } - - @Test - public void testPkgGuard1re2() { - final Guard g = new Guard(true, false, "(pkg|hallo)", false, true); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkga")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("halloa")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("hallo.a")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("hallo.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("hallo")); - } - - @Test - public void testPkgGuard2re2() { - final Guard g = new Guard(true, false, "(pkg|hallo)", true, true); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("hallo.a")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("hallo.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("hallo")); - } - - @Test - public void testClassGuardre2() { - final Guard g = new Guard(true, false, "pk[gx]\\.a", true); - assertNotNull(g); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("asda")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("p")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkga")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkg.a")); - assertEquals(AccessResult.ALLOWED, g.verifyImport("pkx.a")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg.a.b")); - assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); - } -} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlRegExpTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlRegExpTest.java index 833604b8a29..3e726f7d061 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlRegExpTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlRegExpTest.java @@ -31,11 +31,15 @@ public class PkgControlRegExpTest { @Before public void setUp() { - pcRoot.addGuard(new Guard(false, false, ".*\\.(spring|lui)framework", false, true)); - pcRoot.addGuard(new Guard(false, false, "org\\.hibernate", false, true)); - pcRoot.addGuard(new Guard(true, false, "org\\.(apache|lui)\\.commons", false, true)); + pcRoot.addImportRule( + new PkgImportRule(false, false, ".*\\.(spring|lui)framework", false, true)); + pcRoot.addImportRule( + new PkgImportRule(false, false, "org\\.hibernate", false, true)); + pcRoot.addImportRule( + new PkgImportRule(true, false, "org\\.(apache|lui)\\.commons", false, true)); - pcCommon.addGuard(new Guard(true, false, "org\\.h.*", false, true)); + pcCommon.addImportRule( + new PkgImportRule(true, false, "org\\.h.*", false, true)); } @Test diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java index 88644565de5..add556321fd 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java @@ -31,11 +31,15 @@ public class PkgControlTest { @Before public void setUp() { - pcRoot.addGuard(new Guard(false, false, "org.springframework", false, false)); - pcRoot.addGuard(new Guard(false, false, "org.hibernate", false, false)); - pcRoot.addGuard(new Guard(true, false, "org.apache.commons", false, false)); + pcRoot.addImportRule( + new PkgImportRule(false, false, "org.springframework", false, false)); + pcRoot.addImportRule( + new PkgImportRule(false, false, "org.hibernate", false, false)); + pcRoot.addImportRule( + new PkgImportRule(true, false, "org.apache.commons", false, false)); - pcCommon.addGuard(new Guard(true, false, "org.hibernate", false, false)); + pcCommon.addImportRule( + new PkgImportRule(true, false, "org.hibernate", false, false)); } @Test diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgImportRuleTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgImportRuleTest.java new file mode 100644 index 00000000000..df5419faf06 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/PkgImportRuleTest.java @@ -0,0 +1,104 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks.imports; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class PkgImportRuleTest { + + @Test + public void testPkgImportRule() { + final PkgImportRule r = new PkgImportRule(true, false, "pkg", false, false); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkga")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + } + + @Test + public void testPkgImportRuleExactMatch() { + final PkgImportRule r = new PkgImportRule(true, false, "pkg", true, false); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + } + + @Test + public void testPkgImportRuleRegexpSimple() { + final PkgImportRule r = new PkgImportRule(true, false, "pkg", false, true); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkga")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + } + + @Test + public void testPkgImportRuleExactMatchRegexpSimple() { + final PkgImportRule r = new PkgImportRule(true, false, "pkg", true, true); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + } + + @Test + public void testPkgImportRuleRegexp() { + final PkgImportRule r = new PkgImportRule(true, false, "(pkg|hallo)", false, true); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkga")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("halloa")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("hallo.a")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("hallo.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("hallo")); + } + + @Test + public void testPkgImportRuleExactMatchRegexp() { + final PkgImportRule r = new PkgImportRule(true, false, "(pkg|hallo)", true, true); + assertNotNull(r); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("asda")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("p")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("pkg.a")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("pkg")); + assertEquals(AccessResult.ALLOWED, r.verifyImport("hallo.a")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("hallo.a.b")); + assertEquals(AccessResult.UNKNOWN, r.verifyImport("hallo")); + } +}