    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 ShortVariableRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("naming", "ShortVariable");
    18          rule.addProperty("pluginname", "true");
    19      }
    20
    21      public void testAll() {
    22          runTests(new TestDescriptor[]{
    23              new TestDescriptor(TEST1, "param", 1, rule),
    24              new TestDescriptor(TEST2, "none", 0, rule),
    25              new TestDescriptor(TEST3, "local", 1, rule),
    26              new TestDescriptor(TEST4, "for", 0, rule),
    27              new TestDescriptor(TEST5, "field", 1, rule),
    28              new TestDescriptor(TEST6, "catch(Exception e) is OK", 0, rule),
    29          });
    30      }
    31
    32      private static final String TEST1 =
    33              "public class Foo {" + PMD.EOL +
    34              "    public static void main(String a[]) { // a should trigger it." + PMD.EOL +
    35              "    }" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST2 =
    39              "public class Foo {" + PMD.EOL +
    40              "    public static void main(String args[]) {" + PMD.EOL +
    41              "       int bugleDeWump = -1;" + PMD.EOL +
    42              "    }" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST3 =
    46              "public class Foo {" + PMD.EOL +
    47              "" + PMD.EOL +
    48              "    public static void main(String args[]) {" + PMD.EOL +
    49              "       int ab = -1; " + PMD.EOL +
    50              "       // Should trigger ShortVariable rule." + PMD.EOL +
    51              "    }" + PMD.EOL +
    52              "}";
    53
    54      private static final String TEST4 =
    55              "public class Foo {" + PMD.EOL +
    56              "    public static void main(String args[]) {" + PMD.EOL +
    57              "       for (int i = 0; i < 10; i++) { } // Should NOT!! trigger." + PMD.EOL +
    58              "    }" + PMD.EOL +
    59              "}";
    60
    61      private static final String TEST5 =
    62              "public class Foo {" + PMD.EOL +
    63              "    private int qx; // Should cause a problem." + PMD.EOL +
    64              "}";
    65
    66      private static final String TEST6 =
    67              "public class Foo {" + PMD.EOL +
    68              "    private void bar() {" + PMD.EOL +
    69              "     try {} catch (Exception e) {}" + PMD.EOL +
    70              "    }" + PMD.EOL +
    71              "}";
    72
    73  }