    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 UnusedPrivateMethodRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("unusedcode", "UnusedPrivateMethod");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "private method called by public method", 0, rule),
    23              new TestDescriptor(TEST2, "simple unused private method", 1, rule),
    24              new TestDescriptor(TEST3, "anonymous inner class calls private method", 0, rule),
    25              new TestDescriptor(TEST4, "two private methods with same name but different parameters", 1, rule),
    26              new TestDescriptor(TEST5, "calling private method after instantiating new copy of myself", 0, rule),
    27              new TestDescriptor(TEST6, "calling private method using 'this' modifier", 0, rule),
    28              new TestDescriptor(TEST7, "simple unused private static method", 1, rule),
    29              new TestDescriptor(TEST8, "readResolve/writeReplace/etc are OK", 0, rule),
    30              new TestDescriptor(TEST9, "Private methods called only by themselves, BUG 1038229", 1, rule),
    31              new TestDescriptor(TEST10, "private with same name as public, different method signature", 0, rule),
    32              new TestDescriptor(BUG_1114754, "False +, BUG 1114754", 0, rule),
    33              new TestDescriptor(TEST11, "called from constructor", 0, rule),
    34              new TestDescriptor(TEST12, "private method with same name but diff arg count than public method", 0, rule),
    35              new TestDescriptor(TEST13, "static private called from initializer", 0, rule),
    36              new TestDescriptor(TEST14, "static private invoked in static context - i.e., Foo.hi()", 0, rule),
    37              new TestDescriptor(TEST15, "private method with same name as param", 0, rule),
    38              new TestDescriptor(TEST16, "two methods, one private, one public, same name, same arg count, diff types", 0, rule),
    39              new TestDescriptor(TEST17, "two private methods, both used, same name, same arg count, diff types", 0, rule),
    40              new TestDescriptor(TEST18, "private method same name as local", 0, rule),
    41          });
    42      }
    43
    44      private static final String TEST1 =
    45              "public class Foo {" + PMD.EOL +
    46              " public void bar() {" + PMD.EOL +
    47              "  foo();" + PMD.EOL +
    48              " }" + PMD.EOL +
    49              " private void foo() {}" + PMD.EOL +
    50              "}";
    51
    52      private static final String TEST2 =
    53              "public class Foo {" + PMD.EOL +
    54              " private void foo() {}" + PMD.EOL +
    55              "}";
    56
    57      private static final String TEST3 =
    58              "public class Foo {" + PMD.EOL +
    59              " public void bar() {" + PMD.EOL +
    60              "  new Runnable() {" + PMD.EOL +
    61              "   public void run() {" + PMD.EOL +
    62              "    foo();" + PMD.EOL +
    63              "   }" + PMD.EOL +
    64              "  };" + PMD.EOL +
    65              " }" + PMD.EOL +
    66              " private void foo() {}" + PMD.EOL +
    67              "}";
    68
    69      private static final String TEST4 =
    70              "public class Foo {" + PMD.EOL +
    71              " private void foo() {}" + PMD.EOL +
    72              " private void foo(String baz) {}" + PMD.EOL +
    73              " public void bar() {" + PMD.EOL +
    74              "  foo();" + PMD.EOL +
    75              " }" + PMD.EOL +
    76              "}";
    77
    78      private static final String TEST5 =
    79              "public class Foo {" + PMD.EOL +
    80              " private void foo(String[] args) {}" + PMD.EOL +
    81              " public static void main(String[] args) {" + PMD.EOL +
    82              "  Foo u = new Foo();" + PMD.EOL +
    83              "  u.foo(args); " + PMD.EOL +
    84              " }" + PMD.EOL +
    85              "}";
    86
    87      private static final String TEST6 =
    88              "public class Foo {" + PMD.EOL +
    89              " public void bar() {" + PMD.EOL +
    90              "  this.foo();" + PMD.EOL +
    91              " }" + PMD.EOL +
    92              " private void foo() {}" + PMD.EOL +
    93              "}";
    94
    95      private static final String TEST7 =
    96              "public class Foo {" + PMD.EOL +
    97              " private static void foo() {}" + PMD.EOL +
    98              "}";
    99
    100     private static final String TEST8 =
    101             "public class Foo {" + PMD.EOL +
    102             " private void readResolve() {}" + PMD.EOL +
    103             " private void writeReplace() {}" + PMD.EOL +
    104             " private void readObject() {}" + PMD.EOL +
    105             " private void writeObject() {}" + PMD.EOL +
    106             "}";
    107
    108     private static final String TEST9 =
    109             "public class Foo {" + PMD.EOL +
    110             " private void bar() {" + PMD.EOL +
    111             "  bar(); " + PMD.EOL +
    112             " }" + PMD.EOL +
    113             "}";
    114
    115     private static final String TEST10 =
    116             "public class Foo {" + PMD.EOL +
    117             " public void bar(int x) {" + PMD.EOL +
    118             "  bar(); " + PMD.EOL +
    119             " }" + PMD.EOL +
    120             " private void bar() {}" + PMD.EOL +
    121             "}";
    122
    123     private static final String BUG_1114754 =
    124             "public class Foo {" + PMD.EOL +
    125             "   public void methodFlagged(Object[] arrayObj) {" + PMD.EOL +
    126             "       for(int i=0; i<arrayObj.length; i++) {" + PMD.EOL +
    127             "           methodFlagged(arrayObj[i]);" + PMD.EOL +
    128             "       }" + PMD.EOL +
    129             "   }" + PMD.EOL +
    130             "   private void methodFlagged(Object a) {" + PMD.EOL +
    131             "       a.toString();" + PMD.EOL +
    132             "   }" + PMD.EOL +
    133             "}";
    134
    135     private static final String TEST11 =
    136             "public class Foo {" + PMD.EOL +
    137             " public Foo() {" + PMD.EOL +
    138             "  bar();" + PMD.EOL +
    139             " }" + PMD.EOL +
    140             " private void bar() {}" + PMD.EOL +
    141             "}";
    142
    143     private static final String TEST12 =
    144             "public class Foo {" + PMD.EOL +
    145             " public void baz() {" + PMD.EOL +
    146             "  baz(x, y);" + PMD.EOL +
    147             " }" + PMD.EOL +
    148             " private void baz(int x, int y) {}" + PMD.EOL +
    149             "}";
    150
    151     private static final String TEST13 =
    152             "public class Foo {" + PMD.EOL +
    153             " static { foo(); }" + PMD.EOL +
    154             " private static void foo() {}" + PMD.EOL +
    155             "}";
    156
    157
    158     private static final String TEST14 =
    159             "public class Foo {" + PMD.EOL +
    160             " static boolean BUZ = Foo.bar(); " + PMD.EOL +
    161             " private static boolean bar() { return true; }" + PMD.EOL +
    162             "}";
    163
    164     private static final String TEST15 =
    165             "public class Foo {" + PMD.EOL +
    166             " void bar(boolean buz) { " + PMD.EOL +
    167             "  buz();" + PMD.EOL +
    168             " }" + PMD.EOL +
    169             " private void buz() {}" + PMD.EOL +
    170             "}";
    171
    172     private static final String TEST16 =
    173             "public class Foo {" + PMD.EOL +
    174             " public void baz() {" + PMD.EOL +
    175             "  foo(\"hi\");" + PMD.EOL +
    176             " }" + PMD.EOL +
    177             " private void foo(String y) {}" + PMD.EOL +
    178             " public void foo(List y) {}" + PMD.EOL +
    179             "}";
    180
    181     private static final String TEST17 =
    182             "public class Foo {" + PMD.EOL +
    183             " public void baz() {" + PMD.EOL +
    184             "  foo(getBuz());" + PMD.EOL +
    185             " }" + PMD.EOL +
    186             " private void foo(String y) {}" + PMD.EOL +
    187             " private void foo(List y) {}" + PMD.EOL +
    188             "}";
    189
    190     private static final String TEST18 =
    191             "public class Foo {" + PMD.EOL +
    192             " public void baz() {" + PMD.EOL +
    193             "  int x = x();" + PMD.EOL +
    194             " }" + PMD.EOL +
    195             " private int x() { return 42;}" + PMD.EOL +
    196             "}";
    197
    198 }