Merge branch 'pr-1492'

This commit is contained in:
Clément Fournier
2018-12-04 12:46:59 +01:00
3 changed files with 37 additions and 22 deletions

View File

@ -41,6 +41,7 @@ This means, you can use CPD to find duplicated code in your Kotlin projects.
* java
* [#1460](https://github.com/pmd/pmd/issues/1460): \[java] Intermittent PMD failure : PMD processing errors while no violations reported
* java-bestpractices
* [#647](https://github.com/pmd/pmd/issues/647): \[java] JUnitTestsShouldIncludeAssertRule should support `this.exception` as well as just `exception`
* [#1435](https://github.com/pmd/pmd/issues/1435): \[java] JUnitTestsShouldIncludeAssert: Support AssertJ soft assertions
* java-codestyle
* [#1232](https://github.com/pmd/pmd/issues/1232): \[java] Detector for large numbers not separated by _

View File

@ -17,6 +17,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTNormalAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.rule.AbstractJUnitRule;
@ -173,29 +175,25 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
private boolean isExpectStatement(ASTStatementExpression expression,
Map<String, List<NameOccurrence>> expectables) {
if (expression != null) {
ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
if (pe != null) {
Node name = pe.getFirstDescendantOfType(ASTName.class);
// case of an AllocationExpression
if (name == null) {
return false;
ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
if (pe != null) {
ASTPrimaryPrefix primaryPrefix = pe.getFirstChildOfType(ASTPrimaryPrefix.class);
Node name = pe.getFirstDescendantOfType(ASTName.class);
if (!primaryPrefix.usesThisModifier() && name != null) {
String[] parts = name.getImage().split("\\.");
if (parts.length >= 2) {
String varname = parts[0];
String methodName = parts[1];
if (expectables.containsKey(varname) && "expect".equals(methodName)) {
return true;
}
}
String img = name.getImage();
if (img.indexOf(".") == -1) {
return false;
}
String varname = img.split("\\.")[0];
if (!expectables.containsKey(varname)) {
return false;
}
for (NameOccurrence occ : expectables.get(varname)) {
if (occ.getLocation() == name && img.startsWith(varname + ".expect")) {
} else if (primaryPrefix.usesThisModifier()) {
List<ASTPrimarySuffix> primarySuffixes = pe.findChildrenOfType(ASTPrimarySuffix.class);
if (primarySuffixes.size() >= 2) {
String varname = primarySuffixes.get(0).getImage();
String methodName = primarySuffixes.get(1).getImage();
if (expectables.containsKey(varname) && "expect".equals(methodName)) {
return true;
}
}

View File

@ -494,6 +494,22 @@ public class FooTest {
void testFoo() {
softly.assertThat("doesn't matter").isEqualTo("doesn't matter");
}
}]]></code>
</test-code>
<test-code>
<description>#647 JUnitTestsShouldIncludeAssertRule should support `this.exception` as well as just `exception`</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.*;
public class DigitalAssetManagerTest {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void throwsIllegalArgumentExceptionIfIconIsNull() {
this.exception.expect(IllegalArgumentException.class);
this.exception.expectMessage("Icon is null, not a file, or doesn't exist.");
new DigitalAssetManager(null, null);
}
}]]></code>
</test-code>
</test-data>