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