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 java.io.File;
23  import java.util.Arrays;
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.WhenShouldBeUsedCheck;
31  
32  public class XpathRegressionWhenShouldBeUsedTest
33          extends AbstractXpathTestSupport {
34  
35      @Override
36      protected String getCheckName() {
37          return WhenShouldBeUsedCheck.class.getSimpleName();
38      }
39  
40      @Override
41      protected String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/coding/whenshouldbeused";
43      }
44  
45      @Test
46      public void testSimple() throws Exception {
47          final File fileToProcess =
48                  new File(getPath(
49                          "InputXpathWhenShouldBeUsedSimple.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(WhenShouldBeUsedCheck.class);
53          final String[] expectedViolation = {
54              "7:13: " + getCheckMessage(WhenShouldBeUsedCheck.class,
55                      WhenShouldBeUsedCheck.MSG_KEY),
56          };
57  
58          final List<String> expectedXpathQueries = Arrays.asList(
59                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedSimple']]"
60                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/SWITCH_RULE"
61                 + "[./LITERAL_CASE/PATTERN_VARIABLE_DEF/IDENT[@text='s']]",
62                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedSimple']]"
63                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
64                          + "LITERAL_SWITCH/SWITCH_RULE/LITERAL_CASE"
65          );
66          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
67      }
68  
69      @Test
70      public void testNested() throws Exception {
71          final File fileToProcess =
72                  new File(getPath(
73                          "InputXpathWhenShouldBeUsedNested.java"));
74  
75          final DefaultConfiguration moduleConfig =
76                  createModuleConfig(WhenShouldBeUsedCheck.class);
77          final String[] expectedViolation = {
78              "10:21: " + getCheckMessage(WhenShouldBeUsedCheck.class,
79                      WhenShouldBeUsedCheck.MSG_KEY),
80          };
81  
82          final List<String> expectedXpathQueries = Arrays.asList(
83                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedNested']]"
84                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
85                  + "VARIABLE_DEF[./IDENT[@text='x']]/ASSIGN/EXPR/LITERAL_SWITCH"
86                  + "/SWITCH_RULE/SLIST/"
87                  + "LITERAL_SWITCH/SWITCH_RULE[./LITERAL_CASE/PATTERN_VARIABLE_DEF/"
88                  + "IDENT[@text='_']]",
89                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedNested']]"
90                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
91                  + "[./IDENT[@text='x']]/ASSIGN/EXPR/LITERAL_SWITCH/"
92                  + "SWITCH_RULE/SLIST/"
93                  + "LITERAL_SWITCH/SWITCH_RULE/LITERAL_CASE"
94          );
95          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
96      }
97  }