-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/java/io/github/dgroup/arch4u/pmd/AtLeastOneConstructorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
226 changes: 226 additions & 0 deletions
226
src/test/resources/io/github/dgroup/arch4u/pmd/xml/AtLeastOneConstructor.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |