1
2 package test/net/sourceforge/pmd/package-summary.html">> test.net.sourceforge.pmd;
3
4 import net.sourceforge.pmd.AbstractRule;
5 import net.sourceforge.pmd.PMD;
6 import net.sourceforge.pmd.Report;
7 import net.sourceforge.pmd.SourceType;
8 import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
9 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
10 import test.net.sourceforge.pmd.testframework.RuleTst;
11
12 public class SuppressWarningsTest extends RuleTst {
13
14 private static class FooRule extends AbstractRule {
15 public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
16 if (c.getImage().equalsIgnoreCase("Foo")) addViolation(ctx, c);
17 return super.visit(c, ctx);
18 }
19
20 public Object visit(ASTVariableDeclaratorId c, Object ctx) {
21 if (c.getImage().equalsIgnoreCase("Foo")) addViolation(ctx, c);
22 return super.visit(c, ctx);
23 }
24
25 public String getName() {
26 return "NoFoo";
27 }
28 }
29
30 public void testClassLevelSuppression() throws Throwable {
31 Report rpt = new Report();
32 runTestFromString(TEST1, new FooRule(), rpt, SourceType.JAVA_15);
33 assertEquals(0, rpt.size());
34 runTestFromString(TEST2, new FooRule(), rpt, SourceType.JAVA_15);
35 assertEquals(0, rpt.size());
36 }
37
38 public void testInheritedSuppression() throws Throwable {
39 Report rpt = new Report();
40 runTestFromString(TEST3, new FooRule(), rpt, SourceType.JAVA_15);
41 assertEquals(0, rpt.size());
42 }
43
44 public void testMethodLevelSuppression() throws Throwable {
45 Report rpt = new Report();
46 runTestFromString(TEST4, new FooRule(), rpt, SourceType.JAVA_15);
47 assertEquals(1, rpt.size());
48 }
49
50 public void testConstructorLevelSuppression() throws Throwable {
51 Report rpt = new Report();
52 runTestFromString(TEST5, new FooRule(), rpt, SourceType.JAVA_15);
53 assertEquals(0, rpt.size());
54 }
55
56 public void testFieldLevelSuppression() throws Throwable {
57 Report rpt = new Report();
58 runTestFromString(TEST6, new FooRule(), rpt, SourceType.JAVA_15);
59 assertEquals(1, rpt.size());
60 }
61
62 public void testParameterLevelSuppression() throws Throwable {
63 Report rpt = new Report();
64 runTestFromString(TEST7, new FooRule(), rpt, SourceType.JAVA_15);
65 assertEquals(1, rpt.size());
66 }
67
68 public void testLocalVariableLevelSuppression() throws Throwable {
69 Report rpt = new Report();
70 runTestFromString(TEST8, new FooRule(), rpt, SourceType.JAVA_15);
71 assertEquals(1, rpt.size());
72 }
73
74 public void testSpecificSuppression() throws Throwable {
75 Report rpt = new Report();
76 runTestFromString(TEST9, new FooRule(), rpt, SourceType.JAVA_15);
77 assertEquals(1, rpt.size());
78 }
79
80 public void testNoSuppressionBlank() throws Throwable {
81 Report rpt = new Report();
82 runTestFromString(TEST10, new FooRule(), rpt, SourceType.JAVA_15);
83 assertEquals(2, rpt.size());
84 }
85
86 public void testNoSuppressionSomethingElseS() throws Throwable {
87 Report rpt = new Report();
88 runTestFromString(TEST11, new FooRule(), rpt, SourceType.JAVA_15);
89 assertEquals(2, rpt.size());
90 }
91
92 private static final String TEST1 =
93 "@SuppressWarnings(\"PMD\")" + PMD.EOL +
94 "public class Foo {}";
95
96 private static final String TEST2 =
97 "@SuppressWarnings(\"PMD\")" + PMD.EOL +
98 "public class Foo {" + PMD.EOL +
99 " void bar() {" + PMD.EOL +
100 " int foo;" + PMD.EOL +
101 " }" + PMD.EOL +
102 "}";
103
104 private static final String TEST3 =
105 "public class Baz {" + PMD.EOL +
106 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
107 " public class Bar {" + PMD.EOL +
108 " void bar() {" + PMD.EOL +
109 " int foo;" + PMD.EOL +
110 " }" + PMD.EOL +
111 " }" + PMD.EOL +
112 "}";
113
114 private static final String TEST4 =
115 "public class Foo {" + PMD.EOL +
116 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
117 " void bar() {" + PMD.EOL +
118 " int foo;" + PMD.EOL +
119 " }" + PMD.EOL +
120 "}";
121
122 private static final String TEST5 =
123 "public class Bar {" + PMD.EOL +
124 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
125 " public Bar() {" + PMD.EOL +
126 " int foo;" + PMD.EOL +
127 " }" + PMD.EOL +
128 "}";
129
130 private static final String TEST6 =
131 "public class Bar {" + PMD.EOL +
132 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
133 " int foo;" + PMD.EOL +
134 " void bar() {" + PMD.EOL +
135 " int foo;" + PMD.EOL +
136 " }" + PMD.EOL +
137 "}";
138
139 private static final String TEST7 =
140 "public class Bar {" + PMD.EOL +
141 " int foo;" + PMD.EOL +
142 " void bar(@SuppressWarnings(\"PMD\") int foo) {}" + PMD.EOL +
143 "}";
144
145 private static final String TEST8 =
146 "public class Bar {" + PMD.EOL +
147 " int foo;" + PMD.EOL +
148 " void bar() {" + PMD.EOL +
149 " @SuppressWarnings(\"PMD\") int foo;" + PMD.EOL +
150 " }" + PMD.EOL +
151 "}";
152
153 private static final String TEST9 =
154 "public class Bar {" + PMD.EOL +
155 " int foo;" + PMD.EOL +
156 " void bar() {" + PMD.EOL +
157 " @SuppressWarnings(\"PMD.NoFoo\") int foo;" + PMD.EOL +
158 " }" + PMD.EOL +
159 "}";
160
161 private static final String TEST10 =
162 "public class Bar {" + PMD.EOL +
163 " int foo;" + PMD.EOL +
164 " void bar() {" + PMD.EOL +
165 " @SuppressWarnings(\"\") int foo;" + PMD.EOL +
166 " }" + PMD.EOL +
167 "}";
168
169 private static final String TEST11 =
170 "public class Bar {" + PMD.EOL +
171 " int foo;" + PMD.EOL +
172 " void bar() {" + PMD.EOL +
173 " @SuppressWarnings(\"SomethingElse\") int foo;" + PMD.EOL +
174 " }" + PMD.EOL +
175 "}";
176 }
177
178