View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  
33      private final String checkName = PatternVariableAssignmentCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Override
41      public String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/coding/patternvariableassignment";
43      }
44  
45      @Test
46      public void testMethod() throws Exception {
47          final File fileToProcess =
48                  new File(getPath(
49                          "InputXpathPatternVariableAssignmentMethod.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(PatternVariableAssignmentCheck.class);
53  
54          final String[] expectedViolation = {
55              "8:7: " + getCheckMessage(PatternVariableAssignmentCheck.class,
56                      PatternVariableAssignmentCheck.MSG_KEY,
57                      "s"),
58          };
59  
60          final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
61              + "./IDENT[@text='InputXpathPatternVariableAssignmentMethod']]/OBJBLOCK/METHOD_DEF["
62              + "./IDENT[@text='test']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='s']");
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation,
65                  expectedXpathQueries);
66      }
67  
68      @Test
69      public void testLambda() throws Exception {
70          final File fileToProcess =
71                  new File(getPath(
72                          "InputXpathPatternVariableAssignmentLambda.java"));
73  
74          final DefaultConfiguration moduleConfig =
75                  createModuleConfig(PatternVariableAssignmentCheck.class);
76  
77          final String[] expectedViolation = {
78              "12:17: " + getCheckMessage(PatternVariableAssignmentCheck.class,
79                      PatternVariableAssignmentCheck.MSG_KEY,
80                      "x"),
81          };
82  
83          final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
84              + "./IDENT[@text='InputXpathPatternVariableAssignmentLambda']]/OBJBLOCK/METHOD_DEF["
85              + "./IDENT[@text='foo']]/SLIST/EXPR/METHOD_CALL/ELIST/LAMBDA["
86              + "./IDENT[@text='item']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='x']");
87  
88          runVerifications(moduleConfig, fileToProcess, expectedViolation,
89                  expectedXpathQueries);
90      }
91  
92      @Test
93      public void testClass() throws Exception {
94          final File fileToProcess =
95                  new File(getPath(
96                          "InputXpathPatternVariableAssignmentClass.java"));
97  
98          final DefaultConfiguration moduleConfig =
99                  createModuleConfig(PatternVariableAssignmentCheck.class);
100 
101         final String[] expectedViolation = {
102             "10:21: " + getCheckMessage(PatternVariableAssignmentCheck.class,
103                     PatternVariableAssignmentCheck.MSG_KEY,
104                     "x"),
105         };
106 
107         final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
108             + "./IDENT[@text='InputXpathPatternVariableAssignmentClass']]/OBJBLOCK/METHOD_DEF["
109             + "./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='annClass']]/ASSIGN/EXPR/"
110             + "LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK/METHOD_DEF["
111             + "./IDENT[@text='test']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='x']");
112 
113         runVerifications(moduleConfig, fileToProcess, expectedViolation,
114                 expectedXpathQueries);
115     }
116 
117 }