    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.RuleSetNotFoundException;
    6   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    7   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    8
    9   public class SingularFieldRuleTest extends SimpleAggregatorTst {
    10      private Rule rule;
    11
    12      public void setUp() throws RuleSetNotFoundException {
    13          rule = findRule("controversial", "SingularField");
    14      }
    15
    16      public void testAll() {
    17          runTests(new TestDescriptor[]{
    18              new TestDescriptor(TEST1, "failure case", 1, rule),
    19              new TestDescriptor(TEST2, "ok", 0, rule),
    20              new TestDescriptor(TEST3, "second method uses 'this'", 0, rule),
    21              new TestDescriptor(TEST4, "skip publics", 0, rule),
    22              new TestDescriptor(TEST5, "skip statics", 0, rule),
    23              new TestDescriptor(TEST6, "unused fields shouldn't show up", 0, rule),
    24              new TestDescriptor(TEST7, "inner class", 0, rule),
    25          });
    26      }
    27
    28      private static final String TEST1 =
    29              "public class Foo {" + PMD.EOL +
    30              " private int x;" + PMD.EOL +
    31              " int bar(int y) {" + PMD.EOL +
    32              "  x = y + 5; " + PMD.EOL +
    33              "  return x;" + PMD.EOL +
    34              " }" + PMD.EOL +
    35              "}";
    36
    37      private static final String TEST2 =
    38              "public class Foo {" + PMD.EOL +
    39              " private int x;" + PMD.EOL +
    40              " void setX(int x) {" + PMD.EOL +
    41              "  this.x = x;" + PMD.EOL +
    42              " }" + PMD.EOL +
    43              " int getX() {" + PMD.EOL +
    44              "  return x;" + PMD.EOL +
    45              " }" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST3 =
    49              "public class Foo {" + PMD.EOL +
    50              " private int x;" + PMD.EOL +
    51              " void setX(int x) {" + PMD.EOL +
    52              "  this.x = x;" + PMD.EOL +
    53              " }" + PMD.EOL +
    54              " int getX() {" + PMD.EOL +
    55              "  return this.x;" + PMD.EOL +
    56              " }" + PMD.EOL +
    57              "}";
    58
    59      private static final String TEST4 =
    60              "public class Foo {" + PMD.EOL +
    61              " public int x;" + PMD.EOL +
    62              " int bar(int y) {" + PMD.EOL +
    63              "  x = y + 5; " + PMD.EOL +
    64              "  return x;" + PMD.EOL +
    65              " }" + PMD.EOL +
    66              "}";
    67
    68      private static final String TEST5 =
    69              "public class Foo {" + PMD.EOL +
    70              " private static int x;" + PMD.EOL +
    71              " int bar(int y) {" + PMD.EOL +
    72              "  x = y + 5; " + PMD.EOL +
    73              "  return x;" + PMD.EOL +
    74              " }" + PMD.EOL +
    75              "}";
    76
    77      private static final String TEST6 =
    78              "public class Foo {" + PMD.EOL +
    79              " int a = 3;" + PMD.EOL +
    80              " int b = 3;" + PMD.EOL +
    81              " void bar() {" + PMD.EOL +
    82              "  foo(b); " + PMD.EOL +
    83              " }" + PMD.EOL +
    84              "}";
    85
    86      private static final String TEST7 =
    87              "public class Foo {" + PMD.EOL +
    88              " private int a;" + PMD.EOL +
    89              " void bar() {" + PMD.EOL +
    90              "  Object o = new FooAdapter() {" + PMD.EOL +
    91              "   public void bar(Event e) {" + PMD.EOL +
    92              "    a = e.GetInt();" + PMD.EOL +
    93              "   }" + PMD.EOL +
    94              "  }; " + PMD.EOL +
    95              " }" + PMD.EOL +
    96              " int baz() {" + PMD.EOL +
    97              "  return a; " + PMD.EOL +
    98              " }" + PMD.EOL +
    99              "}";
    100
    101 }