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 org.checkstyle.suppressionxpathfilter.whitespace;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck;
32  
33  public class XpathRegressionGenericWhitespaceTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = GenericWhitespaceCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/whitespace/genericwhitespace";
45      }
46  
47      @Test
48      public void testProcessEnd() throws Exception {
49          final File fileToProcess = new File(
50                  getPath("InputXpathGenericWhitespaceEnd.java"));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(GenericWhitespaceCheck.class);
54  
55          final String[] expectedViolation = {
56              "6:22: " + getCheckMessage(GenericWhitespaceCheck.class,
57                      GenericWhitespaceCheck.MSG_WS_PRECEDED, ">"),
58          };
59  
60          final List<String> expectedXpathQueries = Collections.singletonList(
61              "/COMPILATION_UNIT/CLASS_DEF"
62                  + "[./IDENT[@text='InputXpathGenericWhitespaceEnd']]/OBJBLOCK"
63                  + "/METHOD_DEF[./IDENT[@text='bad']]"
64                  + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='cls']]"
65                  + "/TYPE[./IDENT[@text='Class']]/TYPE_ARGUMENTS/GENERIC_END"
66          );
67  
68          runVerifications(moduleConfig, fileToProcess, expectedViolation,
69                  expectedXpathQueries);
70      }
71  
72      @Test
73      public void testProcessNestedGenericsOne() throws Exception {
74          final File fileToProcess = new File(
75                  getPath("InputXpathGenericWhitespaceNestedOne.java"));
76  
77          final DefaultConfiguration moduleConfig =
78                  createModuleConfig(GenericWhitespaceCheck.class);
79  
80          final String[] expectedViolation = {
81              "6:22: " + getCheckMessage(GenericWhitespaceCheck.class,
82                      GenericWhitespaceCheck.MSG_WS_NOT_PRECEDED, "&"),
83          };
84  
85          final List<String> expectedXpathQueries = Collections.singletonList(
86              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
87                  + "@text='InputXpathGenericWhitespaceNestedOne']]"
88                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS"
89                  + "/TYPE_PARAMETER[./IDENT[@text='E']]"
90                  + "/TYPE_UPPER_BOUNDS[./IDENT[@text='Enum']]/TYPE_ARGUMENTS/GENERIC_END"
91          );
92  
93          runVerifications(moduleConfig, fileToProcess, expectedViolation,
94                  expectedXpathQueries);
95      }
96  
97      @Test
98      public void testProcessNestedGenericsTwo() throws Exception {
99          final File fileToProcess = new File(
100                 getPath("InputXpathGenericWhitespaceNestedTwo.java"));
101 
102         final DefaultConfiguration moduleConfig =
103                 createModuleConfig(GenericWhitespaceCheck.class);
104 
105         final String[] expectedViolation = {
106             "6:22: " + getCheckMessage(GenericWhitespaceCheck.class,
107                     GenericWhitespaceCheck.MSG_WS_FOLLOWED, ">"),
108         };
109 
110         final List<String> expectedXpathQueries = Collections.singletonList(
111             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
112                 + "@text='InputXpathGenericWhitespaceNestedTwo']]"
113                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS"
114                 + "/TYPE_PARAMETER[./IDENT[@text='E']]"
115                 + "/TYPE_UPPER_BOUNDS[./IDENT[@text='Enum']]/TYPE_ARGUMENTS/GENERIC_END"
116         );
117 
118         runVerifications(moduleConfig, fileToProcess, expectedViolation,
119                 expectedXpathQueries);
120     }
121 
122     @Test
123     public void testProcessNestedGenericsThree() throws Exception {
124         final File fileToProcess = new File(
125                 getPath("InputXpathGenericWhitespaceNestedThree.java"));
126 
127         final DefaultConfiguration moduleConfig =
128                 createModuleConfig(GenericWhitespaceCheck.class);
129 
130         final String[] expectedViolation = {
131             "6:22: " + getCheckMessage(GenericWhitespaceCheck.class,
132                     GenericWhitespaceCheck.MSG_WS_FOLLOWED, ">"),
133         };
134 
135         final List<String> expectedXpathQueries = Collections.singletonList(
136             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
137                 + "@text='InputXpathGenericWhitespaceNestedThree']]"
138                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS"
139                 + "/TYPE_PARAMETER[./IDENT[@text='E']]"
140                 + "/TYPE_UPPER_BOUNDS[./IDENT[@text='Enum']]/TYPE_ARGUMENTS/GENERIC_END"
141         );
142 
143         runVerifications(moduleConfig, fileToProcess, expectedViolation,
144                 expectedXpathQueries);
145     }
146 
147     @Test
148     public void testProcessSingleGenericOne() throws Exception {
149         final File fileToProcess = new File(
150                 getPath("InputXpathGenericWhitespaceSingleOne.java"));
151 
152         final DefaultConfiguration moduleConfig =
153                 createModuleConfig(GenericWhitespaceCheck.class);
154 
155         final String[] expectedViolation = {
156             "6:37: " + getCheckMessage(GenericWhitespaceCheck.class,
157                     GenericWhitespaceCheck.MSG_WS_FOLLOWED, ">"),
158         };
159 
160         final List<String> expectedXpathQueries = Collections.singletonList(
161             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
162                 + "@text='InputXpathGenericWhitespaceSingleOne']]"
163                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='bad']]/ASSIGN/EXPR/METHOD_CALL"
164                 + "/DOT[./IDENT[@text='Collections']]"
165                 + "/TYPE_ARGUMENTS/GENERIC_END"
166         );
167 
168         runVerifications(moduleConfig, fileToProcess, expectedViolation,
169                 expectedXpathQueries);
170     }
171 
172     @Test
173     public void testProcessSingleGenericTwo() throws Exception {
174         final File fileToProcess = new File(
175                 getPath("InputXpathGenericWhitespaceSingleTwo.java"));
176 
177         final DefaultConfiguration moduleConfig =
178                 createModuleConfig(GenericWhitespaceCheck.class);
179 
180         final String[] expectedViolation = {
181             "6:7: " + getCheckMessage(GenericWhitespaceCheck.class,
182                     GenericWhitespaceCheck.MSG_WS_ILLEGAL_FOLLOW, ">"),
183         };
184 
185         final List<String> expectedXpathQueries = Collections.singletonList(
186             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
187                 + "@text='InputXpathGenericWhitespaceSingleTwo']]"
188                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS/GENERIC_END"
189         );
190 
191         runVerifications(moduleConfig, fileToProcess, expectedViolation,
192                 expectedXpathQueries);
193     }
194 
195     @Test
196     public void testProcessStartOne() throws Exception {
197         final File fileToProcess = new File(
198                 getPath("InputXpathGenericWhitespaceStartOne.java"));
199 
200         final DefaultConfiguration moduleConfig =
201                 createModuleConfig(GenericWhitespaceCheck.class);
202 
203         final String[] expectedViolation = {
204             "6:11: " + getCheckMessage(GenericWhitespaceCheck.class,
205                     GenericWhitespaceCheck.MSG_WS_NOT_PRECEDED, "<"),
206         };
207 
208         final List<String> expectedXpathQueries = Arrays.asList(
209             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
210                     + "[@text='InputXpathGenericWhitespaceStartOne']]"
211                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS",
212             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
213                     + "[@text='InputXpathGenericWhitespaceStartOne']]"
214                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS/GENERIC_START"
215         );
216 
217         runVerifications(moduleConfig, fileToProcess, expectedViolation,
218                 expectedXpathQueries);
219     }
220 
221     @Test
222     public void testProcessStartTwo() throws Exception {
223         final File fileToProcess = new File(
224                 getPath("InputXpathGenericWhitespaceStartTwo.java"));
225 
226         final DefaultConfiguration moduleConfig =
227                 createModuleConfig(GenericWhitespaceCheck.class);
228 
229         final String[] expectedViolation = {
230             "6:34: " + getCheckMessage(GenericWhitespaceCheck.class,
231                     GenericWhitespaceCheck.MSG_WS_PRECEDED, "<"),
232         };
233 
234         final List<String> expectedXpathQueries = Arrays.asList(
235             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
236                 + "[@text='InputXpathGenericWhitespaceStartTwo']]"
237                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/PARAMETERS"
238                 + "/PARAMETER_DEF[./IDENT[@text='consumer']]"
239                 + "/TYPE[./IDENT[@text='Consumer']]/TYPE_ARGUMENTS",
240             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
241                 + "[@text='InputXpathGenericWhitespaceStartTwo']]"
242                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/PARAMETERS"
243                 + "/PARAMETER_DEF[./IDENT[@text='consumer']]"
244                 + "/TYPE[./IDENT[@text='Consumer']]/TYPE_ARGUMENTS/GENERIC_START"
245         );
246 
247         runVerifications(moduleConfig, fileToProcess, expectedViolation,
248                 expectedXpathQueries);
249     }
250 
251     @Test
252     public void testProcessStartThree() throws Exception {
253         final File fileToProcess = new File(
254                 getPath("InputXpathGenericWhitespaceStartThree.java"));
255 
256         final DefaultConfiguration moduleConfig =
257                 createModuleConfig(GenericWhitespaceCheck.class);
258 
259         final String[] expectedViolation = {
260             "6:5: " + getCheckMessage(GenericWhitespaceCheck.class,
261                     GenericWhitespaceCheck.MSG_WS_FOLLOWED, "<"),
262         };
263 
264         final List<String> expectedXpathQueries = Arrays.asList(
265             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
266                 + "[@text='InputXpathGenericWhitespaceStartThree']]"
267                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]",
268             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
269                 + "[@text='InputXpathGenericWhitespaceStartThree']]"
270                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/MODIFIERS",
271             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
272                 + "[@text='InputXpathGenericWhitespaceStartThree']]"
273                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS",
274             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
275                 + "[@text='InputXpathGenericWhitespaceStartThree']]"
276                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bad']]/TYPE_PARAMETERS/GENERIC_START"
277         );
278 
279         runVerifications(moduleConfig, fileToProcess, expectedViolation,
280                 expectedXpathQueries);
281     }
282 
283 }