pmd: added test for bug 3484404 (NPathComplexity and return statements)

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7705 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Andreas Dangel
2012-07-19 16:47:43 +00:00
parent c629e28711
commit 6926ba0647
2 changed files with 52 additions and 2 deletions

View File

@ -10,7 +10,6 @@ import java.util.Iterator;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.java.rule.codesize.NPathComplexityRule;
import net.sourceforge.pmd.testframework.RuleTst;
import net.sourceforge.pmd.testframework.TestDescriptor;
@ -33,10 +32,30 @@ public class NPathComplexityTest extends RuleTst {
rule.setProperty(NPathComplexityRule.MINIMUM_DESCRIPTOR, 1.0);
Report report = new Report();
runTestFromString(tests[0].getCode(), rule, report);
Iterator i = report.iterator();
Iterator<RuleViolation> i = report.iterator();
RuleViolation rv = (RuleViolation) i.next();
assertEquals("correct violation message", "The method bar() has an NPath complexity of 2", rv.getDescription());
}
/**
* Runs the 3rd test case with the proper threshold property.
* @throws Exception any error
*/
@Test
public void testReturnValueComplexity() throws Exception {
rule.setProperty(NPathComplexityRule.MINIMUM_DESCRIPTOR, 25.0);
Report report = new Report();
runTestFromString(tests[2].getCode(), rule, report);
Iterator<RuleViolation> i = report.iterator();
String descriptions = "";
while (i.hasNext()) {
RuleViolation violation = i.next();
descriptions += violation.getDescription() + "\n";
}
assertEquals("expected violations", 2, report.size());
assertEquals("The method x() has an NPath complexity of 25\nThe method y() has an NPath complexity of 25\n",
descriptions);
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(NPathComplexityTest.class);

View File

@ -39,4 +39,35 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
test case for bug 3484404 (Invalid NPath calculation in return statement)
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
class Bar
{
public void x(boolean x, boolean y)
{
z(
(x ? 1 : 2),
(y ? 3 : 4)
);
}
public int y(boolean x, boolean y)
{
return z(
(x ? 1 : 2),
(y ? 3 : 4)
);
}
public int z(int x, int y)
{
return x + y;
}
}
]]></code>
</test-code>
</test-data>