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.coding.UnnecessaryParenthesesCheck;
31  
32  public class XpathRegressionUnnecessaryParenthesesTest extends AbstractXpathTestSupport {
33  
34      @Override
35      protected String getCheckName() {
36          return UnnecessaryParenthesesCheck.class.getSimpleName();
37      }
38  
39      @Test
40      public void testClassFields() throws Exception {
41          final File fileToProcess = new File(
42              getPath("InputXpathUnnecessaryParenthesesClassFields.java")
43          );
44  
45          final DefaultConfiguration moduleConfig =
46              createModuleConfig(UnnecessaryParenthesesCheck.class);
47  
48          final String[] expectedViolation = {
49              "4:13: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
50                  UnnecessaryParenthesesCheck.MSG_ASSIGN),
51          };
52  
53          final List<String> expectedXpathQueries = Arrays.asList(
54              "/COMPILATION_UNIT/CLASS_DEF"
55                  + "[./IDENT[@text='InputXpathUnnecessaryParenthesesClassFields']]"
56                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]"
57                  + "/ASSIGN/EXPR",
58  
59              "/COMPILATION_UNIT/CLASS_DEF"
60                  + "[./IDENT[@text='InputXpathUnnecessaryParenthesesClassFields']]"
61                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]"
62                  + "/ASSIGN/EXPR/LPAREN"
63          );
64  
65          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
66      }
67  
68      @Test
69      public void testConditionals() throws Exception {
70          final File fileToProcess = new File(
71              getPath("InputXpathUnnecessaryParenthesesConditionals.java")
72          );
73  
74          final DefaultConfiguration moduleConfig =
75              createModuleConfig(UnnecessaryParenthesesCheck.class);
76  
77          final String[] expectedViolation = {
78              "5:13: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
79                  UnnecessaryParenthesesCheck.MSG_EXPR),
80          };
81  
82          final List<String> expectedXpathQueries = Arrays.asList(
83              "/COMPILATION_UNIT/CLASS_DEF"
84                  + "[./IDENT[@text='InputXpathUnnecessaryParenthesesConditionals']]"
85                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
86                  + "/SLIST/LITERAL_IF/EXPR",
87  
88              "/COMPILATION_UNIT/CLASS_DEF"
89                  + "[./IDENT[@text='InputXpathUnnecessaryParenthesesConditionals']]"
90                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
91                  + "/SLIST/LITERAL_IF/EXPR/LPAREN"
92          );
93  
94          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
95      }
96  
97      @Test
98      public void testLambdas() throws Exception {
99          final File fileToProcess = new File(
100             getPath("InputXpathUnnecessaryParenthesesLambdas.java")
101         );
102 
103         final DefaultConfiguration moduleConfig =
104             createModuleConfig(UnnecessaryParenthesesCheck.class);
105 
106         final String[] expectedViolation = {
107             "7:35: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
108                 UnnecessaryParenthesesCheck.MSG_LAMBDA),
109         };
110 
111         final List<String> expectedXpathQueries = Collections.singletonList(
112             "/COMPILATION_UNIT/CLASS_DEF"
113                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesLambdas']]"
114                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='predicate']]"
115                 + "/ASSIGN/LAMBDA"
116         );
117 
118         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
119     }
120 
121     @Test
122     public void testLocalVariables() throws Exception {
123         final File fileToProcess = new File(
124             getPath("InputXpathUnnecessaryParenthesesLocalVariables.java")
125         );
126 
127         final DefaultConfiguration moduleConfig =
128             createModuleConfig(UnnecessaryParenthesesCheck.class);
129 
130         final String[] expectedViolation = {
131             "5:18: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
132                 UnnecessaryParenthesesCheck.MSG_IDENT, "a"),
133         };
134 
135         final List<String> expectedXpathQueries = Collections.singletonList(
136             "/COMPILATION_UNIT/CLASS_DEF"
137                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesLocalVariables']]"
138                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
139                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='b']]"
140                 + "/ASSIGN/EXPR/PLUS/IDENT[@text='a']"
141         );
142 
143         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
144     }
145 
146     @Test
147     public void testStringLiteral() throws Exception {
148         final File fileToProcess = new File(
149             getPath("InputXpathUnnecessaryParenthesesStringLiteral.java")
150         );
151 
152         final DefaultConfiguration moduleConfig =
153             createModuleConfig(UnnecessaryParenthesesCheck.class);
154 
155         final String[] expectedViolation = {
156             "5:23: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
157                 UnnecessaryParenthesesCheck.MSG_STRING, "\"Checkstyle\""),
158         };
159 
160         final List<String> expectedXpathQueries = Collections.singletonList(
161             "/COMPILATION_UNIT/CLASS_DEF"
162                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesStringLiteral']]"
163                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
164                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='str']]"
165                 + "/ASSIGN/EXPR/PLUS/STRING_LITERAL[@text='Checkstyle']"
166         );
167 
168         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
169     }
170 
171     @Test
172     public void testMethodDef() throws Exception {
173         final File fileToProcess = new File(
174             getPath("InputXpathUnnecessaryParenthesesMethodDef.java")
175         );
176 
177         final DefaultConfiguration moduleConfig =
178             createModuleConfig(UnnecessaryParenthesesCheck.class);
179 
180         final String[] expectedViolation = {
181             "5:18: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
182                 UnnecessaryParenthesesCheck.MSG_LITERAL, "10"),
183         };
184 
185         final List<String> expectedXpathQueries = Collections.singletonList(
186             "/COMPILATION_UNIT/CLASS_DEF"
187                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesMethodDef']]"
188                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
189                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='a']]"
190                 + "/ASSIGN/EXPR/PLUS/NUM_INT[@text='10']"
191         );
192 
193         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
194     }
195 
196     @Test
197     public void testReturnExpr() throws Exception {
198         final File fileToProcess = new File(
199             getPath("InputXpathUnnecessaryParenthesesReturnExpr.java")
200         );
201 
202         final DefaultConfiguration moduleConfig =
203             createModuleConfig(UnnecessaryParenthesesCheck.class);
204 
205         final String[] expectedViolation = {
206             "5:16: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
207                 UnnecessaryParenthesesCheck.MSG_RETURN),
208         };
209 
210         final List<String> expectedXpathQueries = Arrays.asList(
211             "/COMPILATION_UNIT/CLASS_DEF"
212                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesReturnExpr']]"
213                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
214                 + "/SLIST/LITERAL_RETURN/EXPR",
215 
216             "/COMPILATION_UNIT/CLASS_DEF"
217                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesReturnExpr']]"
218                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
219                 + "/SLIST/LITERAL_RETURN/EXPR/LPAREN"
220         );
221 
222         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
223     }
224 
225     @Test
226     public void testExprWithMethodParam() throws Exception {
227         final File fileToProcess = new File(
228             getPath("InputXpathUnnecessaryParenthesesExprWithMethodParam.java")
229         );
230 
231         final DefaultConfiguration moduleConfig =
232             createModuleConfig(UnnecessaryParenthesesCheck.class);
233 
234         final String[] expectedViolation = {
235             "5:17: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
236                 UnnecessaryParenthesesCheck.MSG_ASSIGN),
237         };
238 
239         final List<String> expectedXpathQueries = Arrays.asList(
240             "/COMPILATION_UNIT/CLASS_DEF"
241                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesExprWithMethodParam']]"
242                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
243                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='c']]"
244                 + "/ASSIGN/EXPR",
245 
246             "/COMPILATION_UNIT/CLASS_DEF"
247                 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesExprWithMethodParam']]"
248                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
249                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='c']]"
250                 + "/ASSIGN/EXPR/LPAREN"
251         );
252 
253         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
254     }
255 }