1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.rules;
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 FinalFieldCouldBeStaticRuleTest extends SimpleAggregatorTst {
13  
14      private Rule rule;
15  
16      public void setUp() throws RuleSetNotFoundException {
17          rule = findRule("design", "FinalFieldCouldBeStatic");
18      }
19  
20      public void testAll() {
21          runTests(new TestDescriptor[]{
22              new TestDescriptor(TEST1, "simple failure case", 1, rule),
23              new TestDescriptor(TEST2, "already static, OK", 0, rule),
24              new TestDescriptor(TEST3, "non-final, OK", 0, rule),
25              new TestDescriptor(TEST4, "non-primitive failure case - only works for String", 1, rule),
26              new TestDescriptor(TEST5, "final field that's a thread, OK", 0, rule),
27              new TestDescriptor(TEST6, "don't flag interfaces", 0, rule)
28          });
29      }
30  
31      private static final String TEST1 =
32              "public class Foo {" + PMD.EOL +
33              " public final int BAR = 42;" + PMD.EOL +
34              "}";
35  
36      private static final String TEST2 =
37              "public class Foo {" + PMD.EOL +
38              " public static final int BAR = 42;" + PMD.EOL +
39              "}";
40  
41      private static final String TEST3 =
42              "public class Foo {" + PMD.EOL +
43              " public int BAR = 42;" + PMD.EOL +
44              "}";
45  
46      private static final String TEST4 =
47              "public class Foo {" + PMD.EOL +
48              " public final String BAR = \"42\";" + PMD.EOL +
49              "}";
50  
51      private static final String TEST5 =
52              "public class Foo {" + PMD.EOL +
53              " public final Thread BAR = new Thread();" + PMD.EOL +
54              "}";
55  
56      private static final String TEST6 =
57              "public interface Foo {" + PMD.EOL +
58              " public final int BAR = 42;" + PMD.EOL +
59              "}";
60  
61  }