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.Arrays;
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.MissingJavadocMethodCheck;
30  
31  public class XpathRegressionMissingJavadocMethodTest extends AbstractXpathTestSupport {
32  
33      @Override
34      protected String getCheckName() {
35          return MissingJavadocMethodCheck.class.getSimpleName();
36      }
37  
38      @Test
39      public void testMissingJavadocMethodCtor() throws Exception {
40          final File fileToProcess = new File(
41              getPath("InputXpathMissingJavadocMethodCtor.java")
42          );
43  
44          final DefaultConfiguration moduleConfig =
45              createModuleConfig(MissingJavadocMethodCheck.class);
46          moduleConfig.addProperty("tokens", "CTOR_DEF");
47  
48          final String[] expectedViolation = {
49              "4:5: " + getCheckMessage(MissingJavadocMethodCheck.class,
50                  MissingJavadocMethodCheck.MSG_JAVADOC_MISSING),
51          };
52  
53          final List<String> expectedXpathQueries = Arrays.asList(
54                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
55                      + "[@text='InputXpathMissingJavadocMethodCtor']]"
56                      + "/OBJBLOCK/CTOR_DEF[."
57                      + "/IDENT[@text='InputXpathMissingJavadocMethodCtor']]",
58  
59                  "/COMPILATION_UNIT/CLASS_DEF"
60                      + "[./IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
61                      + "/OBJBLOCK/CTOR_DEF[."
62                      + "/IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
63                      + "/MODIFIERS",
64  
65                  "/COMPILATION_UNIT/CLASS_DEF"
66                      + "[./IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
67                      + "/OBJBLOCK/CTOR_DEF[."
68                      + "/IDENT[@text='InputXpathMissingJavadocMethodCtor']]"
69                      + "/MODIFIERS/LITERAL_PUBLIC"
70          );
71  
72          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
73      }
74  
75      @Test
76      public void testMissingJavadocMethod() throws Exception {
77          final File fileToProcess = new File(
78              getPath("InputXpathMissingJavadocMethod.java")
79          );
80  
81          final DefaultConfiguration moduleConfig =
82              createModuleConfig(MissingJavadocMethodCheck.class);
83          moduleConfig.addProperty("tokens", "METHOD_DEF");
84  
85          final String[] expectedViolation = {
86              "4:5: " + getCheckMessage(MissingJavadocMethodCheck.class,
87                  MissingJavadocMethodCheck.MSG_JAVADOC_MISSING),
88          };
89  
90          final List<String> expectedXpathQueries = Arrays.asList(
91                  "/COMPILATION_UNIT/CLASS_DEF"
92                      + "[./IDENT[@text='InputXpathMissingJavadocMethod']]"
93                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]",
94  
95                  "/COMPILATION_UNIT/CLASS_DEF"
96                      + "[./IDENT[@text='InputXpathMissingJavadocMethod']]"
97                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
98                      + "/MODIFIERS",
99  
100                 "/COMPILATION_UNIT/CLASS_DEF"
101                     + "[./IDENT[@text='InputXpathMissingJavadocMethod']]"
102                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
103                     + "/MODIFIERS/LITERAL_PUBLIC"
104         );
105 
106         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
107     }
108 }