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