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.javadoc;
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.javadoc.InvalidJavadocPositionCheck;
31  
32  public class XpathRegressionInvalidJavadocPositionTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = InvalidJavadocPositionCheck.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/javadoc/invalidjavadocposition";
44      }
45  
46      @Test
47      public void testOne() throws Exception {
48          final File fileToProcess =
49                  new File(getPath("InputXpathInvalidJavadocPositionOne.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(InvalidJavadocPositionCheck.class);
53  
54          final String[] expectedViolation = {
55              "4:1: " + getCheckMessage(InvalidJavadocPositionCheck.class,
56                  InvalidJavadocPositionCheck.MSG_KEY),
57          };
58  
59          final List<String> expectedXpathQueries = Collections.singletonList(
60              "/COMPILATION_UNIT/CLASS_DEF"
61                      + "[./IDENT[@text='InputXpathInvalidJavadocPositionOne']]"
62                      + "/MODIFIERS/BLOCK_COMMENT_BEGIN[./COMMENT_CONTENT"
63                      + "[@text='* // warn\\n * Javadoc Comment\\n ']]"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation,
67                  expectedXpathQueries);
68      }
69  
70      @Test
71      public void testTwo() throws Exception {
72          final File fileToProcess =
73                  new File(getPath("InputXpathInvalidJavadocPositionTwo.java"));
74  
75          final DefaultConfiguration moduleConfig =
76                  createModuleConfig(InvalidJavadocPositionCheck.class);
77  
78          final String[] expectedViolation = {
79              "5:1: " + getCheckMessage(InvalidJavadocPositionCheck.class,
80                  InvalidJavadocPositionCheck.MSG_KEY),
81          };
82  
83          final List<String> expectedXpathQueries = Collections.singletonList(
84              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
85                      + "[@text='InputXpathInvalidJavadocPositionTwo']]"
86                      + "/OBJBLOCK/BLOCK_COMMENT_BEGIN[./COMMENT_CONTENT"
87                      + "[@text='* // warn\\n * Javadoc comment\\n ']]"
88          );
89  
90          runVerifications(moduleConfig, fileToProcess, expectedViolation,
91                  expectedXpathQueries);
92      }
93  
94      @Test
95      public void testThree() throws Exception {
96          final File fileToProcess =
97                  new File(getPath("InputXpathInvalidJavadocPositionThree.java"));
98  
99          final DefaultConfiguration moduleConfig =
100                 createModuleConfig(InvalidJavadocPositionCheck.class);
101 
102         final String[] expectedViolation = {
103             "6:5: " + getCheckMessage(InvalidJavadocPositionCheck.class,
104                 InvalidJavadocPositionCheck.MSG_KEY),
105         };
106 
107         final List<String> expectedXpathQueries = Collections.singletonList(
108             "/COMPILATION_UNIT/CLASS_DEF"
109                     + "[./IDENT[@text='InputXpathInvalidJavadocPositionThree']]/"
110                     + "OBJBLOCK/BLOCK_COMMENT_BEGIN[./COMMENT_CONTENT"
111                     + "[@text='* // warn\\n     * Javadoc comment\\n     ']]"
112         );
113 
114         runVerifications(moduleConfig, fileToProcess, expectedViolation,
115                 expectedXpathQueries);
116     }
117 
118     @Test
119     public void testFour() throws Exception {
120         final File fileToProcess =
121                 new File(getPath("InputXpathInvalidJavadocPositionFour.java"));
122 
123         final DefaultConfiguration moduleConfig =
124                 createModuleConfig(InvalidJavadocPositionCheck.class);
125 
126         final String[] expectedViolation = {
127             "4:5: " + getCheckMessage(InvalidJavadocPositionCheck.class,
128                 InvalidJavadocPositionCheck.MSG_KEY),
129         };
130 
131         final List<String> expectedXpathQueries = Collections.singletonList(
132             "/COMPILATION_UNIT/CLASS_DEF"
133                 + "[./IDENT[@text='InputXpathInvalidJavadocPositionFour']]"
134                 + "/OBJBLOCK/BLOCK_COMMENT_BEGIN[./COMMENT_CONTENT"
135                 + "[@text='* // warn\\n     * Javadoc Comment\\n     ']]"
136         );
137 
138         runVerifications(moduleConfig, fileToProcess, expectedViolation,
139                 expectedXpathQueries);
140     }
141 
142     @Test
143     public void testFive() throws Exception {
144         final File fileToProcess =
145                 new File(getPath("InputXpathInvalidJavadocPositionFive.java"));
146 
147         final DefaultConfiguration moduleConfig =
148                 createModuleConfig(InvalidJavadocPositionCheck.class);
149 
150         final String[] expectedViolation = {
151             "5:9: " + getCheckMessage(InvalidJavadocPositionCheck.class,
152                 InvalidJavadocPositionCheck.MSG_KEY),
153         };
154 
155         final List<String> expectedXpathQueries = Collections.singletonList(
156             "/COMPILATION_UNIT/CLASS_DEF"
157                 + "[./IDENT[@text='InputXpathInvalidJavadocPositionFive']]"
158                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
159                 + "/SLIST/BLOCK_COMMENT_BEGIN[./COMMENT_CONTENT"
160                 + "[@text='* // warn\\n         * Javadoc comment\\n         ']]"
161         );
162 
163         runVerifications(moduleConfig, fileToProcess, expectedViolation,
164                 expectedXpathQueries);
165     }
166 
167     @Test
168     public void testSix() throws Exception {
169         final File fileToProcess =
170                 new File(getPath("InputXpathInvalidJavadocPositionSix.java"));
171 
172         final DefaultConfiguration moduleConfig =
173                 createModuleConfig(InvalidJavadocPositionCheck.class);
174 
175         final String[] expectedViolation = {
176             "5:5: " + getCheckMessage(InvalidJavadocPositionCheck.class,
177                 InvalidJavadocPositionCheck.MSG_KEY),
178         };
179 
180         final List<String> expectedXpathQueries = Collections.singletonList(
181             "/COMPILATION_UNIT/CLASS_DEF"
182                 + "[./IDENT[@text='InputXpathInvalidJavadocPositionSix']]"
183                 + "/OBJBLOCK/BLOCK_COMMENT_BEGIN[./COMMENT_CONTENT"
184                 + "[@text='* // warn\\n     * Javadoc Comment\\n     ']]"
185         );
186 
187         runVerifications(moduleConfig, fileToProcess, expectedViolation,
188                 expectedXpathQueries);
189     }
190 }