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.rules.design.LongMethodRule;
8   import test.net.sourceforge.pmd.testframework.RuleTst;
9   
10  public class LongMethodRuleTest extends RuleTst {
11  
12      private LongMethodRule getIUT() {
13          LongMethodRule IUT = new LongMethodRule();
14          IUT.addProperty("minimum", "10");
15          return IUT;
16      }
17  
18      public void testShortMethod() throws Throwable {
19          runTestFromString(TEST1, 0, getIUT());
20      }
21      public void testReallyLongMethod() throws Throwable {
22          runTestFromString(TEST2, 1, getIUT());
23      }
24      public void testReallyLongMethodWithLongerRange() throws Throwable {
25          LongMethodRule IUT = getIUT();
26          IUT.addProperty("minimum", "20");
27          runTestFromString(TEST2, 0, IUT);
28      }
29      public void testNotQuiteLongMethod() throws Throwable {
30          runTestFromString(TEST3, 0, getIUT());
31      }
32      public void testLongMethod() throws Throwable {
33          runTestFromString(TEST4, 1, getIUT());
34      }
35  
36      private static final String TEST1 =
37      "public class LongMethod1 {" + PMD.EOL +
38      "    public static void main(String args[]) {" + PMD.EOL +
39      "	System.err.println(\"This is short.\");" + PMD.EOL +
40      "    }" + PMD.EOL +
41      "}";
42  
43      private static final String TEST2 =
44      "public class LongMethod2 {" + PMD.EOL +
45      "    public static void main(String args[]) {" + PMD.EOL +
46      "	System.err.println(\"This is long.\");" + PMD.EOL +
47      "	System.err.println(\"This is long.\");" + PMD.EOL +
48      "	System.err.println(\"This is long.\");" + PMD.EOL +
49      "	System.err.println(\"This is long.\");" + PMD.EOL +
50      "	System.err.println(\"This is long.\");" + PMD.EOL +
51      "	System.err.println(\"This is long.\");" + PMD.EOL +
52      "	System.err.println(\"This is long.\");" + PMD.EOL +
53      "	System.err.println(\"This is long.\");" + PMD.EOL +
54      "	System.err.println(\"This is long.\");" + PMD.EOL +
55      "	System.err.println(\"This is long.\");" + PMD.EOL +
56      "	System.err.println(\"This is long.\");" + PMD.EOL +
57      "	System.err.println(\"This is long.\");" + PMD.EOL +
58      "    } // 11 lines - violation" + PMD.EOL +
59      "}";
60  
61      private static final String TEST3 =
62      "public class LongMethod2 {" + PMD.EOL +
63      "    public static void main(String args[]) {" + PMD.EOL +
64      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
65      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
66      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
67      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
68      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
69      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
70      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
71      "	System.err.println(\"This is long, but not a violation\");" + PMD.EOL +
72      "    } // 9 lines - Not a violation" + PMD.EOL +
73      "}";
74  
75      private static final String TEST4 =
76      "public class LongMethod2 {" + PMD.EOL +
77      "    public static void main(String args[]) {" + PMD.EOL +
78      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
79      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
80      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
81      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
82      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
83      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
84      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
85      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
86      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
87      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
88      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
89      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
90      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
91      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
92      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
93      "	System.err.println(\"This is long, and is a violation\");" + PMD.EOL +
94      "    } // > 10 lines - Not a violation" + PMD.EOL +
95      "}";
96  }
97