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.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.FallThroughCheck;
31
32 public class XpathRegressionFallThroughTest extends AbstractXpathTestSupport {
33
34 private final String checkName = FallThroughCheck.class.getSimpleName();
35
36 @Override
37 protected String getCheckName() {
38 return checkName;
39 }
40
41 @Override
42 protected String getPackageLocation() {
43 return "org/checkstyle/suppressionxpathfilter/coding/fallthrough";
44 }
45
46 @Test
47 public void testFallThrough() throws Exception {
48 final File fileToProcess =
49 new File(getPath("InputXpathFallThrough.java"));
50
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(FallThroughCheck.class);
53
54 final String[] expectedViolation = {
55 "11:13: " + getCheckMessage(FallThroughCheck.class,
56 FallThroughCheck.MSG_FALL_THROUGH),
57 };
58
59 final List<String> expectedXpathQueries = Arrays.asList(
60 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathFallThrough']]"
61 + "/OBJBLOCK"
62 + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP["
63 + "./LITERAL_CASE/EXPR/NUM_INT[@text='2']]",
64 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathFallThrough']]"
65 + "/OBJBLOCK"
66 + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP/LITERAL_CASE"
67 );
68
69 runVerifications(moduleConfig, fileToProcess, expectedViolation,
70 expectedXpathQueries);
71 }
72
73 @Test
74 public void testDefaultCase() throws Exception {
75 final File fileToProcess =
76 new File(getPath("InputXpathFallThroughDefaultCase.java"));
77
78 final DefaultConfiguration moduleConfig =
79 createModuleConfig(FallThroughCheck.class);
80 moduleConfig.addProperty("checkLastCaseGroup", "true");
81
82 final String[] expectedViolation = {
83 "10:17: " + getCheckMessage(FallThroughCheck.class,
84 FallThroughCheck.MSG_FALL_THROUGH_LAST),
85 };
86
87 final List<String> expectedXpathQueries = Arrays.asList(
88 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathFallThroughDefaultCase']]"
89 + "/OBJBLOCK/METHOD_DEF["
90 + "./IDENT[@text='methodFallThruCustomWords']]/SLIST/LITERAL_WHILE/SLIST"
91 + "/LITERAL_SWITCH/CASE_GROUP[./SLIST/EXPR/POST_INC/IDENT[@text='i']]",
92 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathFallThroughDefaultCase']]"
93 + "/OBJBLOCK/METHOD_DEF["
94 + "./IDENT[@text='methodFallThruCustomWords']]/SLIST/LITERAL_WHILE/SLIST"
95 + "/LITERAL_SWITCH/CASE_GROUP/LITERAL_DEFAULT"
96 );
97
98 runVerifications(moduleConfig, fileToProcess, expectedViolation,
99 expectedXpathQueries);
100 }
101 }