1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }