    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 test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    10
    11  public class UnusedPrivateFieldRuleTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("unusedcode", "UnusedPrivateField");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "simple unused private field", 1, rule),
    22              new TestDescriptor(TEST2, "private field referenced in another field's initializer", 0, rule),
    23              new TestDescriptor(TEST3, "private field with field of same name in anonymous inner class", 1, rule),
    24              new TestDescriptor(TEST4, "field is used semantically before it's declared syntactically", 0, rule),
    25              new TestDescriptor(TEST5, "private field referenced via 'this' modifier", 0, rule),
    26              new TestDescriptor(TEST6, "private referenced by anonymous inner class", 0, rule),
    27              new TestDescriptor(TEST7, "interface sanity test", 0, rule),
    28              new TestDescriptor(TEST8, "unused private field in static inner class", 1, rule),
    29              new TestDescriptor(TEST9, "private field referenced in nonstatic inner class", 0, rule),
    30              new TestDescriptor(TEST10, "unused private static field", 1, rule),
    31              new TestDescriptor(TEST11, "private static final referenced with qualifier", 0, rule),
    32              new TestDescriptor(TEST12, "unused private field after class decl", 1, rule),
    33              new TestDescriptor(TEST13, "two unused private fields in separate inner classes", 2, rule),
    34              new TestDescriptor(TEST14, "method param shadows unused private field", 1, rule),
    35              new TestDescriptor(TEST15, "private field referenced via 'this' not shadowed by param of same name", 0, rule),
    36              new TestDescriptor(TEST16, "don't catch public fields", 0, rule),
    37              new TestDescriptor(TEST17, "instantiate self and reference private field on other object", 0, rule),
    38              new TestDescriptor(TEST18, "don't count Serialization fields as being unused", 0, rule),
    39              new TestDescriptor(TEST19, "an assignment does not a usage make", 1, rule),
    40              new TestDescriptor(TEST20, "assignment to field member is a usage", 0, rule),
    41              new TestDescriptor(TEST21, "assignment to field member using this modifier is a usage", 0, rule),
    42              new TestDescriptor(TEST22, "this.foo++ shouldn't throw an NPE, but isn't a usage", 1, rule),
    43              new TestDescriptor(TEST23, "super.foo++ shouldn't throw an NPE", 0, rule),
    44          });
    45      }
    46
    47      private static final String TEST1 =
    48              "public class Foo {" + PMD.EOL +
    49              "private String foo;" + PMD.EOL +
    50              "}";
    51
    52      private static final String TEST2 =
    53              "public class Foo {" + PMD.EOL +
    54              " private String foo;" + PMD.EOL +
    55              " private String bar = foo;" + PMD.EOL +
    56              " void buz() {" + PMD.EOL +
    57              "  bar = bar + 1;" + PMD.EOL +
    58              " }" + PMD.EOL +
    59              "}";
    60
    61      private static final String TEST3 =
    62              "public class Foo {" + PMD.EOL +
    63              " private String foo;" + PMD.EOL +
    64              " void baz() {" + PMD.EOL +
    65              "  Runnable r = new Runnable() {" + PMD.EOL +
    66              "   public void run() {" + PMD.EOL +
    67              "    String foo = \"buz\";" + PMD.EOL +
    68              "   }" + PMD.EOL +
    69              "  };   " + PMD.EOL +
    70              " }" + PMD.EOL +
    71              "}";
    72
    73      private static final String TEST4 =
    74              "public class Foo {" + PMD.EOL +
    75              " void bar() {" + PMD.EOL +
    76              "  foo[0] = foo[0] + 1;" + PMD.EOL +
    77              " }" + PMD.EOL +
    78              " private int[] foo;" + PMD.EOL +
    79              "}";
    80
    81      private static final String TEST5 =
    82              "public class Foo {" + PMD.EOL +
    83              " private String foo;" + PMD.EOL +
    84              " void bar() {   " + PMD.EOL +
    85              "  bar = this.foo;" + PMD.EOL +
    86              " }" + PMD.EOL +
    87              "}";
    88
    89      private static final String TEST6 =
    90              "public class Foo {" + PMD.EOL +
    91              " private static final String FOO = \"foo\";" + PMD.EOL +
    92              "  public Runnable bar() {      " + PMD.EOL +
    93              "   return new Runnable() {" + PMD.EOL +
    94              "    public void run() {" + PMD.EOL +
    95              "     FOO.toString();" + PMD.EOL +
    96              "    }" + PMD.EOL +
    97              "   };" + PMD.EOL +
    98              "  }" + PMD.EOL +
    99              "}";
    100
    101     private static final String TEST7 =
    102             "public interface Foo {" + PMD.EOL +
    103             " public static final String FOO = \"FOO\"; " + PMD.EOL +
    104             " public boolean equals(Object another);" + PMD.EOL +
    105             " public int hashCode();" + PMD.EOL +
    106             " public String toString();" + PMD.EOL +
    107             "}";
    108
    109     private static final String TEST8 =
    110             "public class Foo {" + PMD.EOL +
    111             " public static class Services {" + PMD.EOL +
    112             "  private String x;    " + PMD.EOL +
    113             " }" + PMD.EOL +
    114             "}" + PMD.EOL +
    115             "";
    116
    117     private static final String TEST9 =
    118             "public class Foo {" + PMD.EOL +
    119             " private int x;" + PMD.EOL +
    120             " private class Bar {" + PMD.EOL +
    121             "  void baz() {" + PMD.EOL +
    122             "   x = x + 2;" + PMD.EOL +
    123             "  }" + PMD.EOL +
    124             " }" + PMD.EOL +
    125             "}";
    126
    127     private static final String TEST10 =
    128             "public class Foo {" + PMD.EOL +
    129             " private static String foo;" + PMD.EOL +
    130             "}";
    131
    132     private static final String TEST11 =
    133             "public class Foo {" + PMD.EOL +
    134             " private static final int BAR = 2;" + PMD.EOL +
    135             " int x = Foo.BAR;" + PMD.EOL +
    136             "}";
    137
    138     private static final String TEST12 =
    139             "public class Foo {" + PMD.EOL +
    140             " public class Foo {}" + PMD.EOL +
    141             " private int x;" + PMD.EOL +
    142             "}";
    143
    144     private static final String TEST13 =
    145             "public class Foo {" + PMD.EOL +
    146             " public class Foo {private int x;}" + PMD.EOL +
    147             " public class Bar {private int x;}" + PMD.EOL +
    148             "}";
    149
    150     private static final String TEST14 =
    151             "public class Foo {" + PMD.EOL +
    152             " private int value;" + PMD.EOL +
    153             " int doSomething(int value) { " + PMD.EOL +
    154             "  return value + 1; " + PMD.EOL +
    155             " }" + PMD.EOL +
    156             "}";
    157
    158     private static final String TEST15 =
    159             "public class Foo {" + PMD.EOL +
    160             " private int x; " + PMD.EOL +
    161             " public Foo(int x) {" + PMD.EOL +
    162             "  this.x= this.x + 1;" + PMD.EOL +
    163             " }" + PMD.EOL +
    164             "}";
    165
    166     private static final String TEST16 =
    167             "public class Foo {" + PMD.EOL +
    168             " public static final int FOO = 1;" + PMD.EOL +
    169             "}";
    170
    171     private static final String TEST17 =
    172             "public class Foo {" + PMD.EOL +
    173             " private int x;" + PMD.EOL +
    174             " void foo() {" + PMD.EOL +
    175             "  Foo foo = new Foo();  " + PMD.EOL +
    176             "  foo.x = foo.x + 2;" + PMD.EOL +
    177             " }" + PMD.EOL +
    178             "}";
    179
    180     private static final String TEST18 =
    181             "public class Foo {" + PMD.EOL +
    182             " private static final ObjectStreamField[] serialPersistentFields = {new ObjectStreamField(\"foo\", String.class)};" + PMD.EOL +
    183             "}";
    184
    185     private static final String TEST19 =
    186             "public class Foo {" + PMD.EOL +
    187             " private int x;" + PMD.EOL +
    188             " void bar() {" + PMD.EOL +
    189             "  x = 4;" + PMD.EOL +
    190             " }" + PMD.EOL +
    191             "}";
    192
    193     private static final String TEST20 =
    194             "public class Foo {" + PMD.EOL +
    195             " private Foo x = new Foo();" + PMD.EOL +
    196             " void bar() {" + PMD.EOL +
    197             "  x.y = 42;" + PMD.EOL +
    198             " }" + PMD.EOL +
    199             "}";
    200
    201     private static final String TEST21 =
    202             "public class Foo {" + PMD.EOL +
    203             " private Foo x = new Foo();" + PMD.EOL +
    204             " void bar() {" + PMD.EOL +
    205             "  this.x.y = 42;" + PMD.EOL +
    206             " }" + PMD.EOL +
    207             "}";
    208
    209     private static final String TEST22 =
    210             "public class Foo {" + PMD.EOL +
    211             " private int x;" + PMD.EOL +
    212             " void bar() {" + PMD.EOL +
    213             "  this.x++;" + PMD.EOL +
    214             " }" + PMD.EOL +
    215             "}";
    216
    217     private static final String TEST23 =
    218             "public class Foo {" + PMD.EOL +
    219             " void bar() {" + PMD.EOL +
    220             "  super.x++;" + PMD.EOL +
    221             " }" + PMD.EOL +
    222             "}";
    223
    224 }