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 VariableNamingConventionsTest extends SimpleAggregatorTst { 13 private Rule rule; 14 15 public void setUp() throws RuleSetNotFoundException { 16 rule = findRule("naming", "VariableNamingConventions"); 17 } 18 19 public void testAll() { 20 runTests(new TestDescriptor[]{ 21 new TestDescriptor(TEST1, "final statics should be all caps", 1, rule), 22 new TestDescriptor(TEST2, "non-finals shouldn't have underscores", 1, rule), 23 new TestDescriptor(TEST3, "variables names should start with lowercase character", 1, rule), 24 new TestDescriptor(TEST4, "all is well", 0, rule), 25 new TestDescriptor(TEST5, "local finals are ok", 0, rule), 26 new TestDescriptor(TEST6, "serialVersionUID is OK", 0, rule), 27 new TestDescriptor(TEST7, "interface fields are tested", 1, rule), 28 new TestDescriptor(TEST8, "final non-statics need not be all caps", 0, rule), 29 }); 30 } 31 32 public void testPrefixStripping() throws Throwable { 33 Rule r = findRule("naming", "VariableNamingConventions"); 34 r.addProperty("staticPrefix", "s_"); 35 runTestFromString(TEST9, 0, r); 36 } 37 38 public void testSuffixStripping() throws Throwable { 39 Rule r = findRule("naming", "VariableNamingConventions"); 40 r.addProperty("staticSuffix", "_s"); 41 runTestFromString(TEST10, 0, r); 42 } 43 44 private static final String TEST1 = 45 "public class Foo {" + PMD.EOL + 46 " private static final int foo = 2;" + PMD.EOL + 47 "}"; 48 49 private static final String TEST2 = 50 "public class Foo {" + PMD.EOL + 51 " private int foo_bar = 2;" + PMD.EOL + 52 "}"; 53 54 private static final String TEST3 = 55 "public class Foo {" + PMD.EOL + 56 " private int Ubar = 2;" + PMD.EOL + 57 "}"; 58 59 private static final String TEST4 = 60 "public class Foo {" + PMD.EOL + 61 " private int bar = 2;" + PMD.EOL + 62 " private static final int FOO_BAR = 2;" + PMD.EOL + 63 "}"; 64 65 private static final String TEST5 = 66 "public class Foo {" + PMD.EOL + 67 " private void bar() {" + PMD.EOL + 68 " final int STATE_READING = 0;" + PMD.EOL + 69 " }" + PMD.EOL + 70 "}"; 71 72 private static final String TEST6 = 73 "public class Foo {" + PMD.EOL + 74 " static final long serialVersionUID = 423343L;" + PMD.EOL + 75 "}"; 76 77 private static final String TEST7 = 78 "public interface Foo {" + PMD.EOL + 79 " int foo = 42;" + PMD.EOL + 80 "}"; 81 82 private static final String TEST8 = 83 "public class Foo {" + PMD.EOL + 84 " final int foo = 42;" + PMD.EOL + 85 "}"; 86 87 private static final String TEST9 = 88 "public class Foo {" + PMD.EOL + 89 " static int s_foo = 42;" + PMD.EOL + 90 "}"; 91 92 private static final String TEST10 = 93 "public class Foo {" + PMD.EOL + 94 " static int foo_s = 42;" + PMD.EOL + 95 "}"; 96 }