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 org.junit.jupiter.api.Test;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.checks.OrderedPropertiesCheck;
26  import com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck;
27  import com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class SuppressWithPlainTextCommentFilterExamplesTest
31          extends AbstractExamplesModuleTestSupport {
32  
33      @Override
34      public String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/filters/suppresswithplaintextcommentfilter";
36      }
37  
38      @Test
39      public void testExample1() throws Exception {
40          final String[] expectedWithoutFilter = {
41              "11: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
42                      "keyB", 2),
43              "15: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
44                      "keyC", 2),
45          };
46  
47          final String[] expectedWithFilter = {
48              "15: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
49                      "keyC", 2),
50          };
51  
52          verifyFilterWithInlineConfigParser(getPath("Example1.properties"),
53                  expectedWithoutFilter, expectedWithFilter);
54      }
55  
56      @Test
57      public void testExample2() throws Exception {
58          final String[] expectedWithoutFilter = {
59              "15: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
60                      "keyB", 2),
61              "19: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
62                      "keyC", 2),
63          };
64  
65          final String[] expectedWithFilter = {
66              "19: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
67                      "keyC", 2),
68          };
69  
70          verifyFilterWithInlineConfigParser(getPath("Example2.properties"),
71                  expectedWithoutFilter, expectedWithFilter);
72      }
73  
74      @Test
75      public void testExample3() throws Exception {
76          final String[] expectedWithoutFilter = {
77              "18: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
78                      "keyB", 2),
79              "21: " + getCheckMessage(OrderedPropertiesCheck.class, OrderedPropertiesCheck.MSG_KEY,
80                      "keyA", "keyB"),
81              "24: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
82                      "keyC", 2),
83          };
84  
85          final String[] expectedWithFilter = {
86              "21: " + getCheckMessage(OrderedPropertiesCheck.class, OrderedPropertiesCheck.MSG_KEY,
87                      "keyA", "keyB"),
88              "24: " + getCheckMessage(UniquePropertiesCheck.class, UniquePropertiesCheck.MSG_KEY,
89                      "keyC", 2),
90          };
91  
92          verifyFilterWithInlineConfigParser(getPath("Example3.properties"),
93                  expectedWithoutFilter, expectedWithFilter);
94      }
95  
96      @Test
97      public void testExample4() throws Exception {
98          final String[] expectedWithoutFilter = {
99              "26: Type code is not allowed. Use type raw instead.",
100             "33: Type code is not allowed. Use type raw instead.",
101         };
102 
103         final String[] expectedWithFilter = {
104             "33: Type code is not allowed. Use type raw instead.",
105         };
106 
107         verifyFilterWithInlineConfigParser(getPath("Example4.xml"),
108                 expectedWithoutFilter, expectedWithFilter);
109     }
110 
111     @Test
112     public void testExample5() throws Exception {
113         final String[] expectedWithoutFilter = {
114             "26: Type code is not allowed. Use type raw instead.",
115             "33: Type code is not allowed. Use type raw instead.",
116         };
117 
118         final String[] expectedWithFilter = {
119             "26: Type code is not allowed. Use type raw instead.",
120             "33: Type code is not allowed. Use type raw instead.",
121         };
122 
123         verifyFilterWithInlineConfigParser(getPath("Example5.xml"),
124                 expectedWithoutFilter, expectedWithFilter);
125     }
126 
127     @Test
128     public void testExample6() throws Exception {
129         final String[] expectedWithoutFilter = {
130             "33: Type config is not allowed in this file.",
131             "39: Type code is not allowed. Use type raw instead.",
132         };
133 
134         final String[] expectedWithFilter = {
135             "33: Type config is not allowed in this file.",
136         };
137 
138         verifyFilterWithInlineConfigParser(getPath("Example6.xml"),
139                 expectedWithoutFilter, expectedWithFilter);
140     }
141 
142     @Test
143     public void testExample7() throws Exception {
144         final String[] expectedWithoutFilter = {
145             "34: Type config is not allowed in this file.",
146             "40: Type code is not allowed. Use type raw instead.",
147         };
148 
149         final String[] expectedWithFilter = {
150             "34: Type config is not allowed in this file.",
151         };
152 
153         verifyFilterWithInlineConfigParser(getPath("Example7.xml"),
154                 expectedWithoutFilter, expectedWithFilter);
155     }
156 
157     @Test
158     public void testExample8() throws Exception {
159         final String[] expectedWithoutFilter = {
160             "29: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 60, 66),
161         };
162 
163         final String[] expectedWithFilter = CommonUtil.EMPTY_STRING_ARRAY;
164 
165         verifyFilterWithInlineConfigParser(getPath("Example8.sql"),
166                 expectedWithoutFilter, expectedWithFilter);
167     }
168 
169     @Test
170     public void testUseCase1() throws Exception {
171 
172         final String[] expectedWithoutFilter = {
173             "23: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 100, 147),
174             "24: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 100, 133),
175             "25: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 100, 116),
176             "32: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 100, 183),
177         };
178 
179         final String[] expectedWithFilter = {
180             "32: " + getCheckMessage(LineLengthCheck.class, LineLengthCheck.MSG_KEY, 100, 183),
181         };
182 
183         verifyFilterWithInlineConfigParser(getPath("UseCase1.java"),
184                 expectedWithoutFilter, expectedWithFilter);
185     }
186 
187 }