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 static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.MSG_EXPECTED_TAG;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.MSG_INVALID_INHERIT_DOC;
24  
25  import java.io.File;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
31  import org.junit.jupiter.api.Test;
32  
33  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34  import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck;
35  
36  public class XpathRegressionJavadocMethodTest extends AbstractXpathTestSupport {
37  
38      private final String checkName = JavadocMethodCheck.class.getSimpleName();
39  
40      @Override
41      protected String getCheckName() {
42          return checkName;
43      }
44  
45      @Override
46      protected String getPackageLocation() {
47          return "org/checkstyle/suppressionxpathfilter/javadoc/javadocmethod";
48      }
49  
50      @Test
51      public void testOne() throws Exception {
52          final File fileToProcess =
53                  new File(getPath("InputXpathJavadocMethodOne.java"));
54  
55          final DefaultConfiguration moduleConfig =
56                  createModuleConfig(JavadocMethodCheck.class);
57  
58          final String[] expectedViolation = {
59              "14:5: " + getCheckMessage(JavadocMethodCheck.class, MSG_INVALID_INHERIT_DOC),
60          };
61  
62          final List<String> expectedXpathQueries = Arrays.asList(
63                  "/COMPILATION_UNIT/CLASS_DEF"
64                          + "[./IDENT[@text='InputXpathJavadocMethodOne']]"
65                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='uninheritableMethod']]",
66                  "/COMPILATION_UNIT/CLASS_DEF"
67                          + "[./IDENT[@text='InputXpathJavadocMethodOne']]"
68                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='uninheritableMethod']]/MODIFIERS",
69                  "/COMPILATION_UNIT/CLASS_DEF"
70                          + "[./IDENT[@text='InputXpathJavadocMethodOne']]"
71                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='uninheritableMethod']]/MODIFIERS"
72                          + "/LITERAL_PRIVATE");
73  
74          runVerifications(moduleConfig, fileToProcess, expectedViolation,
75                  expectedXpathQueries);
76      }
77  
78      @Test
79      public void testTwo() throws Exception {
80          final File fileToProcess =
81                  new File(getPath("InputXpathJavadocMethodTwo.java"));
82  
83          final DefaultConfiguration moduleConfig =
84                  createModuleConfig(JavadocMethodCheck.class);
85  
86          final String[] expectedViolation = {
87              "13:31: " + getCheckMessage(JavadocMethodCheck.class, MSG_EXPECTED_TAG,
88                      "@param", "x"),
89          };
90  
91          final List<String> expectedXpathQueries = Collections.singletonList(
92                  "/COMPILATION_UNIT/CLASS_DEF"
93                          + "[./IDENT[@text='InputXpathJavadocMethodTwo']]"
94                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='checkParam']]/PARAMETERS"
95                          + "/PARAMETER_DEF/IDENT[@text='x']");
96  
97          runVerifications(moduleConfig, fileToProcess, expectedViolation,
98                  expectedXpathQueries);
99      }
100 
101     @Test
102     public void testThree() throws Exception {
103         final File fileToProcess =
104                 new File(getPath("InputXpathJavadocMethodThree.java"));
105 
106         final DefaultConfiguration moduleConfig =
107                 createModuleConfig(JavadocMethodCheck.class);
108 
109         final String[] expectedViolation = {
110             "14:13: " + getCheckMessage(JavadocMethodCheck.class, MSG_EXPECTED_TAG,
111                     "@param", "<T>"),
112         };
113 
114         final List<String> expectedXpathQueries = Arrays.asList(
115                 "/COMPILATION_UNIT/CLASS_DEF"
116                         + "[./IDENT[@text='InputXpathJavadocMethodThree']]"
117                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='checkTypeParam']]/TYPE_PARAMETERS"
118                         + "/TYPE_PARAMETER[./IDENT[@text='T']]",
119                 "/COMPILATION_UNIT/CLASS_DEF"
120                         + "[./IDENT[@text='InputXpathJavadocMethodThree']]"
121                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='checkTypeParam']]/TYPE_PARAMETERS"
122                         + "/TYPE_PARAMETER/IDENT[@text='T']");
123 
124         runVerifications(moduleConfig, fileToProcess, expectedViolation,
125                 expectedXpathQueries);
126     }
127 
128     @Test
129     public void testFour() throws Exception {
130         final File fileToProcess =
131                 new File(getPath("InputXpathJavadocMethodFour.java"));
132 
133         final DefaultConfiguration moduleConfig =
134                 createModuleConfig(JavadocMethodCheck.class);
135 
136         moduleConfig.addProperty("validateThrows", "true");
137 
138         final String[] expectedViolation = {
139             "12:30: " + getCheckMessage(JavadocMethodCheck.class, MSG_EXPECTED_TAG,
140                     "@throws", "Exception"),
141         };
142 
143         final List<String> expectedXpathQueries = Collections.singletonList(
144                 "/COMPILATION_UNIT/CLASS_DEF"
145                         + "[./IDENT[@text='InputXpathJavadocMethodFour']]"
146                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
147                         + "/LITERAL_THROWS/IDENT[@text='Exception']");
148 
149         runVerifications(moduleConfig, fileToProcess, expectedViolation,
150                 expectedXpathQueries);
151     }
152 
153     @Test
154     public void testFive() throws Exception {
155         final File fileToProcess =
156                 new File(getPath("InputXpathJavadocMethodFive.java"));
157 
158         final DefaultConfiguration moduleConfig =
159                 createModuleConfig(JavadocMethodCheck.class);
160 
161         moduleConfig.addProperty("validateThrows", "true");
162 
163         final String[] expectedViolation = {
164             "13:19: " + getCheckMessage(JavadocMethodCheck.class, MSG_EXPECTED_TAG,
165                     "@throws", "org.apache.tools.ant.BuildException"),
166         };
167 
168         final List<String> expectedXpathQueries = Collections.singletonList(
169                 "/COMPILATION_UNIT/CLASS_DEF"
170                         + "[./IDENT[@text='InputXpathJavadocMethodFive']]"
171                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='bar']]/SLIST"
172                         + "/LITERAL_THROW/EXPR/LITERAL_NEW"
173                         + "/DOT[./IDENT[@text='BuildException']]"
174                         + "/DOT[./IDENT[@text='ant']]"
175                         + "/DOT[./IDENT[@text='tools']]"
176                         + "/DOT/IDENT[@text='org']");
177 
178         runVerifications(moduleConfig, fileToProcess, expectedViolation,
179                 expectedXpathQueries);
180     }
181 
182 }