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