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.indentation.IndentationCheck;
31  
32  public class XpathRegressionIndentationTest extends AbstractXpathTestSupport {
33      private final String checkName = IndentationCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Test
41      public void testDefault() throws Exception {
42          final File fileToProcess =
43                  new File(getPath("InputXpathIndentationDefault.java"));
44  
45          final DefaultConfiguration moduleConfig =
46                  createModuleConfig(IndentationCheck.class);
47  
48          final String[] expectedViolation = {
49              "4:1: " + getCheckMessage(IndentationCheck.class,
50                      IndentationCheck.MSG_ERROR, "method def modifier", 0, 4),
51          };
52  
53          final List<String> expectedXpathQueries = Arrays.asList(
54              "/COMPILATION_UNIT/CLASS_DEF"
55                      + "[./IDENT[@text='InputXpathIndentationDefault']]"
56                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='wrongIntend']]",
57  
58               "/COMPILATION_UNIT/CLASS_DEF"
59                       + "[./IDENT[@text='InputXpathIndentationDefault']]"
60                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='wrongIntend']]/MODIFIERS",
61  
62              "/COMPILATION_UNIT/CLASS_DEF"
63                      + "[./IDENT[@text='InputXpathIndentationDefault']]"
64                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='wrongIntend']]/TYPE",
65  
66              "/COMPILATION_UNIT/CLASS_DEF"
67                      + "[./IDENT[@text='InputXpathIndentationDefault']]"
68                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='wrongIntend']]/TYPE/LITERAL_VOID"
69          );
70  
71          runVerifications(moduleConfig, fileToProcess, expectedViolation,
72                  expectedXpathQueries);
73      }
74  
75      @Test
76      public void testBasicOffset() throws Exception {
77          final File fileToProcess =
78                  new File(getPath("InputXpathIndentationBasicOffset.java"));
79  
80          final DefaultConfiguration moduleConfig =
81                  createModuleConfig(IndentationCheck.class);
82          moduleConfig.addProperty("arrayInitIndent", "4");
83          moduleConfig.addProperty("basicOffset", "10");
84          moduleConfig.addProperty("braceAdjustment", "0");
85          moduleConfig.addProperty("caseIndent", "4");
86          moduleConfig.addProperty("forceStrictCondition", "false");
87          moduleConfig.addProperty("lineWrappingIndentation", "4");
88          moduleConfig.addProperty("tabWidth", "4");
89          moduleConfig.addProperty("throwsIndent", "4");
90  
91          final String[] expectedViolation = {
92              "4:5: " + getCheckMessage(IndentationCheck.class,
93                      IndentationCheck.MSG_ERROR, "method def modifier", 4, 10),
94          };
95  
96          final List<String> expectedXpathQueries = Arrays.asList(
97              "/COMPILATION_UNIT/CLASS_DEF"
98                      + "[./IDENT[@text='InputXpathIndentationBasicOffset']]"
99                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
100 
101             "/COMPILATION_UNIT/CLASS_DEF"
102                     + "[./IDENT[@text='InputXpathIndentationBasicOffset']]"
103                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
104 
105             "/COMPILATION_UNIT/CLASS_DEF"
106                     + "[./IDENT[@text='InputXpathIndentationBasicOffset']]"
107                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/TYPE",
108 
109             "/COMPILATION_UNIT/CLASS_DEF"
110                     + "[./IDENT[@text='InputXpathIndentationBasicOffset']]"
111                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/TYPE/LITERAL_VOID"
112         );
113 
114         runVerifications(moduleConfig, fileToProcess, expectedViolation,
115                 expectedXpathQueries);
116     }
117 
118     @Test
119     public void testCaseIndent() throws Exception {
120         final File fileToProcess =
121                 new File(getPath("InputXpathIndentationSwitchCase.java"));
122 
123         final DefaultConfiguration moduleConfig =
124                 createModuleConfig(IndentationCheck.class);
125         moduleConfig.addProperty("arrayInitIndent", "4");
126         moduleConfig.addProperty("basicOffset", "4");
127         moduleConfig.addProperty("braceAdjustment", "0");
128         moduleConfig.addProperty("caseIndent", "4");
129         moduleConfig.addProperty("forceStrictCondition", "false");
130         moduleConfig.addProperty("lineWrappingIndentation", "4");
131         moduleConfig.addProperty("tabWidth", "4");
132         moduleConfig.addProperty("throwsIndent", "4");
133 
134         final String[] expectedViolation = {
135             "7:9: " + getCheckMessage(IndentationCheck.class,
136                     IndentationCheck.MSG_CHILD_ERROR, "case", 8, 12),
137         };
138 
139         final List<String> expectedXpathQueries = Arrays.asList(
140             "/COMPILATION_UNIT/CLASS_DEF"
141                     + "[./IDENT[@text='InputXpathIndentationSwitchCase']]"
142                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/"
143                     + "CASE_GROUP",
144 
145             "/COMPILATION_UNIT/CLASS_DEF"
146                     + "[./IDENT[@text='InputXpathIndentationSwitchCase']]"
147                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/"
148                     + "CASE_GROUP/LITERAL_CASE"
149         );
150 
151         runVerifications(moduleConfig, fileToProcess, expectedViolation,
152                 expectedXpathQueries);
153     }
154 
155     @Test
156     public void testLambdaOne() throws Exception {
157         final File fileToProcess =
158                 new File(getPath("InputXpathIndentationLambdaOne.java"));
159 
160         final DefaultConfiguration moduleConfig =
161                 createModuleConfig(IndentationCheck.class);
162         moduleConfig.addProperty("arrayInitIndent", "4");
163         moduleConfig.addProperty("basicOffset", "4");
164         moduleConfig.addProperty("braceAdjustment", "0");
165         moduleConfig.addProperty("caseIndent", "4");
166         moduleConfig.addProperty("forceStrictCondition", "false");
167         moduleConfig.addProperty("lineWrappingIndentation", "4");
168         moduleConfig.addProperty("tabWidth", "4");
169         moduleConfig.addProperty("throwsIndent", "4");
170 
171         final String[] expectedViolation = {
172             "6:9: " + getCheckMessage(IndentationCheck.class,
173                     IndentationCheck.MSG_ERROR, "(", 8, 12),
174         };
175 
176         final List<String> expectedXpathQueries = Collections.singletonList(
177             "/COMPILATION_UNIT/CLASS_DEF"
178                     + "[./IDENT[@text='InputXpathIndentationLambdaOne"
179                     + "']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
180                     + "[./IDENT[@text='getA']]/ASSIGN/LAMBDA/LPAREN"
181         );
182 
183         runVerifications(moduleConfig, fileToProcess, expectedViolation,
184                 expectedXpathQueries);
185     }
186 
187     @Test
188     public void testLambdaTwo() throws Exception {
189         final File fileToProcess =
190                 new File(getPath("InputXpathIndentationLambdaTwo.java"));
191 
192         final DefaultConfiguration moduleConfig =
193                 createModuleConfig(IndentationCheck.class);
194         moduleConfig.addProperty("arrayInitIndent", "4");
195         moduleConfig.addProperty("basicOffset", "4");
196         moduleConfig.addProperty("braceAdjustment", "0");
197         moduleConfig.addProperty("caseIndent", "4");
198         moduleConfig.addProperty("forceStrictCondition", "false");
199         moduleConfig.addProperty("lineWrappingIndentation", "4");
200         moduleConfig.addProperty("tabWidth", "4");
201         moduleConfig.addProperty("throwsIndent", "4");
202 
203         final String[] expectedViolation = {
204             "14:15: " + getCheckMessage(IndentationCheck.class,
205                     IndentationCheck.MSG_CHILD_ERROR_MULTI, "block", 14, "12, 16"),
206         };
207 
208         final List<String> expectedXpathQueries = Collections.singletonList(
209             "/COMPILATION_UNIT/CLASS_DEF"
210                     + "[./IDENT[@text='InputXpathIndentationLambdaTwo']]"
211                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
212                     + "./IDENT[@text='div']]/ASSIGN/LAMBDA/SLIST/LITERAL_RETURN"
213         );
214 
215         runVerifications(moduleConfig, fileToProcess, expectedViolation,
216                 expectedXpathQueries);
217     }
218 
219     @Test
220     public void testIfWithNoCurlies() throws Exception {
221         final File fileToProcess =
222             new File(getPath("InputXpathIndentationIfWithoutCurly.java"));
223 
224         final DefaultConfiguration moduleConfig =
225                 createModuleConfig(IndentationCheck.class);
226 
227         moduleConfig.addProperty("arrayInitIndent", "4");
228         moduleConfig.addProperty("basicOffset", "4");
229         moduleConfig.addProperty("braceAdjustment", "0");
230         moduleConfig.addProperty("caseIndent", "4");
231         moduleConfig.addProperty("forceStrictCondition", "false");
232         moduleConfig.addProperty("lineWrappingIndentation", "4");
233         moduleConfig.addProperty("tabWidth", "4");
234         moduleConfig.addProperty("throwsIndent", "4");
235 
236         final String[] expectedViolation = {
237             "8:9: " + getCheckMessage(IndentationCheck.class,
238                 IndentationCheck.MSG_CHILD_ERROR, "if", 8, 12),
239         };
240 
241         final List<String> expectedXpathQueries = Collections.singletonList(
242             "/COMPILATION_UNIT/CLASS_DEF"
243                     + "[./IDENT[@text='InputXpathIndentationIfWithoutCurly']]"
244                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR/"
245                     + "METHOD_CALL/IDENT[@text='e']"
246         );
247 
248         runVerifications(moduleConfig, fileToProcess, expectedViolation,
249             expectedXpathQueries);
250     }
251 
252     @Test
253     public void testElseWithNoCurlies() throws Exception {
254         final File fileToProcess =
255                 new File(getPath("InputXpathIndentationElseWithoutCurly.java"));
256 
257         final DefaultConfiguration moduleConfig =
258                 createModuleConfig(IndentationCheck.class);
259 
260         moduleConfig.addProperty("arrayInitIndent", "4");
261         moduleConfig.addProperty("basicOffset", "4");
262         moduleConfig.addProperty("braceAdjustment", "0");
263         moduleConfig.addProperty("caseIndent", "4");
264         moduleConfig.addProperty("forceStrictCondition", "false");
265         moduleConfig.addProperty("lineWrappingIndentation", "4");
266         moduleConfig.addProperty("tabWidth", "4");
267         moduleConfig.addProperty("throwsIndent", "4");
268 
269         final String[] expectedViolation = {
270             "12:9: " + getCheckMessage(IndentationCheck.class,
271                 IndentationCheck.MSG_CHILD_ERROR, "else", 8, 12),
272         };
273 
274         final List<String> expectedXpathQueries = Collections.singletonList(
275             "/COMPILATION_UNIT/CLASS_DEF"
276                     + "[./IDENT[@text='InputXpathIndentationElseWithoutCurly']]"
277                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF/LITERAL_ELSE"
278                     + "/EXPR/METHOD_CALL/IDENT[@text='exp']"
279         );
280 
281         runVerifications(moduleConfig, fileToProcess, expectedViolation,
282             expectedXpathQueries);
283     }
284 }