Finished the tests
This commit is contained in:
@ -13,6 +13,8 @@ import net.sourceforge.pmd.lang.java.rule.AbstractJavaMetricsRule;
|
|||||||
import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
|
import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Simple n-path complexity rule.
|
||||||
|
*
|
||||||
* @author Clément Fournier
|
* @author Clément Fournier
|
||||||
*/
|
*/
|
||||||
public class NPathComplexityRule extends AbstractJavaMetricsRule {
|
public class NPathComplexityRule extends AbstractJavaMetricsRule {
|
||||||
@ -48,5 +50,4 @@ public class NPathComplexityRule extends AbstractJavaMetricsRule {
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,6 @@ public class Foo { // This has a Cyclomatic Complexity = 12
|
|||||||
</example>
|
</example>
|
||||||
</rule>
|
</rule>
|
||||||
|
|
||||||
<!-- TODO -->
|
|
||||||
<rule name="NcssCount"
|
<rule name="NcssCount"
|
||||||
message="The {0} ''{1}'' has a NCSS line count of {2}."
|
message="The {0} ''{1}'' has a NCSS line count of {2}."
|
||||||
since="3.9"
|
since="3.9"
|
||||||
@ -82,16 +81,15 @@ public class Foo { // This has a Cyclomatic Complexity = 12
|
|||||||
<example>
|
<example>
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
public class Foo extends Bar {
|
public class Foo extends Bar {
|
||||||
public Foo() {
|
public Foo() { //this class only has 4 NCSS lines
|
||||||
//this class only has 4 NCSS lines
|
super();
|
||||||
super();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
super.foo();
|
super.foo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]]>
|
]]>
|
||||||
</example>
|
</example>
|
||||||
@ -99,7 +97,7 @@ public class Foo extends Bar {
|
|||||||
|
|
||||||
<rule name="NPathComplexity"
|
<rule name="NPathComplexity"
|
||||||
since="3.9"
|
since="3.9"
|
||||||
message="The {0} {1} has an NPath complexity of {2}"
|
message="The {0} ''{1}'' has an NPath complexity of {2}"
|
||||||
class="net.sourceforge.pmd.lang.java.metrics.rule.NPathComplexityRule"
|
class="net.sourceforge.pmd.lang.java.metrics.rule.NPathComplexityRule"
|
||||||
metrics="true"
|
metrics="true"
|
||||||
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#NPathComplexity">
|
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#NPathComplexity">
|
||||||
@ -111,8 +109,8 @@ public class Foo extends Bar {
|
|||||||
<priority>3</priority>
|
<priority>3</priority>
|
||||||
<example>
|
<example>
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
void bar() { // this is something more complex than it needs to be,
|
void bar() { // This is something more complex than it needs to be,
|
||||||
if (y) { // it should be broken down into smaller methods or functions
|
if (y) { // it should be broken down into smaller methods or functions
|
||||||
for (j = 0; j < m; j++) {
|
for (j = 0; j < m; j++) {
|
||||||
if (j > r) {
|
if (j > r) {
|
||||||
doSomething();
|
doSomething();
|
||||||
@ -134,9 +132,9 @@ void bar() { // this is something more complex than it needs to be,
|
|||||||
doSomethingDangerous();
|
doSomethingDangerous();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
makeAmends();
|
makeAmends();
|
||||||
} finally {
|
} finally {
|
||||||
dontDoItAgain();
|
dontDoItAgain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
|
|||||||
/**
|
/**
|
||||||
* @author Clément Fournier
|
* @author Clément Fournier
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class MetricsRulesTest extends SimpleAggregatorTst {
|
public class MetricsRulesTest extends SimpleAggregatorTst {
|
||||||
|
|
||||||
private static final String RULESET = "java-metrics";
|
private static final String RULESET = "java-metrics";
|
||||||
@ -26,8 +25,8 @@ public class MetricsRulesTest extends SimpleAggregatorTst {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
// addRule(RULESET, "CyclomaticComplexity");
|
addRule(RULESET, "CyclomaticComplexity");
|
||||||
// addRule(RULESET, "NcssCount");
|
addRule(RULESET, "NcssCount");
|
||||||
addRule(RULESET, "NPathComplexity");
|
addRule(RULESET, "NPathComplexity");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
public class Foo {
|
public class Foo {
|
||||||
public static void bar() {
|
public static void bar() {
|
||||||
boolean a, b = true;
|
boolean a, b = true;
|
||||||
try { // 2 * 2 + 2
|
try { // total 6
|
||||||
if (true) { // 2
|
if (true) { // 2
|
||||||
List buz = new ArrayList();
|
List buz = new ArrayList();
|
||||||
}
|
}
|
||||||
@ -210,6 +210,29 @@ public class Foo {
|
|||||||
]]></code>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Ensure switch has same complexity as equivalent ifs</description>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<expected-messages>
|
||||||
|
<message>'.Foo#bar()' has value 7.</message>
|
||||||
|
</expected-messages>
|
||||||
|
<code><![CDATA[
|
||||||
|
class Foo {
|
||||||
|
void bar() {
|
||||||
|
boolean a, b;
|
||||||
|
int j = 23;
|
||||||
|
if (j == 1 || j == 2) {
|
||||||
|
} else if (j == 3) {
|
||||||
|
j = 5;
|
||||||
|
} else if (j == 4) {
|
||||||
|
if (b && a) { bar(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
|
||||||
<test-code>
|
<test-code>
|
||||||
<description>Boolean operators</description>
|
<description>Boolean operators</description>
|
||||||
<expected-problems>1</expected-problems>
|
<expected-problems>1</expected-problems>
|
||||||
@ -227,8 +250,7 @@ public class Foo {
|
|||||||
|
|
||||||
|
|
||||||
<code-fragment id="bug3484404"><![CDATA[
|
<code-fragment id="bug3484404"><![CDATA[
|
||||||
class Bar
|
class Bar {
|
||||||
{
|
|
||||||
public void x(boolean x, boolean y) {
|
public void x(boolean x, boolean y) {
|
||||||
z((x ? 1 : 2), (y ? 3 : 4));
|
z((x ? 1 : 2), (y ? 3 : 4));
|
||||||
}
|
}
|
||||||
@ -254,4 +276,28 @@ class Bar
|
|||||||
<code-ref id="bug3484404"/>
|
<code-ref id="bug3484404"/>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>NPath supports constructors</description>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<expected-messages>
|
||||||
|
<message>'.Foo#Foo()' has value 7.</message>
|
||||||
|
</expected-messages>
|
||||||
|
<code><![CDATA[
|
||||||
|
class Foo {
|
||||||
|
Foo() {
|
||||||
|
boolean a, b;
|
||||||
|
int j = 23;
|
||||||
|
switch(j) {
|
||||||
|
case 1:
|
||||||
|
case 2: break;
|
||||||
|
case 3: j = 5; break;
|
||||||
|
case 4: if (b && a) { bar(); } break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
</test-data>
|
</test-data>
|
||||||
|
@ -1,100 +1,196 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<test-data>
|
<test-data>
|
||||||
|
|
||||||
<test-code>
|
<code-fragment id="full-example"><![CDATA[
|
||||||
<description>ok</description>
|
|
||||||
<expected-problems>0</expected-problems>
|
|
||||||
<code><![CDATA[
|
|
||||||
public class Foo {
|
public class Foo {
|
||||||
public static void bar() {
|
public static void bar() {
|
||||||
if (true) {List buz = new ArrayList();}
|
boolean a, b = true;
|
||||||
}
|
try { // 2 * 2 + 2
|
||||||
|
if (true) { // 2
|
||||||
|
List buz = new ArrayList();
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < 19; i++) { // 2
|
||||||
|
List buz = new ArrayList();
|
||||||
|
}
|
||||||
|
} catch(Exception e) {
|
||||||
|
if (true) { // 2
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int j = 0;
|
||||||
|
if (true || a && b) { // 4
|
||||||
|
j = 10;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (j++ < 20) { // 2
|
||||||
|
List buz = new ArrayList();
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(j) { // 7
|
||||||
|
case 1:
|
||||||
|
case 2: break;
|
||||||
|
case 3: j = 5; break;
|
||||||
|
case 4: if (b && a) { bar(); } break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
do { // 3
|
||||||
|
List buz = new ArrayList();
|
||||||
|
} while (a && j++ < 30);
|
||||||
|
|
||||||
|
if (b) { // 2
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]]></code>
|
]]></code-fragment>
|
||||||
</test-code>
|
|
||||||
|
|
||||||
<test-code>
|
<test-code>
|
||||||
<description>fail, with minimum</description>
|
<description>Full example</description>
|
||||||
<rule-property name="reportLevel">1</rule-property>
|
|
||||||
<expected-problems>1</expected-problems>
|
<expected-problems>1</expected-problems>
|
||||||
<expected-messages>
|
<expected-messages>
|
||||||
<message>The method bar() has an NPath complexity of 2</message>
|
<message>The method 'bar()' has an NPath complexity of 2016</message>
|
||||||
|
</expected-messages>
|
||||||
|
<code-ref id="full-example"/>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Test default report level - report 200</description>
|
||||||
|
<rule-property name="reportLevel">0</rule-property>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<expected-messages>
|
||||||
|
<message>The method 'bar()' has an NPath complexity of 200</message>
|
||||||
</expected-messages>
|
</expected-messages>
|
||||||
<code><![CDATA[
|
<code><![CDATA[
|
||||||
public class Foo {
|
public class Foo {
|
||||||
public static void bar() {
|
public static void bar() {
|
||||||
if (true) {List buz = new ArrayList();}
|
boolean a, b = true;
|
||||||
}
|
int j = 0;
|
||||||
}
|
|
||||||
]]></code>
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
|
||||||
<test-code>
|
<test-code>
|
||||||
<description>Failure case</description>
|
<description>Test default report level - ignore 199</description>
|
||||||
<expected-problems>1</expected-problems>
|
<expected-problems>0</expected-problems>
|
||||||
<code><![CDATA[
|
<code><![CDATA[
|
||||||
public class Foo {
|
class Foo {
|
||||||
public static int bar() {
|
void bar() {
|
||||||
try{
|
boolean a, b;
|
||||||
if (true) {List buz = new ArrayList();}
|
try {
|
||||||
for(int i = 0; i < 19; i++) {List buz = new ArrayList();}
|
switch(j) { // 7
|
||||||
int j = 0;
|
case 1:
|
||||||
if (true) {j = 10;}
|
case 2: break;
|
||||||
while (j++ < 20) {List buz = new ArrayList();}
|
case 3: j = 5; break;
|
||||||
if (true) {j = 21;}
|
case 4: if (b && a) { bar(); } break;
|
||||||
if(false) {j = 0;}
|
default: break;
|
||||||
do {List buz = new ArrayList();} while (j++ < 30);
|
}
|
||||||
} catch(Exception e){
|
|
||||||
if (true) {e.printStackTrace();}
|
switch(j) { // * 7
|
||||||
}
|
case 1:
|
||||||
if (true) {return 1;}
|
case 2: break;
|
||||||
else {return 2;}
|
case 3: j = 5; break;
|
||||||
}
|
case 4: if (b && a) { bar(); } break;
|
||||||
}
|
default: break;
|
||||||
]]></code>
|
}
|
||||||
|
|
||||||
|
if (true || a || b); // * 4
|
||||||
|
|
||||||
|
} catch (ScaryException e) {
|
||||||
|
if (true || a); // + 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Boolean operators</description>
|
||||||
|
<rule-property name="reportLevel">2</rule-property>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<expected-messages>
|
||||||
|
<message>The method 'bar()' has an NPath complexity of 4</message>
|
||||||
|
</expected-messages>
|
||||||
|
<code><![CDATA[
|
||||||
|
class Foo {
|
||||||
|
void bar() {
|
||||||
|
if (a && b || c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
|
||||||
<code-fragment id="bug3484404"><![CDATA[
|
<code-fragment id="bug3484404"><![CDATA[
|
||||||
class Bar
|
class Bar {
|
||||||
{
|
public void x(boolean x, boolean y) {
|
||||||
public void x(boolean x, boolean y)
|
z((x ? 1 : 2), (y ? 3 : 4));
|
||||||
{
|
}
|
||||||
z(
|
|
||||||
(x ? 1 : 2),
|
|
||||||
(y ? 3 : 4)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int y(boolean x, boolean y)
|
public int y(boolean x, boolean y) {
|
||||||
{
|
return z((x ? 1 : 2), (y ? 3 : 4));
|
||||||
return z(
|
}
|
||||||
(x ? 1 : 2),
|
|
||||||
(y ? 3 : 4)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int z(int x, int y)
|
public int z(int x, int y) {
|
||||||
{
|
return x + y;
|
||||||
return x + y;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]]></code-fragment>
|
]]></code-fragment>
|
||||||
|
|
||||||
<test-code>
|
<test-code>
|
||||||
<description>test case for bug 3484404 (Invalid NPath calculation in return statement)</description>
|
<description>test case for bug 3484404 (Invalid NPath calculation in return statement)</description>
|
||||||
<expected-problems>0</expected-problems>
|
<rule-property name="reportLevel">5</rule-property>
|
||||||
<code-ref id="bug3484404"/>
|
|
||||||
</test-code>
|
|
||||||
|
|
||||||
<test-code>
|
|
||||||
<description>test case for bug 3484404 (Invalid NPath calculation in return statement) with minimum 25</description>
|
|
||||||
<rule-property name="minimum">25.0</rule-property>
|
|
||||||
<expected-problems>2</expected-problems>
|
<expected-problems>2</expected-problems>
|
||||||
<expected-messages>
|
<expected-messages>
|
||||||
<message>The method x() has an NPath complexity of 25</message>
|
<message>The method 'x(boolean, boolean)' has an NPath complexity of 25</message>
|
||||||
<message>The method y() has an NPath complexity of 25</message>
|
<message>The method 'y(boolean, boolean)' has an NPath complexity of 25</message>
|
||||||
</expected-messages>
|
</expected-messages>
|
||||||
<code-ref id="bug3484404"/>
|
<code-ref id="bug3484404"/>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>NPath supports constructors</description>
|
||||||
|
<rule-property name="reportLevel">6</rule-property>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<expected-messages>
|
||||||
|
<message>The constructor 'Foo()' has an NPath complexity of 7</message>
|
||||||
|
</expected-messages>
|
||||||
|
<code><![CDATA[
|
||||||
|
class Foo {
|
||||||
|
Foo() {
|
||||||
|
boolean a, b;
|
||||||
|
int j = 23;
|
||||||
|
switch(j) {
|
||||||
|
case 1:
|
||||||
|
case 2: break;
|
||||||
|
case 3: j = 5; break;
|
||||||
|
case 4: if (b && a) { bar(); } break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
</test-data>
|
</test-data>
|
||||||
|
Reference in New Issue
Block a user