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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.ParameterAssignmentCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.checks.coding.ParameterAssignmentCheck;
33  
34  public class XpathRegressionParameterAssignmentTest extends AbstractXpathTestSupport {
35  
36      @Override
37      protected String getCheckName() {
38          return ParameterAssignmentCheck.class.getSimpleName();
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/coding/parameterassignment";
44      }
45  
46      @Test
47      public void testMethods() throws Exception {
48          final String filePath =
49                  getPath("InputXpathParameterAssignmentMethods.java");
50          final File fileToProcess = new File(filePath);
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(ParameterAssignmentCheck.class);
54  
55          final String[] expectedViolations = {
56              "9:15: " + getCheckMessage(ParameterAssignmentCheck.class, MSG_KEY, "field"),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61              + "@text='InputXpathParameterAssignmentMethods']]"
62              + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='Test1']]/SLIST/EXPR"
63              + "[./PLUS_ASSIGN/IDENT[@text='field']]",
64              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
65              + "@text='InputXpathParameterAssignmentMethods']]"
66              + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='Test1']]"
67              + "/SLIST/EXPR/PLUS_ASSIGN[./IDENT[@text='field']]"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
71  
72      }
73  
74      @Test
75      public void testLambdas() throws Exception {
76          final String filePath =
77                  getPath("InputXpathParameterAssignmentLambdas.java");
78          final File fileToProcess = new File(filePath);
79  
80          final DefaultConfiguration moduleConfig =
81                  createModuleConfig(ParameterAssignmentCheck.class);
82  
83          final String[] expectedViolations = {
84              "9:32: " + getCheckMessage(ParameterAssignmentCheck.class, MSG_KEY, "q"),
85          };
86  
87          final List<String> expectedXpathQueries = Arrays.asList(
88              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
89              + "@text='InputXpathParameterAssignmentLambdas']]"
90              + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='obj1']]"
91              + "/ASSIGN/LAMBDA[./IDENT[@text='q']]/EXPR",
92              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
93              + "@text='InputXpathParameterAssignmentLambdas']]"
94              + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='obj1']]/ASSIGN/LAMBDA[./IDENT["
95              + "@text='q']]/EXPR/POST_INC[./IDENT[@text='q']]"
96          );
97  
98          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
99      }
100 
101     @Test
102     public void testCtor() throws Exception {
103         final String filePath =
104                 getPath("InputXpathParameterAssignmentCtor.java");
105         final File fileToProcess = new File(filePath);
106 
107         final DefaultConfiguration moduleConfig =
108                 createModuleConfig(ParameterAssignmentCheck.class);
109 
110         final String[] expectedViolations = {
111             "9:15: " + getCheckMessage(ParameterAssignmentCheck.class, MSG_KEY, "field"),
112         };
113 
114         final List<String> expectedXpathQueries = Arrays.asList(
115             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
116             + "@text='InputXpathParameterAssignmentCtor']]"
117             + "/OBJBLOCK/CTOR_DEF[./IDENT["
118             + "@text='InputXpathParameterAssignmentCtor']]"
119             + "/SLIST/EXPR[./PLUS_ASSIGN/IDENT[@text='field']]",
120             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
121             + "@text='InputXpathParameterAssignmentCtor']]"
122             + "/OBJBLOCK/CTOR_DEF[./IDENT["
123             + "@text='InputXpathParameterAssignmentCtor']]"
124             + "/SLIST/EXPR/PLUS_ASSIGN[./IDENT[@text='field']]"
125         );
126 
127         runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
128     }
129 
130 }