From 17d82f5885911f8f807a843bc4d26c3594fc4cc4 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 29 May 2017 09:16:17 +0100 Subject: [PATCH 1/9] sample-parent: set level to 5-complexity plugin-sample: overrides level back down to 2-naming --- plugin-sample/pom.xml | 4 +++- sample-parent/pom.xml | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/plugin-sample/pom.xml b/plugin-sample/pom.xml index 8ae0218..2c7d70d 100644 --- a/plugin-sample/pom.xml +++ b/plugin-sample/pom.xml @@ -2,7 +2,9 @@ - + + 2-naming + 4.0.0 diff --git a/sample-parent/pom.xml b/sample-parent/pom.xml index ed2f245..f59ce12 100644 --- a/sample-parent/pom.xml +++ b/sample-parent/pom.xml @@ -14,7 +14,8 @@ Sample parent for modules that use kemitix-checkstyle-ruleset-maven-plugin - 2-naming + ${project.version} + 5-complexity UTF-8 UTF-8 @@ -50,9 +51,9 @@ net.kemitix kemitix-checkstyle-ruleset-maven-plugin - ${project.version} + ${kemitix-checkstyle-ruleset.version} - ${kemitix-checkstyle-ruleset-level} + ${kemitix-checkstyle-ruleset.level} From e634dba450e350addf63b05558edb5f3b303e95b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 29 May 2017 09:17:31 +0100 Subject: [PATCH 2/9] regressions: ExplicitInitialization: added Regression added in preparation for checkstyle adding an option in 7.8.x to allow primatives to be initialised to their default values. --- .../regressions/ExplicitInitialization.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 regressions/src/main/java/net/kemitix/checkstyle/regressions/ExplicitInitialization.java diff --git a/regressions/src/main/java/net/kemitix/checkstyle/regressions/ExplicitInitialization.java b/regressions/src/main/java/net/kemitix/checkstyle/regressions/ExplicitInitialization.java new file mode 100644 index 0000000..322b589 --- /dev/null +++ b/regressions/src/main/java/net/kemitix/checkstyle/regressions/ExplicitInitialization.java @@ -0,0 +1,60 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.regressions; + +/** + * Regression demo for {@code ExplicitInitializationCheck}. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +@SuppressWarnings("hideutilityclassconstructor") +class ExplicitInitialization { + + /** + * This will become valid in next release. + */ + @SuppressWarnings("explicitinitialization") + private boolean validBoolean = false; + + /** + * This will become valid in next release. + */ + @SuppressWarnings("explicitinitialization") + private int validInt = 0; + + private String validString = ""; + + private Object validObject = new Object(); + + @SuppressWarnings("explicitinitialization") + private Boolean invalidBoolean = null; + + @SuppressWarnings("explicitinitialization") + private Integer invalidInteger = null; + + @SuppressWarnings("explicitinitialization") + private String invalidString = null; + + @SuppressWarnings("explicitinitialization") + private Object invalidObject = null; + +} From da5461555e31483940f288858d4c94c04969ada4 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 29 May 2017 09:34:18 +0100 Subject: [PATCH 3/9] pom.xml: upgrade checkstyle to 7.8 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f5c9d8f..5c317d7 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ 2.8.2 2.17 - 7.7 + 7.8 1.24.0 1.10.19 3.8.0 From a0d58fca81d1caa8c95bcde8e09741b45dce608d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 29 May 2017 20:31:22 +0100 Subject: [PATCH 4/9] travis-ci: verbose deploy --- travis-ci/travis-deploy.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/travis-ci/travis-deploy.sh b/travis-ci/travis-deploy.sh index 240dd26..6ff7008 100755 --- a/travis-ci/travis-deploy.sh +++ b/travis-ci/travis-deploy.sh @@ -1,10 +1,18 @@ #!/usr/bin/env bash if [ "$TRAVIS_BRANCH" = 'master' ] && [ "$TRAVIS_PULL_REQUEST" == 'false' ]; then + echo "Preparing to deploy to nexus..." openssl aes-256-cbc -K $encrypted_efec3258f55d_key -iv $encrypted_efec3258f55d_iv \ -in travis-ci/codesigning.asc.enc -out travis-ci/codesigning.asc -d + echo "Signing key decrypted" gpg --batch --fast-import travis-ci/codesigning.asc + echo "Signing key imported" ./mvnw --projects plugin,ruleset --settings travis-ci/travis-settings.xml \ -Dskip-Tests=true -P release -B deploy + echo "Deploy complete" +else + echo "Not deploying" + echo " TRAVIS_BRANCH: $TRAVIS_BRANCH" + echo " TRAVIS_PULL_REQUEST: $TRAVIS_PULL_REQUEST" fi From a0ca4958fc0256608fd5fdc9af389db02241b800 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 29 May 2017 23:55:42 +0100 Subject: [PATCH 5/9] builder: use fully qualified classnames in XML files In Checkstyle 7.8 a new mechanisim is used to lookup rules by name. This new method causes an exception to be thrown is there are more than one classes found that match the rule name. The solution is to use fully qualified class names in the module tag in the XML ruleset files. --- builder/pom.xml | 15 + .../CheckstyleClassNotFoundException.java | 39 +++ .../CheckstyleSourceLoadException.java | 42 +++ .../ruleset/builder/CheckstyleWriter.java | 4 +- .../checkstyle/ruleset/builder/Rule.java | 27 +- .../ruleset/builder/RuleSource.java | 40 ++- .../CheckstyleClassNotFoundExceptionTest.java | 24 ++ .../CheckstyleSourceLoadExceptionTest.java | 28 ++ .../ruleset/builder/CheckstyleWriterTest.java | 16 +- .../net/kemitix/checkstyle-1-layout.xml | 73 ++-- .../net/kemitix/checkstyle-2-naming.xml | 129 +++---- .../net/kemitix/checkstyle-3-javadoc.xml | 157 ++++----- .../net/kemitix/checkstyle-4-tweaks.xml | 249 +++++++------- .../net/kemitix/checkstyle-5-complexity.xml | 315 +++++++++--------- 14 files changed, 683 insertions(+), 475 deletions(-) create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundException.java create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadException.java create mode 100644 builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java create mode 100644 builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java diff --git a/builder/pom.xml b/builder/pom.xml index 1d0ffbf..73294ba 100644 --- a/builder/pom.xml +++ b/builder/pom.xml @@ -42,6 +42,8 @@ 2.3.5 1.0.0 4.3.0 + 7.8 + 1.24.0 @@ -92,6 +94,19 @@ ${map-builder.version} test + + + com.puppycrawl.tools + checkstyle + ${checkstyle.version} + compile + + + com.github.sevntu-checkstyle + sevntu-checks + ${sevntu.version} + + diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundException.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundException.java new file mode 100644 index 0000000..ffa297c --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundException.java @@ -0,0 +1,39 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +/** + * Raised when a Class to implement a Rule is not found. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +public class CheckstyleClassNotFoundException extends RuntimeException { + + /** + * Constructor. + * + * @param name the name of the unimplemented rule + */ + public CheckstyleClassNotFoundException(final String name) { + super("No class found to implement " + name); + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadException.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadException.java new file mode 100644 index 0000000..ef9b959 --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadException.java @@ -0,0 +1,42 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +import java.io.IOException; + +/** + * Raised when there is an error scanning for check classes. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +public class CheckstyleSourceLoadException extends RuntimeException { + + /** + * Constructor. + * + * @param basePackage the base package classes were being loaded from + * @param cause the cause + */ + public CheckstyleSourceLoadException(final String basePackage, final IOException cause) { + super("Error loading checkstyle rules from package: " + basePackage, cause); + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java index 0619a7d..5e3558a 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java @@ -101,9 +101,9 @@ class CheckstyleWriter implements CommandLineRunner { private String formatRuleAsModule(final Rule rule) { if (rule.getProperties() .isEmpty()) { - return String.format("", rule.getName()); + return String.format("", rule.getCanonicalClassName()); } - return String.format("%n %s%n", rule.getName(), + return String.format("%n %s%n", rule.getCanonicalClassName(), formatProperties(rule.getProperties()) ); } diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java index 6e6798e..70f9ae7 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java @@ -40,6 +40,11 @@ public class Rule { private static final Locale LOCALE = Locale.ENGLISH; + /** + * Configuration properties. + */ + private final Map properties = new HashMap<>(); + /** * The name of the rule's Check class. */ @@ -80,11 +85,6 @@ public class Rule { */ private String reason; - /** - * Configuration properties. - */ - private final Map properties = new HashMap<>(); - /** * Compare two Rules lexicographically for sorting by rule name, ignoring case. * @@ -104,4 +104,21 @@ public class Rule { return getName().toLowerCase(LOCALE); } + /** + * Returns the canonical name of the class that implements this rule. + * + * @return the canonical name of the implementing class + */ + public String getCanonicalClassName() { + return source.getCheckClasses() + .filter(this::byRuleName) + .findFirst() + .orElseThrow(() -> new CheckstyleClassNotFoundException(name)); + } + + private boolean byRuleName(final String classname) { + final String classNameWithDelimiter = "." + name; + return classname.endsWith(classNameWithDelimiter) || classname.endsWith(classNameWithDelimiter + "Check"); + } + } diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java index d675310..e10cb95 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java @@ -21,6 +21,14 @@ package net.kemitix.checkstyle.ruleset.builder; +import com.google.common.reflect.ClassPath; +import lombok.Getter; + +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + /** * The origin of the rule. * @@ -28,6 +36,34 @@ package net.kemitix.checkstyle.ruleset.builder; */ public enum RuleSource { - CHECKSTYLE, - SEVNTU, + CHECKSTYLE("com.puppycrawl.tools.checkstyle"), + SEVNTU("com.github.sevntu.checkstyle.checks"); + + @Getter + private final String basePackage; + + private final List checkClasses; + + /** + * Constructor. + * + * @param basePackage the base package that contains all checks from this source + */ + RuleSource(final String basePackage) { + this.basePackage = basePackage; + try { + checkClasses = ClassPath.from(getClass().getClassLoader()) + .getTopLevelClassesRecursive(basePackage) + .stream() + .map(ClassPath.ClassInfo::getName) + .collect(Collectors.toList()); + } catch (IOException e) { + throw new CheckstyleSourceLoadException(basePackage, e); + } + } + + public Stream getCheckClasses() { + return checkClasses.stream() + .sorted(); + } } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java new file mode 100644 index 0000000..eadf0a5 --- /dev/null +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java @@ -0,0 +1,24 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link CheckstyleClassNotFoundException}. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +public class CheckstyleClassNotFoundExceptionTest { + + @Test + public void canRaiseException() { + //given + final String classname = "class name"; + //when + final CheckstyleClassNotFoundException exception = new CheckstyleClassNotFoundException(classname); + //then + assertThat(exception.getMessage()).startsWith("No class found to implement ") + .endsWith(classname); + } +} diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java new file mode 100644 index 0000000..cb3d1b1 --- /dev/null +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java @@ -0,0 +1,28 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import org.junit.Test; + +import java.io.IOException; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +/** + * Tests for {@link CheckstyleSourceLoadException}. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +public class CheckstyleSourceLoadExceptionTest { + + @Test + public void canRaiseException() { + //given + final String basePackage = "base package"; + final IOException cause = new IOException(); + //when + final CheckstyleSourceLoadException exception = new CheckstyleSourceLoadException(basePackage, cause); + //then + assertThat(exception).hasCause(cause) + .hasMessageStartingWith("Error loading checkstyle rules from package: ") + .hasMessageEndingWith(basePackage); + } +} diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java index da7d873..4c7547b 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java @@ -17,7 +17,6 @@ import java.nio.file.StandardOpenOption; import java.util.AbstractMap; import java.util.List; import java.util.Map; -import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @@ -42,6 +41,8 @@ public class CheckstyleWriterTest { private String ruleName; + private String ruleClassname; + private Map outputFiles; private Path outputDirectory; @@ -57,8 +58,8 @@ public class CheckstyleWriterTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - ruleName = UUID.randomUUID() - .toString(); + ruleName = "RegexpOnFilename"; + ruleClassname = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck"; outputProperties = new OutputProperties(); outputFiles = new MapBuilder().put(getOutputFile(RuleLevel.LAYOUT)) .put(getOutputFile(RuleLevel.NAMING)) @@ -91,7 +92,7 @@ public class CheckstyleWriterTest { checkstyleWriter.run(); //then val lines = loadOutputFile(RuleLevel.LAYOUT); - assertThat(lines).containsExactly("C:", String.format("TW:", ruleName)); + assertThat(lines).containsExactly("C:", String.format("TW:", ruleClassname)); } // write rule that is below current level @@ -105,7 +106,7 @@ public class CheckstyleWriterTest { checkstyleWriter.run(); //then val lines = loadOutputFile(RuleLevel.NAMING); - assertThat(lines).containsExactly("C:", String.format("TW:", ruleName)); + assertThat(lines).containsExactly("C:", String.format("TW:", ruleClassname)); } // write rule with checker parent @@ -119,7 +120,7 @@ public class CheckstyleWriterTest { checkstyleWriter.run(); //then val lines = loadOutputFile(RuleLevel.LAYOUT); - assertThat(lines).containsExactly(String.format("C:", ruleName), "TW:"); + assertThat(lines).containsExactly(String.format("C:", ruleClassname), "TW:"); } // write rule with properties @@ -135,7 +136,7 @@ public class CheckstyleWriterTest { checkstyleWriter.run(); //then val lines = loadOutputFile(RuleLevel.LAYOUT); - assertThat(lines).containsExactly("C:", String.format("TW:", ruleName), + assertThat(lines).containsExactly("C:", String.format("TW:", ruleClassname), " ", "" ); } @@ -207,6 +208,7 @@ public class CheckstyleWriterTest { private Rule enabledRule(final RuleLevel level, final RuleParent parent) { val rule = new Rule(); rule.setName(ruleName); + rule.setSource(RuleSource.CHECKSTYLE); rule.setEnabled(true); rule.setLevel(level); rule.setParent(parent); diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml index c7c3f72..7dd6341 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml @@ -4,62 +4,63 @@ "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> - + - - + + - + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - + + + - - - - - - - + + + + + + + - - - - - - - - - + + + + + + + + + + diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml index 040fe94..8388113 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml @@ -4,94 +4,95 @@ "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> - + - - + + - + - + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - + + + + + - - - - + + + + - - - + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml index 811e021..4b469d9 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml @@ -4,120 +4,121 @@ "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> - + - - + + - - + + - - - + + + - - - - - - + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - + + + - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - - + + + + + + - - - - + + + + - - - + + + - - + + - - - - + + + + - - - - - - - - - - - + + + + + + + + + + + + diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml index 2e27c90..a6ad5cc 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml @@ -4,179 +4,180 @@ "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> - + - - + + - - + + - - - + + + - - - - - - + + + + + + - + - - - - - - - - - + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - - - + + + + + + - - - - + + + + - - - + + + - - + + - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - - - - - - + + + + + + + + + - - - - + + + + - - + + - - - - + + + + - - + + - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml index bc2b68b..f486e8c 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml @@ -4,232 +4,233 @@ "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> - + - - - + + + - - + + - - - + + + - - - - - - - + + + + + + + - + - - - - + + + + - + - - - - - - - - + + + + + + + + - - - - - + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - + + + + + + - - - - - + + + + + - - - + + + - + - - + + - - - - - + + + + + - + - - - - - - + + + + + + - - - - - - - - + + + + + + + + - - - - - + + + + + - - + + - - - - - - - - + + + + + + + + - - + + - - - + + + - - - + + + - - - - - - + + + + + + - - - + + + - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 061cc5ff911efa0bbf591a7ae9a9243e8d8098f6 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 30 May 2017 00:02:11 +0100 Subject: [PATCH 6/9] wercker.yml: added --- wercker.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 wercker.yml diff --git a/wercker.yml b/wercker.yml new file mode 100644 index 0000000..b152705 --- /dev/null +++ b/wercker.yml @@ -0,0 +1,6 @@ +box: maven:3.5.0-jdk-8 + +build: + steps: + - xenoterracide/maven: + goals: clean install From 2e8e26c3517874ba4a0ff07a3c207de9e6c3c7b1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 30 May 2017 12:32:29 +0100 Subject: [PATCH 7/9] builder: refactored and improved test coverage --- .../ruleset/builder/BuilderConfiguration.java | 49 ++++++++++++ .../ruleset/builder/CheckstyleWriter.java | 6 +- .../builder/DefaultPackageScanner.java | 47 +++++++++++ .../builder/DefaultRuleClassLocator.java | 77 +++++++++++++++++++ .../ruleset/builder/PackageScanner.java | 34 ++++++++ .../checkstyle/ruleset/builder/Rule.java | 18 ----- .../ruleset/builder/RuleClassLocator.java | 33 ++++++++ .../ruleset/builder/RuleSource.java | 21 ----- .../ruleset/builder/CheckstyleWriterTest.java | 64 ++++++++------- .../builder/DefaultPackageScannerTest.java | 46 +++++++++++ .../builder/DefaultRuleClassLocatorTest.java | 55 +++++++++++++ .../ruleset/builder/RuleSourceTest.java | 12 +++ .../net/kemitix/checkstyle-1-layout.xml | 1 - .../net/kemitix/checkstyle-2-naming.xml | 1 - .../net/kemitix/checkstyle-3-javadoc.xml | 1 - .../net/kemitix/checkstyle-4-tweaks.xml | 1 - .../net/kemitix/checkstyle-5-complexity.xml | 1 - 17 files changed, 393 insertions(+), 74 deletions(-) create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocator.java create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleClassLocator.java create mode 100644 builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java create mode 100644 builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java new file mode 100644 index 0000000..197c38c --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java @@ -0,0 +1,49 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +import com.google.common.reflect.ClassPath; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.IOException; + +/** + * Configuration for Builder. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +@Configuration +public class BuilderConfiguration { + + /** + * Create the ClassPath used to scan packages. + * + * @return the ClassPath + * + * @throws IOException if there is an error + */ + @Bean + public ClassPath classPath() throws IOException { + return ClassPath.from(getClass().getClassLoader()); + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java index 5e3558a..a9df0cd 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java @@ -55,6 +55,8 @@ class CheckstyleWriter implements CommandLineRunner { private final RulesProperties rulesProperties; + private final RuleClassLocator ruleClassLocator; + @Override public void run(final String... args) throws Exception { Stream.of(RuleLevel.values()) @@ -101,9 +103,9 @@ class CheckstyleWriter implements CommandLineRunner { private String formatRuleAsModule(final Rule rule) { if (rule.getProperties() .isEmpty()) { - return String.format("", rule.getCanonicalClassName()); + return String.format("", ruleClassLocator.apply(rule)); } - return String.format("%n %s%n", rule.getCanonicalClassName(), + return String.format("%n %s%n", ruleClassLocator.apply(rule), formatProperties(rule.getProperties()) ); } diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java new file mode 100644 index 0000000..aea4769 --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java @@ -0,0 +1,47 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +import com.google.common.reflect.ClassPath; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.stream.Stream; + +/** + * Default implementation of {@link PackageScanner}. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +@Service +@RequiredArgsConstructor +public class DefaultPackageScanner implements PackageScanner { + + private final ClassPath classPath; + + @Override + public final Stream apply(final RuleSource ruleSource) { + return classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage()) + .stream() + .map(ClassPath.ClassInfo::getName); + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocator.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocator.java new file mode 100644 index 0000000..6a91c6b --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocator.java @@ -0,0 +1,77 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.AbstractMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Default implementation of {@link RuleClassLocator}. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +@Service +@RequiredArgsConstructor +public class DefaultRuleClassLocator implements RuleClassLocator { + + private final PackageScanner packageScanner; + + private Map> checkClasses; + + /** + * Initialise class lists for {@link RuleSource}s. + */ + @PostConstruct + public final void init() { + checkClasses = Stream.of(RuleSource.values()) + .map(source -> new AbstractMap.SimpleEntry<>( + source, packageScanner.apply(source) + .collect(Collectors.toList()))) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + @Override + public final String apply(final Rule rule) { + return getCanonicalClassName(rule.getSource(), rule.getName()); + } + + private String getCanonicalClassName(final RuleSource source, final String name) { + return checkClasses.get(source) + .stream() + .sorted() + .filter(classname -> byRuleName(classname, name)) + .findFirst() + .orElseThrow(() -> new CheckstyleClassNotFoundException(name)); + } + + private boolean byRuleName(final String classname, final String name) { + final String classNameWithDelimiter = "." + name; + return classname.endsWith(classNameWithDelimiter) || classname.endsWith(classNameWithDelimiter + "Check"); + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java new file mode 100644 index 0000000..7cc8c2b --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java @@ -0,0 +1,34 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +import java.util.function.Function; +import java.util.stream.Stream; + +/** + * Scans a package for all classes. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +interface PackageScanner extends Function> { + +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java index 70f9ae7..33c5624 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/Rule.java @@ -103,22 +103,4 @@ public class Rule { private String getLowerCaseRuleName() { return getName().toLowerCase(LOCALE); } - - /** - * Returns the canonical name of the class that implements this rule. - * - * @return the canonical name of the implementing class - */ - public String getCanonicalClassName() { - return source.getCheckClasses() - .filter(this::byRuleName) - .findFirst() - .orElseThrow(() -> new CheckstyleClassNotFoundException(name)); - } - - private boolean byRuleName(final String classname) { - final String classNameWithDelimiter = "." + name; - return classname.endsWith(classNameWithDelimiter) || classname.endsWith(classNameWithDelimiter + "Check"); - } - } diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleClassLocator.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleClassLocator.java new file mode 100644 index 0000000..14b7d50 --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleClassLocator.java @@ -0,0 +1,33 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +import java.util.function.Function; + +/** + * Returns the canonical name of the class that implements a {@link Rule}. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +interface RuleClassLocator extends Function { + +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java index e10cb95..a74871e 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleSource.java @@ -21,14 +21,8 @@ package net.kemitix.checkstyle.ruleset.builder; -import com.google.common.reflect.ClassPath; import lombok.Getter; -import java.io.IOException; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - /** * The origin of the rule. * @@ -42,7 +36,6 @@ public enum RuleSource { @Getter private final String basePackage; - private final List checkClasses; /** * Constructor. @@ -51,19 +44,5 @@ public enum RuleSource { */ RuleSource(final String basePackage) { this.basePackage = basePackage; - try { - checkClasses = ClassPath.from(getClass().getClassLoader()) - .getTopLevelClassesRecursive(basePackage) - .stream() - .map(ClassPath.ClassInfo::getName) - .collect(Collectors.toList()); - } catch (IOException e) { - throw new CheckstyleSourceLoadException(basePackage, e); - } - } - - public Stream getCheckClasses() { - return checkClasses.stream() - .sorted(); } } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java index 4c7547b..a409d3d 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java @@ -6,6 +6,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.io.IOException; @@ -19,6 +20,8 @@ import java.util.List; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; /** * Tests for {@link CheckstyleWriter}. @@ -31,6 +34,12 @@ public class CheckstyleWriterTest { private static final String FILE_SEPARATOR = System.getProperty("file.separator"); + @org.junit.Rule + public ExpectedException exception = ExpectedException.none(); + + @org.junit.Rule + public TemporaryFolder folder = new TemporaryFolder(); + private CheckstyleWriter checkstyleWriter; private OutputProperties outputProperties; @@ -49,11 +58,8 @@ public class CheckstyleWriterTest { private Path checkstyleTemplate; - @org.junit.Rule - public ExpectedException exception = ExpectedException.none(); - - @org.junit.Rule - public TemporaryFolder folder = new TemporaryFolder(); + @Mock + private RuleClassLocator ruleClassLocator; @Before public void setUp() throws Exception { @@ -78,7 +84,14 @@ public class CheckstyleWriterTest { checkstyleTemplate, TEMPLATE.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING); templateProperties.setCheckstyleXml(checkstyleTemplate); rulesProperties = new RulesProperties(); - checkstyleWriter = new CheckstyleWriter(outputProperties, templateProperties, rulesProperties); + checkstyleWriter = + new CheckstyleWriter(outputProperties, templateProperties, rulesProperties, ruleClassLocator); + given(ruleClassLocator.apply(any())).willReturn(ruleClassname); + } + + private Map.Entry getOutputFile(final RuleLevel level) throws IOException { + final String xmlFile = String.format("checkstyle-%s.xml", level.toString()); + return new AbstractMap.SimpleImmutableEntry<>(level, xmlFile); } // write rule that matches current level @@ -95,6 +108,23 @@ public class CheckstyleWriterTest { assertThat(lines).containsExactly("C:", String.format("TW:", ruleClassname)); } + private List loadOutputFile(final RuleLevel level) throws IOException { + val path = outputDirectory.resolve(outputFiles.get(level)); + assertThat(path).as("Output path exists") + .exists(); + return Files.readAllLines(path, StandardCharsets.UTF_8); + } + + private Rule enabledRule(final RuleLevel level, final RuleParent parent) { + val rule = new Rule(); + rule.setName(ruleName); + rule.setSource(RuleSource.CHECKSTYLE); + rule.setEnabled(true); + rule.setLevel(level); + rule.setParent(parent); + return rule; + } + // write rule that is below current level @Test public void writeRuleThatIsBelowCurrentLevel() throws Exception { @@ -192,26 +222,4 @@ public class CheckstyleWriterTest { //when checkstyleWriter.run(); } - - private Map.Entry getOutputFile(final RuleLevel level) throws IOException { - final String xmlFile = String.format("checkstyle-%s.xml", level.toString()); - return new AbstractMap.SimpleImmutableEntry<>(level, xmlFile); - } - - private List loadOutputFile(final RuleLevel level) throws IOException { - val path = outputDirectory.resolve(outputFiles.get(level)); - assertThat(path).as("Output path exists") - .exists(); - return Files.readAllLines(path, StandardCharsets.UTF_8); - } - - private Rule enabledRule(final RuleLevel level, final RuleParent parent) { - val rule = new Rule(); - rule.setName(ruleName); - rule.setSource(RuleSource.CHECKSTYLE); - rule.setEnabled(true); - rule.setLevel(level); - rule.setParent(parent); - return rule; - } } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java new file mode 100644 index 0000000..978200e --- /dev/null +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java @@ -0,0 +1,46 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import com.google.common.reflect.ClassPath; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link DefaultPackageScanner}. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class DefaultPackageScannerTest { + + private ClassPath classPath; + + private DefaultPackageScanner scanner; + + @Before + public void setUp() throws Exception { + classPath = ClassPath.from(getClass().getClassLoader()); + scanner = new DefaultPackageScanner(classPath); + } + + @Test + public void canScanCheckstylePackage() throws IOException { + //when + final Stream result = scanner.apply(RuleSource.CHECKSTYLE); + //then + assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.CHECKSTYLE.getBasePackage())) + .contains("com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck"); + } + + @Test + public void canScanSevntuPackage() throws IOException { + //when + final Stream result = scanner.apply(RuleSource.SEVNTU); + //then + assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.SEVNTU.getBasePackage())) + .contains("com.github.sevntu.checkstyle.checks.design.NestedSwitchCheck"); + } +} diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java new file mode 100644 index 0000000..e8ea62a --- /dev/null +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java @@ -0,0 +1,55 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; + +/** + * Tests for {@link DefaultRuleClassLocator}. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class DefaultRuleClassLocatorTest { + + private DefaultRuleClassLocator subject; + + @Mock + private PackageScanner packageScanner; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + subject = new DefaultRuleClassLocator(packageScanner); + } + + @Test + public void canLookupRule() { + //given + final String rulename = "RegexpOnFilename"; + final String expected = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck"; + final List checkstyleClasses = Collections.singletonList(expected); + final List sevntuClasses = Collections.emptyList(); + given(packageScanner.apply(RuleSource.CHECKSTYLE)).willReturn(checkstyleClasses.stream()); + given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(sevntuClasses.stream()); + subject.init(); + final Rule rule = createCheckstyleRule(rulename); + //when + final String result = subject.apply(rule); + //then + assertThat(result).isEqualTo(expected); + } + + private Rule createCheckstyleRule(final String rulename) { + final Rule rule = new Rule(); + rule.setSource(RuleSource.CHECKSTYLE); + rule.setName(rulename); + return rule; + } +} diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java index 3cf5562..47397a4 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java @@ -30,4 +30,16 @@ public class RuleSourceTest { //then assertThat(values).containsExactlyElementsOf(expected); } + + @Test + public void basePackages() { + //given + final String puppycrawl = "puppycrawl"; + final String sevntu = "sevntu"; + //then + assertThat(RuleSource.CHECKSTYLE.getBasePackage()).contains(puppycrawl) + .doesNotContain(sevntu); + assertThat(RuleSource.SEVNTU.getBasePackage()).contains(sevntu) + .doesNotContain(puppycrawl); + } } diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml index 7dd6341..dcb2199 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml @@ -63,4 +63,3 @@ - diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml index 8388113..a4a70a0 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml @@ -95,4 +95,3 @@ - diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml index 4b469d9..5536268 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-3-javadoc.xml @@ -121,4 +121,3 @@ - diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml index a6ad5cc..4a5e460 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml @@ -180,4 +180,3 @@ - diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml index f486e8c..7181911 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml @@ -233,4 +233,3 @@ - From 1b318253d2a6e863e271d05da36961235922f84e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 30 May 2017 12:43:34 +0100 Subject: [PATCH 8/9] builder: BuilderConfigurationTest: added --- .../builder/BuilderConfigurationTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java new file mode 100644 index 0000000..294cb87 --- /dev/null +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java @@ -0,0 +1,24 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import com.google.common.reflect.ClassPath; +import org.junit.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link BuilderConfiguration}. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class BuilderConfigurationTest { + + @Test + public void canGetClassPath() throws IOException { + //when + final ClassPath classPath = new BuilderConfiguration().classPath(); + //then + assertThat(classPath).isNotNull(); + } +} From f189fb3b846e9fcbce265b8ce50501900fcc5ffc Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 30 May 2017 12:44:33 +0100 Subject: [PATCH 9/9] builder: DefaultPackageScannerTest: make classPath local variable --- .../checkstyle/ruleset/builder/DefaultPackageScannerTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java index 978200e..e0540f2 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java @@ -16,13 +16,11 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class DefaultPackageScannerTest { - private ClassPath classPath; - private DefaultPackageScanner scanner; @Before public void setUp() throws Exception { - classPath = ClassPath.from(getClass().getClassLoader()); + final ClassPath classPath = ClassPath.from(getClass().getClassLoader()); scanner = new DefaultPackageScanner(classPath); }