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 static com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
30  import org.junit.jupiter.api.Test;
31  
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck;
34  
35  public class XpathRegressionSimplifyBooleanExpressionTest extends AbstractXpathTestSupport {
36  
37      @Override
38      protected String getCheckName() {
39          return SimplifyBooleanExpressionCheck.class.getSimpleName();
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/coding/simplifybooleanexpression";
45      }
46  
47      @Test
48      public void testSimple() throws Exception {
49          final String fileName = "InputXpathSimplifyBooleanExpressionSimple.java";
50          final File fileToProcess = new File(getPath(fileName));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(SimplifyBooleanExpressionCheck.class);
54  
55          final String[] expectedViolations = {
56              "8:13: " + getCheckMessage(SimplifyBooleanExpressionCheck.class, MSG_KEY),
57          };
58  
59          final List<String> expectedXpathQuery = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
61                  + "[@text='InputXpathSimplifyBooleanExpressionSimple']]"
62                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR",
63                   "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
64                  + "[@text='InputXpathSimplifyBooleanExpressionSimple']]"
65                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR/LNOT"
66          );
67  
68          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQuery);
69      }
70  
71      @Test
72      public void testAnonymous() throws Exception {
73          final String fileName =
74                  "InputXpathSimplifyBooleanExpressionAnonymous.java";
75          final File fileToProcess = new File(getPath(fileName));
76  
77          final DefaultConfiguration moduleConfig =
78                  createModuleConfig(SimplifyBooleanExpressionCheck.class);
79  
80          final String[] expectedViolations = {
81              "8:19: " + getCheckMessage(SimplifyBooleanExpressionCheck.class, MSG_KEY),
82          };
83  
84          final List<String> expectedXpathQuery = Arrays.asList(
85                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
86                  + "[@text='InputXpathSimplifyBooleanExpressionAnonymous']]"
87                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
88                  + "[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR",
89                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
90                  + "[@text='InputXpathSimplifyBooleanExpressionAnonymous']]"
91                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
92                  + "[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR/EQUAL[./IDENT[@text='a']]"
93          );
94  
95          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQuery);
96      }
97  
98      @Test
99      public void testInterface() throws Exception {
100         final String fileName =
101                 "InputXpathSimplifyBooleanExpressionInterface.java";
102         final File fileToProcess = new File(getPath(fileName));
103 
104         final DefaultConfiguration moduleConfig =
105                 createModuleConfig(SimplifyBooleanExpressionCheck.class);
106 
107         final String[] expectedViolations = {
108             "7:20: " + getCheckMessage(SimplifyBooleanExpressionCheck.class, MSG_KEY),
109         };
110 
111         final List<String> expectedXpathQuery = Collections.singletonList(
112                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
113                 + "[@text='InputXpathSimplifyBooleanExpressionInterface']]"
114                 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF[./IDENT"
115                 + "[@text='test']]/SLIST/LITERAL_IF/EXPR/LNOT/NOT_EQUAL[./IDENT[@text='b']]"
116         );
117 
118         runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQuery);
119     }
120 }