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.javadoc;
21  
22  import java.io.File;
23  import java.util.Arrays;
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.MissingJavadocMethodCheck;
31  
32  public class XpathRegressionMissingJavadocMethodTest extends AbstractXpathTestSupport {
33  
34      @Override
35      protected String getCheckName() {
36          return MissingJavadocMethodCheck.class.getSimpleName();
37      }
38  
39      @Override
40      public String getPackageLocation() {
41          return "org/checkstyle/suppressionxpathfilter/javadoc/missingjavadocmethod";
42      }
43  
44      @Test
45      public void testMissingJavadocMethodCtor() throws Exception {
46          final File fileToProcess = new File(
47              getPath("InputXpathMissingJavadocMethodCtor.java")
48          );
49  
50          final DefaultConfiguration moduleConfig =
51              createModuleConfig(MissingJavadocMethodCheck.class);
52          moduleConfig.addProperty("tokens", "CTOR_DEF");
53  
54          final String[] expectedViolation = {
55              "4:5: " + getCheckMessage(MissingJavadocMethodCheck.class,
56                  MissingJavadocMethodCheck.MSG_JAVADOC_MISSING,
57                  "InputXpathMissingJavadocMethodCtor"),
58          };
59  
60          final List<String> expectedXpathQueries = Arrays.asList(
61                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
62                      + "[@text='InputXpathMissingJavadocMethodCtor']]"
63                      + "/OBJBLOCK/CTOR_DEF[."
64                      + "/IDENT[@text='InputXpathMissingJavadocMethodCtor']]",
65  
66                  "/COMPILATION_UNIT/CLASS_DEF"
67                      + "[./IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
68                      + "/OBJBLOCK/CTOR_DEF[."
69                      + "/IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
70                      + "/MODIFIERS",
71  
72                  "/COMPILATION_UNIT/CLASS_DEF"
73                      + "[./IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
74                      + "/OBJBLOCK/CTOR_DEF[."
75                      + "/IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
76                      + "/MODIFIERS/LITERAL_PUBLIC"
77          );
78  
79          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
80      }
81  
82      @Test
83      public void testMissingJavadocMethod() throws Exception {
84          final File fileToProcess = new File(
85              getPath("InputXpathMissingJavadocMethod.java")
86          );
87  
88          final DefaultConfiguration moduleConfig =
89              createModuleConfig(MissingJavadocMethodCheck.class);
90          moduleConfig.addProperty("tokens", "METHOD_DEF");
91  
92          final String[] expectedViolation = {
93              "4:5: " + getCheckMessage(MissingJavadocMethodCheck.class,
94                  MissingJavadocMethodCheck.MSG_JAVADOC_MISSING, "foo"),
95          };
96  
97          final List<String> expectedXpathQueries = Arrays.asList(
98                  "/COMPILATION_UNIT/CLASS_DEF"
99                      + "[./IDENT[@text='InputXpathMissingJavadocMethod']]"
100                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]",
101 
102                 "/COMPILATION_UNIT/CLASS_DEF"
103                     + "[./IDENT[@text='InputXpathMissingJavadocMethod']]"
104                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
105                     + "/MODIFIERS",
106 
107                 "/COMPILATION_UNIT/CLASS_DEF"
108                     + "[./IDENT[@text='InputXpathMissingJavadocMethod']]"
109                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
110                     + "/MODIFIERS/LITERAL_PUBLIC"
111         );
112 
113         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
114     }
115 
116     @Test
117     public void testMissingJavadocMethodInInterface() throws Exception {
118         final File fileToProcess = new File(
119             getPath("InputXpathMissingJavadocMethodInInterface.java")
120         );
121 
122         final DefaultConfiguration moduleConfig =
123             createModuleConfig(MissingJavadocMethodCheck.class);
124         moduleConfig.addProperty("tokens", "METHOD_DEF");
125 
126         final String[] expectedViolation = {
127             "4:5: " + getCheckMessage(MissingJavadocMethodCheck.class,
128                 MissingJavadocMethodCheck.MSG_JAVADOC_MISSING, "foo"),
129         };
130 
131         final List<String> expectedXpathQueries = Arrays.asList(
132                 "/COMPILATION_UNIT/INTERFACE_DEF"
133                     + "[./IDENT[@text='InputXpathMissingJavadocMethodInInterface']]"
134                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]",
135 
136                 "/COMPILATION_UNIT/INTERFACE_DEF"
137                     + "[./IDENT[@text='InputXpathMissingJavadocMethodInInterface']]"
138                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
139                     + "/MODIFIERS",
140 
141                 "/COMPILATION_UNIT/INTERFACE_DEF"
142                     + "[./IDENT[@text='InputXpathMissingJavadocMethodInInterface']]"
143                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
144                     + "/MODIFIERS/LITERAL_PUBLIC"
145         );
146 
147         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
148     }
149 
150 }