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
5 changed files with 148 additions and 23 deletions

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;
}
}