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.TestDescriptor;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  
22  
23  @RunWith(SimpleAggregatorTst.CustomXmlTestClassMethodsRunner.class)
24  public class ModifiedCyclomaticComplexityTest extends RuleTst {
25       private Rule rule;
26       private TestDescriptor[] tests;
27   
28       @Before public void setUp() {
29           rule = findRule("java-codesize", "ModifiedCyclomaticComplexity");
30           tests = extractTestsFromXml(rule);
31       }
32   
33       @Test
34       public void testOneMethod() throws Throwable {
35           rule.setProperty(ModifiedCyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1);
36           Report report = new Report();
37           runTestFromString(tests[0].getCode(), rule, report);
38           Iterator<RuleViolation> i = report.iterator();
39           RuleViolation rv = i.next();
40           assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1);
41       }
42   
43       @Test
44       public void testNastyComplicatedMethod() throws Throwable {
45           rule.setProperty(ModifiedCyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 8);
46           Report report = new Report();
47           runTestFromString(tests[1].getCode(), rule, report);
48           Iterator<RuleViolation> i = report.iterator();
49           RuleViolation rv = i.next();
50           assertNotSame(rv.getDescription().indexOf("Highest = 9"), -1);
51       }
52   
53       @Test
54       public void testConstructor() throws Throwable {
55           rule.setProperty(ModifiedCyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 1);
56           Report report = new Report();
57           runTestFromString(tests[2].getCode(), rule, report);
58           Iterator<RuleViolation> i = report.iterator();
59           RuleViolation rv = i.next();
60           assertNotSame(rv.getDescription().indexOf("Highest = 1"), -1);
61       }
62   
63       @Test
64       public void testLessComplicatedThanReportLevel() throws Throwable {
65           rule.setProperty(ModifiedCyclomaticComplexityRule.REPORT_LEVEL_DESCRIPTOR, 10);
66           Report report = new Report();
67           runTestFromString(tests[0].getCode(), rule, report);
68           assertEquals(0, report.size());
69       }
70  
71       public static junit.framework.Test suite() {
72           return new junit.framework.JUnit4TestAdapter(ModifiedCyclomaticComplexityTest.class);
73       }
74   }