View Javadoc

1   
2    package net.sourceforge.pmd;
3   
4   import static org.junit.Assert.assertEquals;
5   import junit.framework.JUnit4TestAdapter;
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.Report;
8   import net.sourceforge.pmd.lang.LanguageVersion;
9   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
10  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
11  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
12  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
13  import net.sourceforge.pmd.testframework.RuleTst;
14  
15  import org.junit.Test;
16  
17  
18   public class SuppressWarningsTest extends RuleTst {
19  
20       private static class FooRule extends AbstractJavaRule {
21          @Override
22          public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
23              if (c.getImage().equalsIgnoreCase("Foo")) {
24                  addViolation(ctx, c);
25              }
26              return super.visit(c, ctx);
27          }
28  
29          @Override
30          public Object visit(ASTVariableDeclaratorId c, Object ctx) {
31              if (c.getImage().equalsIgnoreCase("Foo")) {
32                  addViolation(ctx, c);
33              }
34              return super.visit(c, ctx);
35          }
36  
37          @Override
38          public String getName() {
39              return "NoFoo";
40          }
41       }
42  
43       private static class BarRule extends AbstractJavaRule {
44          @Override
45          public Object visit(ASTCompilationUnit cu, Object ctx) {
46              // Convoluted rule to make sure the violation is reported for the ASTCompilationUnit node
47              for (ASTClassOrInterfaceDeclaration c : cu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class)) {
48                  if (c.getImage().equalsIgnoreCase("bar")) {
49                      addViolation(ctx, cu);
50                  }
51              }
52              return super.visit(cu, ctx);
53          }
54  
55          @Override
56          public String getName() {
57              return "NoBar";
58          }
59       }
60  
61       @Test
62       public void testClassLevelSuppression() throws Throwable {
63           Report rpt = new Report();
64           runTestFromString(TEST1, new FooRule(), rpt, LanguageVersion.JAVA_15);
65           assertEquals(0, rpt.size());
66           runTestFromString(TEST2, new FooRule(), rpt, LanguageVersion.JAVA_15);
67           assertEquals(0, rpt.size());
68       }
69  
70       @Test
71       public void testInheritedSuppression() throws Throwable {
72           Report rpt = new Report();
73           runTestFromString(TEST3, new FooRule(), rpt, LanguageVersion.JAVA_15);
74           assertEquals(0, rpt.size());
75       }
76  
77       @Test
78       public void testMethodLevelSuppression() throws Throwable {
79           Report rpt = new Report();
80           runTestFromString(TEST4, new FooRule(), rpt, LanguageVersion.JAVA_15);
81           assertEquals(1, rpt.size());
82       }
83  
84       @Test
85       public void testConstructorLevelSuppression() throws Throwable {
86           Report rpt = new Report();
87           runTestFromString(TEST5, new FooRule(), rpt, LanguageVersion.JAVA_15);
88           assertEquals(0, rpt.size());
89       }
90  
91       @Test
92       public void testFieldLevelSuppression() throws Throwable {
93           Report rpt = new Report();
94           runTestFromString(TEST6, new FooRule(), rpt, LanguageVersion.JAVA_15);
95           assertEquals(1, rpt.size());
96       }
97  
98       @Test
99       public void testParameterLevelSuppression() throws Throwable {
100          Report rpt = new Report();
101          runTestFromString(TEST7, new FooRule(), rpt, LanguageVersion.JAVA_15);
102          assertEquals(1, rpt.size());
103      }
104 
105      @Test
106      public void testLocalVariableLevelSuppression() throws Throwable {
107          Report rpt = new Report();
108          runTestFromString(TEST8, new FooRule(), rpt, LanguageVersion.JAVA_15);
109          assertEquals(1, rpt.size());
110      }
111 
112      @Test
113      public void testSpecificSuppression() throws Throwable {
114          Report rpt = new Report();
115          runTestFromString(TEST9, new FooRule(), rpt, LanguageVersion.JAVA_15);
116          assertEquals(1, rpt.size());
117      }
118 
119      @Test
120      public void testNoSuppressionBlank() throws Throwable {
121          Report rpt = new Report();
122          runTestFromString(TEST10, new FooRule(), rpt, LanguageVersion.JAVA_15);
123          assertEquals(2, rpt.size());
124      }
125 
126      @Test
127      public void testNoSuppressionSomethingElseS() throws Throwable {
128          Report rpt = new Report();
129          runTestFromString(TEST11, new FooRule(), rpt, LanguageVersion.JAVA_15);
130          assertEquals(2, rpt.size());
131      }
132 
133      @Test
134      public void testSuppressAll() throws Throwable {
135          Report rpt = new Report();
136          runTestFromString(TEST12, new FooRule(), rpt, LanguageVersion.JAVA_15);
137          assertEquals(0, rpt.size());
138      }
139 
140      @Test
141      public void testSpecificSuppressionAtTopLevel() throws Throwable {
142          Report rpt = new Report();
143          runTestFromString(TEST13, new BarRule(), rpt, LanguageVersion.JAVA_15);
144          assertEquals(0, rpt.size());
145      }
146 
147      private static final String TEST1 =
148              "@SuppressWarnings(\"PMD\")" + PMD.EOL +
149              "public class Foo {}";
150 
151      private static final String TEST2 =
152              "@SuppressWarnings(\"PMD\")" + PMD.EOL +
153              "public class Foo {" + PMD.EOL +
154              " void bar() {" + PMD.EOL +
155              "  int foo;" + PMD.EOL +
156              " }" + PMD.EOL +
157              "}";
158 
159      private static final String TEST3 =
160              "public class Baz {" + PMD.EOL +
161              " @SuppressWarnings(\"PMD\")" + PMD.EOL +
162              " public class Bar {" + PMD.EOL +
163              "  void bar() {" + PMD.EOL +
164              "   int foo;" + PMD.EOL +
165              "  }" + PMD.EOL +
166              " }" + PMD.EOL +
167              "}";
168 
169      private static final String TEST4 =
170              "public class Foo {" + PMD.EOL +
171              " @SuppressWarnings(\"PMD\")" + PMD.EOL +
172              " void bar() {" + PMD.EOL +
173              "  int foo;" + PMD.EOL +
174              " }" + PMD.EOL +
175              "}";
176 
177      private static final String TEST5 =
178              "public class Bar {" + PMD.EOL +
179              " @SuppressWarnings(\"PMD\")" + PMD.EOL +
180              " public Bar() {" + PMD.EOL +
181              "  int foo;" + PMD.EOL +
182              " }" + PMD.EOL +
183              "}";
184 
185      private static final String TEST6 =
186              "public class Bar {" + PMD.EOL +
187              " @SuppressWarnings(\"PMD\")" + PMD.EOL +
188              " int foo;" + PMD.EOL +
189              " void bar() {" + PMD.EOL +
190              "  int foo;" + PMD.EOL +
191              " }" + PMD.EOL +
192              "}";
193 
194      private static final String TEST7 =
195              "public class Bar {" + PMD.EOL +
196              " int foo;" + PMD.EOL +
197              " void bar(@SuppressWarnings(\"PMD\") int foo) {}" + PMD.EOL +
198              "}";
199 
200      private static final String TEST8 =
201              "public class Bar {" + PMD.EOL +
202              " int foo;" + PMD.EOL +
203              " void bar() {" + PMD.EOL +
204              "  @SuppressWarnings(\"PMD\") int foo;" + PMD.EOL +
205              " }" + PMD.EOL +
206              "}";
207 
208      private static final String TEST9 =
209              "public class Bar {" + PMD.EOL +
210              " int foo;" + PMD.EOL +
211              " void bar() {" + PMD.EOL +
212              "  @SuppressWarnings(\"PMD.NoFoo\") int foo;" + PMD.EOL +
213              " }" + PMD.EOL +
214              "}";
215 
216      private static final String TEST10 =
217              "public class Bar {" + PMD.EOL +
218              " int foo;" + PMD.EOL +
219              " void bar() {" + PMD.EOL +
220              "  @SuppressWarnings(\"\") int foo;" + PMD.EOL +
221              " }" + PMD.EOL +
222              "}";
223 
224      private static final String TEST11 =
225              "public class Bar {" + PMD.EOL +
226              " int foo;" + PMD.EOL +
227              " void bar() {" + PMD.EOL +
228              "  @SuppressWarnings(\"SomethingElse\") int foo;" + PMD.EOL +
229              " }" + PMD.EOL +
230              "}";
231 
232      private static final String TEST12 =
233              "public class Bar {" + PMD.EOL +
234              " @SuppressWarnings(\"all\") int foo;" + PMD.EOL +
235              "}";
236 
237      private static final String TEST13 =
238              "@SuppressWarnings(\"PMD.NoBar\")" + PMD.EOL +
239              "public class Bar {" + PMD.EOL +
240              "}";
241 
242     public static junit.framework.Test suite() {
243         return new JUnit4TestAdapter(SuppressWarningsTest.class);
244     }
245  }
246 
247