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