View Javadoc

1    /**
2     * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3     */
4   package net.sourceforge.pmd.lang.java.rule.codesize;
5    
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertNotSame;
8   
9   import java.util.Iterator;
10  
11  import net.sourceforge.pmd.Report;
12  import net.sourceforge.pmd.Rule;
13  import net.sourceforge.pmd.RuleViolation;
14  import net.sourceforge.pmd.lang.java.rule.codesize.CyclomaticComplexityRule;
15  import net.sourceforge.pmd.testframework.RuleTst;
16  import net.sourceforge.pmd.testframework.TestDescriptor;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  
22   public class CyclomaticComplexityTest extends RuleTst {
23       private Rule rule;
24       private TestDescriptor[] tests;
25   
26       @Before public void setUp() {
27           rule = findRule("java-codesize", "CyclomaticComplexity");
28           tests = extractTestsFromXml(rule);
29       }
30   
31       @Test
32       public void testOneMethod() throws Throwable {
33           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1);
34           Report report = new Report();
35           runTestFromString(tests[0].getCode(), rule, report);
36           Iterator i = report.iterator();
37           RuleViolation rv = (RuleViolation) i.next();
38           assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1);
39       }
40   
41       @Test
42       public void testNastyComplicatedMethod() throws Throwable {
43           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 10);
44           Report report = new Report();
45           runTestFromString(tests[1].getCode(), rule, report);
46           Iterator i = report.iterator();
47           RuleViolation rv = (RuleViolation) i.next();
48           assertNotSame(rv.getDescription().indexOf("Highest = 11"), -1);
49       }
50   
51       @Test
52       public void testConstructor() throws Throwable {
53           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1);
54           Report report = new Report();
55           runTestFromString(tests[2].getCode(), rule, report);
56           Iterator i = report.iterator();
57           RuleViolation rv = (RuleViolation) i.next();
58           assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1);
59       }
60   
61       @Test
62       public void testLessComplicatedThanReportLevel() throws Throwable {
63           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 10);
64           Report report = new Report();
65           runTestFromString(tests[0].getCode(), rule, report);
66           assertEquals(0, report.size());
67       }
68  
69       public static junit.framework.Test suite() {
70           return new junit.framework.JUnit4TestAdapter(CyclomaticComplexityTest.class);
71       }
72   }