View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.utils.CommonUtil;
26  
27  public class SuppressWithPlainTextCommentFilterExamplesTest
28          extends AbstractExamplesModuleTestSupport {
29  
30      @Override
31      protected String getPackageLocation() {
32          return "com/puppycrawl/tools/checkstyle/filters/suppresswithplaintextcommentfilter";
33      }
34  
35      @Test
36      public void testExample1() throws Exception {
37          final String fileWithConfig = getPath("Example1.java");
38          final String targetFile = getPath("Example1.properties");
39  
40          final String[] expectedWithoutFilter = {
41              "2: Duplicated property 'keyB' (2 occurrence(s)).",
42              "6: Duplicated property 'keyC' (2 occurrence(s)).",
43          };
44  
45          final String[] expectedWithFilter = {
46              "6: Duplicated property 'keyC' (2 occurrence(s)).",
47          };
48  
49          verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
50                  expectedWithoutFilter,
51                  expectedWithFilter);
52      }
53  
54      @Test
55      public void testExample2() throws Exception {
56          final String fileWithConfig = getPath("Example2.java");
57          final String targetFile = getPath("Example2.properties");
58  
59          final String[] expectedWithoutFilter = {
60              "2: Duplicated property 'keyB' (2 occurrence(s)).",
61              "6: Duplicated property 'keyC' (2 occurrence(s)).",
62          };
63  
64          final String[] expectedWithFilter = {
65              "6: Duplicated property 'keyC' (2 occurrence(s)).",
66          };
67  
68          verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
69                  expectedWithoutFilter,
70                  expectedWithFilter);
71      }
72  
73      @Test
74      public void testExample3() throws Exception {
75          final String fileWithConfig = getPath("Example3.java");
76          final String targetFile = getPath("Example3.properties");
77  
78          final String[] expectedWithoutFilter = {
79              "3: Duplicated property 'keyB' (2 occurrence(s)).",
80              "6: Property key 'keyA' is not in the right order with previous property 'keyB'.",
81              "9: Duplicated property 'keyC' (2 occurrence(s)).",
82          };
83  
84          final String[] expectedWithFilter = {
85              "6: Property key 'keyA' is not in the right order with previous property 'keyB'.",
86              "9: Duplicated property 'keyC' (2 occurrence(s)).",
87          };
88  
89          verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
90                  expectedWithoutFilter,
91                  expectedWithFilter);
92      }
93  
94      @Test
95      public void testExample4() throws Exception {
96          final String fileWithConfig = getPath("Example4.java");
97          final String targetFile = getPath("Example4.xml");
98  
99          final String[] expectedWithoutFilter = {
100             "6: Type code is not allowed. Use type raw instead.",
101             "13: Type code is not allowed. Use type raw instead.",
102         };
103 
104         final String[] expectedWithFilter = {
105             "13: Type code is not allowed. Use type raw instead.",
106         };
107 
108         verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
109                 expectedWithoutFilter,
110                 expectedWithFilter);
111     }
112 
113     @Test
114     public void testExample5() throws Exception {
115         final String fileWithConfig = getPath("Example5.java");
116         final String targetFile = getPath("Example5.xml");
117 
118         final String[] expectedWithoutFilter = {
119             "6: Type code is not allowed. Use type raw instead.",
120             "13: Type code is not allowed. Use type raw instead.",
121         };
122 
123         final String[] expectedWithFilter = {
124             "6: Type code is not allowed. Use type raw instead.",
125             "13: Type code is not allowed. Use type raw instead.",
126         };
127 
128         verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
129                 expectedWithoutFilter,
130                 expectedWithFilter);
131     }
132 
133     @Test
134     public void testExample6() throws Exception {
135         final String fileWithConfig = getPath("Example6.java");
136         final String targetFile = getPath("Example6.xml");
137 
138         final String[] expectedWithoutFilter = {
139             "6: Type config is not allowed in this file.",
140             "12: Type code is not allowed. Use type raw instead.",
141         };
142 
143         final String[] expectedWithFilter = {
144             "6: Type config is not allowed in this file.",
145         };
146 
147         verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
148                 expectedWithoutFilter,
149                 expectedWithFilter);
150     }
151 
152     @Test
153     public void testExample7() throws Exception {
154         final String fileWithConfig = getPath("Example7.java");
155         final String targetFile = getPath("Example7.xml");
156 
157         final String[] expectedWithoutFilter = {
158             "6: Type config is not allowed in this file.",
159             "12: Type code is not allowed. Use type raw instead.",
160         };
161 
162         final String[] expectedWithFilter = {
163             "6: Type config is not allowed in this file.",
164         };
165 
166         verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
167                 expectedWithoutFilter,
168                 expectedWithFilter);
169     }
170 
171     @Test
172     public void testExample8() throws Exception {
173         final String fileWithConfig = getPath("Example8.java");
174         final String targetFile = getPath("Example8.sql");
175 
176         final String[] expectedWithoutFilter = {
177             "7: Line is longer than 60 characters (found 66).",
178         };
179 
180         final String[] expectedWithFilter = CommonUtil.EMPTY_STRING_ARRAY;
181 
182         verifyFilterWithInlineConfigParserSeparateConfigAndTarget(fileWithConfig, targetFile,
183                 expectedWithoutFilter,
184                 expectedWithFilter);
185     }
186 
187     @Test
188     public void testExample9() throws Exception {
189 
190         final String[] expectedWithoutFilter = {
191             "23: Line is longer than 100 characters (found 147).",
192             "24: Line is longer than 100 characters (found 133).",
193             "25: Line is longer than 100 characters (found 116).",
194             "32: Line is longer than 100 characters (found 183).",
195         };
196 
197         final String[] expectedWithFilter = {
198             "32: Line is longer than 100 characters (found 183).",
199         };
200 
201         verifyFilterWithInlineConfigParser(getPath("Example9.java"),
202                 expectedWithoutFilter, expectedWithFilter);
203     }
204 }