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