Skip to content

Commit 2a8a182

Browse files
committed
Modernize PMD ruleset and bump to 7.24.0
Refactor PMD ruleset to use category-level references with targeted excludes instead of listing individual rules. This reduces boilerplate and makes the ruleset easier to maintain. Changes: - Add @FunctionalInterface to LineReader for clarity - Add explicit UTF-8 charset to InputStreamReader and PrintWriter - Update SimpleMath tests: remove @SuppressWarnings annotation, migrate to MockMvcTester, and clean up imports - Bump PMD version from 7.16.0 to 7.24.0 in libs.versions.toml
1 parent 998a9d2 commit 2a8a182

8 files changed

Lines changed: 45 additions & 141 deletions

File tree

config/pmd/ruleset.xml

Lines changed: 28 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,29 @@
1010
</description>
1111

1212
<!-- Best Practices -->
13-
<rule ref="category/java/bestpractices.xml/AbstractClassWithoutAbstractMethod"/>
14-
<rule ref="category/java/bestpractices.xml/AccessorClassGeneration"/>
15-
<rule ref="category/java/bestpractices.xml/AccessorMethodGeneration"/>
16-
<rule ref="category/java/bestpractices.xml/ArrayIsStoredDirectly"/>
17-
<rule ref="category/java/bestpractices.xml/AvoidMessageDigestField"/>
18-
<rule ref="category/java/bestpractices.xml/AvoidPrintStackTrace"/>
19-
<rule ref="category/java/bestpractices.xml/AvoidReassigningCatchVariables"/>
20-
<rule ref="category/java/bestpractices.xml/AvoidReassigningLoopVariables"/>
21-
<rule ref="category/java/bestpractices.xml/AvoidReassigningParameters"/>
22-
<rule ref="category/java/bestpractices.xml/AvoidStringBufferField"/>
23-
<rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP"/>
24-
<rule ref="category/java/bestpractices.xml/CheckResultSet"/>
25-
<rule ref="category/java/bestpractices.xml/ConstantsInInterface"/>
26-
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitch"/>
27-
<rule ref="category/java/bestpractices.xml/DoubleBraceInitialization"/>
28-
<rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach"/>
29-
<rule ref="category/java/bestpractices.xml/ForLoopVariableCount"/>
30-
<rule ref="category/java/bestpractices.xml/GuardLogStatement"/>
31-
<rule ref="category/java/bestpractices.xml/JUnit5TestShouldBePackagePrivate"/>
32-
<rule ref="category/java/bestpractices.xml/UnitTestAssertionsShouldIncludeMessage"/>
13+
<rule ref="category/java/bestpractices.xml">
14+
<exclude name="UnitTestContainsTooManyAsserts"/> <!--customized below-->
15+
</rule>
3316
<rule ref="category/java/bestpractices.xml/UnitTestContainsTooManyAsserts">
3417
<properties>
3518
<property name="maximumAsserts" value="7"/>
3619
</properties>
3720
</rule>
38-
<rule ref="category/java/bestpractices.xml/LiteralsFirstInComparisons"/>
39-
<rule ref="category/java/bestpractices.xml/LooseCoupling"/>
40-
<rule ref="category/java/bestpractices.xml/MethodReturnsInternalArray"/>
41-
<rule ref="category/java/bestpractices.xml/MissingOverride"/>
42-
<rule ref="category/java/bestpractices.xml/OneDeclarationPerLine"/>
43-
<rule ref="category/java/bestpractices.xml/PreserveStackTrace"/>
44-
<rule ref="category/java/bestpractices.xml/PrimitiveWrapperInstantiation"/>
45-
<rule ref="category/java/bestpractices.xml/ReplaceEnumerationWithIterator"/>
46-
<rule ref="category/java/bestpractices.xml/ReplaceHashtableWithMap"/>
47-
<rule ref="category/java/bestpractices.xml/ReplaceVectorWithList"/>
48-
<rule ref="category/java/bestpractices.xml/SimplifiableTestAssertion"/>
49-
<rule ref="category/java/bestpractices.xml/NonExhaustiveSwitch"/>
50-
<rule ref="category/java/bestpractices.xml/SystemPrintln"/>
51-
<rule ref="category/java/bestpractices.xml/UnnecessaryVarargsArrayCreation"/>
52-
<rule ref="category/java/bestpractices.xml/UnusedAssignment"/>
53-
<rule ref="category/java/bestpractices.xml/UnusedFormalParameter"/>
54-
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/>
55-
<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>
56-
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod"/>
57-
<rule ref="category/java/bestpractices.xml/UseCollectionIsEmpty"/>
58-
<rule ref="category/java/bestpractices.xml/UseEnumCollections"/>
59-
<rule ref="category/java/bestpractices.xml/UseStandardCharsets"/>
60-
<rule ref="category/java/bestpractices.xml/UseTryWithResources"/>
61-
<rule ref="category/java/bestpractices.xml/UseVarargs"/>
62-
<rule ref="category/java/bestpractices.xml/WhileLoopWithLiteralBoolean"/>
6321
<!-- Best Practices -->
6422

6523
<!-- Code Style -->
66-
<rule ref="category/java/codestyle.xml/AvoidDollarSigns"/>
67-
<rule ref="category/java/codestyle.xml/AvoidProtectedFieldInFinalClass"/>
68-
<rule ref="category/java/codestyle.xml/AvoidProtectedMethodInFinalClassNotExtending"/>
69-
<rule ref="category/java/codestyle.xml/AvoidUsingNativeCode"/>
70-
<rule ref="category/java/codestyle.xml/BooleanGetMethodName"/>
71-
<rule ref="category/java/codestyle.xml/ClassNamingConventions"/>
72-
<rule ref="category/java/codestyle.xml/ConfusingTernary"/>
73-
<rule ref="category/java/codestyle.xml/ControlStatementBraces"/>
74-
<rule ref="category/java/codestyle.xml/EmptyControlStatement"/>
75-
<rule ref="category/java/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract"/>
76-
<rule ref="category/java/codestyle.xml/ExtendsObject"/>
77-
<rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass"/>
78-
<rule ref="category/java/codestyle.xml/FieldNamingConventions"/>
79-
<rule ref="category/java/codestyle.xml/FinalParameterInAbstractMethod"/>
80-
<rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop"/>
81-
<rule ref="category/java/codestyle.xml/FormalParameterNamingConventions"/>
82-
<rule ref="category/java/codestyle.xml/GenericsNaming"/>
83-
<rule ref="category/java/codestyle.xml/IdenticalCatchBranches"/>
84-
<rule ref="category/java/codestyle.xml/LambdaCanBeMethodReference"/>
85-
<rule ref="category/java/codestyle.xml/LinguisticNaming"/>
86-
<rule ref="category/java/codestyle.xml/LocalHomeNamingConvention"/>
87-
<rule ref="category/java/codestyle.xml/LocalInterfaceSessionNamingConvention"/>
88-
<rule ref="category/java/codestyle.xml/LocalVariableNamingConventions"/>
24+
<rule ref="category/java/codestyle.xml">
25+
<exclude name="AtLeastOneConstructor"/>
26+
<exclude name="CallSuperInConstructor"/>
27+
<exclude name="CommentDefaultAccessModifier"/>
28+
<exclude name="LocalVariableCouldBeFinal"/>
29+
<exclude name="LongVariable"/> <!--customized below-->
30+
<exclude name="MethodArgumentCouldBeFinal"/>
31+
<exclude name="MethodNamingConventions"/> <!--customized below-->
32+
<exclude name="OnlyOneReturn"/>
33+
<exclude name="ShortClassName"/>
34+
<exclude name="ShortMethodName"/> <!--customized below-->
35+
</rule>
8936
<rule ref="category/java/codestyle.xml/LongVariable">
9037
<properties>
9138
<property name="minimum" value="25"/>
@@ -96,83 +43,37 @@
9643
<property name="junit5TestPattern" value="^[a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
9744
</properties>
9845
</rule>
99-
<rule ref="category/java/codestyle.xml/NoPackage"/>
100-
<rule ref="category/java/codestyle.xml/PackageCase"/>
101-
<rule ref="category/java/codestyle.xml/PrematureDeclaration"/>
10246
<rule ref="category/java/codestyle.xml/ShortMethodName">
10347
<properties>
10448
<property name="minimum" value="2"/>
10549
</properties>
10650
</rule>
107-
<rule ref="category/java/codestyle.xml/ShortVariable"/>
108-
<rule ref="category/java/codestyle.xml/TooManyStaticImports"/>
109-
<rule ref="category/java/codestyle.xml/UnnecessaryAnnotationValueElement"/>
110-
<rule ref="category/java/codestyle.xml/UnnecessaryBoxing"/>
111-
<rule ref="category/java/codestyle.xml/UnnecessaryCast"/>
112-
<rule ref="category/java/codestyle.xml/UnnecessaryConstructor"/>
113-
<rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName"/>
114-
<rule ref="category/java/codestyle.xml/UnnecessaryImport"/>
115-
<rule ref="category/java/codestyle.xml/UnnecessaryLocalBeforeReturn"/>
116-
<rule ref="category/java/codestyle.xml/UnnecessaryModifier"/>
117-
<rule ref="category/java/codestyle.xml/UnnecessaryReturn"/>
118-
<rule ref="category/java/codestyle.xml/UnnecessarySemicolon"/>
119-
<rule ref="category/java/codestyle.xml/UseDiamondOperator"/>
120-
<rule ref="category/java/codestyle.xml/UseExplicitTypes"/>
121-
<rule ref="category/java/codestyle.xml/UselessParentheses"/>
122-
<rule ref="category/java/codestyle.xml/UselessQualifiedThis"/>
123-
<rule ref="category/java/codestyle.xml/UseShortArrayInitializer"/>
124-
<rule ref="category/java/codestyle.xml/UseUnderscoresInNumericLiterals"/>
12551
<!-- Code Style -->
12652

12753
<!-- Design -->
128-
<rule ref="category/java/design.xml/AbstractClassWithoutAnyMethod"/>
129-
<rule ref="category/java/design.xml/AvoidCatchingGenericException"/>
130-
<rule ref="category/java/design.xml/AvoidDeeplyNestedIfStmts"/>
131-
<rule ref="category/java/design.xml/AvoidRethrowingException"/>
132-
<rule ref="category/java/design.xml/AvoidThrowingNewInstanceOfSameException"/>
133-
<rule ref="category/java/design.xml/AvoidThrowingNullPointerException"/>
134-
<rule ref="category/java/design.xml/AvoidThrowingRawExceptionTypes"/>
135-
<rule ref="category/java/design.xml/AvoidUncheckedExceptionsInSignatures"/>
136-
<rule ref="category/java/design.xml/ClassWithOnlyPrivateConstructorsShouldBeFinal"/>
137-
<rule ref="category/java/design.xml/CognitiveComplexity"/>
138-
<rule ref="category/java/design.xml/CollapsibleIfStatements"/>
139-
<rule ref="category/java/design.xml/CouplingBetweenObjects"/>
140-
<rule ref="category/java/design.xml/CyclomaticComplexity"/>
141-
<rule ref="category/java/design.xml/DataClass"/>
142-
<rule ref="category/java/design.xml/DoNotExtendJavaLangError"/>
143-
<rule ref="category/java/design.xml/ExceptionAsFlowControl"/>
144-
<rule ref="category/java/design.xml/ExcessiveImports"/>
145-
<rule ref="category/java/design.xml/ExcessiveParameterList"/>
146-
<rule ref="category/java/design.xml/ExcessivePublicCount"/>
147-
<rule ref="category/java/design.xml/FinalFieldCouldBeStatic"/>
148-
<rule ref="category/java/design.xml/GodClass"/>
149-
<rule ref="category/java/design.xml/ImmutableField"/>
150-
<rule ref="category/java/design.xml/InvalidJavaBean"/>
151-
<rule ref="category/java/design.xml/LawOfDemeter"/>
152-
<rule ref="category/java/design.xml/LogicInversion"/>
153-
<rule ref="category/java/design.xml/MutableStaticState"/>
154-
<rule ref="category/java/design.xml/NcssCount"/>
155-
<rule ref="category/java/design.xml/NPathComplexity"/>
156-
<rule ref="category/java/design.xml/SignatureDeclareThrowsException"/>
157-
<rule ref="category/java/design.xml/SimplifiedTernary"/>
158-
<rule ref="category/java/design.xml/SimplifyBooleanExpressions"/>
159-
<rule ref="category/java/design.xml/SimplifyBooleanReturns"/>
160-
<rule ref="category/java/design.xml/SimplifyConditional"/>
161-
<rule ref="category/java/design.xml/SingularField"/>
162-
<rule ref="category/java/design.xml/SwitchDensity"/>
163-
<rule ref="category/java/design.xml/TooManyFields"/>
54+
<rule ref="category/java/design.xml">
55+
<exclude name="LoosePackageCoupling"/>
56+
<exclude name="TooManyMethods"/> <!--customized below-->
57+
<exclude name="UseUtilityClass"/>
58+
</rule>
16459
<rule ref="category/java/design.xml/TooManyMethods">
16560
<properties>
16661
<property name="maxmethods" value="15"/>
16762
</properties>
16863
</rule>
169-
<rule ref="category/java/design.xml/UselessOverridingMethod"/>
170-
<rule ref="category/java/design.xml/UseObjectForClearerAPI"/>
17164
<!-- Design -->
17265

66+
<!-- Error Prone -->
17367
<rule ref="category/java/errorprone.xml">
68+
<exclude name="AssignmentInOperand"/> <!--customized below-->
17469
<exclude name="AvoidFieldNameMatchingMethodName"/>
17570
</rule>
71+
<rule ref="category/java/errorprone.xml/AssignmentInOperand">
72+
<properties>
73+
<property name="allowIncrementDecrement" value="true"/>
74+
</properties>
75+
</rule>
76+
<!-- Error Prone -->
17677
<rule ref="category/java/multithreading.xml"/>
17778
<rule ref="category/java/performance.xml"/>
17879
<rule ref="category/java/security.xml"/>

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ checkstyle = "13.4.2"
44
jacoco = "0.8.14"
55
javafxplugin = "0.1.0"
66
junit-jupiter = "6.0.3"
7-
pmd = "7.16.0"
7+
pmd = "7.24.0"
88
spring-boot = "4.0.6"
99

1010
[libraries]

line-io/src/main/java/dev/delivercraft/io/InputStreamLineReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.InputStream;
66
import java.io.InputStreamReader;
77
import java.io.UncheckedIOException;
8+
import java.nio.charset.StandardCharsets;
89
import java.util.Objects;
910

1011
/**
@@ -25,7 +26,7 @@ public final class InputStreamLineReader implements LineReader {
2526
*/
2627
public InputStreamLineReader(InputStream inputStream) {
2728
Objects.requireNonNull(inputStream, "inputStream must not be null");
28-
this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
29+
this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
2930
}
3031

3132
/**

line-io/src/main/java/dev/delivercraft/io/LineReader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/**
44
* Reads one line of text.
55
*/
6+
@FunctionalInterface
67
public interface LineReader {
78

89
/**

line-io/src/main/java/dev/delivercraft/io/OutputStreamLineWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.OutputStream;
44
import java.io.PrintWriter;
5+
import java.nio.charset.StandardCharsets;
56
import java.util.Objects;
67

78
/**
@@ -22,7 +23,7 @@ public final class OutputStreamLineWriter implements LineWriter {
2223
*/
2324
public OutputStreamLineWriter(OutputStream outputStream) {
2425
Objects.requireNonNull(outputStream, "outputStream must not be null");
25-
this.printWriter = new PrintWriter(outputStream, true);
26+
this.printWriter = new PrintWriter(outputStream, true, StandardCharsets.UTF_8);
2627
}
2728

2829
/**

line-io/src/test/java/dev/delivercraft/io/InputStreamLineReaderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.IOException;
77
import java.io.InputStream;
88
import java.io.UncheckedIOException;
9+
import java.nio.charset.StandardCharsets;
910

1011
import static org.assertj.core.api.Assertions.assertThat;
1112
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -26,7 +27,7 @@ void readLine_GivenNoInput_ShouldReturnNull() {
2627
@Test
2728
void readLine_GivenInput_ShouldReturnInputLine() {
2829
String input = "Hello, World!\n";
29-
ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes());
30+
ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
3031
LineReader lineReader = new InputStreamLineReader(inputStream);
3132

3233
String result = lineReader.readLine();

simple-math/src/test/java/dev/delivercraft/math/console/PositiveIntegerTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package dev.delivercraft.math.console;
22

3-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
4-
53
import org.assertj.core.api.SoftAssertions;
64
import org.junit.jupiter.api.Test;
75
import org.junit.jupiter.params.ParameterizedTest;
86
import org.junit.jupiter.params.provider.NullAndEmptySource;
97

8+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
9+
1010
class PositiveIntegerTest {
1111

1212
@Test
@@ -32,7 +32,6 @@ void valueMustNotNullOrEmpty(String value) {
3232
}
3333

3434
@Test
35-
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
3635
void encapsulatesAPositiveNumber() {
3736
PositiveInteger positiveInteger = PositiveInteger.of("42");
3837

simple-math/src/test/java/dev/delivercraft/math/gui/ApplicationTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
import org.junit.jupiter.params.provider.ValueSource;
99
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
1010
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.assertj.MockMvcTester;
1112
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
1213
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1314
import org.springframework.web.context.WebApplicationContext;
1415

1516
import java.io.IOException;
1617

1718
import static org.assertj.core.api.Assertions.assertThat;
18-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2019

2120
@WebMvcTest
2221
class ApplicationTest {
@@ -37,20 +36,21 @@ class ApplicationTest {
3736

3837
private static final String DIVISION_ID = "division";
3938

40-
private final MockMvc mockMvc;
39+
private final MockMvcTester mvc;
4140

4241
private final WebClient webClient;
4342

4443
ApplicationTest(WebApplicationContext context) {
45-
this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
44+
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
45+
this.mvc = MockMvcTester.create(mockMvc);
4646
this.webClient = MockMvcWebClientBuilder.webAppContextSetup(context).build();
4747
}
4848

4949
@ParameterizedTest
5050
@ValueSource(strings = {"/", "/index.html"})
51-
void indexFileIsAccessibleImplicitlyOrExplicitly(String path) throws Exception {
52-
this.mockMvc.perform(get(path))
53-
.andExpect(status().isOk());
51+
void indexFileIsAccessibleImplicitlyOrExplicitly(String path) {
52+
assertThat(this.mvc.get().uri(path))
53+
.hasStatusOk();
5454
}
5555

5656
@Test

0 commit comments

Comments
 (0)