Skip to content

Commit

Permalink
#129: Add support for ignore cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroup committed Apr 24, 2022
1 parent 2ee5581 commit 68a343f
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</rule>
<rule ref="category/java/codestyle.xml/AtLeastOneConstructor">
<properties>
<property name="violationSuppressXPath" value=".[ends-with(@SimpleName,'Test')]"/>
<property name="ignoredAnnotations" value="lombok.Data|lombok.Value|lombok.Builder|lombok.NoArgsConstructor|lombok.RequiredArgsConstructor|lombok.AllArgsConstructor|org.springframework.context.annotation.Configuration|org.springframework.web.bind.annotation.ControllerAdvice|org.springframework.web.bind.annotation.RestController|lombok.experimental.SuperBuilder|org.springframework.stereotype.Component|org.springframework.stereotype.Service|org.springframework.stereotype.Repository|org.springframework.boot.context.properties.ConfigurationProperties|org.mapstruct.Mapper|javax.xml.bind.annotation.XmlRootElement|org.junit.jupiter.api.extension.ExtendWith|org.mockserver.junit.jupiter.MockServerSettings|org.springframework.test.context.ContextConfiguration|org.springframework.test.context.web.WebAppConfiguration|org.junit.runner.RunWith" />
</properties>
</rule>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 AtLeastOneConstructor} rule.
*
* @since 0.1.0
*/
@SuppressWarnings({"PMD.TestClassWithoutTestCases", "PMD.JUnit4TestShouldUseBeforeAnnotation"})
public final class AtLeastOneConstructorTest extends SimpleAggregatorTst {

@Override
public void setUp() {
addRule("io/github/dgroup/arch4u/pmd/arch4u-ruleset.xml", "AtLeastOneConstructor");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">

<test-code>
<description>no constructor in unit test</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class FooTest {
}
]]></code>
</test-code>

<test-code>
<description>ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class FooTest {
public Foo() {}
}
]]></code>
</test-code>

<test-code>
<description>simple failure case</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {}
]]></code>
</test-code>

<test-code>
<description>inner bad, outer ok</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public class Bar {}
public Foo() {}
}
]]></code>
</test-code>

<test-code>
<description>inner ok, outer bad</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public class Bar {
public Bar() {}
}
}
]]></code>
</test-code>

<test-code>
<description>inner and outer both bad</description>
<expected-problems>2</expected-problems>
<code><![CDATA[
public class Foo {
public class Bar {}
}
]]></code>
</test-code>

<test-code>
<description>inner and outer both ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public class Bar {
public Bar() {}
}
public Foo() {}
}
]]></code>
</test-code>

<test-code>
<description>skip interfaces</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public interface Foo {
}
]]></code>
</test-code>

<test-code>
<description>skip static classes</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public static class Foo {
}
]]></code>
</test-code>

<test-code>
<description>skip classes with only static methods</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public static class Foo {
public static void method() {}
public static void otherMethods() {}
}
]]></code>
</test-code>

<test-code>
<description>Don't skip classes with non-static methods; #1216 AtLeastOneConstructor ignores classes with *any* methods</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class TestAtLeastOneConstructor {
public void NotStatic() {
System.out.println("This class should have a constructor");
}
}
]]></code>
</test-code>

<test-code>
<description>Ignore classes with lombok-generated constructors (Value)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import lombok.Value;
@Value
public class TestAtLeastOneConstructor {
}
]]></code>
</test-code>

<test-code>
<description>Ignore classes with lombok-generated constructors (Required)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class TestAtLeastOneConstructor {
}
]]></code>
</test-code>

<test-code>
<description>Ignore classes with lombok-generated constructors (No)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import lombok.NoArgsConstructor;
@NoArgsConstructor
public class TestAtLeastOneConstructor {
}
]]></code>
</test-code>

<test-code>
<description>Ignore classes with lombok-generated constructors (All)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class TestAtLeastOneConstructor {
}
]]></code>
</test-code>

<test-code>
<description>Ignore classes with lombok-generated constructors (Builder)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import lombok.Builder;
@Builder
public class TestAtLeastOneConstructor {
}
]]></code>
</test-code>

<test-code>
<description>Report classes with at least one non-static method</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class TestAtLeastOneConstructor {
public static void foo() { }
public void bar () { }
}
]]></code>
</test-code>

<test-code>
<description>Report classes with at least one non-static field</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class TestAtLeastOneConstructor {
public int bar;
public static void foo() { }
}
]]></code>
</test-code>
</test-data>

0 comments on commit 68a343f

Please sign in to comment.