From dd82e9730e27d0398db51415bb298f7258924bb2 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Fri, 7 Feb 2003 18:50:06 +0000 Subject: [PATCH] Added some tests for CyclomaticComplexityRule git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1408 51baf565-9d33-0410-a72c-fc3788e3496d --- .../rules/CyclomaticComplexityRuleTest.java | 47 ++++++++++++++ pmd/test-data/CyclomaticComplexity1.java | 3 + pmd/test-data/CyclomaticComplexity2.java | 64 +++++++++++++++++++ pmd/test-data/CyclomaticComplexity3.java | 3 + 4 files changed, 117 insertions(+) create mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/CyclomaticComplexityRuleTest.java create mode 100644 pmd/test-data/CyclomaticComplexity1.java create mode 100644 pmd/test-data/CyclomaticComplexity2.java create mode 100644 pmd/test-data/CyclomaticComplexity3.java diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/CyclomaticComplexityRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/CyclomaticComplexityRuleTest.java new file mode 100644 index 0000000000..64bfe4f05e --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/CyclomaticComplexityRuleTest.java @@ -0,0 +1,47 @@ +package test.net.sourceforge.pmd.rules; + +import net.sourceforge.pmd.rules.CyclomaticComplexityRule; +import net.sourceforge.pmd.Report; +import net.sourceforge.pmd.RuleViolation; + +import java.util.Iterator; + +public class CyclomaticComplexityRuleTest extends RuleTst { + + private CyclomaticComplexityRule rule = new CyclomaticComplexityRule(); + + public void setUp() { + rule.setMessage("The {0} ''{1}'' has a Cyclomatic Complexity of {2}."); + } + + public void testOneMethod() throws Throwable { + rule.addProperty("reportLevel", "1"); + Report report = process("CyclomaticComplexity1.java", rule); + Iterator i = report.iterator(); + RuleViolation rv = (RuleViolation)i.next(); + assertTrue(rv.getDescription().indexOf("Highest = 1") != -1); + } + + public void testNastyComplicatedMethod() throws Throwable { + rule.addProperty("reportLevel", "10"); + Report report = process("CyclomaticComplexity2.java", rule); + Iterator i = report.iterator(); + RuleViolation rv = (RuleViolation)i.next(); + assertTrue(rv.getDescription().indexOf("Highest = 12") != -1); + } + + public void testConstructor() throws Throwable { + rule.addProperty("reportLevel", "1"); + Report report = process("CyclomaticComplexity3.java", rule); + Iterator i = report.iterator(); + RuleViolation rv = (RuleViolation)i.next(); + assertTrue(rv.getDescription().indexOf("Highest = 1") != -1); + } + + public void testLessComplicatedThanReportLevel() throws Throwable { + rule.addProperty("reportLevel", "10"); + Report report = process("CyclomaticComplexity1.java", rule); + assertEquals(0, report.size()); + } + +} diff --git a/pmd/test-data/CyclomaticComplexity1.java b/pmd/test-data/CyclomaticComplexity1.java new file mode 100644 index 0000000000..73b003ac21 --- /dev/null +++ b/pmd/test-data/CyclomaticComplexity1.java @@ -0,0 +1,3 @@ +public class CyclomaticComplexity1 { + public void foo() {} +} \ No newline at end of file diff --git a/pmd/test-data/CyclomaticComplexity2.java b/pmd/test-data/CyclomaticComplexity2.java new file mode 100644 index 0000000000..7439375e08 --- /dev/null +++ b/pmd/test-data/CyclomaticComplexity2.java @@ -0,0 +1,64 @@ +public class CyclomaticComplexity2 { + public void example() { + int x = 0; + int a = 0; + int b = 0; + int c = 0; + int d = 0; + int a1 = 0; + int a2 = 0; + int b1 = 0; + int b2 = 0; + int z = 0; + int h = 0; + int e = 0; + int f = 0; + + if (a == b) { + if (a1 == b1) { + x=2; + } else if (a2 == b2) { + x=2; + } + else + { + x=2; + } + } + else if (c == d) + { + while (c == d) + { + x=2; + } + } + else if (e == f) + { + for (int n = 0; n < h; n++) + { + x=2; + } + } + else + { + switch (z) + { + case 1: + x=2; + break; + + case 2: + x=2; + break; + + case 3: + x=2; + break; + + default: + x=2; + break; + } + } + } +} \ No newline at end of file diff --git a/pmd/test-data/CyclomaticComplexity3.java b/pmd/test-data/CyclomaticComplexity3.java new file mode 100644 index 0000000000..e927153c78 --- /dev/null +++ b/pmd/test-data/CyclomaticComplexity3.java @@ -0,0 +1,3 @@ +public class CyclomaticComplexity3 { + public CyclomaticComplexity3() {} +} \ No newline at end of file