Skip to content

Commit

Permalink
chore: Fix PMD errorprone rules (#66)
Browse files Browse the repository at this point in the history
* chore: Fix PMD errorprone rules - AssignmentInOperand

* chore: Fix PMD errorprone rules - AvoidDuplicateLiterals

* chore: Fix PMD errorprone rules - AvoidFieldNameMatchingTypeName

* chore: Fix PMD errorprone rules - CloseResource

* chore: minor code enhancement

* chore: Fix PMD errorprone rules - TestClassWithoutTestCases

* chore: Fix PMD errorprone rules - TestClassWithoutTestCases

* chore: Fix PMD errorprone rules - NullAssignment
  • Loading branch information
melbeltagy authored Oct 4, 2024
1 parent 31d4832 commit 71a3be0
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 22 deletions.
18 changes: 12 additions & 6 deletions config/pmd/custom-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,21 @@
</rule>

<rule ref="category/java/errorprone.xml">
<exclude name="AssignmentInOperand"/>
<exclude name="AvoidDuplicateLiterals"/>
<exclude name="AvoidFieldNameMatchingTypeName"/>
<exclude name="AvoidLiteralsInIfCondition"/>
<exclude name="CloseResource"/>
<exclude name="MissingSerialVersionUID"/>
<exclude name="NullAssignment"/>
<exclude name="TestClassWithoutTestCases"/>
</rule>
<rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals">
<properties>
<property name="maxDuplicateLiterals" value="10" />
<property name="skipAnnotations" value="true" />
</properties>
</rule>
<rule ref="category/java/errorprone.xml/CloseResource">
<properties>
<property name="allowedResourceTypes" value="java.io.ByteArrayOutputStream,java.io.ByteArrayInputStream,java.io.StringWriter,java.io.CharArrayWriter,java.util.stream.Stream,java.util.stream.IntStream,java.util.stream.LongStream,java.util.stream.DoubleStream,java.util.concurrent.ScheduledExecutorService" />
</properties>
</rule>


<rule ref="category/java/multithreading.xml">
<exclude name="AvoidSynchronizedAtMethodLevel"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -610,15 +608,10 @@ private static <T> T getResponseBody(String url, Class<T> returnType) {
return response.getBody();
}

@SuppressWarnings("PMD.CloseResource")
private static RestTemplate createTemplate() {
try {
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s)
throws CertificateException {
return true;
}
};
TrustStrategy acceptingTrustStrategy = (x509Certificates, s) -> true;
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public String getWsdl(XRoadServiceIdentifierType service, CatalogService catalog

if (!(wsdl.value instanceof byte[])) {
DataHandler dh = null;
@SuppressWarnings("PMD.CloseResource")
final Client client = ClientProxy.getClient(metaServicesPort);
@SuppressWarnings("unchecked") final Collection<Attachment> attachments = (Collection<Attachment>) client.getResponseContext()
.get(Message.ATTACHMENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,21 @@ public ResourceToHttpHandler(String fileName) {
this.fileName = fileName;
}

@SuppressWarnings("PMD.AssignmentInOperand")
@Override
public void handle(HttpExchange exchange) throws IOException {
ClassPathResource resource = new ClassPathResource(fileName);
InputStream is = resource.getInputStream();
exchange.getResponseHeaders().add("Content-Type", "application/xml");
exchange.sendResponseHeaders(200, 0);
OutputStream os = exchange.getResponseBody();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
try (InputStream is = resource.getInputStream();
OutputStream os = exchange.getResponseBody()) {

byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
}
os.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
@SpringBootTest(classes = CollectorApplication.class)
@ActiveProfiles("test")
@Transactional
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class CompanyServiceTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
@SpringBootTest(classes = CollectorApplication.class)
@ActiveProfiles("test")
@Transactional
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class OrganizationServiceTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;

@Component
@SuppressWarnings("PMD.TestClassWithoutTestCases")
public class TestUtil {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

@Configuration
@Slf4j
@SuppressWarnings("PMD.TestClassWithoutTestCases")
public class TestingConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

@SpringBootTest(classes = CollectorApplication.class)
@ActiveProfiles("test")
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class ListClientsTaskTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
@EqualsAndHashCode
@Getter
@Setter
@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName")
public class Email implements Serializable {

private static final long serialVersionUID = 4049961366368846285L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@EqualsAndHashCode
@Getter
@Setter
@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName")
public class MemberDataList implements Serializable {

private static final long serialVersionUID = 4049561366368846285L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
@EqualsAndHashCode
@Getter
@Setter
@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName")
public class SecurityServerDataList implements Serializable {

private static final long serialVersionUID = 4049561366368846285L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
*/
@Component
@Transactional
@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName")
public class CatalogServiceImpl implements CatalogService {

private static final String MULTIPLE_MATCHES_FOUND_TO = "multiple matches found to ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
@SpringBootTest(classes = ListerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class ApplicationTests {

private final Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.stream.StreamSupport;

@Component
@SuppressWarnings("PMD.TestClassWithoutTestCases")
public final class TestUtil {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
@SpringBootTest(classes = ListerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = { "xroad-catalog.shared-params-file=src/test/resources/shared-params.xml" })
@ActiveProfiles("test")
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class ServiceControllerTests {

private static final String XROAD_INSTANCE = "DEV";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
@ToString(exclude = { "company" })
@EqualsAndHashCode(exclude = { "id", "company", "statusInfo" })
@Builder
@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName")
public class Language {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public StatusInfo() {
// Empty contructor
}

@SuppressWarnings("PMD.NullAssignment")
public void setTimestampsForSaved(LocalDateTime timestamp, boolean isModified) {
if (removed != null) {
changed = timestamp;
Expand All @@ -74,6 +75,7 @@ public boolean isRemoved() {
return removed != null;
}

@SuppressWarnings("PMD.NullAssignment")
public void setTimestampsForFetched(LocalDateTime timestamp) {
if (isRemoved()) {
// resurrect this item
Expand All @@ -86,6 +88,7 @@ public void setTimestampsForFetched(LocalDateTime timestamp) {
}
}

@SuppressWarnings("PMD.NullAssignment")
public void setTimestampsForNew(LocalDateTime timestamp) {
created = timestamp;
changed = timestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.stream.StreamSupport;

@Component
@SuppressWarnings("PMD.TestClassWithoutTestCases")
public class TestUtil {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

@SpringBootTest
@ActiveProfiles("test")
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class ServiceRepositoryTest {

@Autowired
Expand Down

0 comments on commit 71a3be0

Please sign in to comment.