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.Collections;
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.ExpressionOverBlockLambdaCheck;
31  
32  public class XpathRegressionExpressionOverBlockLambdaTest
33      extends AbstractXpathTestSupport {
34  
35      private final String checkName =
36              ExpressionOverBlockLambdaCheck.class.getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      public String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/coding/expressionoverblocklambda";
46      }
47  
48      @Test
49      public void testDefault() throws Exception {
50          final File fileToProcess =
51                  new File(getPath("InputXpathExpressionOverBlockLambdaDefault.java"));
52  
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(ExpressionOverBlockLambdaCheck.class);
55  
56          final String[] expectedViolation = {
57              "4:21: " + getCheckMessage(ExpressionOverBlockLambdaCheck.class,
58                      ExpressionOverBlockLambdaCheck.MSG_KEY),
59          };
60  
61          final List<String> expectedXpathQueries = Collections.singletonList(
62                  "/COMPILATION_UNIT/CLASS_DEF"
63                          + "[./IDENT[@text='InputXpathExpressionOverBlockLambdaDefault']]"
64                          + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]/ASSIGN/LAMBDA"
65          );
66  
67          runVerifications(moduleConfig, fileToProcess, expectedViolation,
68                  expectedXpathQueries);
69      }
70  
71      @Test
72      public void testMethodCall() throws Exception {
73          final File fileToProcess = new File(
74                  getPath(
75                      "InputXpathExpressionOver"
76                      + "BlockLambdaMethodCall.java"));
77  
78          final DefaultConfiguration moduleConfig =
79                  createModuleConfig(
80                      ExpressionOverBlockLambdaCheck.class);
81  
82          final String[] expectedViolation = {
83              "7:24: " + getCheckMessage(
84                      ExpressionOverBlockLambdaCheck.class,
85                      ExpressionOverBlockLambdaCheck.MSG_KEY),
86          };
87  
88          final List<String> expectedXpathQueries =
89              List.of(
90                  "/COMPILATION_UNIT/CLASS_DEF"
91                      + "[./IDENT[@text="
92                      + "'InputXpathExpressionOver"
93                      + "BlockLambdaMethodCall']]"
94                      + "/OBJBLOCK/METHOD_DEF"
95                      + "[./IDENT[@text='testMethod']]"
96                      + "/SLIST/EXPR/METHOD_CALL/ELIST",
97                  "/COMPILATION_UNIT/CLASS_DEF"
98                      + "[./IDENT[@text="
99                      + "'InputXpathExpressionOver"
100                     + "BlockLambdaMethodCall']]"
101                     + "/OBJBLOCK/METHOD_DEF"
102                     + "[./IDENT[@text='testMethod']]"
103                     + "/SLIST/EXPR/METHOD_CALL"
104                     + "/ELIST/LAMBDA"
105                     + "[./IDENT[@text='s']]"
106             );
107 
108         runVerifications(moduleConfig, fileToProcess,
109                 expectedViolation, expectedXpathQueries);
110     }
111 
112     @Test
113     public void testLocalVar() throws Exception {
114         final File fileToProcess = new File(
115                 getPath(
116                     "InputXpathExpressionOver"
117                     + "BlockLambdaLocalVar.java"));
118 
119         final DefaultConfiguration moduleConfig =
120                 createModuleConfig(
121                     ExpressionOverBlockLambdaCheck.class);
122 
123         final String[] expectedViolation = {
124             "8:15: " + getCheckMessage(
125                     ExpressionOverBlockLambdaCheck.class,
126                     ExpressionOverBlockLambdaCheck.MSG_KEY),
127         };
128 
129         final List<String> expectedXpathQueries =
130                 Collections.singletonList(
131                 "/COMPILATION_UNIT/CLASS_DEF"
132                         + "[./IDENT[@text="
133                         + "'InputXpathExpressionOver"
134                         + "BlockLambdaLocalVar']]"
135                         + "/OBJBLOCK/METHOD_DEF"
136                         + "[./IDENT[@text='testLocal']]"
137                         + "/SLIST/VARIABLE_DEF"
138                         + "[./IDENT[@text='f']]"
139                         + "/ASSIGN/LAMBDA"
140                         + "[./IDENT[@text='x']]"
141         );
142 
143         runVerifications(moduleConfig, fileToProcess,
144                 expectedViolation, expectedXpathQueries);
145     }
146 
147 }