From 2452db15f431c10ab8729a7f18df768ad09ce5e1 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Mon, 30 Nov 2015 10:05:42 +0100 Subject: [PATCH] #1373 JUnitAssertionsShouldIncludeMessage is no longer compatible with TestNG --- .../java/rule/junit/AbstractJUnitRule.java | 13 +++++++++++- .../JUnitAssertionsShouldIncludeMessage.xml | 21 +++++++++++++++++++ src/site/markdown/overview/changelog.md | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java index 93dbf8a469..9d75de22a3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java @@ -11,6 +11,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTExtendsList; +import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTResultType; @@ -59,12 +60,22 @@ public abstract class AbstractJUnitRule extends AbstractJavaRule { isJUnit4Class = isJUnit4Class(node); } - if (isJUnit3Class || isJUnit4Class) { + if (!isTestNgClass(node) && (isJUnit3Class || isJUnit4Class)) { return super.visit(node, data); } return data; } + private boolean isTestNgClass(ASTCompilationUnit node) { + List imports = node.findDescendantsOfType(ASTImportDeclaration.class); + for (ASTImportDeclaration i : imports) { + if (i.getImportedName() != null && i.getImportedName().startsWith("org.testng")) { + return true; + } + } + return false; + } + public boolean isJUnitMethod(ASTMethodDeclaration method, Object data) { if (!method.isPublic() || method.isAbstract() || method.isNative() || method.isStatic()) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/JUnitAssertionsShouldIncludeMessage.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/JUnitAssertionsShouldIncludeMessage.xml index c139af82de..771ffb346c 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/JUnitAssertionsShouldIncludeMessage.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/JUnitAssertionsShouldIncludeMessage.xml @@ -399,6 +399,27 @@ public class TestClass extends Assert { assertNull(null); assertThat("", containsString("")); } +} + ]]> + + + #1373 JUnitAssertionsShouldIncludeMessage is no longer compatible with TestNG + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index c233066283..40b7a8f537 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -25,6 +25,8 @@ * [#1440](https://sourceforge.net/p/pmd/bugs/1440/): NPE in AvoidCallingFinalize * java-imports/UnnecessaryFullyQualifiedName * [#1436](https://sourceforge.net/p/pmd/bugs/1436/): UnnecessaryFullyQualifiedName false positive on clashing static imports with enums +* java-junit/JUnitAssertionsShouldIncludeMessage + * [#1373](https://sourceforge.net/p/pmd/bugs/1373/): JUnitAssertionsShouldIncludeMessage is no longer compatible with TestNG * java-migrating/JUnit4TestShouldUseBeforeAnnotation * [#1446](https://sourceforge.net/p/pmd/bugs/1446/): False positive with JUnit4TestShouldUseBeforeAnnotation when TestNG is used * java-naming/SuspiciousEqualsMethodName