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.Collections;
25 import java.util.List;
26
27 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheck;
32
33 public class XpathRegressionMissingSwitchDefaultTest extends AbstractXpathTestSupport {
34
35 private static final Class<MissingSwitchDefaultCheck> CLAZZ = MissingSwitchDefaultCheck.class;
36
37 @Override
38 protected String getCheckName() {
39 return CLAZZ.getSimpleName();
40 }
41
42 @Override
43 protected String getPackageLocation() {
44 return "org/checkstyle/suppressionxpathfilter/coding/missingswitchdefault";
45 }
46
47 @Test
48 public void testSimple() throws Exception {
49 final File fileToProcess =
50 new File(getPath("InputXpathMissingSwitchDefaultSimple.java"));
51
52 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
53 final String[] expectedViolation = {
54 "6:9: " + getCheckMessage(CLAZZ, MissingSwitchDefaultCheck.MSG_KEY),
55 };
56
57 final List<String> expectedXpathQueries = Collections.singletonList(
58 "/COMPILATION_UNIT/CLASS_DEF"
59 + "[./IDENT[@text='InputXpathMissingSwitchDefaultSimple']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test1']]"
61 + "/SLIST/LITERAL_SWITCH"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
65 }
66
67 @Test
68 public void testNested() throws Exception {
69 final File fileToProcess =
70 new File(getPath("InputXpathMissingSwitchDefaultNested.java"));
71
72 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
73
74 final String[] expectedViolation = {
75 "12:17: " + getCheckMessage(CLAZZ, MissingSwitchDefaultCheck.MSG_KEY),
76 };
77
78 final List<String> expectedXpathQueries = Arrays.asList(
79 "/COMPILATION_UNIT/CLASS_DEF"
80 + "[./IDENT[@text='InputXpathMissingSwitchDefaultNested']]"
81 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test2']]"
82 + "/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST",
83 "/COMPILATION_UNIT/CLASS_DEF"
84 + "[./IDENT[@text='InputXpathMissingSwitchDefaultNested']]"
85 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test2']]"
86 + "/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST/"
87 + "LITERAL_SWITCH"
88 );
89
90 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
91 }
92 }