View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.filters;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTypeCheck.MSG_JAVADOC_MISSING;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24  
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.stream.Collectors;
28  
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.TreeWalker;
34  import com.puppycrawl.tools.checkstyle.api.Configuration;
35  import com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder;
36  import com.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck;
37  import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck;
38  import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTypeCheck;
39  import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
40  import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
41  import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
42  import com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck;
43  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
44  
45  public class SuppressWarningsFilterTest
46      extends AbstractModuleTestSupport {
47      private static final String[] ALL_MESSAGES = {
48          "16: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
49          "17: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
50          "19: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
51          "22:45: "
52              + getCheckMessage(AbstractNameCheck.class,
53                  MSG_INVALID_PATTERN, "I", "^[a-z][a-zA-Z0-9]*$"),
54          "24:17: "
55              + getCheckMessage(AbstractNameCheck.class,
56                  MSG_INVALID_PATTERN, "J", "^[a-z][a-zA-Z0-9]*$"),
57          "25:17: "
58              + getCheckMessage(AbstractNameCheck.class,
59                  MSG_INVALID_PATTERN, "K", "^[a-z][a-zA-Z0-9]*$"),
60          "29:17: "
61              + getCheckMessage(AbstractNameCheck.class,
62                  MSG_INVALID_PATTERN, "L", "^[a-z][a-zA-Z0-9]*$"),
63          "29:32: "
64              + getCheckMessage(AbstractNameCheck.class,
65                  MSG_INVALID_PATTERN, "X", "^[a-z][a-zA-Z0-9]*$"),
66          "33:30: "
67              + getCheckMessage(AbstractNameCheck.class,
68                  MSG_INVALID_PATTERN, "m", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
69          "34:30: "
70              + getCheckMessage(AbstractNameCheck.class,
71                  MSG_INVALID_PATTERN, "n", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
72          "39:17: "
73              + getCheckMessage(ParameterNumberCheck.class, ParameterNumberCheck.MSG_KEY, 7, 8),
74          "45:9: "
75              + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
76          "56:9: "
77              + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
78          "61: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
79          "71: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
80          "76: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
81          "77: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
82          "83: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
83          "84: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
84          "90: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
85          "91: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
86          "97: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
87      };
88  
89      @Override
90      protected String getPackageLocation() {
91          return "com/puppycrawl/tools/checkstyle/filters/suppresswarningsfilter";
92      }
93  
94      @Test
95      public void testNone() throws Exception {
96          final DefaultConfiguration filterConfig = null;
97          final String[] suppressed = CommonUtils.EMPTY_STRING_ARRAY;
98          verifySuppressed(filterConfig, suppressed);
99      }
100 
101     @Test
102     public void testDefault() throws Exception {
103         final DefaultConfiguration filterConfig =
104             createModuleConfig(SuppressWarningsFilter.class);
105         final String[] suppressed = {
106             "24:17: "
107                 + getCheckMessage(AbstractNameCheck.class,
108                     MSG_INVALID_PATTERN, "J", "^[a-z][a-zA-Z0-9]*$"),
109             "29:17: "
110                 + getCheckMessage(AbstractNameCheck.class,
111                     MSG_INVALID_PATTERN, "L", "^[a-z][a-zA-Z0-9]*$"),
112             "33:30: "
113                 + getCheckMessage(AbstractNameCheck.class,
114                     MSG_INVALID_PATTERN, "m", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
115             "39:17: "
116                 + getCheckMessage(ParameterNumberCheck.class, ParameterNumberCheck.MSG_KEY, 7, 8),
117             "56:9: "
118                 + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
119             "71: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
120             "77: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
121             "84: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
122             "91: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
123             "97: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
124         };
125         verifySuppressed(filterConfig, suppressed);
126     }
127 
128     private void verifySuppressed(Configuration moduleConfig,
129             String... aSuppressed)
130             throws Exception {
131         verifySuppressed(moduleConfig, getPath("InputSuppressWarningsFilter.java"),
132                ALL_MESSAGES, aSuppressed);
133     }
134 
135     private void verifySuppressed(Configuration moduleConfig, String fileName,
136             String[] expectedViolations, String... suppressedViolations) throws Exception {
137         final DefaultConfiguration holderConfig =
138             createModuleConfig(SuppressWarningsHolder.class);
139         holderConfig.addAttribute("aliasList",
140             "com.puppycrawl.tools.checkstyle.checks.sizes."
141                 + "ParameterNumberCheck=paramnum");
142 
143         final DefaultConfiguration memberNameCheckConfig =
144                 createModuleConfig(MemberNameCheck.class);
145         memberNameCheckConfig.addAttribute("id", "ignore");
146 
147         final DefaultConfiguration constantNameCheckConfig =
148             createModuleConfig(ConstantNameCheck.class);
149         constantNameCheckConfig.addAttribute("id", "");
150 
151         final DefaultConfiguration uncommentedMainCheckConfig =
152             createModuleConfig(UncommentedMainCheck.class);
153         uncommentedMainCheckConfig.addAttribute("id", "ignore");
154 
155         final DefaultConfiguration treewalkerConfig =
156                 createModuleConfig(TreeWalker.class);
157         treewalkerConfig.addChild(holderConfig);
158         treewalkerConfig.addChild(memberNameCheckConfig);
159         treewalkerConfig.addChild(constantNameCheckConfig);
160         treewalkerConfig.addChild(createModuleConfig(ParameterNumberCheck.class));
161         treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class));
162         treewalkerConfig.addChild(uncommentedMainCheckConfig);
163         treewalkerConfig.addChild(createModuleConfig(JavadocTypeCheck.class));
164 
165         final DefaultConfiguration checkerConfig =
166                 createRootConfig(treewalkerConfig);
167         if (moduleConfig != null) {
168             checkerConfig.addChild(moduleConfig);
169         }
170 
171         verify(checkerConfig,
172                 fileName,
173             removeSuppressed(expectedViolations, suppressedViolations));
174     }
175 
176     private static String[] removeSuppressed(String[] from, String... remove) {
177         final Collection<String> coll = Arrays.stream(from).collect(Collectors.toList());
178         coll.removeAll(Arrays.asList(remove));
179         return coll.toArray(new String[coll.size()]);
180     }
181 
182     @Test
183     public void testSuppressById() throws Exception {
184         final DefaultConfiguration filterConfig =
185             createModuleConfig(SuppressWarningsFilter.class);
186         final String[] suppressedViolationMessages = {
187             "6:17: "
188                 + getCheckMessage(AbstractNameCheck.class,
189                     MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
190             "8: "
191                 + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
192         };
193         final String[] expectedViolationMessages = {
194             "3: " + getCheckMessage(JavadocTypeCheck.class, MSG_JAVADOC_MISSING),
195             "6:17: "
196                 + getCheckMessage(AbstractNameCheck.class,
197                     MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
198             "8: "
199                 + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
200         };
201 
202         verifySuppressed(filterConfig, getPath("InputSuppressWarningsFilterById.java"),
203                 expectedViolationMessages, suppressedViolationMessages);
204     }
205 }