1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package test.net.sourceforge.pmd.rules.design;
5   
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.Rule;
8   import net.sourceforge.pmd.RuleSetNotFoundException;
9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
11  
12  public class ExcessiveMethodLengthTest extends SimpleAggregatorTst  {
13  
14      private Rule rule;
15  
16      public void setUp() throws RuleSetNotFoundException {
17          rule = findRule("codesize", "ExcessiveMethodLength");
18          rule.addProperty("minimum", "10");
19      }
20  
21      public void testAll() {
22         runTests(new TestDescriptor[] {
23             new TestDescriptor(TEST1, "short", 0, rule),
24             new TestDescriptor(TEST2, "long", 1, rule),
25             new TestDescriptor(TEST3, "not quite long", 0, rule),
26             new TestDescriptor(TEST4, "long", 1, rule),
27         });
28      }
29  
30      public void testReallyLongMethodWithLongerRange() throws Throwable {
31          Rule r = findRule("codesize", "ExcessiveMethodLength");
32          r.addProperty("minimum", "20");
33          runTestFromString(TEST2, 0, r);
34      }
35  
36  /*
37      public void testOverrideMinimumWithTopScore() throws Throwable {
38          Rule r = findRule("codesize", "ExcessiveMethodLength");
39          r.addProperty("minimum", "1");
40          r.addProperty("topscore", "2");
41          Report rpt = new Report();
42          runTestFromString(TEST5, r, rpt);
43          for (Iterator i = rpt.iterator(); i.hasNext();) {
44              RuleViolation rv = (RuleViolation)i.next();
45              assertTrue(rv.getLine() == 2 || rv.getLine() == 6);
46          }
47      }
48  */
49  
50      private static final String TEST1 =
51      "public class Foo {" + PMD.EOL +
52      "    public static void main(String args[]) {" + PMD.EOL +
53      "	  bar();" + PMD.EOL +
54      "    }" + PMD.EOL +
55      "}";
56  
57      private static final String TEST2 =
58      "public class Foo {" + PMD.EOL +
59      "    public static void main(String args[]) {" + PMD.EOL +
60      "	  bar();" + PMD.EOL +
61      "	  bar();" + PMD.EOL +
62      "	  bar();" + PMD.EOL +
63      "	  bar();" + PMD.EOL +
64      "	  bar();" + PMD.EOL +
65      "	  bar();" + PMD.EOL +
66      "	  bar();" + PMD.EOL +
67      "	  bar();" + PMD.EOL +
68      "	  bar();" + PMD.EOL +
69      "	  bar();" + PMD.EOL +
70      "	  bar();" + PMD.EOL +
71      "    } // 11 lines - violation" + PMD.EOL +
72      "}";
73  
74      private static final String TEST3 =
75      "public class Foo {" + PMD.EOL +
76      "    public static void main(String args[]) {" + PMD.EOL +
77      "	  bar();" + PMD.EOL +
78      "	  bar();" + PMD.EOL +
79      "	  bar();" + PMD.EOL +
80      "	  bar();" + PMD.EOL +
81      "	  bar();" + PMD.EOL +
82      "	  bar();" + PMD.EOL +
83      "	  bar();" + PMD.EOL +
84      "	  bar();" + PMD.EOL +
85      "    } // 9 lines - Not a violation" + PMD.EOL +
86      "}";
87  
88      private static final String TEST4 =
89      "public class Foo {" + PMD.EOL +
90      "    public static void main(String args[]) {" + PMD.EOL +
91      "	  bar();" + PMD.EOL +
92      "	  bar();" + PMD.EOL +
93      "	  bar();" + PMD.EOL +
94      "	  bar();" + PMD.EOL +
95      "	  bar();" + PMD.EOL +
96      "	  bar();" + PMD.EOL +
97      "	  bar();" + PMD.EOL +
98      "	  bar();" + PMD.EOL +
99      "	  bar();" + PMD.EOL +
100     "	  bar();" + PMD.EOL +
101     "	  bar();" + PMD.EOL +
102     "	  bar();" + PMD.EOL +
103     "	  bar();" + PMD.EOL +
104     "	  bar();" + PMD.EOL +
105     "	  bar();" + PMD.EOL +
106     "    } // > 10 lines - Not a violation" + PMD.EOL +
107     "}";
108 
109     private static final String TEST5 =
110     "public class Foo {" + PMD.EOL +
111     "    void foo1() {" + PMD.EOL +
112     "	  bar();" + PMD.EOL +
113     "	  bar();" + PMD.EOL +
114     "	  baz();" + PMD.EOL +
115     "    }" + PMD.EOL +
116     "    void foo2() {" + PMD.EOL +
117     "	  bar();" + PMD.EOL +
118     "	  bar();" + PMD.EOL +
119     "	  baz();" + PMD.EOL +
120     "    }" + PMD.EOL +
121     "    void foo3() {" + PMD.EOL +
122     "	  bar();" + PMD.EOL +
123     "	  bar();" + PMD.EOL +
124     "	  baz();" + PMD.EOL +
125     "    }" + PMD.EOL +
126     "    void foo4() {" + PMD.EOL +
127     "	  bar();" + PMD.EOL +
128     "	  bar();" + PMD.EOL +
129     "	  baz();" + PMD.EOL +
130     "    }" + PMD.EOL +
131     "}";
132 
133 }
134