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