View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.MissingJavadocTypeCheck.MSG_JAVADOC_MISSING;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck;
29  import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck;
30  import com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck;
31  import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
32  import com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  
35  public class SuppressWarningsFilterTest
36      extends AbstractModuleTestSupport {
37  
38      private static final String[] ALL_MESSAGES = {
39          "50:5: "
40              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "I"),
41          "51:5: "
42              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "E"),
43          "53:5: "
44              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "A"),
45          "56:37: "
46              + getCheckMessage(AbstractNameCheck.class,
47                  MSG_INVALID_PATTERN, "I", "^[a-z][a-zA-Z0-9]*$"),
48          "58:17: "
49              + getCheckMessage(AbstractNameCheck.class,
50                  MSG_INVALID_PATTERN, "J", "^[a-z][a-zA-Z0-9]*$"),
51          "59:17: "
52              + getCheckMessage(AbstractNameCheck.class,
53                  MSG_INVALID_PATTERN, "K", "^[a-z][a-zA-Z0-9]*$"),
54          "63:17: "
55              + getCheckMessage(AbstractNameCheck.class,
56                  MSG_INVALID_PATTERN, "L", "^[a-z][a-zA-Z0-9]*$"),
57          "63:32: "
58              + getCheckMessage(AbstractNameCheck.class,
59                  MSG_INVALID_PATTERN, "X", "^[a-z][a-zA-Z0-9]*$"),
60          "68:30: "
61              + getCheckMessage(AbstractNameCheck.class,
62                  MSG_INVALID_PATTERN, "m", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
63          "69:30: "
64              + getCheckMessage(AbstractNameCheck.class,
65                  MSG_INVALID_PATTERN, "n", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
66          "74:10: "
67              + getCheckMessage(ParameterNumberCheck.class, ParameterNumberCheck.MSG_KEY, 7, 8),
68          "78:9: "
69              + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
70          "87:9: "
71              + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
72          "92:5: "
73              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "AnEnum"),
74          "102:5: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
75          "105:5: "
76              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "TestClass1"),
77          "107:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
78          "111:5: "
79              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "TestClass2"),
80          "113:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
81          "117:5: "
82              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "TestClass3"),
83          "119:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
84          "124:5: "
85              + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING, "Task"),
86      };
87  
88      @Override
89      public String getPackageLocation() {
90          return "com/puppycrawl/tools/checkstyle/filters/suppresswarningsfilter";
91      }
92  
93      @Test
94      public void testNone() throws Exception {
95          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
96          verifySuppressedWithParser(
97              getPath("InputSuppressWarningsFilterWithoutFilter.java"), suppressed);
98      }
99  
100     @Test
101     public void testDefault() throws Exception {
102         final String[] suppressed = {
103             "58:17: "
104                 + getCheckMessage(AbstractNameCheck.class,
105                     MSG_INVALID_PATTERN, "J", "^[a-z][a-zA-Z0-9]*$"),
106             "63:17: "
107                 + getCheckMessage(AbstractNameCheck.class,
108                     MSG_INVALID_PATTERN, "L", "^[a-z][a-zA-Z0-9]*$"),
109             "68:30: "
110                 + getCheckMessage(AbstractNameCheck.class,
111                     MSG_INVALID_PATTERN, "m", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
112             "74:10: "
113                 + getCheckMessage(ParameterNumberCheck.class, ParameterNumberCheck.MSG_KEY, 7, 8),
114             "87:9: "
115                 + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
116             "102:5: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
117             "107:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
118             "113:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
119             "119:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
120         };
121         verifySuppressedWithParser(getPath("InputSuppressWarningsFilter.java"), suppressed);
122     }
123 
124     @Test
125     public void testSuppressById() throws Exception {
126         final String[] suppressedViolationMessages = {
127             "51:17: "
128                 + getCheckMessage(AbstractNameCheck.class,
129                     MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
130             "54:5: "
131                 + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
132         };
133         final String[] expectedViolationMessages = {
134             "48:1: "
135                 + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING,
136                     "InputSuppressWarningsFilterById"),
137             "51:17: "
138                 + getCheckMessage(AbstractNameCheck.class,
139                     MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
140             "54:5: "
141                 + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
142         };
143 
144         verifySuppressedWithParser(getPath("InputSuppressWarningsFilterById.java"),
145                 expectedViolationMessages, suppressedViolationMessages);
146     }
147 
148     private void verifySuppressedWithParser(String fileName, String... suppressed)
149             throws Exception {
150         verifyFilterWithInlineConfigParser(fileName, ALL_MESSAGES,
151                                            removeSuppressed(ALL_MESSAGES, suppressed));
152     }
153 
154     private void verifySuppressedWithParser(String fileName, String[] messages,
155                                             String... suppressed)
156             throws Exception {
157         verifyFilterWithInlineConfigParser(fileName, messages,
158                                            removeSuppressed(messages, suppressed));
159     }
160 
161 }