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.indentation;
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck;
31  
32  public class XpathRegressionCommentsIndentationTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = CommentsIndentationCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/indentation/commentsindentation";
44      }
45  
46      @Test
47      public void testSingleLine() throws Exception {
48          final File fileToProcess =
49                  new File(getPath("InputXpathCommentsIndentationSingleLine.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(CommentsIndentationCheck.class);
53  
54          final String[] expectedViolation = {
55              "5:9: " + getCheckMessage(CommentsIndentationCheck.class,
56                  "comments.indentation.single", 4, 8, 4),
57          };
58  
59          final List<String> expectedXpathQueries = Collections.singletonList(
60              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
61                  + "[@text='InputXpathCommentsIndentationSingleLine']]"
62                  + "/OBJBLOCK/SINGLE_LINE_COMMENT[./COMMENT_CONTENT[@text=' Comment // warn\\n']]"
63          );
64  
65          runVerifications(moduleConfig, fileToProcess, expectedViolation,
66                  expectedXpathQueries);
67      }
68  
69      @Test
70      public void testBlock() throws Exception {
71          final File fileToProcess =
72                  new File(getPath("InputXpathCommentsIndentationBlock.java"));
73  
74          final DefaultConfiguration moduleConfig =
75                  createModuleConfig(CommentsIndentationCheck.class);
76  
77          final String[] expectedViolation = {
78              "4:11: " + getCheckMessage(CommentsIndentationCheck.class,
79                  "comments.indentation.block", 7, 10, 4),
80          };
81  
82          final List<String> expectedXpathQueries = Collections.singletonList(
83              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
84                  + "[@text='InputXpathCommentsIndentationBlock']]/OBJBLOCK/"
85                  + "VARIABLE_DEF[./IDENT[@text='f']]/TYPE/BLOCK_COMMENT_BEGIN[./COMMENT_CONTENT"
86                  + "[@text=' // warn\\n           * Javadoc comment\\n           ']]"
87          );
88  
89          runVerifications(moduleConfig, fileToProcess, expectedViolation,
90                  expectedXpathQueries);
91      }
92  
93      @Test
94      public void testSeparator() throws Exception {
95          final File fileToProcess =
96                  new File(getPath("InputXpathCommentsIndentationSeparator.java"));
97  
98          final DefaultConfiguration moduleConfig =
99                  createModuleConfig(CommentsIndentationCheck.class);
100 
101         final String[] expectedViolation = {
102             "8:13: " + getCheckMessage(CommentsIndentationCheck.class,
103                 "comments.indentation.single", 10, 12, 4),
104         };
105 
106         final List<String> expectedXpathQueries = Collections.singletonList(
107             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
108                 + "[@text='InputXpathCommentsIndentationSeparator']]"
109                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/MODIFIERS/SINGLE_LINE_COMMENT"
110                 + "[./COMMENT_CONTENT[@text='///////////// Comment separator // warn\\n']]"
111         );
112 
113         runVerifications(moduleConfig, fileToProcess, expectedViolation,
114                 expectedXpathQueries);
115     }
116 
117     @Test
118     public void testDistributedStatement() throws Exception {
119         final File fileToProcess =
120                 new File(getPath("InputXpathCommentsIndentationDistributedStatement.java"));
121 
122         final DefaultConfiguration moduleConfig =
123                 createModuleConfig(CommentsIndentationCheck.class);
124 
125         final String[] expectedViolation = {
126             "10:25: " + getCheckMessage(CommentsIndentationCheck.class,
127                 "comments.indentation.single", 8, 24, 8),
128         };
129 
130         final List<String> expectedXpathQueries = Collections.singletonList(
131             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
132                 + "[@text='InputXpathCommentsIndentationDistributedStatement']]"
133                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/SINGLE_LINE_COMMENT"
134                 + "[./COMMENT_CONTENT[@text=' Comment // warn\\n']]"
135         );
136 
137         runVerifications(moduleConfig, fileToProcess, expectedViolation,
138                 expectedXpathQueries);
139     }
140 
141     @Test
142     public void testSingleLineBlock() throws Exception {
143         final File fileToProcess =
144                 new File(getPath("InputXpathCommentsIndentationSingleLineBlock.java"));
145 
146         final DefaultConfiguration moduleConfig =
147                 createModuleConfig(CommentsIndentationCheck.class);
148 
149         final String[] expectedViolation = {
150             "6:1: " + getCheckMessage(CommentsIndentationCheck.class,
151                 "comments.indentation.single", 7, 0, 4),
152         };
153 
154         final List<String> expectedXpathQueries = Collections.singletonList(
155             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
156                 + "[@text='InputXpathCommentsIndentationSingleLineBlock']]"
157                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/SINGLE_LINE_COMMENT"
158                 + "[./COMMENT_CONTENT[@text=' block Comment // warn\\n']]"
159         );
160 
161         runVerifications(moduleConfig, fileToProcess, expectedViolation,
162                 expectedXpathQueries);
163     }
164 
165     @Test
166     public void testNonEmptyCase() throws Exception {
167         final File fileToProcess =
168                 new File(getPath("InputXpathCommentsIndentationNonEmptyCase.java"));
169 
170         final DefaultConfiguration moduleConfig =
171                 createModuleConfig(CommentsIndentationCheck.class);
172 
173         final String[] expectedViolation = {
174             "10:20: " + getCheckMessage(CommentsIndentationCheck.class,
175                 "comments.indentation.single", "9, 11", 19, "16, 12"),
176         };
177 
178         final List<String> expectedXpathQueries = Collections.singletonList(
179             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
180                 + "[@text='InputXpathCommentsIndentationNonEmptyCase']]"
181                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/LITERAL_SWITCH/"
182                 + "CASE_GROUP/SINGLE_LINE_COMMENT[./COMMENT_CONTENT[@text=' Comment // warn\\n']]"
183         );
184 
185         runVerifications(moduleConfig, fileToProcess, expectedViolation,
186                 expectedXpathQueries);
187     }
188 
189     @Test
190     public void testEmptyCase() throws Exception {
191         final File fileToProcess =
192                 new File(getPath("InputXpathCommentsIndentationEmptyCase.java"));
193 
194         final DefaultConfiguration moduleConfig =
195                 createModuleConfig(CommentsIndentationCheck.class);
196 
197         final String[] expectedViolation = {
198             "9:1: " + getCheckMessage(CommentsIndentationCheck.class,
199                 "comments.indentation.single", "8, 10", 0, "12, 12"),
200         };
201 
202         final List<String> expectedXpathQueries = Collections.singletonList(
203             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
204                 + "[@text='InputXpathCommentsIndentationEmptyCase']]"
205                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/LITERAL_SWITCH/"
206                 + "CASE_GROUP/SINGLE_LINE_COMMENT[./COMMENT_CONTENT[@text=' Comment // warn\\n']]"
207         );
208 
209         runVerifications(moduleConfig, fileToProcess, expectedViolation,
210                 expectedXpathQueries);
211     }
212 
213     @Test
214     public void testWithinBlockStatement() throws Exception {
215         final File fileToProcess =
216                 new File(getPath("InputXpathCommentsIndentationWithinBlockStatement.java"));
217 
218         final DefaultConfiguration moduleConfig =
219                 createModuleConfig(CommentsIndentationCheck.class);
220 
221         final String[] expectedViolation = {
222             "6:9: " + getCheckMessage(CommentsIndentationCheck.class,
223                 "comments.indentation.single", 7, 8, 12),
224         };
225 
226         final List<String> expectedXpathQueries = Collections.singletonList(
227             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
228                 + "[@text='InputXpathCommentsIndentationWithinBlockStatement']]"
229                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF"
230                 + "[./IDENT[@text='s']]/ASSIGN/EXPR/PLUS[./STRING_LITERAL[@text='O']]"
231                 + "/SINGLE_LINE_COMMENT[./COMMENT_CONTENT[@text=' Comment // warn\\n']]"
232         );
233 
234         runVerifications(moduleConfig, fileToProcess, expectedViolation,
235                 expectedXpathQueries);
236     }
237 }