View Javadoc
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.rules.XPathRule; 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() { 17 rule = new XPathRule(); 18 rule.addProperty("xpath", "//FieldDeclaration[not (ancestor::InterfaceDeclaration)][@Final='true' and @Static='false']/VariableDeclarator/VariableInitializer/Expression/ConditionalAndExpression/InstanceOfExpression/UnaryExpression/PrimaryExpression/PrimaryPrefix/Literal"); 19 } 20 21 public void testAll() { 22 runTests(new TestDescriptor[] { 23 new TestDescriptor(TEST1, "simple failure case", 1, rule), 24 new TestDescriptor(TEST2, "already static, OK", 0, rule), 25 new TestDescriptor(TEST3, "non-final, OK", 0, rule), 26 new TestDescriptor(TEST4, "non-primitive failure case - only works for String", 1, rule), 27 new TestDescriptor(TEST5, "final field that's a thread, OK", 0, rule), 28 new TestDescriptor(TEST6, "don't flag interfaces", 0, rule) 29 }); 30 } 31 32 private static final String TEST1 = 33 "public class Foo {" + PMD.EOL + 34 " public final int BAR = 42;" + PMD.EOL + 35 "}"; 36 37 private static final String TEST2 = 38 "public class Foo {" + PMD.EOL + 39 " public static final int BAR = 42;" + PMD.EOL + 40 "}"; 41 42 private static final String TEST3 = 43 "public class Foo {" + PMD.EOL + 44 " public int BAR = 42;" + PMD.EOL + 45 "}"; 46 47 private static final String TEST4 = 48 "public class Foo {" + PMD.EOL + 49 " public final String BAR = \"42\";" + PMD.EOL + 50 "}"; 51 52 private static final String TEST5 = 53 "public class Foo {" + PMD.EOL + 54 " public final Thread BAR = new Thread();" + PMD.EOL + 55 "}"; 56 57 private static final String TEST6 = 58 "public interface Foo {" + PMD.EOL + 59 " public final int BAR = 42;" + PMD.EOL + 60 "}"; 61 62 }

This page was automatically generated by Maven