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.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck;
28  import com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck;
29  import com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck;
30  import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck;
31  import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
32  import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
33  import com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck;
34  
35  public class SuppressionFilterExamplesTest extends AbstractExamplesModuleTestSupport {
36  
37      @Override
38      public String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/filters/suppressionfilter";
40      }
41  
42      @Test
43      public void testExample1() throws Exception {
44          final String[] expected = {
45              "20:7: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN,
46                      "MyVariable", "^[a-z][a-zA-Z0-9]*$"),
47              "22:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "10"),
48              "26:15: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "100"),
49              "28:15: " + getCheckMessage(EmptyBlockCheck.class,
50                      "block.noStatement"),
51          };
52  
53          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
54                  expected, expected);
55      }
56  
57      @Test
58      public void testExample2() throws Exception {
59  
60          final String[] expectedWithoutFilter = {
61              "20:7: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN,
62                      "MyVariable", "^[a-z][a-zA-Z0-9]*$"),
63              "22:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "10"),
64              "26:15: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "100"),
65              "28:15: " + getCheckMessage(EmptyBlockCheck.class,
66                      EmptyBlockCheck.MSG_KEY_BLOCK_NO_STATEMENT),
67          };
68  
69          final String[] expectedWithFilter = {
70              "28:15: " + getCheckMessage(EmptyBlockCheck.class,
71                      EmptyBlockCheck.MSG_KEY_BLOCK_NO_STATEMENT),
72          };
73  
74          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
75                  expectedWithoutFilter,
76                  expectedWithFilter);
77      }
78  
79      @Test
80      public void testExample3() throws Exception {
81          final String[] expected = {
82              "20:7: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN,
83                      "MyVariable", "^[a-z][a-zA-Z0-9]*$"),
84              "22:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "10"),
85              "26:15: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "100"),
86              "28:15: " + getCheckMessage(EmptyBlockCheck.class, "block.noStatement"),
87          };
88  
89          verifyFilterWithInlineConfigParser(getPath("Example3.java"),
90                  expected, expected);
91      }
92  
93      @Test
94      public void testUseCase1() throws Exception {
95  
96          final String[] expectedWithoutFilter = {
97              "24: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 80, 84),
98              "31:22: " + getCheckMessage(EqualsAvoidNullCheck.class,
99                      EqualsAvoidNullCheck.MSG_EQUALS_AVOID_NULL),
100             "35:32: " + getCheckMessage(EqualsAvoidNullCheck.class,
101                     EqualsAvoidNullCheck.MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
102         };
103 
104         final String[] expectedWithFilter = {
105             "24: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 80, 84),
106         };
107 
108         verifyFilterWithInlineConfigParser(getPath("UseCase1.java"),
109                 expectedWithoutFilter,
110                 expectedWithFilter);
111     }
112 
113     @Test
114     public void testUseCase2() throws Exception {
115 
116         final String[] expectedWithoutFilter = {
117             "11: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
118                     "keyB", 2),
119             "14: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
120                     "keyC", 2),
121         };
122 
123         final String[] expectedWithFilter = {};
124 
125         verifyFilterWithInlineConfigParser(getPath(".hidden/UseCase2.properties"),
126                 expectedWithoutFilter, expectedWithFilter);
127     }
128 
129     @Test
130     public void testUseCase3() throws Exception {
131         final String memberPattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
132         final String constantPattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
133 
134         final String[] expectedWithoutFilter = {
135             "20:14: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN,
136                     "log", memberPattern),
137             "23:14: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN,
138                     "constant", memberPattern),
139             "29:27: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
140                     "log", constantPattern),
141             "32:30: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
142                     "line", constantPattern),
143         };
144 
145         final String[] expectedWithFilter = {
146             "23:14: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN,
147                     "constant", memberPattern),
148             "32:30: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
149                     "line", constantPattern),
150         };
151 
152         verifyFilterWithInlineConfigParser(getPath("UseCase3.java"),
153                 expectedWithoutFilter, expectedWithFilter);
154     }
155 
156 }