View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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 static com.puppycrawl.tools.checkstyle.checks.TodoCommentCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.TodoCommentCheck;
32  
33  public class XpathRegressionTodoCommentTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = TodoCommentCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Test
43      public void testSingleLine() throws Exception {
44          final File fileToProcess =
45                  new File(getPath("InputXpathTodoCommentSingleLine.java"));
46  
47          final DefaultConfiguration moduleConfig =
48                  createModuleConfig(TodoCommentCheck.class);
49          moduleConfig.addProperty("format", "FIXME:");
50  
51          final String[] expectedViolation = {
52              "4:7: " + getCheckMessage(TodoCommentCheck.class, MSG_KEY, "FIXME:"),
53          };
54  
55          final List<String> expectedXpathQueries = Collections.singletonList(
56                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
57                          + "'InputXpathTodoCommentSingleLine']]/OBJBLOCK/"
58                          + "SINGLE_LINE_COMMENT/COMMENT_CONTENT[@text=' warn FIXME:\\n']");
59  
60          runVerifications(moduleConfig, fileToProcess, expectedViolation,
61                  expectedXpathQueries);
62      }
63  
64      @Test
65      public void testBlock() throws Exception {
66          final File fileToProcess =
67                  new File(getPath("InputXpathTodoCommentBlock.java"));
68  
69          final DefaultConfiguration moduleConfig =
70                  createModuleConfig(TodoCommentCheck.class);
71          moduleConfig.addProperty("format", "FIXME:");
72  
73          final String[] expectedViolation = {
74              "4:7: " + getCheckMessage(TodoCommentCheck.class, MSG_KEY, "FIXME:"),
75          };
76  
77          final List<String> expectedXpathQueries = Collections.singletonList(
78                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
79                          + "'InputXpathTodoCommentBlock']]/"
80                          + "OBJBLOCK/BLOCK_COMMENT_BEGIN/COMMENT_CONTENT"
81                          + "[@text=' // warn\\n     * FIXME:\\n     * TODO\\n     ']"
82          );
83  
84          runVerifications(moduleConfig, fileToProcess, expectedViolation,
85                  expectedXpathQueries);
86      }
87  
88      @Test
89      public void testInsideMethod() throws Exception {
90          final File fileToProcess =
91                  new File(getPath("InputXpathTodoCommentInsideMethod.java"));
92  
93          final DefaultConfiguration moduleConfig =
94                  createModuleConfig(TodoCommentCheck.class);
95          moduleConfig.addProperty("format", "FIXME:");
96  
97          final String[] expectedViolation = {
98              "5:11: " + getCheckMessage(TodoCommentCheck.class, MSG_KEY, "FIXME:"),
99          };
100 
101         final List<String> expectedXpathQueries = Collections.singletonList(
102                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
103                         + "'InputXpathTodoCommentInsideMethod']]/OBJBLOCK/"
104                         + "METHOD_DEF[./IDENT[@text='method']]/SLIST/"
105                         + "SINGLE_LINE_COMMENT/COMMENT_CONTENT[@text=' FIXME: //warn\\n']"
106         );
107 
108         runVerifications(moduleConfig, fileToProcess, expectedViolation,
109                 expectedXpathQueries);
110     }
111 
112 }