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 java.io.File;
23  import java.util.List;
24  
25  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.coding.PatternVariableAssignmentCheck;
30  
31  public class XpathRegressionPatternVariableAssignmentTest extends AbstractXpathTestSupport {
32      private final String checkName = PatternVariableAssignmentCheck.class.getSimpleName();
33  
34      @Override
35      protected String getCheckName() {
36          return checkName;
37      }
38  
39      @Override
40      protected String getPackageLocation() {
41          return "org/checkstyle/suppressionxpathfilter/coding/patternvariableassignment";
42      }
43  
44      @Test
45      public void testMethod() throws Exception {
46          final File fileToProcess =
47                  new File(getPath(
48                          "InputXpathPatternVariableAssignmentMethod.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(PatternVariableAssignmentCheck.class);
52  
53          final String[] expectedViolation = {
54              "8:7: " + getCheckMessage(PatternVariableAssignmentCheck.class,
55                      PatternVariableAssignmentCheck.MSG_KEY,
56                      "s"),
57          };
58  
59          final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
60              + "./IDENT[@text='InputXpathPatternVariableAssignmentMethod']]/OBJBLOCK/METHOD_DEF["
61              + "./IDENT[@text='test']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='s']");
62  
63          runVerifications(moduleConfig, fileToProcess, expectedViolation,
64                  expectedXpathQueries);
65      }
66  
67      @Test
68      public void testLambda() throws Exception {
69          final File fileToProcess =
70                  new File(getPath(
71                          "InputXpathPatternVariableAssignmentLambda.java"));
72  
73          final DefaultConfiguration moduleConfig =
74                  createModuleConfig(PatternVariableAssignmentCheck.class);
75  
76          final String[] expectedViolation = {
77              "12:17: " + getCheckMessage(PatternVariableAssignmentCheck.class,
78                      PatternVariableAssignmentCheck.MSG_KEY,
79                      "x"),
80          };
81  
82          final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
83              + "./IDENT[@text='InputXpathPatternVariableAssignmentLambda']]/OBJBLOCK/METHOD_DEF["
84              + "./IDENT[@text='foo']]/SLIST/EXPR/METHOD_CALL/ELIST/LAMBDA["
85              + "./IDENT[@text='item']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='x']");
86  
87          runVerifications(moduleConfig, fileToProcess, expectedViolation,
88                  expectedXpathQueries);
89      }
90  
91      @Test
92      public void testClass() throws Exception {
93          final File fileToProcess =
94                  new File(getPath(
95                          "InputXpathPatternVariableAssignmentClass.java"));
96  
97          final DefaultConfiguration moduleConfig =
98                  createModuleConfig(PatternVariableAssignmentCheck.class);
99  
100         final String[] expectedViolation = {
101             "10:21: " + getCheckMessage(PatternVariableAssignmentCheck.class,
102                     PatternVariableAssignmentCheck.MSG_KEY,
103                     "x"),
104         };
105 
106         final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
107             + "./IDENT[@text='InputXpathPatternVariableAssignmentClass']]/OBJBLOCK/METHOD_DEF["
108             + "./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='annClass']]/ASSIGN/EXPR/"
109             + "LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK/METHOD_DEF["
110             + "./IDENT[@text='test']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='x']");
111 
112         runVerifications(moduleConfig, fileToProcess, expectedViolation,
113                 expectedXpathQueries);
114     }
115 
116 }