diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index c77a1e2791..c8ca8089c9 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,5 +1,6 @@ ????? ??, 2013 - 5.0.3: +Fixed bug 984: Cyclomatic complexity should treat constructors like methods Fixed bug 992: Class java.beans.Statement triggered in CloseResource rule Fixed bug 997: Rule NonThreadSafeSingleton gives analysis problem Fixed bug 1002: False +: FinalFieldCouldBeStatic on inner class diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java index abee674b6c..b3880c8205 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java @@ -204,7 +204,6 @@ public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { public Object visit(ASTMethodDeclaration node, Object data) { entryStack.push( new Entry( node ) ); super.visit( node, data ); - if ( showMethodsComplexity ) { Entry methodEntry = entryStack.pop(); int methodDecisionPoints = methodEntry.decisionPoints; Entry classEntry = entryStack.peek(); @@ -224,12 +223,11 @@ public Object visit(ASTMethodDeclaration node, Object data) { } } - if ( methodEntry.decisionPoints >= reportLevel ) { + if ( showMethodsComplexity && methodEntry.decisionPoints >= reportLevel ) { addViolation( data, node, new String[] { "method", methodDeclarator == null ? "" : methodDeclarator.getImage(), String.valueOf( methodEntry.decisionPoints ) } ); } - } return data; } @@ -261,12 +259,11 @@ public Object visit(ASTConstructorDeclaration node, Object data) { if ( constructorDecisionPointCount > classEntry.highestDecisionPoints ) { classEntry.highestDecisionPoints = constructorDecisionPointCount; } - if ( constructorEntry.decisionPoints >= reportLevel ) { + if ( showMethodsComplexity && constructorEntry.decisionPoints >= reportLevel ) { addViolation( data, node, new String[] { "constructor", classEntry.node.getImage(), String.valueOf( constructorDecisionPointCount ) } ); } return data; } - } diff --git a/pmd/src/test/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityTest.java b/pmd/src/test/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityTest.java index 8958c39782..4739d9036b 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityTest.java @@ -13,13 +13,18 @@ import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleViolation; import net.sourceforge.pmd.lang.java.rule.codesize.CyclomaticComplexityRule; import net.sourceforge.pmd.testframework.RuleTst; +import net.sourceforge.pmd.testframework.SimpleAggregatorTst; import net.sourceforge.pmd.testframework.TestDescriptor; +import net.sourceforge.pmd.testframework.SimpleAggregatorTst.CustomXmlTestClassMethodsRunner; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runner.notification.Failure; - public class CyclomaticComplexityTest extends RuleTst { +@RunWith(SimpleAggregatorTst.CustomXmlTestClassMethodsRunner.class) +public class CyclomaticComplexityTest extends RuleTst { private Rule rule; private TestDescriptor[] tests; @@ -33,8 +38,8 @@ import org.junit.Test; rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1); Report report = new Report(); runTestFromString(tests[0].getCode(), rule, report); - Iterator i = report.iterator(); - RuleViolation rv = (RuleViolation) i.next(); + Iterator i = report.iterator(); + RuleViolation rv = i.next(); assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1); } @@ -43,8 +48,8 @@ import org.junit.Test; rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 10); Report report = new Report(); runTestFromString(tests[1].getCode(), rule, report); - Iterator i = report.iterator(); - RuleViolation rv = (RuleViolation) i.next(); + Iterator i = report.iterator(); + RuleViolation rv = i.next(); assertNotSame(rv.getDescription().indexOf("Highest = 11"), -1); } @@ -53,8 +58,8 @@ import org.junit.Test; rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1); Report report = new Report(); runTestFromString(tests[2].getCode(), rule, report); - Iterator i = report.iterator(); - RuleViolation rv = (RuleViolation) i.next(); + Iterator i = report.iterator(); + RuleViolation rv = i.next(); assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1); } @@ -66,6 +71,22 @@ import org.junit.Test; assertEquals(0, report.size()); } + @Test + public void testRemainingTestCases() { + for (int i = 0; i < tests.length; i++) { + if (i == 0 || i == 1 || i == 2) { + continue; // skip - covered by above test methods + } + + try { + runTest(tests[i]); + } catch (Throwable t) { + Failure f = CustomXmlTestClassMethodsRunner.createFailure(rule, t); + CustomXmlTestClassMethodsRunner.addFailure(f); + } + } + } + public static junit.framework.Test suite() { return new junit.framework.JUnit4TestAdapter(CyclomaticComplexityTest.class); } diff --git a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml index 2900eb3fdf..d428e1e2f3 100644 --- a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml +++ b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml @@ -112,9 +112,36 @@ Testing new parameter showMethodsMethods Testing default value of showClassMethods and showClassesComplexity ]]> - "" - "" 2 + + + #984 Cyclomatic complexity should treat constructors like methods: 1 - showMethodsComplexity=true + false + true + 1 + 1 + + + + #984 Cyclomatic complexity should treat constructors like methods: 2 - showMethodsComplexity=false + false + false + 1 + 0 + +