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
36 });
37 }
38
39 private static final String TEST1 =
40 "public class Foo {" + PMD.EOL +
41 " public void bar() {" + PMD.EOL +
42 " foo();" + PMD.EOL +
43 " }" + PMD.EOL +
44 " private void foo() {}" + PMD.EOL +
45 "}";
46
47 private static final String TEST2 =
48 "public class Foo {" + PMD.EOL +
49 " private void foo() {}" + PMD.EOL +
50 "}";
51
52 private static final String TEST3 =
53 "public class Foo {" + PMD.EOL +
54 " public void bar() {" + PMD.EOL +
55 " new Runnable() {" + PMD.EOL +
56 " public void run() {" + PMD.EOL +
57 " foo();" + PMD.EOL +
58 " }" + PMD.EOL +
59 " };" + PMD.EOL +
60 " }" + PMD.EOL +
61 " private void foo() {}" + PMD.EOL +
62 "}";
63
64 private static final String TEST4 =
65 "public class Foo {" + PMD.EOL +
66 " private void foo() {}" + PMD.EOL +
67 " private void foo(String baz) {}" + PMD.EOL +
68 " public void bar() {" + PMD.EOL +
69 " foo();" + PMD.EOL +
70 " }" + PMD.EOL +
71 "}";
72
73 private static final String TEST5 =
74 "public class Foo {" + PMD.EOL +
75 " private void foo(String[] args) {}" + PMD.EOL +
76 " public static void main(String[] args) {" + PMD.EOL +
77 " Foo u = new Foo();" + PMD.EOL +
78 " u.foo(args); " + PMD.EOL +
79 " }" + PMD.EOL +
80 "}";
81
82 private static final String TEST6 =
83 "public class Foo {" + PMD.EOL +
84 " public void bar() {" + PMD.EOL +
85 " this.foo();" + PMD.EOL +
86 " }" + PMD.EOL +
87 " private void foo() {}" + PMD.EOL +
88 "}";
89
90 private static final String TEST7 =
91 "public class Foo {" + PMD.EOL +
92 " private static void foo() {}" + PMD.EOL +
93 "}";
94
95 private static final String TEST8 =
96 "public class Foo {" + PMD.EOL +
97 " private void readResolve() {}" + PMD.EOL +
98 " private void writeReplace() {}" + PMD.EOL +
99 " private void readObject() {}" + PMD.EOL +
100 " private void writeObject() {}" + PMD.EOL +
101 "}";
102
103 private static final String TEST9 =
104 "public class Foo {" + PMD.EOL +
105 " private void bar() {" + PMD.EOL +
106 " bar(); " + PMD.EOL +
107 " }" + PMD.EOL +
108 "}";
109
110 private static final String TEST10 =
111 "public class Foo {" + PMD.EOL +
112 " public void bar(int x) {" + PMD.EOL +
113 " bar(); " + PMD.EOL +
114 " }" + PMD.EOL +
115 " private void bar() {}" + PMD.EOL +
116 "}";
117
118 private static final String BUG_1114754 =
119 "public class Foo {" + PMD.EOL +
120 " public void methodFlagged(Object[] arrayObj) {" + PMD.EOL +
121 " for(int i=0; i<arrayObj.length; i++) {" + PMD.EOL +
122 " methodFlagged(arrayObj[i]);" + PMD.EOL +
123 " }" + PMD.EOL +
124 " }" + PMD.EOL +
125 " private void methodFlagged(Object a) {" + PMD.EOL +
126 " a.toString();" + PMD.EOL +
127 " }" + PMD.EOL +
128 "}";
129
130 private static final String TEST11 =
131 "public class Foo {" + PMD.EOL +
132 " public Foo() {" + PMD.EOL +
133 " bar();" + PMD.EOL +
134 " }" + PMD.EOL +
135 " private void bar() {}" + PMD.EOL +
136 "}";
137
138 private static final String TEST12 =
139 "public class Foo {" + PMD.EOL +
140 " public void baz() {" + PMD.EOL +
141 " baz(x, y);" + PMD.EOL +
142 " }" + PMD.EOL +
143 " private void baz(int x, int y) {}" + PMD.EOL +
144 "}";
145
146 private static final String TEST13 =
147 "public class Foo {" + PMD.EOL +
148 " public void baz() {" + PMD.EOL +
149 " foo(\"hi\");" + PMD.EOL +
150 " }" + PMD.EOL +
151 " private void foo(String y) {}" + PMD.EOL +
152 " public void foo(List y) {}" + PMD.EOL +
153 "}";
154
155 }