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