diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/rule/NPathComplexityRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/rule/NPathComplexityRule.java index 76500357d4..a719a817e7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/rule/NPathComplexityRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/rule/NPathComplexityRule.java @@ -13,6 +13,8 @@ import net.sourceforge.pmd.lang.java.rule.AbstractJavaMetricsRule; import net.sourceforge.pmd.lang.rule.properties.IntegerProperty; /** + * Simple n-path complexity rule. + * * @author Clément Fournier */ public class NPathComplexityRule extends AbstractJavaMetricsRule { @@ -48,5 +50,4 @@ public class NPathComplexityRule extends AbstractJavaMetricsRule { return data; } - } diff --git a/pmd-java/src/main/resources/rulesets/java/metrics.xml b/pmd-java/src/main/resources/rulesets/java/metrics.xml index e3c2e40ac2..befa73650f 100644 --- a/pmd-java/src/main/resources/rulesets/java/metrics.xml +++ b/pmd-java/src/main/resources/rulesets/java/metrics.xml @@ -66,7 +66,6 @@ public class Foo { // This has a Cyclomatic Complexity = 12 - @@ -99,7 +97,7 @@ public class Foo extends Bar { @@ -111,8 +109,8 @@ public class Foo extends Bar { 3 r) { doSomething(); @@ -134,9 +132,9 @@ void bar() { // this is something more complex than it needs to be, doSomethingDangerous(); } catch (Exception ex) { makeAmends(); - } finally { - dontDoItAgain(); - } + } finally { + dontDoItAgain(); + } } } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/metrics/MetricsRulesTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/metrics/MetricsRulesTest.java index ea7452204b..22148254aa 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/metrics/MetricsRulesTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/metrics/MetricsRulesTest.java @@ -11,7 +11,6 @@ import net.sourceforge.pmd.testframework.SimpleAggregatorTst; /** * @author Clément Fournier */ - public class MetricsRulesTest extends SimpleAggregatorTst { private static final String RULESET = "java-metrics"; @@ -26,8 +25,8 @@ public class MetricsRulesTest extends SimpleAggregatorTst { @Override public void setUp() { -// addRule(RULESET, "CyclomaticComplexity"); -// addRule(RULESET, "NcssCount"); + addRule(RULESET, "CyclomaticComplexity"); + addRule(RULESET, "NcssCount"); addRule(RULESET, "NPathComplexity"); } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NPathTest.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NPathTest.xml index 71ca904ac6..8c2387f79f 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NPathTest.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NPathTest.xml @@ -5,7 +5,7 @@ public class Foo { public static void bar() { boolean a, b = true; - try { // 2 * 2 + 2 + try { // total 6 if (true) { // 2 List buz = new ArrayList(); } @@ -210,6 +210,29 @@ public class Foo { ]]> + + Ensure switch has same complexity as equivalent ifs + 1 + + '.Foo#bar()' has value 7. + + + + + Boolean operators 1 @@ -227,8 +250,7 @@ public class Foo { + + + NPath supports constructors + 1 + + '.Foo#Foo()' has value 7. + + + + diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/NPathComplexity.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/NPathComplexity.xml index 186c1522cc..491811515a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/NPathComplexity.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/NPathComplexity.xml @@ -1,100 +1,196 @@ - - ok - 0 - - + ]]> + - fail, with minimum - 1 + Full example 1 - The method bar() has an NPath complexity of 2 + The method 'bar()' has an NPath complexity of 2016 + + + + + + + Test default report level - report 200 + 0 + 1 + + The method 'bar()' has an NPath complexity of 200 + public class Foo { + public static void bar() { + boolean a, b = true; + int j = 0; + + switch (j) { // 5 + case 0: + case 1: + case 3: if (a || b) {} break; + } + + switch (j) { // * 5 + case 0: + case 1: + case 3: if (a || b) {} break; + } + + if (true || a && b); // * 4 + while (j++ < 20); // * 2 + } + } + ]]> - Failure case - 1 + Test default report level - ignore 199 + 0 + class Foo { + void bar() { + boolean a, b; + try { + switch(j) { // 7 + case 1: + case 2: break; + case 3: j = 5; break; + case 4: if (b && a) { bar(); } break; + default: break; + } + + switch(j) { // * 7 + case 1: + case 2: break; + case 3: j = 5; break; + case 4: if (b && a) { bar(); } break; + default: break; + } + + if (true || a || b); // * 4 + + } catch (ScaryException e) { + if (true || a); // + 3 + } + } + } + ]]> + + Boolean operators + 2 + 1 + + The method 'bar()' has an NPath complexity of 4 + + + + + test case for bug 3484404 (Invalid NPath calculation in return statement) - 0 - - - - - test case for bug 3484404 (Invalid NPath calculation in return statement) with minimum 25 - 25.0 + 5 2 - The method x() has an NPath complexity of 25 - The method y() has an NPath complexity of 25 + The method 'x(boolean, boolean)' has an NPath complexity of 25 + The method 'y(boolean, boolean)' has an NPath complexity of 25 + + + NPath supports constructors + 6 + 1 + + The constructor 'Foo()' has an NPath complexity of 7 + + +