From 62066eb76742f0cdcb7d91eb569ca302c55ed2a2 Mon Sep 17 00:00:00 2001 From: Oleksii Dykov Date: Sat, 8 Jan 2022 21:08:36 +0200 Subject: [PATCH 1/6] add unit tests --- ...AvoidUsingObjectMapperAsALocalVariable.xml | 24 ++++ ...NoMandatoryConstructorInExceptionClass.xml | 133 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml diff --git a/src/test/resources/io/github/dgroup/arch4u/pmd/xml/AvoidUsingObjectMapperAsALocalVariable.xml b/src/test/resources/io/github/dgroup/arch4u/pmd/xml/AvoidUsingObjectMapperAsALocalVariable.xml index ebbcfb9..ad8d95e 100644 --- a/src/test/resources/io/github/dgroup/arch4u/pmd/xml/AvoidUsingObjectMapperAsALocalVariable.xml +++ b/src/test/resources/io/github/dgroup/arch4u/pmd/xml/AvoidUsingObjectMapperAsALocalVariable.xml @@ -1,4 +1,28 @@ + + + + + + + + [GOOD]: all mandatory constructors are provided + 0 + + + + + + [GOOD]: mandatory constructor MyException(String, Object...) is provided + + 0 + + + + + + [GOOD]: mandatory constructor MyException(Throwable, String, Object...) is provided + + 0 + + + + + [BAD]: no constructors + 1 + 1 + + + + + [BAD]: no mandatory constructor + 1 + 1 + + + + + [BAD]: no mandatory constructor + 1 + 1 + + + + + [BAD]: no mandatory constructor + 1 + 1 + + + + + [BAD]: no mandatory constructor + 1 + 1 + + + + From 2ac73b9afe789cda9d68b17d5b9a5b829790133d Mon Sep 17 00:00:00 2001 From: Oleksii Dykov Date: Sat, 8 Jan 2022 21:10:30 +0200 Subject: [PATCH 2/6] add test class --- ...datoryConstructorInExceptionClassTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/java/io/github/dgroup/arch4u/pmd/NoMandatoryConstructorInExceptionClassTest.java diff --git a/src/test/java/io/github/dgroup/arch4u/pmd/NoMandatoryConstructorInExceptionClassTest.java b/src/test/java/io/github/dgroup/arch4u/pmd/NoMandatoryConstructorInExceptionClassTest.java new file mode 100644 index 0000000..5faed9d --- /dev/null +++ b/src/test/java/io/github/dgroup/arch4u/pmd/NoMandatoryConstructorInExceptionClassTest.java @@ -0,0 +1,44 @@ +/* + * MIT License + * + * Copyright (c) 2019-2022 Yurii Dubinka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE + * OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package io.github.dgroup.arch4u.pmd; + +import net.sourceforge.pmd.testframework.SimpleAggregatorTst; + +/** + * Test case for {@code NoMandatoryConstructorInExceptionClass} rule. + * + * @since 0.2.0 + */ +@SuppressWarnings({"PMD.TestClassWithoutTestCases", "PMD.JUnit4TestShouldUseBeforeAnnotation"}) +public final class NoMandatoryConstructorInExceptionClassTest extends SimpleAggregatorTst { + + @Override + public void setUp() { + addRule( + "io/github/dgroup/arch4u/pmd/default-ruleset.xml", + "NoMandatoryConstructorInExceptionClass" + ); + } +} From e09b6307ec359a2c392d744bd42ff4e395a3fa87 Mon Sep 17 00:00:00 2001 From: Oleksii Dykov Date: Sat, 8 Jan 2022 21:49:55 +0200 Subject: [PATCH 3/6] add unit tests --- ...NoMandatoryConstructorInExceptionClass.xml | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml b/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml index 3431923..75c01a6 100644 --- a/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml +++ b/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml @@ -46,7 +46,7 @@ class MyException extends Exception { - [GOOD]: mandatory constructor MyException(String, Object...) is provided + [GOOD]: mandatory constructor `MyException(String, Object...)` is provided 0 - [GOOD]: mandatory constructor MyException(Throwable, String, Object...) is provided + [GOOD]: mandatory constructor `MyException(Throwable, String, Object...)` is provided 0 + + + [GOOD]: exception with mandatory constructor extends ArithmeticException + + 0 + + + + + [GOOD]: inner exception class with mandatory constructor + 0 + + + [BAD]: no constructors 1 @@ -83,7 +107,7 @@ class MyException extends Exception { //violation - [BAD]: no mandatory constructor + [BAD]: no mandatory constructor: public MyException() 1 1 - [BAD]: no mandatory constructor + [BAD]: no mandatory constructor: public MyException(Throwable, Object...) 1 1 + + [BAD]: constructor has non-public modifier + 1 + 1 + + + + + [BAD]: inner exception class without mandatory constructor + 1 + 2 + + From 2cfedac7893e577e9861e14ebae82d791cd3f7d7 Mon Sep 17 00:00:00 2001 From: Oleksii Dykov Date: Sat, 8 Jan 2022 21:50:55 +0200 Subject: [PATCH 4/6] create NoMandatoryConstructorInExceptionClass rule --- .../dgroup/arch4u/pmd/default-ruleset.xml | 77 +++++++++++++++---- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/src/main/resources/io/github/dgroup/arch4u/pmd/default-ruleset.xml b/src/main/resources/io/github/dgroup/arch4u/pmd/default-ruleset.xml index cfdc679..b2fda91 100644 --- a/src/main/resources/io/github/dgroup/arch4u/pmd/default-ruleset.xml +++ b/src/main/resources/io/github/dgroup/arch4u/pmd/default-ruleset.xml @@ -13,15 +13,15 @@ language="java" message="ObjectMapper is better to have as field than a local variable due to performance reasons." class="net.sourceforge.pmd.lang.rule.XPathRule"> - - ObjectMapper is better to have as field than a local variable due to performance reasons. - It is allowed to be declared in fields, constructors and initialization blocks. - - 3 - - - - + + ObjectMapper is better to have as field than a local variable due to performance reasons. + It is allowed to be declared in fields, constructors and initialization blocks. + + 3 + + + + - - - - + + + + - - + + + + + + Exception class must have at least one constructor with signature + `public Ctor(Throwable, String, Object...)` or `public Ctor(String, Object...)`. + + 3 + + + + + + + + + + + + From a426cc85540f05e97bd90a0b95088bed5d8a178a Mon Sep 17 00:00:00 2001 From: Oleksii Dykov Date: Sat, 8 Jan 2022 21:52:42 +0200 Subject: [PATCH 5/6] update version number --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3505cb5..5c74360 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ io.github.dgroup arch4u-pmd - 0.1.0 + 0.2.0 jar ${project.artifactId} From 59d2c7821a7705827c8a1727fc05e21414afd088 Mon Sep 17 00:00:00 2001 From: Oleksii Dykov Date: Sat, 8 Jan 2022 22:06:53 +0200 Subject: [PATCH 6/6] fix unit test --- .../arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml b/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml index 75c01a6..ce3bde7 100644 --- a/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml +++ b/src/test/resources/io/github/dgroup/arch4u/pmd/xml/NoMandatoryConstructorInExceptionClass.xml @@ -160,7 +160,7 @@ class MyException extends Exception { //violation 1