1 package test.net.sourceforge.pmd.rules;
2
3 import net.sourceforge.pmd.PMD;
4 import net.sourceforge.pmd.Rule;
5 import net.sourceforge.pmd.rules.XPathRule;
6
7 public class FinalFieldCouldBeStaticRuleTest extends SimpleAggregatorTst {
8
9 private Rule rule;
10
11 public void setUp() {
12 rule = new XPathRule();
13 rule.addProperty("xpath", "//FieldDeclaration[@Final='true' and @Static='false']/VariableDeclarator/VariableInitializer/Expression/ConditionalAndExpression/InstanceOfExpression/PrimaryExpression/PrimaryPrefix/Literal");
14 }
15
16 public void testAll() {
17 runTests(new TestDescriptor[] {
18 new TestDescriptor(TEST1, "simple failure case", 1, rule),
19 new TestDescriptor(TEST2, "already static, OK", 0, rule),
20 new TestDescriptor(TEST3, "non-final, OK", 0, rule),
21 new TestDescriptor(TEST4, "non-primitive failure case - only works for String", 1, rule),
22 new TestDescriptor(TEST5, "final field that's a thread, OK", 0, rule)
23 });
24 }
25
26 private static final String TEST1 =
27 "public class Foo {" + PMD.EOL +
28 " public final int BAR = 42;" + PMD.EOL +
29 "}";
30
31 private static final String TEST2 =
32 "public class Foo {" + PMD.EOL +
33 " public static final int BAR = 42;" + PMD.EOL +
34 "}";
35
36 private static final String TEST3 =
37 "public class Foo {" + PMD.EOL +
38 " public int BAR = 42;" + PMD.EOL +
39 "}";
40
41 private static final String TEST4 =
42 "public class Foo {" + PMD.EOL +
43 " public final String BAR = \"42\";" + PMD.EOL +
44 "}";
45
46 private static final String TEST5 =
47 "public class Foo {" + PMD.EOL +
48 " public final Thread BAR = new Thread();" + PMD.EOL +
49 "}";
50
51 }
This page was automatically generated by Maven