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