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  
26  public class SuppressWithNearbyTextFilterExamplesTest extends AbstractExamplesModuleTestSupport {
27      @Override
28      protected String getPackageLocation() {
29          return "com/puppycrawl/tools/checkstyle/filters/suppresswithnearbytextfilter";
30      }
31  
32      @Test
33      public void testExample1() throws Exception {
34  
35          final String[] expectedWithoutFilter = {
36              "13:20: '24' is a magic number.",
37              "14:20: '7' is a magic number.",
38          };
39  
40          final String[] expectedWithFilter = {
41              "14:20: '7' is a magic number.",
42          };
43  
44          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
45                  expectedWithoutFilter, expectedWithFilter);
46      }
47  
48      @Test
49      public void testExample2() throws Exception {
50  
51          final String[] expectedWithoutFilter = {
52              "15:11: '42' is a magic number.",
53              "16:11: '43' is a magic number.",
54          };
55  
56          final String[] expectedWithFilter = {
57              "16:11: '43' is a magic number.",
58          };
59  
60          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
61                  expectedWithoutFilter, expectedWithFilter);
62      }
63  
64      @Test
65      public void testExample3() throws Exception {
66  
67          final String[] expectedWithoutFilter = {
68              "16: Line is longer than 70 characters (found 74).",
69          };
70  
71          final String[] expectedWithFilter = {
72  
73          };
74  
75          verifyFilterWithInlineConfigParser(getPath("Example3.java"),
76                  expectedWithoutFilter, expectedWithFilter);
77      }
78  
79      @Test
80      public void testExample4() throws Exception {
81  
82          final String[] expectedWithoutFilter = {
83              "20:11: '42' is a magic number.",
84              "21: Line is longer than 55 characters (found 65).",
85          };
86  
87          final String[] expectedWithFilter = {
88              "21: Line is longer than 55 characters (found 65).",
89          };
90  
91          verifyFilterWithInlineConfigParser(getPath("Example4.java"),
92                  expectedWithoutFilter, expectedWithFilter);
93      }
94  
95      @Test
96      public void testExample5() throws Exception {
97  
98          final String[] expectedWithoutFilters = {
99              "1: Duplicated property 'key.one' (3 occurrence(s)).",
100             "5: Duplicated property 'key.two' (2 occurrence(s)).",
101         };
102 
103         final String[] expectedWithFilters = {
104             "5: Duplicated property 'key.two' (2 occurrence(s)).",
105         };
106 
107         verifyFilterWithInlineConfigParserSeparateConfigAndTarget(
108                 getPath("Example5.java"),
109                 getPath("Example5.properties"),
110                 expectedWithoutFilters,
111                 expectedWithFilters);
112     }
113 
114     @Test
115     public void testExample6() throws Exception {
116 
117         final String[] expectedWithoutFilters = {
118             "2: Duplicated property 'key.one' (2 occurrence(s)).",
119             "4: Duplicated property 'key.two' (2 occurrence(s)).",
120         };
121 
122         final String[] expectedWithFilters = {
123             "4: Duplicated property 'key.two' (2 occurrence(s)).",
124         };
125 
126         verifyFilterWithInlineConfigParserSeparateConfigAndTarget(
127                 getPath("Example6.java"),
128                 getPath("Example6.properties"),
129                 expectedWithoutFilters,
130                 expectedWithFilters);
131     }
132 
133     @Test
134     public void testExample7() throws Exception {
135 
136         final String[] expectedWithoutFilter = {
137             "17:11: '42' is a magic number.",
138             "18:11: '43' is a magic number.",
139         };
140 
141         final String[] expectedWithFilter = {
142             "18:11: '43' is a magic number.",
143         };
144 
145         verifyFilterWithInlineConfigParser(getPath("Example7.java"),
146                 expectedWithoutFilter, expectedWithFilter);
147     }
148 
149     @Test
150     public void testExample8() throws Exception {
151 
152         final String[] expectedWithoutFilter = {
153             "18:11: '42' is a magic number.",
154             "19:11: '43' is a magic number.",
155             "20:11: '44' is a magic number.",
156             "21:11: '45' is a magic number.",
157             "22:11: '46' is a magic number.",
158         };
159 
160         final String[] expectedWithFilter = {
161             "22:11: '46' is a magic number.",
162         };
163 
164         verifyFilterWithInlineConfigParser(getPath("Example8.java"),
165                 expectedWithoutFilter, expectedWithFilter);
166     }
167 
168     @Test
169     public void testExample9() throws Exception {
170 
171         final String[] expectedWithoutFilter = {
172             "19: Line is longer than 70 characters (found 72).",
173         };
174 
175         final String[] expectedWithFilter = {
176 
177         };
178 
179         verifyFilterWithInlineConfigParser(getPath("Example9.java"),
180                 expectedWithoutFilter, expectedWithFilter);
181     }
182 }