[java] Rename JUnit rules with overly restrictive names (#4965)

Merge pull request #4965 from Monits:issue-4532
This commit is contained in:
Andreas Dangel 2024-10-03 20:07:50 +02:00
commit 463ca964dc
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
20 changed files with 437 additions and 353 deletions

View File

@ -14,11 +14,29 @@ This is a {{ site.pmd.release_type }} release.
### 🚀 New and noteworthy
### 🌟 Rule Changes
#### Renamed Rules
Several rules for unit testing have been renamed to better reflect their actual scope. Lots of them were called
after JUnit / JUnit 4, even when they applied to JUnit 5 and / or TestNG.
* {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseAfterAnnotation`.
* {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseBeforeAnnotation`.
* {% rule java/bestpractices/UnitTestShouldUseTestAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseTestAnnotation`.
* {% rule java/bestpractices/UnitTestAssertionsShouldIncludeMessage %} (Java Best Practices) has been renamed from `JUnitAssertionsShouldIncludeMessage`.
* {% rule java/bestpractices/UnitTestContainsTooManyAsserts %} (Java Best Practices) has been renamed from `JUnitTestContainsTooManyAsserts`.
* {% rule java/bestpractices/UnitTestsShouldIncludeAssert %} (Java Best Practices) has been renamed from `JUnitTestsShouldIncludeAssert`.
The old rule names still work but are deprecated.
### 🐛 Fixed Issues
* java
* [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules
### 🚨 API Changes
### ✨ External Contributions
### ✨ Merged pull requests
* [#4965](https://github.com/pmd/pmd/pull/4965): \[java] Rename JUnit rules with overly restrictive names - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
{% endtocmaker %}

View File

@ -4,41 +4,10 @@
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.TestFrameworksUtil;
import net.sourceforge.pmd.lang.java.types.InvocationMatcher;
import net.sourceforge.pmd.lang.java.types.InvocationMatcher.CompoundInvocationMatcher;
/**
* @deprecated The rule was renamed {@link UnitTestAssertionsShouldIncludeMessageRule}
*/
@Deprecated
public class JUnitAssertionsShouldIncludeMessageRule extends UnitTestAssertionsShouldIncludeMessageRule {
public class JUnitAssertionsShouldIncludeMessageRule extends AbstractJavaRulechainRule {
private final CompoundInvocationMatcher checks =
InvocationMatcher.parseAll(
"_#assertEquals(_,_)",
"_#assertTrue(_)",
"_#assertFalse(_)",
"_#assertSame(_,_)",
"_#assertNotSame(_,_)",
"_#assertNull(_)",
"_#assertNotNull(_)",
"_#assertArrayEquals(_,_)",
"_#assertThat(_,_)",
"_#fail()",
"_#assertEquals(float,float,float)",
"_#assertEquals(double,double,double)"
);
public JUnitAssertionsShouldIncludeMessageRule() {
super(ASTMethodCall.class);
}
@Override
public Object visit(ASTMethodCall node, Object data) {
if (TestFrameworksUtil.isCallOnAssertionContainer(node)) {
if (checks.anyMatch(node)) {
asCtx(data).addViolation(node);
}
}
return null;
}
}

View File

@ -4,54 +4,10 @@
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @deprecated The rule was renamed {@link UnitTestContainsTooManyAssertsRule}
*/
@Deprecated
public class JUnitTestContainsTooManyAssertsRule extends UnitTestContainsTooManyAssertsRule {
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.TestFrameworksUtil;
import net.sourceforge.pmd.properties.NumericConstraints;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
public class JUnitTestContainsTooManyAssertsRule extends AbstractJavaRulechainRule {
private static final PropertyDescriptor<Integer> MAX_ASSERTS =
PropertyFactory.intProperty("maximumAsserts")
.desc("Maximum number of assert calls in a test method")
.require(NumericConstraints.positive())
.defaultValue(1)
.build();
private static final PropertyDescriptor<Set<String>> EXTRA_ASSERT_METHOD_NAMES =
PropertyFactory.stringProperty("extraAssertMethodNames")
.desc("Extra valid assertion methods names")
.map(Collectors.toSet())
.emptyDefaultValue()
.build();
public JUnitTestContainsTooManyAssertsRule() {
super(ASTMethodDeclaration.class);
definePropertyDescriptor(MAX_ASSERTS);
definePropertyDescriptor(EXTRA_ASSERT_METHOD_NAMES);
}
@Override
public Object visit(ASTMethodDeclaration method, Object data) {
ASTBlock body = method.getBody();
if (body != null && TestFrameworksUtil.isTestMethod(method)) {
Set<String> extraAsserts = getProperty(EXTRA_ASSERT_METHOD_NAMES);
int assertCount = body.descendants(ASTMethodCall.class)
.filter(call -> TestFrameworksUtil.isProbableAssertCall(call)
|| extraAsserts.contains(call.getMethodName()))
.count();
if (assertCount > getProperty(MAX_ASSERTS)) {
asCtx(data).addViolation(method);
}
}
return data;
}
}

View File

@ -4,43 +4,10 @@
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @deprecated The rule was renamed {@link UnitTestsShouldIncludeAssertRule}
*/
@Deprecated
public class JUnitTestsShouldIncludeAssertRule extends UnitTestsShouldIncludeAssertRule {
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.TestFrameworksUtil;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
public class JUnitTestsShouldIncludeAssertRule extends AbstractJavaRulechainRule {
private static final PropertyDescriptor<Set<String>> EXTRA_ASSERT_METHOD_NAMES =
PropertyFactory.stringProperty("extraAssertMethodNames")
.desc("Extra valid assertion methods names")
.map(Collectors.toSet())
.emptyDefaultValue()
.build();
public JUnitTestsShouldIncludeAssertRule() {
super(ASTMethodDeclaration.class);
definePropertyDescriptor(EXTRA_ASSERT_METHOD_NAMES);
}
@Override
public Object visit(ASTMethodDeclaration method, Object data) {
ASTBlock body = method.getBody();
Set<String> extraAsserts = getProperty(EXTRA_ASSERT_METHOD_NAMES);
if (body != null
&& TestFrameworksUtil.isTestMethod(method)
&& !TestFrameworksUtil.isExpectAnnotated(method)
&& body.descendants(ASTMethodCall.class)
.none(call -> TestFrameworksUtil.isProbableAssertCall(call)
|| extraAsserts.contains(call.getMethodName()))) {
asCtx(data).addViolation(method);
}
return data;
}
}

View File

@ -0,0 +1,44 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.TestFrameworksUtil;
import net.sourceforge.pmd.lang.java.types.InvocationMatcher;
import net.sourceforge.pmd.lang.java.types.InvocationMatcher.CompoundInvocationMatcher;
public class UnitTestAssertionsShouldIncludeMessageRule extends AbstractJavaRulechainRule {
private final CompoundInvocationMatcher checks =
InvocationMatcher.parseAll(
"_#assertEquals(_,_)",
"_#assertTrue(_)",
"_#assertFalse(_)",
"_#assertSame(_,_)",
"_#assertNotSame(_,_)",
"_#assertNull(_)",
"_#assertNotNull(_)",
"_#assertArrayEquals(_,_)",
"_#assertThat(_,_)",
"_#fail()",
"_#assertEquals(float,float,float)",
"_#assertEquals(double,double,double)"
);
public UnitTestAssertionsShouldIncludeMessageRule() {
super(ASTMethodCall.class);
}
@Override
public Object visit(ASTMethodCall node, Object data) {
if (TestFrameworksUtil.isCallOnAssertionContainer(node)) {
if (checks.anyMatch(node)) {
asCtx(data).addViolation(node);
}
}
return null;
}
}

View File

@ -0,0 +1,57 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import java.util.Set;
import java.util.stream.Collectors;
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.TestFrameworksUtil;
import net.sourceforge.pmd.properties.NumericConstraints;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
public class UnitTestContainsTooManyAssertsRule extends AbstractJavaRulechainRule {
private static final PropertyDescriptor<Integer> MAX_ASSERTS =
PropertyFactory.intProperty("maximumAsserts")
.desc("Maximum number of assert calls in a test method")
.require(NumericConstraints.positive())
.defaultValue(1)
.build();
private static final PropertyDescriptor<Set<String>> EXTRA_ASSERT_METHOD_NAMES =
PropertyFactory.stringProperty("extraAssertMethodNames")
.desc("Extra valid assertion methods names")
.map(Collectors.toSet())
.emptyDefaultValue()
.build();
public UnitTestContainsTooManyAssertsRule() {
super(ASTMethodDeclaration.class);
definePropertyDescriptor(MAX_ASSERTS);
definePropertyDescriptor(EXTRA_ASSERT_METHOD_NAMES);
}
@Override
public Object visit(ASTMethodDeclaration method, Object data) {
ASTBlock body = method.getBody();
if (body != null && TestFrameworksUtil.isTestMethod(method)) {
Set<String> extraAsserts = getProperty(EXTRA_ASSERT_METHOD_NAMES);
int assertCount = body.descendants(ASTMethodCall.class)
.filter(call -> TestFrameworksUtil.isProbableAssertCall(call)
|| extraAsserts.contains(call.getMethodName()))
.count();
if (assertCount > getProperty(MAX_ASSERTS)) {
asCtx(data).addViolation(method);
}
}
return data;
}
}

View File

@ -0,0 +1,46 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import java.util.Set;
import java.util.stream.Collectors;
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.TestFrameworksUtil;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
public class UnitTestsShouldIncludeAssertRule extends AbstractJavaRulechainRule {
private static final PropertyDescriptor<Set<String>> EXTRA_ASSERT_METHOD_NAMES =
PropertyFactory.stringProperty("extraAssertMethodNames")
.desc("Extra valid assertion methods names")
.map(Collectors.toSet())
.emptyDefaultValue()
.build();
public UnitTestsShouldIncludeAssertRule() {
super(ASTMethodDeclaration.class);
definePropertyDescriptor(EXTRA_ASSERT_METHOD_NAMES);
}
@Override
public Object visit(ASTMethodDeclaration method, Object data) {
ASTBlock body = method.getBody();
Set<String> extraAsserts = getProperty(EXTRA_ASSERT_METHOD_NAMES);
if (body != null
&& TestFrameworksUtil.isTestMethod(method)
&& !TestFrameworksUtil.isExpectAnnotated(method)
&& body.descendants(ASTMethodCall.class)
.none(call -> TestFrameworksUtil.isProbableAssertCall(call)
|| extraAsserts.contains(call.getMethodName()))) {
asCtx(data).addViolation(method);
}
return data;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.test.PmdRuleTst;
class JUnitAssertionsShouldIncludeMessageTest extends PmdRuleTst {
class UnitTestAssertionsShouldIncludeMessageTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.test.PmdRuleTst;
class JUnitTestContainsTooManyAssertsTest extends PmdRuleTst {
class UnitTestContainsTooManyAssertsTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -9,7 +9,7 @@ import org.junit.Before;
import net.sourceforge.pmd.test.PmdRuleTst;
class JUnit4TestShouldUseAfterAnnotationTest extends PmdRuleTst {
class UnitTestShouldUseAfterAnnotationTest extends PmdRuleTst {
// no additional unit tests
public static class BaseTest {

View File

@ -9,7 +9,7 @@ import org.junit.Before;
import net.sourceforge.pmd.test.PmdRuleTst;
class JUnit4TestShouldUseBeforeAnnotationTest extends PmdRuleTst {
class UnitTestShouldUseBeforeAnnotationTest extends PmdRuleTst {
// no additional unit tests
public static class BaseTest {

View File

@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.test.PmdRuleTst;
class JUnit4TestShouldUseTestAnnotationTest extends PmdRuleTst {
class UnitTestShouldUseTestAnnotationTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.test.PmdRuleTst;
class JUnitTestsShouldIncludeAssertTest extends PmdRuleTst {
class UnitTestsShouldIncludeAssertTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -136,7 +136,7 @@ public class Foo {
<description>[java] JUnit4TestShouldUseBeforeAnnotation false positive when overriding setUp #1592</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import net.sourceforge.pmd.lang.java.rule.bestpractices.JUnit4TestShouldUseBeforeAnnotationTest.BaseTest;
import net.sourceforge.pmd.lang.java.rule.bestpractices.UnitTestShouldUseAfterAnnotationTest.BaseTest;
public class AReallyCoolFeatureTest extends BaseTest {
@Override

View File

@ -158,7 +158,7 @@ public class Foo {
<description>[java] JUnit4TestShouldUseBeforeAnnotation false positive when overriding setUp #1592</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import net.sourceforge.pmd.lang.java.rule.bestpractices.JUnit4TestShouldUseBeforeAnnotationTest.BaseTest;
import net.sourceforge.pmd.lang.java.rule.bestpractices.UnitTestShouldUseBeforeAnnotationTest.BaseTest;
public class AReallyCoolFeatureTest extends BaseTest {
@Override