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