From b9f8f2d4fbf977edd715c40d8dfcd65ceeecb0ed Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 28 Jan 2016 22:24:34 +0100 Subject: [PATCH] fixes #1453 Test Class Without Test Cases gives false positive --- .../lang/java/rule/junit/AbstractJUnitRule.java | 8 +++++--- .../junit/TestClassWithoutTestCasesRule.java | 13 +++++-------- .../junit/xml/TestClassWithoutTestCases.xml | 17 +++++++++++++++++ src/site/markdown/overview/changelog.md | 2 ++ 4 files changed, 29 insertions(+), 11 deletions(-) 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 9d75de22a3..92bfda43ca 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 @@ -82,11 +82,13 @@ public abstract class AbstractJUnitRule extends AbstractJavaRule { return false; // skip various inapplicable method variations } + boolean result = false; if (isJUnit3Class) { - return isJUnit3Method(method); - } else { - return isJUnit4Method(method); + result = isJUnit3Method(method); } + + result |= isJUnit4Method(method); + return result; } private boolean isJUnit4Method(ASTMethodDeclaration method) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java index aa34cb0982..e93b8f0c9b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java @@ -3,7 +3,6 @@ */ package net.sourceforge.pmd.lang.java.rule.junit; -import java.util.Iterator; import java.util.List; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; @@ -21,17 +20,15 @@ public class TestClassWithoutTestCasesRule extends AbstractJUnitRule { boolean testsFound = false; if (m != null) { - for (Iterator it = m.iterator(); it.hasNext() && !testsFound;) { - ASTMethodDeclaration md = it.next(); - if (!isInInnerClassOrInterface(md) - && isJUnitMethod(md, data)) { - testsFound = true; - } + for (ASTMethodDeclaration md : m) { + if (!isInInnerClassOrInterface(md) && isJUnitMethod(md, data)) { + testsFound = true; + } } } if (!testsFound) { - addViolation(data, node); + addViolation(data, node); } return data; diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml index f714f6f62d..5ff35a6e4f 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml @@ -96,6 +96,23 @@ failure case does not extend TestCase 0 + + + + #1453 False positive when the test class extends an other. + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 7fe6f90e9f..810c8f54a9 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -29,6 +29,8 @@ * [#1449](https://sourceforge.net/p/pmd/bugs/1449/): false positive when casting a variable to short * java-design/AccessorClassGeneration: * [#1452](https://sourceforge.net/p/pmd/bugs/1452/): ArrayIndexOutOfBoundsException with Annotations for AccessorClassGenerationRule +* java-junit/TestClassWithoutTestCases: + * [#1453](https://sourceforge.net/p/pmd/bugs/1453/): Test Class Without Test Cases gives false positive * General * [#1455](https://sourceforge.net/p/pmd/bugs/1455/): PMD doesn't handle Java 8 explicit receiver parameters