Merge pull request #23 from kemitix/plugin-unit-tests

Plugin unit tests
This commit is contained in:
Paul Campbell 2017-05-27 12:26:23 +01:00 committed by GitHub
commit d6adfa74c4
26 changed files with 1156 additions and 412 deletions

View file

@ -25,7 +25,7 @@ The ruleset includes checks from both the core Checkstyle library and from the S
To use this ruleset add the plugin `kemitix-checktyle-ruleset-maven-plugin`.
The `maven-checkstyle-plugin` will be included automatically.
The following goals implement increasingly strict rulesets:
The following levels implement increasingly strict rulesets:
* 1-layout
* 2-naming
@ -33,6 +33,13 @@ The following goals implement increasingly strict rulesets:
* 4-tweaks
* 5-complexity
### Change from 2.x
In 2.x, the level was specified as the goal to invoke. In 3.x, there is only the 'check' goal.
The level is now specified as a configuration parameter. See the example below.
### Example
````
<properties>
<kemitix-checkstyle-ruleset.version>2.1.0</kemitix-checkstyle-ruleset.version>
@ -45,11 +52,14 @@ The following goals implement increasingly strict rulesets:
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId>
<version>${kemitix-checkstyle-ruleset.version}</version>
<configuration>
<level>${kemitix-checkstyle-ruleset.level}</level>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>${kemitix-checkstyle-ruleset.level}</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
@ -75,7 +85,7 @@ Rule|Level|Source|Enabled|Suppressable
[AnnotationLocation](#annotationlocation)|layout|checkstyle|Yes|
[AnnotationUseStyle](#annotationusestyle)|layout|checkstyle|Yes|
[AnonInnerLength](#anoninnerlength)|complexity|checkstyle|Yes|
[ArrayTrailingComma](#arraytrailingcomma)|unspecified|checkstyle||
[ArrayTrailingComma](#arraytrailingcomma)|tweaks|checkstyle||
[ArrayTypeStyle](#arraytypestyle)|layout|checkstyle|Yes|
[AtclauseOrder](#atclauseorder)|javadoc|checkstyle|Yes|
[AvoidConditionInversion](#avoidconditioninversion)|complexity|sevntu||
@ -125,7 +135,7 @@ Rule|Level|Source|Enabled|Suppressable
[FileTabCharacter](#filetabcharacter)|layout|checkstyle|Yes|
[FinalClass](#finalclass)|complexity|checkstyle|Yes|
[FinalizeImplementation](#finalizeimplementation)|unspecified|sevntu||
[FinalLocalVariable](#finallocalvariable)|unspecified|checkstyle||
[FinalLocalVariable](#finallocalvariable)|tweaks|checkstyle||
[FinalParameters](#finalparameters)|tweaks|checkstyle|Yes|
[ForbidAnnotation](#forbidannotation)|unspecified|sevntu||
[ForbidCCommentsInMethods](#forbidccommentsinmethods)|layout|sevntu|Yes|

View file

@ -7,12 +7,12 @@
<parent>
<groupId>net.kemitix</groupId>
<artifactId>kemitix-parent</artifactId>
<version>2.4.0</version>
<version>2.8.0</version>
<relativePath/>
</parent>
<artifactId>kemitix-checkstyle-ruleset-builder</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Kemitix Checkstyle Ruleset Builder</name>
@ -37,9 +37,8 @@
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>1.5.1.RELEASE</spring-boot.version>
<lombok.version>1.16.12</lombok.version>
<assertj.version>3.6.2</assertj.version>
<spring-boot.version>1.5.3.RELEASE</spring-boot.version>
<assertj.version>3.8.0</assertj.version>
<mapstream.version>2.3.5</mapstream.version>
<map-builder.version>1.0.0</map-builder.version>
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>

View file

@ -25,7 +25,7 @@ The ruleset includes checks from both the core Checkstyle library and from the S
To use this ruleset add the plugin `kemitix-checktyle-ruleset-maven-plugin`.
The `maven-checkstyle-plugin` will be included automatically.
The following goals implement increasingly strict rulesets:
The following levels implement increasingly strict rulesets:
* 1-layout
* 2-naming
@ -33,6 +33,13 @@ The following goals implement increasingly strict rulesets:
* 4-tweaks
* 5-complexity
### Change from 2.x
In 2.x, the level was specified as the goal to invoke. In 3.x, there is only the 'check' goal.
The level is now specified as a configuration parameter. See the example below.
### Example
````
<properties>
<kemitix-checkstyle-ruleset.version>2.1.0</kemitix-checkstyle-ruleset.version>
@ -45,11 +52,14 @@ The following goals implement increasingly strict rulesets:
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId>
<version>${kemitix-checkstyle-ruleset.version}</version>
<configuration>
<level>${kemitix-checkstyle-ruleset.level}</level>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>${kemitix-checkstyle-ruleset.level}</goal>
<goal>check</goal>
</goals>
</execution>
</executions>

View file

@ -1182,7 +1182,7 @@ rules:
-
name: ArrayTrailingComma
parent: TREEWALKER
level: TWEAK
level: TWEAKS
enabled: false
source: CHECKSTYLE
uri: http://checkstyle.sourceforge.net/config_coding.html#ArrayTrailingComma
@ -1190,7 +1190,7 @@ rules:
-
name: FinalLocalVariable
parent: TREEWALKER
level: TWEAK
level: TWEAKS
enabled: false
source: CHECKSTYLE
uri: http://checkstyle.sourceforge.net/config_coding.html#FinalLocalVariable

View file

@ -8,12 +8,12 @@
<parent>
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-sample-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<relativePath>../sample-parent</relativePath>
</parent>
<artifactId>kemitix-checkstyle-ruleset-plugin-sample</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<name>Kemitix Checkstyle Ruleset Plugin Sample</name>
<description>Sample usage of the Kemitix Checkstyle Ruleset Plugin</description>

View file

@ -7,7 +7,7 @@
<parent>
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId>
@ -19,7 +19,7 @@
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.version>3.3.9</maven.version>
<maven.version>3.5.0</maven.version>
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
<maven-plugin-plugin.version>3.5</maven-plugin-plugin.version>
<maven-plugin-annotations.version>3.5</maven-plugin-annotations.version>
@ -27,9 +27,13 @@
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<mojo-executor.version>2.2.0</mojo-executor.version>
<lombok.version>1.16.12</lombok.version>
<mojo-executor.version>2.3.0</mojo-executor.version>
<lombok.version>1.16.16</lombok.version>
<map-builder.version>1.0.0</map-builder.version>
<jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
<jacoco-class-line-covered-ratio>0.50</jacoco-class-line-covered-ratio>
<jacoco-class-instruction-covered-ratio>0.80</jacoco-class-instruction-covered-ratio>
<jacoco-class-missed-count-maximum>0</jacoco-class-missed-count-maximum>
</properties>
<dependencies>
@ -65,6 +69,16 @@
<artifactId>map-builder</artifactId>
<version>${map-builder.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -96,6 +110,58 @@
<target>1.8</target>
</configuration>
</plugin><!-- maven-compiler-plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<excludes>
<exclude>*HelpMojo</exclude>
</excludes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco-class-line-covered-ratio}</minimum>
</limit>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco-class-instruction-covered-ratio}</minimum>
</limit>
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>${jacoco-class-missed-count-maximum}</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</plugin><!-- jacoco-maven-plugin -->
</plugins>
</build>
<profiles>

View file

@ -1,167 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2016 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.plugin;
import lombok.Setter;
import lombok.val;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.twdata.maven.mojoexecutor.MojoExecutor;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
abstract class AbstractCheckMojo extends AbstractMojo {
private static final String CHECKSTYLE_GROUPID = "com.puppycrawl.tools";
private static final String CHECKSTYLE_ARTIFACTID = "checkstyle";
private static final String SEVNTU_GROUPID = "com.github.sevntu.checkstyle";
private static final String SEVNTU_ARTIFACTID = "sevntu-checkstyle-maven-plugin";
private static final String KEMITIX_GROUPID = "net.kemitix";
private static final String KEMITIX_ARTIFACTID = "kemitix-checkstyle-ruleset";
private static final String APACHE_PLUGIN_GROUPID = "org.apache.maven.plugins";
private static final String APACHE_PLUGIN_ARTIFACTID = "maven-checkstyle-plugin";
private static final String CONFIG_LOCATION = "configLocation";
private static final String SOURCE_DIR = "sourceDirectory";
private String rulesetVersion;
@Setter
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject mavenProject;
@Setter
@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession mavenSession;
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository artifactRepository = null;
@Component
private BuildPluginManager pluginManager = null;
/**
* Execute Checkstyle Check.
*
* @param level The level of config file to use.
*
* @throws MojoExecutionException on execution error
* @throws MojoFailureException on execution failure
*/
final void performCheck(final String level) throws MojoExecutionException, MojoFailureException {
rulesetVersion = mavenProject.getPlugin(KEMITIX_GROUPID + ":" + KEMITIX_ARTIFACTID + "-maven-plugin")
.getVersion();
debug("rulesetVersion: %s", rulesetVersion);
val properties = getProperties();
debug("properties: %s", properties);
// get versions from pom.xml properties
val pluginVersion = getProperty(properties, "maven-checkstyle-plugin.version");
val checkstyleVersion = getProperty(properties, "checkstyle.version");
val sevntuVersion = getProperty(properties, "sevntu.version");
// configure
val checkstylePlugin = getPlugin(pluginVersion, checkstyleVersion, sevntuVersion);
val configuration = MojoExecutor.configuration(
MojoExecutor.element(CONFIG_LOCATION, String.format("net/kemitix/checkstyle-%s.xml", level)),
MojoExecutor.element(SOURCE_DIR, mavenProject.getBuild().getSourceDirectory())
);
val environment = MojoExecutor.executionEnvironment(mavenProject, mavenSession, pluginManager);
// run
info("Running Checkstyle %s (sevntu: %s) with ruleset %s (%s)", checkstyleVersion, sevntuVersion, level,
rulesetVersion
);
MojoExecutor.executeMojo(checkstylePlugin, "check", configuration, environment);
info("Checkstyle complete");
}
private String getProperty(final Properties properties, final String key) {
val property = properties.getProperty(key);
debug(key + ": %s", property);
return property;
}
private Plugin getPlugin(final String pluginVersion, final String checkstyleVersion, final String sevntuVersion) {
// create checkstyle dependencies
val checkstyle = MojoExecutor.dependency(CHECKSTYLE_GROUPID, CHECKSTYLE_ARTIFACTID, checkstyleVersion);
val sevntu = MojoExecutor.dependency(SEVNTU_GROUPID, SEVNTU_ARTIFACTID, sevntuVersion);
val ruleset = MojoExecutor.dependency(KEMITIX_GROUPID, KEMITIX_ARTIFACTID, rulesetVersion);
val dependencies = MojoExecutor.dependencies(checkstyle, sevntu, ruleset);
return MojoExecutor.plugin(APACHE_PLUGIN_GROUPID, APACHE_PLUGIN_ARTIFACTID, pluginVersion, dependencies);
}
private Properties getProperties() throws MojoFailureException {
// load properties from the plugin pom.xml
val pluginArtifactId = KEMITIX_ARTIFACTID + "-parent";
val pluginArtifact = new DefaultArtifact(KEMITIX_GROUPID, pluginArtifactId, rulesetVersion, null, "", null,
new DefaultArtifactHandler("pom")
);
try {
val pomReader = new FileReader(artifactRepository.find(pluginArtifact)
.getFile());
return new MavenXpp3Reader().read(pomReader)
.getProperties();
} catch (XmlPullParserException | IOException e) {
throw new MojoFailureException("Can't load properties from plugin's pom.xml", e);
}
}
private void info(final String format, final Object... args) {
getLog().info(String.format(format, args));
}
private void debug(final String format, final Object... args) {
getLog().debug(String.format(format, args));
}
}

View file

@ -0,0 +1,53 @@
/**
* 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.plugin;
import lombok.Builder;
import lombok.Getter;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.project.MavenProject;
/**
* Configuration for performing a Checkstyle Check.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Getter
@Builder
class CheckConfiguration {
private MavenProject mavenProject;
private MavenSession mavenSession;
private ArtifactRepository artifactRepository;
private BuildPluginManager pluginManager;
private String rulesetVersion;
private String sourceDirectory;
private String level;
}

View file

@ -0,0 +1,102 @@
/*
The MIT License (MIT)
Copyright (c) 2016 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.plugin;
import lombok.Setter;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
/**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE)
public class CheckMojo extends AbstractMojo {
private static final String KEMITIX_GROUPID = "net.kemitix";
private static final String KEMITIX_ARTIFACTID = "kemitix-checkstyle-ruleset";
@Setter
private CheckstyleExecutor checkstyleExecutor =
new DefaultCheckstyleExecutor(new DefaultPluginExecutor(), new MavenXpp3Reader());
@Setter
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject mavenProject;
@Setter
@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession mavenSession;
@Setter
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository artifactRepository;
@Parameter(defaultValue = "5-complexity", readonly = true)
private String level = "5-complexity";
@Setter
@Component
private BuildPluginManager pluginManager;
@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
checkstyleExecutor.setLog(getLog());
CheckConfiguration config = getCheckConfiguration();
checkstyleExecutor.performCheck(config);
}
private CheckConfiguration getCheckConfiguration() {
final Plugin rulesetPlugin = getRulesetPlugin();
final Build build = mavenProject.getBuild();
return CheckConfiguration.builder()
.mavenProject(mavenProject)
.mavenSession(mavenSession)
.artifactRepository(artifactRepository)
.pluginManager(pluginManager)
.rulesetVersion(rulesetPlugin.getVersion())
.sourceDirectory(build.getSourceDirectory())
.level(level)
.build();
}
private Plugin getRulesetPlugin() {
return mavenProject.getPlugin(KEMITIX_GROUPID + ":" + KEMITIX_ARTIFACTID + "-maven-plugin");
}
}

View file

@ -0,0 +1,46 @@
/**
* 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.plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
/**
* Executes the Checkstyle plugin.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
interface CheckstyleExecutor {
/**
* Execute Checkstyle Check.
*
* @param configuration The Configuration
*
* @throws MojoExecutionException on execution error
* @throws MojoFailureException on execution failure
*/
void performCheck(CheckConfiguration configuration) throws MojoExecutionException, MojoFailureException;
void setLog(Log log);
}

View file

@ -1,44 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2016 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.plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset: COMPLEXITY, TWEAKS, JAVADOC, NAMING and LAYOUT.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Mojo(name = "5-complexity", defaultPhase = LifecyclePhase.VALIDATE)
public class ComplexityCheckMojo extends AbstractCheckMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
performCheck("5-complexity");
}
}

View file

@ -0,0 +1,185 @@
/**
* 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.plugin;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.val;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.twdata.maven.mojoexecutor.MojoExecutor;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Properties;
/**
* Default implementation of {@link CheckstyleExecutor}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@RequiredArgsConstructor
public class DefaultCheckstyleExecutor implements CheckstyleExecutor {
private static final String CHECKSTYLE_GROUPID = "com.puppycrawl.tools";
private static final String CHECKSTYLE_ARTIFACTID = "checkstyle";
private static final String SEVNTU_GROUPID = "com.github.sevntu.checkstyle";
private static final String SEVNTU_ARTIFACTID = "sevntu-checkstyle-maven-plugin";
private static final String KEMITIX_GROUPID = "net.kemitix";
private static final String KEMITIX_ARTIFACTID = "kemitix-checkstyle-ruleset";
private static final String APACHE_PLUGIN_GROUPID = "org.apache.maven.plugins";
private static final String APACHE_PLUGIN_ARTIFACTID = "maven-checkstyle-plugin";
private static final String CONFIG_LOCATION = "configLocation";
private static final String SOURCE_DIR = "sourceDirectory";
private static final ArtifactHandler POM_ARTIFACT_HANDLER = new DefaultArtifactHandler("pom");
private static final String PLUGIN_ARTIFACT_ID = KEMITIX_ARTIFACTID + "-parent";
private final PluginExecutor pluginExecutor;
private final MavenXpp3Reader mavenXpp3Reader;
@Setter
@Getter
private Log log;
private String rulesetVersion;
@Override
public final void performCheck(final CheckConfiguration config)
throws MojoExecutionException, MojoFailureException {
rulesetVersion = config.getRulesetVersion();
val properties = getProperties(config.getArtifactRepository());
// configure
val checkstylePlugin = getPlugin(properties);
final String sourceDirectory = config.getSourceDirectory();
final String level = config.getLevel();
info("Ruleset: %s", level);
final String configFile = String.format("net/kemitix/checkstyle-%s.xml", level);
final Xpp3Dom configuration = pluginExecutor.configuration(pluginExecutor.element(CONFIG_LOCATION, configFile),
pluginExecutor.element(SOURCE_DIR, sourceDirectory)
);
final MojoExecutor.ExecutionEnvironment environment = pluginExecutor.executionEnvironment(config);
// run
pluginExecutor.executeMojo(checkstylePlugin, "check", configuration, environment);
info("Checkstyle complete");
}
private String getProperty(final Properties properties, final String key) {
val property = properties.getProperty(key);
debug("%s: %s", key, property);
return property;
}
private Plugin getPlugin(final Properties properties) {
// get versions from pom.xml properties
val pluginVersion = getProperty(properties, "maven-checkstyle-plugin.version");
val checkstyleVersion = getProperty(properties, "checkstyle.version");
val sevntuVersion = getProperty(properties, "sevntu.version");
info("Checkstyle %s (plugin: %s, sevntu: %s, ruleset: %s)", checkstyleVersion,
pluginVersion, sevntuVersion, rulesetVersion
);
// create checkstyle dependencies
val checkstyle = getCheckstyleDependency(checkstyleVersion);
val sevntu = getSevntuDependency(sevntuVersion);
val ruleset = getRulesetDependency();
final List<Dependency> dependencies = pluginExecutor.dependencies(checkstyle, sevntu, ruleset);
return pluginExecutor.plugin(APACHE_PLUGIN_GROUPID, APACHE_PLUGIN_ARTIFACTID, pluginVersion, dependencies);
}
private Dependency getRulesetDependency() {
return pluginExecutor.dependency(KEMITIX_GROUPID, KEMITIX_ARTIFACTID, rulesetVersion);
}
private Dependency getSevntuDependency(final String sevntuVersion) {
return pluginExecutor.dependency(SEVNTU_GROUPID, SEVNTU_ARTIFACTID, sevntuVersion);
}
private Dependency getCheckstyleDependency(final String checkstyleVersion) {
return pluginExecutor.dependency(CHECKSTYLE_GROUPID, CHECKSTYLE_ARTIFACTID, checkstyleVersion);
}
private Properties getProperties(final ArtifactRepository artifactRepository) throws MojoFailureException {
// load properties from the plugin pom.xml
final Artifact pluginArtifact = getPluginArtifact(artifactRepository);
try {
final File pomFile = pluginArtifact.getFile();
return parsePomFile(pomFile).getProperties();
} catch (XmlPullParserException | IOException e) {
throw new MojoFailureException("Can't load properties from plugin's pom.xml", e);
}
}
private Model parsePomFile(final File pomFile) throws IOException, XmlPullParserException {
final Reader pomReader = new FileReader(pomFile);
return mavenXpp3Reader.read(pomReader);
}
private Artifact getPluginArtifact(final ArtifactRepository artifactRepository) {
final Artifact templateArtifact = getRulesetArtifactTemplate();
return artifactRepository.find(templateArtifact);
}
private DefaultArtifact getRulesetArtifactTemplate() {
return new DefaultArtifact(KEMITIX_GROUPID, PLUGIN_ARTIFACT_ID, rulesetVersion, null, "", null,
POM_ARTIFACT_HANDLER
);
}
private void info(final String format, final Object... args) {
getLog().info(String.format(format, args));
}
private void debug(final String format, final Object... args) {
getLog().debug(String.format(format, args));
}
}

View file

@ -0,0 +1,79 @@
/**
* 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.plugin;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.twdata.maven.mojoexecutor.MojoExecutor;
import java.util.List;
/**
* Wrapper for {@link org.twdata.maven.mojoexecutor.MojoExecutor}.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
public final class DefaultPluginExecutor implements PluginExecutor {
@Override
public MojoExecutor.Element element(final String key, final String value) {
return MojoExecutor.element(key, value);
}
@Override
public Xpp3Dom configuration(final MojoExecutor.Element... elements) {
return MojoExecutor.configuration(elements);
}
@Override
public MojoExecutor.ExecutionEnvironment executionEnvironment(final CheckConfiguration config) {
return MojoExecutor.executionEnvironment(
config.getMavenProject(), config.getMavenSession(), config.getPluginManager());
}
@Override
public void executeMojo(
final Plugin plugin, final String goal, final Xpp3Dom configuration,
final MojoExecutor.ExecutionEnvironment env
) throws MojoExecutionException {
MojoExecutor.executeMojo(plugin, goal, configuration, env);
}
@Override
public Dependency dependency(final String groupid, final String artifactid, final String version) {
return MojoExecutor.dependency(groupid, artifactid, version);
}
@Override
public List<Dependency> dependencies(final Dependency... dependencies) {
return MojoExecutor.dependencies(dependencies);
}
@Override
public Plugin plugin(
final String groupid, final String artifactid, final String version, final List<Dependency> dependencies
) {
return MojoExecutor.plugin(groupid, artifactid, version, dependencies);
}
}

View file

@ -1,44 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2016 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.plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset: JAVADOC, NAMING and LAYOUT.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Mojo(name = "3-javadoc", defaultPhase = LifecyclePhase.VALIDATE)
public class JavadocCheckMojo extends AbstractCheckMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
performCheck("3-javadoc");
}
}

View file

@ -1,44 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2016 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.plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset: LAYOUT.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Mojo(name = "1-layout", defaultPhase = LifecyclePhase.VALIDATE)
public class LayoutCheckMojo extends AbstractCheckMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
performCheck("1-layout");
}
}

View file

@ -1,44 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2016 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.plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Rulesets: NAMING and LAYOUT.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Mojo(name = "2-naming", defaultPhase = LifecyclePhase.VALIDATE)
public class NamingCheckMojo extends AbstractCheckMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
performCheck("2-naming");
}
}

View file

@ -0,0 +1,114 @@
/**
* 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.plugin;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.twdata.maven.mojoexecutor.MojoExecutor;
import java.util.List;
/**
* Interface for wrapping calls to static methods on {@link org.twdata.maven.mojoexecutor.MojoExecutor}.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
public interface PluginExecutor {
/**
* Constructs the element with a textual body.
*
* @param name The element name
* @param value The element text value
*
* @return The element object
*/
MojoExecutor.Element element(String name, String value);
/**
* Builds the configuration for the goal using Elements.
*
* @param elements A list of elements for the configuration section
*
* @return The elements transformed into the Maven-native XML format
*/
Xpp3Dom configuration(MojoExecutor.Element... elements);
/**
* Constructs the {@link MojoExecutor.ExecutionEnvironment} instance fluently.
*
* @param configuration The Configuration
*
* @return The execution environment
*/
MojoExecutor.ExecutionEnvironment executionEnvironment(CheckConfiguration configuration);
/**
* Entry point for executing a mojo.
*
* @param plugin The plugin to execute
* @param goal The goal to execute
* @param configuration The execution configuration
* @param env The execution environment
*
* @throws MojoExecutionException If there are any exceptions locating or executing the mojo
*/
void executeMojo(
Plugin plugin, String goal, Xpp3Dom configuration, MojoExecutor.ExecutionEnvironment env
) throws MojoExecutionException;
/**
* Defines a dependency.
*
* @param groupId The group id
* @param artifactId The artifact id
* @param version The plugin version
*
* @return the dependency instance
*/
Dependency dependency(String groupId, String artifactId, String version);
/**
* Creates a list of dependencies.
*
* @param dependencies the dependencies
*
* @return A list of dependencies
*/
List<Dependency> dependencies(Dependency... dependencies);
/**
* Defines a plugin.
*
* @param groupId The group id
* @param artifactId The artifact id
* @param version The plugin version
* @param dependencies The plugin dependencies
*
* @return The plugin instance
*/
Plugin plugin(
String groupId, String artifactId, String version, List<Dependency> dependencies
);
}

View file

@ -1,44 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2016 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.plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset: TWEAKS, JAVADOC, NAMING and LAYOUT.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Mojo(name = "4-tweaks", defaultPhase = LifecyclePhase.VALIDATE)
public class TweaksCheckMojo extends AbstractCheckMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
performCheck("4-tweaks");
}
}

View file

@ -0,0 +1,27 @@
package net.kemitix.checkstyle.ruleset.plugin;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Paul Campbell (pcampbell@kemitix.net).
*/
public class CheckConfigurationTest {
@Test
public void toStringCoverage() {
//given
final String rulesetVersion = "ruleset version";
final String sourceDirectory = "source directory";
//when
final String result = CheckConfiguration.builder()
.rulesetVersion(rulesetVersion)
.sourceDirectory(sourceDirectory)
.toString();
//then
System.out.println("result = " + result);
assertThat(result).contains("rulesetVersion=ruleset version")
.contains("sourceDirectory=source directory");
}
}

View file

@ -0,0 +1,80 @@
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Matchers.any;
/**
* Tests for {@link CheckMojo}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class CheckMojoTest {
@Mock
private CheckstyleExecutor checkstyleExecutor;
@Mock
private MavenProject mavenProject;
@Mock
private MavenSession mavenSession;
@Mock
private ArtifactRepository artifactRepository;
@Mock
private BuildPluginManager pluginManager;
@Captor
private ArgumentCaptor<CheckConfiguration> configuration;
@Mock
private Plugin rulesetPlugin;
@Mock
private Build build;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void canExecute() throws MojoFailureException, MojoExecutionException {
//given
final CheckMojo mojo = new CheckMojo();
mojo.setCheckstyleExecutor(checkstyleExecutor);
mojo.setMavenProject(mavenProject);
mojo.setMavenSession(mavenSession);
mojo.setArtifactRepository(artifactRepository);
mojo.setPluginManager(pluginManager);
given(mavenProject.getPlugin(any())).willReturn(rulesetPlugin);
given(mavenProject.getBuild()).willReturn(build);
//when
mojo.execute();
//then
then(checkstyleExecutor).should()
.performCheck(configuration.capture());
assertThat(configuration.getValue()).returns(mavenProject, CheckConfiguration::getMavenProject)
.returns(mavenSession, CheckConfiguration::getMavenSession)
.returns(artifactRepository, CheckConfiguration::getArtifactRepository)
.returns(pluginManager, CheckConfiguration::getPluginManager);
}
}

View file

@ -0,0 +1,163 @@
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.twdata.maven.mojoexecutor.MojoExecutor;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
/**
* Tests for {@link DefaultCheckstyleExecutor}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DefaultCheckstyleExecutorTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Mock
private PluginExecutor pluginExecutor;
@Mock
private MavenProject mavenProject;
@Mock
private MavenSession mavenSession;
@Mock
private ArtifactRepository artifactRepository;
@Mock
private BuildPluginManager pluginManager;
@Mock
private Plugin rulesetPlugin;
@Mock
private Log log;
private DefaultCheckstyleExecutor checkstyleExecutor;
private String level;
@Mock
private Artifact artifact;
private File artifactFile;
@Mock
private MavenXpp3Reader mavenXpp3Reader;
@Mock
private Model pomModel;
@Mock
private MojoExecutor.Element configElement;
@Mock
private MojoExecutor.Element sourceElement;
@Mock
private Xpp3Dom pluginConfig;
@Mock
private MojoExecutor.ExecutionEnvironment env;
@Mock
private Dependency checkstyle;
@Mock
private Dependency sevntu;
@Mock
private Dependency ruleset;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
level = "test level";
checkstyleExecutor = new DefaultCheckstyleExecutor(pluginExecutor, mavenXpp3Reader);
checkstyleExecutor.setLog(log);
}
@Test
public void canPerformCheck()
throws MojoFailureException, MojoExecutionException, IOException, XmlPullParserException {
//given
final CheckConfiguration configuration = CheckConfiguration.builder()
.mavenProject(mavenProject)
.mavenSession(mavenSession)
.artifactRepository(artifactRepository)
.pluginManager(pluginManager)
.rulesetVersion("ruleset version")
.level(level)
.build();
artifactFile = folder.newFile("pom.xml");
given(artifactRepository.find(any())).willReturn(artifact);
given(artifact.getFile()).willReturn(artifactFile);
given(mavenXpp3Reader.read(any(FileReader.class))).willReturn(pomModel);
final Properties properties = new Properties();
given(pomModel.getProperties()).willReturn(properties);
given(pluginExecutor.element(eq("configLocation"), any())).willReturn(configElement);
given(pluginExecutor.element(eq("sourceDirectory"), any())).willReturn(sourceElement);
given(pluginExecutor.configuration(configElement, sourceElement)).willReturn(pluginConfig);
given(pluginExecutor.executionEnvironment(configuration)).willReturn(env);
final List<Dependency> dependencies = new ArrayList<>();
dependencies.add(checkstyle);
dependencies.add(sevntu);
dependencies.add(ruleset);
final String pluginVersion = "plugin version";
final String checkstyleVersion = "checkstyle version";
final String sevntuVersion = "sevntu version";
final String rulesetVersion = "ruleset version";
given(pluginExecutor.dependencies(checkstyle, sevntu, ruleset)).willReturn(dependencies);
given(pluginExecutor.dependency("com.puppycrawl.tools", "checkstyle", checkstyleVersion)).willReturn(
checkstyle);
given(pluginExecutor.dependency(
"com.github.sevntu.checkstyle", "sevntu-checkstyle-maven-plugin", sevntuVersion)).willReturn(sevntu);
given(pluginExecutor.dependency("net.kemitix", "kemtiix-checkstyle-ruleset", rulesetVersion)).willReturn(
ruleset);
properties.setProperty("maven-checkstyle-plugin.version", pluginVersion);
properties.setProperty("checkstyle.version", checkstyleVersion);
properties.setProperty("sevntu.version", sevntuVersion);
given(pluginExecutor.plugin(
eq("org.apache.maven.plugins"), eq("maven-checkstyle-plugin"), eq(pluginVersion), any())).willReturn(
rulesetPlugin);
//when
checkstyleExecutor.performCheck(configuration);
//then
then(pluginExecutor).should()
.executeMojo(rulesetPlugin, "check", pluginConfig, env);
}
}

View file

@ -0,0 +1,177 @@
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.InvalidPluginDescriptorException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.PluginConfigurationException;
import org.apache.maven.plugin.PluginDescriptorParsingException;
import org.apache.maven.plugin.PluginManagerException;
import org.apache.maven.plugin.PluginNotFoundException;
import org.apache.maven.plugin.PluginResolutionException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.twdata.maven.mojoexecutor.MojoExecutor;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
/**
* Tests for {@link DefaultPluginExecutor}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DefaultPluginExecutorTest {
private PluginExecutor pluginExecutor;
@Mock
private MavenProject mavenProject;
@Mock
private MavenSession mavenSession;
@Mock
private BuildPluginManager pluginManager;
@Mock
private Plugin plugin;
@Mock
private Xpp3Dom configuration;
@Mock
private PluginDescriptor pluginDescriptor;
@Mock
private MojoDescriptor mojoDescriptor;
@Mock
private PlexusConfiguration mojoConfiguration;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
pluginExecutor = new DefaultPluginExecutor();
}
@Test
public void canElement() {
//when
final MojoExecutor.Element element = pluginExecutor.element("name", "value");
//then
assertThat(element.toDom()).returns("name", Xpp3Dom::getName)
.returns("value", Xpp3Dom::getValue);
}
@Test
public void canConfiguration() {
//given
final MojoExecutor.Element alpha = pluginExecutor.element("alpha", "first");
final MojoExecutor.Element beta = pluginExecutor.element("beta", "second");
//when
final Xpp3Dom configuration = pluginExecutor.configuration(alpha, beta);
//then
assertThat(configuration.getChild("alpha")).returns("alpha", Xpp3Dom::getName)
.returns("first", Xpp3Dom::getValue);
assertThat(configuration.getChild("beta")).returns("beta", Xpp3Dom::getName)
.returns("second", Xpp3Dom::getValue);
}
@Test
public void canExecutionEnvironment() {
//given
final CheckConfiguration configuration = createCheckConfiguration();
//when
final MojoExecutor.ExecutionEnvironment environment = pluginExecutor.executionEnvironment(configuration);
//then
assertThat(environment).returns(mavenProject, MojoExecutor.ExecutionEnvironment::getMavenProject)
.returns(mavenSession, MojoExecutor.ExecutionEnvironment::getMavenSession)
.returns(pluginManager, MojoExecutor.ExecutionEnvironment::getPluginManager);
}
private CheckConfiguration createCheckConfiguration() {
return CheckConfiguration.builder()
.mavenProject(mavenProject)
.mavenSession(mavenSession)
.pluginManager(pluginManager)
.build();
}
@Test
public void canExecuteMojo()
throws MojoExecutionException, InvalidPluginDescriptorException, PluginResolutionException,
PluginDescriptorParsingException, PluginNotFoundException, PluginManagerException,
PluginConfigurationException, MojoFailureException {
//given
final String goal = "check";
final MojoExecutor.ExecutionEnvironment environment =
pluginExecutor.executionEnvironment(createCheckConfiguration());
given(mavenSession.getCurrentProject()).willReturn(mavenProject);
given(pluginManager.loadPlugin(eq(plugin), any(), any())).willReturn(pluginDescriptor);
given(pluginDescriptor.getMojo(goal)).willReturn(mojoDescriptor);
given(mojoDescriptor.getMojoConfiguration()).willReturn(mojoConfiguration);
given(mojoConfiguration.getAttributeNames()).willReturn(new String[]{});
given(mojoConfiguration.getChildren()).willReturn(new PlexusConfiguration[]{});
//when
pluginExecutor.executeMojo(plugin, goal, configuration, environment);
//then
then(pluginManager).should()
.executeMojo(eq(mavenSession), any());
}
@Test
public void canDependency() {
//given
final String groupId = "groupId";
final String artifactId = "artifactId";
final String version = "version";
//when
final Dependency dependency = pluginExecutor.dependency(groupId, artifactId, version);
//then
assertThat(dependency).returns(groupId, Dependency::getGroupId)
.returns(artifactId, Dependency::getArtifactId)
.returns(version, Dependency::getVersion);
}
@Test
public void canDependencies() {
//given
final Dependency dependency = pluginExecutor.dependency("groupId", "artifactId", "version");
//when
final List<Dependency> dependencies = pluginExecutor.dependencies(dependency);
//then
assertThat(dependencies).containsExactly(dependency);
}
@Test
public void canPlugin() {
//given
final String groupId = "groupId";
final String artifactId = "artifactId";
final String version = "version";
final List<Dependency> dependencies = pluginExecutor.dependencies(pluginExecutor.dependency("pg", "pa", "pv"));
//when
final Plugin plugin = pluginExecutor.plugin(groupId, artifactId, version, dependencies);
//then
assertThat(plugin).returns(groupId, Plugin::getGroupId)
.returns(artifactId, Plugin::getArtifactId)
.returns(version, Plugin::getVersion)
.returns(dependencies, Plugin::getDependencies);
}
}

19
pom.xml
View file

@ -6,7 +6,7 @@
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Kemitix Checkstyle Ruleset (Parent)</name>
@ -23,6 +23,8 @@
<maven-checkstyle-plugin.version>2.17</maven-checkstyle-plugin.version>
<checkstyle.version>7.6.1</checkstyle.version>
<sevntu.version>1.23.1</sevntu.version>
<mockito.version>1.10.19</mockito.version>
<assertj.version>3.8.0</assertj.version>
</properties>
<modules>
@ -133,6 +135,21 @@
</profile><!-- release -->
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>

View file

@ -5,7 +5,7 @@
<parent>
<artifactId>kemitix-checkstyle-ruleset-sample-parent</artifactId>
<groupId>net.kemitix</groupId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<relativePath>../sample-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View file

@ -7,11 +7,11 @@
<parent>
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>kemitix-checkstyle-ruleset</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Kemitix Checkstyle Ruleset</name>

View file

@ -8,7 +8,7 @@
<artifactId>kemitix-checkstyle-ruleset-sample-parent</artifactId>
<packaging>pom</packaging>
<version>2.4.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<name>Kemitix Checkstyle Ruleset Sample Parent</name>
<description>Sample parent for modules that use kemitix-checkstyle-ruleset-maven-plugin</description>
@ -51,11 +51,14 @@
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<level>${kemitix-checkstyle-ruleset-level}</level>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>${kemitix-checkstyle-ruleset-level}</goal>
<goal>check</goal>
</goals>
</execution>
</executions>