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.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.UnusedLambdaParameterShouldBeUnnamedCheck;
31  
32  public class XpathRegressionUnusedLambdaParameterShouldBeUnnamedTest
33                      extends AbstractXpathTestSupport {
34  
35      private final String checkName = UnusedLambdaParameterShouldBeUnnamedCheck.class
36                                                  .getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      protected String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/coding/unusedlambdaparametershouldbeunnamed";
46      }
47  
48      @Test
49      public void testSimple() throws Exception {
50          final File fileToProcess =
51                  new File(getPath(
52                          "InputXpathUnusedLambdaParameterShouldBeUnnamedSimple.java"));
53  
54          final DefaultConfiguration moduleConfig =
55                  createModuleConfig(UnusedLambdaParameterShouldBeUnnamedCheck.class);
56  
57          final String[] expectedViolation = {
58              "9:41: " + getCheckMessage(UnusedLambdaParameterShouldBeUnnamedCheck.class,
59                      UnusedLambdaParameterShouldBeUnnamedCheck.MSG_UNUSED_LAMBDA_PARAMETER,
60                      "x"),
61          };
62  
63          final List<String> expectedXpathQueries = Arrays.asList(
64              "/COMPILATION_UNIT/CLASS_DEF["
65                  + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
66                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT["
67                  + "@text='f']]/ASSIGN/LAMBDA/PARAMETERS",
68              "/COMPILATION_UNIT/CLASS_DEF["
69                  + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
70                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
71                  + "./IDENT[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='x']]",
72              "/COMPILATION_UNIT/CLASS_DEF["
73                  + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
74                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
75                  + "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS"
76                  + "/PARAMETER_DEF[./IDENT[@text='x']]/MODIFIERS",
77              "/COMPILATION_UNIT/CLASS_DEF["
78                  + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
79                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
80                  + "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/"
81                  + "PARAMETER_DEF[./IDENT[@text='x']]/TYPE",
82              "/COMPILATION_UNIT/CLASS_DEF["
83                  + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
84                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
85                  + "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF/IDENT[@text='x']"
86          );
87  
88          runVerifications(moduleConfig, fileToProcess, expectedViolation,
89                  expectedXpathQueries);
90      }
91  
92      @Test
93      public void testNested() throws Exception {
94          final File fileToProcess =
95                  new File(getPath(
96                          "InputXpathUnusedLambdaParameterShouldBeUnnamedNested.java"));
97  
98          final DefaultConfiguration moduleConfig =
99                  createModuleConfig(UnusedLambdaParameterShouldBeUnnamedCheck.class);
100 
101         final String[] expectedViolation = {
102             "10:45: " + getCheckMessage(UnusedLambdaParameterShouldBeUnnamedCheck.class,
103                     UnusedLambdaParameterShouldBeUnnamedCheck.MSG_UNUSED_LAMBDA_PARAMETER,
104                     "y"),
105         };
106 
107         final List<String> expectedXpathQueries = Arrays.asList(
108             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
109                 + "[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
110                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
111                 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
112                 + "/ASSIGN/LAMBDA/PARAMETERS",
113             "/COMPILATION_UNIT/CLASS_DEF"
114                 + "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
115                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
116                 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
117                 + "/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]",
118             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
119                 + "[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
120                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
121                 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
122                 + "/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]/MODIFIERS",
123             "/COMPILATION_UNIT/CLASS_DEF"
124                 + "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
125                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
126                 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]/ASSIGN"
127                 + "/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]/TYPE",
128             "/COMPILATION_UNIT/CLASS_DEF"
129                 + "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
130                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
131                 + "./IDENT[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF["
132                 + "./IDENT[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF/IDENT[@text='y']"
133         );
134         runVerifications(moduleConfig, fileToProcess, expectedViolation,
135                 expectedXpathQueries);
136     }
137 }