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