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.sizes.ParameterNumberCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck;
32  
33  public class XpathRegressionParameterNumberTest extends AbstractXpathTestSupport {
34  
35      @Override
36      protected String getCheckName() {
37          return ParameterNumberCheck.class.getSimpleName();
38      }
39  
40      @Test
41      public void testDefault() throws Exception {
42          final File fileToProcess =
43                  new File(getPath("InputXpathParameterNumberDefault.java"));
44  
45          final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class);
46  
47          final String[] expectedViolations = {
48              "5:10: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 7, 11),
49          };
50  
51          final List<String> expectedXpathQueries = Collections.singletonList(
52                  "/COMPILATION_UNIT/CLASS_DEF"
53                  + "[./IDENT[@text='InputXpathParameterNumberDefault']]"
54                  + "/OBJBLOCK/METHOD_DEF/IDENT[@text='myMethod']"
55          );
56  
57          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
58  
59      }
60  
61      @Test
62      public void testMethods() throws Exception {
63          final File fileToProcess =
64                  new File(getPath("InputXpathParameterNumberMethods.java"));
65  
66          final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class);
67          moduleConfig.addProperty("max", "10");
68          moduleConfig.addProperty("tokens", "METHOD_DEF");
69  
70          final String[] expectedViolations = {
71              "7:10: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 10, 11),
72          };
73  
74          final List<String> expectedXpathQueries = Collections.singletonList(
75                 "/COMPILATION_UNIT/CLASS_DEF"
76                 + "[./IDENT[@text='InputXpathParameterNumberMethods']]"
77                 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='myMethod']"
78          );
79  
80          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
81      }
82  
83      @Test
84      public void testIgnoreOverriddenMethods() throws Exception {
85          final String filePath =
86                  getPath("InputXpathParameterNumberIgnoreOverriddenMethods.java");
87          final File fileToProcess = new File(filePath);
88  
89          final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class);
90          moduleConfig.addProperty("ignoreOverriddenMethods", "true");
91  
92          final String[] expectedViolations = {
93              "6:13: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 7, 8),
94          };
95  
96          final List<String> expectedXpathQueries = Collections.singletonList(
97                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
98                  + "[@text='InputXpathParameterNumberIgnoreOverriddenMethods']]"
99                  + "/OBJBLOCK/CTOR_DEF/IDENT"
100                 + "[@text='InputXpathParameterNumberIgnoreOverriddenMethods']"
101         );
102 
103         runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
104     }
105 
106     @Test
107     public void testIgnoreAnnotatedBy() throws Exception {
108         final String filePath =
109                 getPath("InputXpathParameterNumberIgnoreAnnotatedBy.java");
110         final File fileToProcess = new File(filePath);
111 
112         final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class);
113         moduleConfig.addProperty("ignoreAnnotatedBy", "MyAnno");
114         moduleConfig.addProperty("max", "2");
115 
116         final String[] expectedViolations = {
117             "15:34: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 2, 3),
118         };
119 
120         final List<String> expectedXpathQueries = Collections.singletonList(
121                 "/COMPILATION_UNIT/CLASS_DEF"
122                 + "[./IDENT[@text='InputXpathParameterNumberIgnoreAnnotatedBy']]"
123                 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]"
124                 + "/OBJBLOCK/STATIC_INIT/SLIST/EXPR/LITERAL_NEW[./IDENT[@text='Object']]"
125                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
126                 + "/SLIST/LITERAL_IF/SLIST/EXPR/LITERAL_NEW[./IDENT[@text='Object']]"
127                 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='checkedMethod']"
128         );
129 
130         runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
131     }
132 }