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