Fail the build when naming conventions violated (#3974)

* Failing build if unit test name does not follow one of the following conventions : ..IntegrationTest, ..LongRunningUnitTest, ..ManualTest, ..JdbcTest, ..LiveTest

* Custom PMD Rules project

* Fixed this issue when unit test classes were not being scanned by custom PMD rule (This happened after I pulled code from upstream project)

* Don't fail the build if PMD custom rule fails

* Reconfigure PMD

* Reformat POM

* Adjust PMD rules

* Rename test
This commit is contained in:
Grzegorz Piwowarek 2018-04-10 23:50:17 +02:00 committed by GitHub
parent ecca8c9a89
commit d337266c7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 23 deletions

10
baeldung-pmd-rules.xml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset name="Baeldung custom pmd rules" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Baeldung custom PMD rules</description>
<rule name="UnitTestMustFollowNamingConventionRule" message="Test class name doesn't follow the naming convention" class="org.baeldung.pmd.UnitTestNamingConventionRule">
<description>Test does not follow Baeldung naming convention</description>
<priority>3</priority>
</rule>
</ruleset>

1
custom-pmd/README.md Normal file
View File

@ -0,0 +1 @@
## Custom PMD Rules

43
custom-pmd/pom.xml Normal file
View File

@ -0,0 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung.pmd</groupId>
<artifactId>custom-pmd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>custom-pmd</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<pmdVersion>6.0.1</pmdVersion>
</properties>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${pmdVersion}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${pmdVersion}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,32 @@
package org.baeldung.pmd;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class UnitTestNamingConventionRule extends AbstractJavaRule {
private static List<String> allowedEndings = Arrays.asList(
"IntegrationTest",
"LongRunningUnitTest",
"ManualTest",
"JdbcTest",
"LiveTest");
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
String className = node.getImage();
Objects.requireNonNull(className);
if (className.endsWith("Test") || className.endsWith("Tests")) {
if (allowedEndings.stream()
.noneMatch(className::endsWith)) {
addViolation(data, node);
}
}
return data;
}
}

85
pom.xml
View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
@ -27,7 +28,18 @@
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
</properties>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</reporting>
<modules>
<module>custom-pmd</module>
<module>parent-boot-5</module>
<module>asm</module>
<module>atomix</module>
@ -49,7 +61,7 @@
<module>bootique</module>
<module>cdi</module>
<!-- <module>core-java-9</module> -->
<!--<module>core-java-9</module>-->
<module>core-java</module>
<module>core-java-io</module>
<module>core-java-8</module>
@ -63,12 +75,12 @@
<module>ethereumj</module>
<!--<module>ejb</module> -->
<!--<module>ejb</module>-->
<module>feign</module>
<module>flips</module>
<!-- <module>testing-modules/gatling</module> --> <!-- not meant to run as part of the standard build -->
<!--<module>testing-modules/gatling</module> -->
<module>geotools</module>
<module>testing-modules/groovy-spock</module>
@ -88,12 +100,12 @@
<module>httpclient</module>
<module>hystrix</module>
<!-- <module>image-processing</module> --> <!-- TODO: to add back -->
<!--<module>image-processing</module>-->
<module>immutables</module>
<module>influxdb</module>
<module>jackson</module>
<!-- <module>persistence-modules/java-cassandra</module> -->
<!--<module>persistence-modules/java-cassandra</module>-->
<module>vavr</module>
<module>java-lite</module>
<module>java-rmi</module>
@ -104,7 +116,7 @@
<module>javafx</module>
<module>jgroups</module>
<module>jee-7</module>
<!-- <module>jhipster/jhipster-monolithic</module> -->
<!--<module>jhipster/jhipster-monolithic</module>-->
<module>jjwt</module>
<module>jpa-storedprocedure</module>
<module>jsf</module>
@ -114,9 +126,7 @@
<module>testing-modules/junit-5</module>
<module>jws</module>
<!--
<module>libraries</module>
-->
<!--<module>libraries</module>-->
<module>libraries-data</module>
<module>linkrest</module>
<module>logging-modules/log-mdc</module>
@ -124,9 +134,8 @@
<module>logging-modules/log4j2</module>
<module>logging-modules/logback</module>
<module>lombok</module>
<!-- <module>kotlin</module> -->
<module>mapstruct</module>
<!-- <module>metrics</module> -->
<module>metrics</module>
<module>maven</module>
<module>mesos-marathon</module>
<module>testing-modules/mockito</module>
@ -145,7 +154,6 @@
<module>persistence-modules/querydsl</module>
<!-- <module>raml</module> -->
<module>reactor-core</module>
<module>persistence-modules/redis</module>
<module>testing-modules/rest-assured</module>
@ -160,7 +168,7 @@
<module>spring-5</module>
<module>spring-5-reactive</module>
<module>spring-5-mvc</module>
<!-- <module>spring-5-security</module> --> <!-- TODO: uncomment -->
<module>spring-5-security</module>
<module>spring-activiti</module>
<module>spring-akka</module>
<module>spring-amqp</module>
@ -199,7 +207,7 @@
<module>spring-integration</module>
<module>spring-jenkins-pipeline</module>
<module>spring-jersey</module>
<!-- <module>jmeter</module> --> <!-- TODO: uncomment -->
<!--<module>jmeter</module> -->
<module>spring-jms</module>
<module>spring-jooq</module>
<module>persistence-modules/spring-jpa</module>
@ -290,7 +298,7 @@
<module>lucene</module>
<module>vraptor</module>
<module>persistence-modules/java-cockroachdb</module>
<module>spring-security-thymeleaf</module>
<module>spring-security-thymeleaf</module>
<module>persistence-modules/java-jdbi</module>
<module>jersey</module>
<module>java-spi</module>
@ -354,7 +362,7 @@
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
@ -363,7 +371,7 @@
<executable>maven</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
@ -380,7 +388,7 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -390,9 +398,41 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.9.0</version>
<dependencies>
<dependency>
<groupId>org.baeldung.pmd</groupId>
<artifactId>custom-pmd</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<configuration>
<failurePriority>5</failurePriority> <!-- TODO change to 0 after fixing the project -->
<aggregate>true</aggregate>
<failOnViolation>false</failOnViolation>
<verbose>true</verbose>
<linkXRef>true</linkXRef>
<includeTests>true</includeTests>
<sourceEncoding>UTF-8</sourceEncoding>
<targetJdk>1.8</targetJdk>
<rulesets>
<ruleset>${user.dir}/baeldung-pmd-rules.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>com.vackosar.gitflowincrementalbuilder</groupId>
@ -400,7 +440,6 @@
<version>3.4</version>
</extension>
</extensions>
</build>
</project>