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