View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.List;
24  
25  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.coding.UseEnhancedSwitchCheck;
30  
31  public class XpathRegressionUseEnhancedSwitchTest
32      extends AbstractXpathTestSupport {
33  
34      @Override
35      protected String getCheckName() {
36          return UseEnhancedSwitchCheck.class.getSimpleName();
37      }
38  
39      @Override
40      public String getPackageLocation() {
41          return "org/checkstyle/suppressionxpathfilter/coding/useenhancedswitch";
42      }
43  
44      @Test
45      public void testSimple() throws Exception {
46          final File fileToProcess = new File(getPath("InputXpathUseEnhancedSwitchSimple.java"));
47  
48          final DefaultConfiguration moduleConfig = createModuleConfig(UseEnhancedSwitchCheck.class);
49          final String[] expectedViolation = {
50              "6:9: " + getCheckMessage(UseEnhancedSwitchCheck.class, UseEnhancedSwitchCheck.MSG_KEY),
51          };
52  
53          final List<String> expectedXpathQueries = List.of(
54              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathUseEnhancedSwitchSimple']]"
55                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH"
56          );
57          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
58      }
59  
60      @Test
61      public void testExpressions() throws Exception {
62          final File fileToProcess = new File(getPath("InputXpathUseEnhancedSwitchExpressions.java"));
63  
64          final DefaultConfiguration moduleConfig = createModuleConfig(UseEnhancedSwitchCheck.class);
65          final String[] expectedViolation = {
66              "5:18: " + getCheckMessage(UseEnhancedSwitchCheck.class,
67                  UseEnhancedSwitchCheck.MSG_KEY),
68          };
69  
70          final String exprXpathQuery = "/COMPILATION_UNIT"
71              + "/CLASS_DEF[./IDENT[@text='InputXpathUseEnhancedSwitchExpressions']]/OBJBLOCK"
72              + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT[@text='id']]/ASSIGN"
73              + "/EXPR";
74          final List<String> expectedXpathQueries = List.of(
75              exprXpathQuery,
76              exprXpathQuery + "/LITERAL_SWITCH"
77          );
78          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
79      }
80  }