From d337266c7a6763600ea6dc91f04ac59643a96cdf Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 10 Apr 2018 23:50:17 +0200 Subject: [PATCH] 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 --- baeldung-pmd-rules.xml | 10 +++ custom-pmd/README.md | 1 + custom-pmd/pom.xml | 43 ++++++++++ .../pmd/UnitTestNamingConventionRule.java | 32 +++++++ pom.xml | 85 ++++++++++++++----- 5 files changed, 148 insertions(+), 23 deletions(-) create mode 100644 baeldung-pmd-rules.xml create mode 100644 custom-pmd/README.md create mode 100644 custom-pmd/pom.xml create mode 100644 custom-pmd/src/main/java/org/baeldung/pmd/UnitTestNamingConventionRule.java diff --git a/baeldung-pmd-rules.xml b/baeldung-pmd-rules.xml new file mode 100644 index 0000000000..7625d68242 --- /dev/null +++ b/baeldung-pmd-rules.xml @@ -0,0 +1,10 @@ + + + Baeldung custom PMD rules + + Test does not follow Baeldung naming convention + 3 + + \ No newline at end of file diff --git a/custom-pmd/README.md b/custom-pmd/README.md new file mode 100644 index 0000000000..065a1128f3 --- /dev/null +++ b/custom-pmd/README.md @@ -0,0 +1 @@ +## Custom PMD Rules diff --git a/custom-pmd/pom.xml b/custom-pmd/pom.xml new file mode 100644 index 0000000000..7438ec0a34 --- /dev/null +++ b/custom-pmd/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + org.baeldung.pmd + custom-pmd + 0.0.1-SNAPSHOT + jar + custom-pmd + http://maven.apache.org + + + UTF-8 + 3.7.0 + 6.0.1 + + + + + net.sourceforge.pmd + pmd-core + ${pmdVersion} + + + net.sourceforge.pmd + pmd-java + ${pmdVersion} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/custom-pmd/src/main/java/org/baeldung/pmd/UnitTestNamingConventionRule.java b/custom-pmd/src/main/java/org/baeldung/pmd/UnitTestNamingConventionRule.java new file mode 100644 index 0000000000..863d3c9348 --- /dev/null +++ b/custom-pmd/src/main/java/org/baeldung/pmd/UnitTestNamingConventionRule.java @@ -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 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; + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 205d4d2e97..ae2efe5161 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 com.baeldung parent-modules @@ -27,7 +28,18 @@ 1.6.0 + + + + org.apache.maven.plugins + maven-jxr-plugin + 2.3 + + + + + custom-pmd parent-boot-5 asm atomix @@ -49,7 +61,7 @@ bootique cdi - + core-java core-java-io core-java-8 @@ -63,12 +75,12 @@ ethereumj - + feign flips - + geotools testing-modules/groovy-spock @@ -88,12 +100,12 @@ httpclient hystrix - + immutables influxdb jackson - + vavr java-lite java-rmi @@ -104,7 +116,7 @@ javafx jgroups jee-7 - + jjwt jpa-storedprocedure jsf @@ -114,9 +126,7 @@ testing-modules/junit-5 jws - + libraries-data linkrest logging-modules/log-mdc @@ -124,9 +134,8 @@ logging-modules/log4j2 logging-modules/logback lombok - mapstruct - + metrics maven mesos-marathon testing-modules/mockito @@ -145,7 +154,6 @@ persistence-modules/querydsl - reactor-core persistence-modules/redis testing-modules/rest-assured @@ -160,7 +168,7 @@ spring-5 spring-5-reactive spring-5-mvc - + spring-5-security spring-activiti spring-akka spring-amqp @@ -199,7 +207,7 @@ spring-integration spring-jenkins-pipeline spring-jersey - + spring-jms spring-jooq persistence-modules/spring-jpa @@ -290,7 +298,7 @@ lucene vraptor persistence-modules/java-cockroachdb - spring-security-thymeleaf + spring-security-thymeleaf persistence-modules/java-jdbi jersey java-spi @@ -354,7 +362,7 @@ - + org.codehaus.mojo exec-maven-plugin @@ -363,7 +371,7 @@ maven - + org.apache.maven.plugins maven-surefire-plugin @@ -380,7 +388,7 @@ - + org.apache.maven.plugins maven-compiler-plugin @@ -390,9 +398,41 @@ 1.8 - + + org.apache.maven.plugins + maven-pmd-plugin + 3.9.0 + + + org.baeldung.pmd + custom-pmd + 0.0.1-SNAPSHOT + + + + 5 + true + false + true + true + true + UTF-8 + 1.8 + + ${user.dir}/baeldung-pmd-rules.xml + + + + + compile + + check + + + + - + com.vackosar.gitflowincrementalbuilder @@ -400,7 +440,6 @@ 3.4 - + -