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 java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
31  import com.puppycrawl.tools.checkstyle.checks.naming.LambdaParameterNameCheck;
32  
33  public class XpathRegressionLambdaParameterNameTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = LambdaParameterNameCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Test
43      public void testDefault() throws Exception {
44          final File fileToProcess =
45                  new File(getPath("InputXpathLambdaParameterNameDefault.java"));
46  
47          final DefaultConfiguration moduleConfig =
48                  createModuleConfig(LambdaParameterNameCheck.class);
49          final String defaultPattern = "^([a-z][a-zA-Z0-9]*|_)$";
50  
51          final String[] expectedViolation = {
52              "7:44: " + getCheckMessage(LambdaParameterNameCheck.class,
53                      AbstractNameCheck.MSG_INVALID_PATTERN, "S", defaultPattern),
54          };
55  
56          final List<String> expectedXpathQueries = Collections.singletonList(
57                 "/COMPILATION_UNIT/CLASS_DEF"
58                         + "[./IDENT[@text='InputXpathLambdaParameterNameDefault']]"
59                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
60                         + "./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/IDENT[@text='S']"
61          );
62  
63          runVerifications(moduleConfig, fileToProcess, expectedViolation,
64                  expectedXpathQueries);
65      }
66  
67      @Test
68      public void testNonDefaultPattern() throws Exception {
69          final File fileToProcess =
70                  new File(getPath("InputXpathLambdaParameterNameNonDefaultPattern.java"));
71  
72          final String nonDefaultPattern = "^_[a-zA-Z0-9]*$";
73  
74          final DefaultConfiguration moduleConfig =
75                  createModuleConfig(LambdaParameterNameCheck.class);
76          moduleConfig.addProperty("format", nonDefaultPattern);
77  
78          final String[] expectedViolation = {
79              "7:45: " + getCheckMessage(LambdaParameterNameCheck.class,
80                      AbstractNameCheck.MSG_INVALID_PATTERN, "s", nonDefaultPattern),
81          };
82  
83          final List<String> expectedXpathQueries = Arrays.asList(
84                  "/COMPILATION_UNIT/CLASS_DEF"
85                          + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
86                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
87                          + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS",
88  
89                  "/COMPILATION_UNIT/CLASS_DEF"
90                          + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
91                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
92                          + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
93                          + "/PARAMETER_DEF[./IDENT[@text='s']]",
94  
95                  "/COMPILATION_UNIT/CLASS_DEF"
96                          + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
97                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST"
98                          + "/VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
99                          + "/PARAMETER_DEF[./IDENT[@text='s']]/MODIFIERS",
100 
101                 "/COMPILATION_UNIT/CLASS_DEF"
102                         + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
103                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
104                         + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
105                         + "/PARAMETER_DEF[./IDENT[@text='s']]/TYPE",
106 
107                 "/COMPILATION_UNIT/CLASS_DEF"
108                         + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
109                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
110                         + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
111                         + "/PARAMETER_DEF/IDENT[@text='s']"
112         );
113 
114         runVerifications(moduleConfig, fileToProcess, expectedViolation,
115                 expectedXpathQueries);
116     }
117 }