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.SimpleAggregatorTst;
17  import net.sourceforge.pmd.testframework.TestDescriptor;
18  import net.sourceforge.pmd.testframework.SimpleAggregatorTst.CustomXmlTestClassMethodsRunner;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.junit.runner.notification.Failure;
24  
25  
26  @RunWith(SimpleAggregatorTst.CustomXmlTestClassMethodsRunner.class)
27  public class CyclomaticComplexityTest extends RuleTst {
28       private Rule rule;
29       private TestDescriptor[] tests;
30   
31       @Before public void setUp() {
32           rule = findRule("java-codesize", "CyclomaticComplexity");
33           tests = extractTestsFromXml(rule);
34       }
35   
36       @Test
37       public void testOneMethod() throws Throwable {
38           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1);
39           Report report = new Report();
40           runTestFromString(tests[0].getCode(), rule, report);
41           Iterator<RuleViolation> i = report.iterator();
42           RuleViolation rv = i.next();
43           assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1);
44       }
45   
46       @Test
47       public void testNastyComplicatedMethod() throws Throwable {
48           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 10);
49           Report report = new Report();
50           runTestFromString(tests[1].getCode(), rule, report);
51           Iterator<RuleViolation> i = report.iterator();
52           RuleViolation rv = i.next();
53           assertNotSame(rv.getDescription().indexOf("Highest = 11"), -1);
54       }
55   
56       @Test
57       public void testConstructor() throws Throwable {
58           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1);
59           Report report = new Report();
60           runTestFromString(tests[2].getCode(), rule, report);
61           Iterator<RuleViolation> i = report.iterator();
62           RuleViolation rv = i.next();
63           assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1);
64       }
65   
66       @Test
67       public void testLessComplicatedThanReportLevel() throws Throwable {
68           rule.setProperty(CyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 10);
69           Report report = new Report();
70           runTestFromString(tests[0].getCode(), rule, report);
71           assertEquals(0, report.size());
72       }
73  
74       @Test
75       public void testRemainingTestCases() {
76           for (int i = 0; i < tests.length; i++) {
77               if (i == 0 || i == 1 || i == 2) {
78                   continue; // skip - covered by above test methods
79               }
80  
81               try {
82                   runTest(tests[i]);
83               } catch (Throwable t) {
84                   Failure f = CustomXmlTestClassMethodsRunner.createFailure(rule, t);
85                   CustomXmlTestClassMethodsRunner.addFailure(f);
86               }
87           }
88       }
89  
90       public static junit.framework.Test suite() {
91           return new junit.framework.JUnit4TestAdapter(CyclomaticComplexityTest.class);
92       }
93   }