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.DefaultComesLastCheck;
32
33 public class XpathRegressionDefaultComesLastTest extends AbstractXpathTestSupport {
34
35 private final String checkName = DefaultComesLastCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Override
43 protected String getPackageLocation() {
44 return "org/checkstyle/suppressionxpathfilter/coding/defaultcomeslast";
45 }
46
47 @Test
48 public void testNonEmptyCase() throws Exception {
49 final File fileToProcess =
50 new File(getPath("InputXpathDefaultComesLastNonEmptyCase.java"));
51
52 final DefaultConfiguration moduleConfig =
53 createModuleConfig(DefaultComesLastCheck.class);
54
55 final String[] expectedViolation = {
56 "8:13: " + getCheckMessage(DefaultComesLastCheck.class,
57 DefaultComesLastCheck.MSG_KEY),
58 };
59
60 final List<String> expectedXpathQueries = Arrays.asList(
61 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
62 + "[@text='InputXpathDefaultComesLastNonEmptyCase']]/OBJBLOCK"
63 + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP["
64 + "./SLIST/EXPR/ASSIGN/IDENT[@text='id']]",
65 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
66 + "[@text='InputXpathDefaultComesLastNonEmptyCase']]/OBJBLOCK"
67 + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP"
68 + "/LITERAL_DEFAULT"
69 );
70
71 runVerifications(moduleConfig, fileToProcess, expectedViolation,
72 expectedXpathQueries);
73 }
74
75 @Test
76 public void testEmptyCase() throws Exception {
77 final File fileToProcess =
78 new File(getPath("InputXpathDefaultComesLastEmptyCase.java"));
79
80 final DefaultConfiguration moduleConfig =
81 createModuleConfig(DefaultComesLastCheck.class);
82 moduleConfig.addProperty("skipIfLastAndSharedWithCase", "true");
83
84 final String[] expectedViolation = {
85 "15:13: " + getCheckMessage(DefaultComesLastCheck.class,
86 DefaultComesLastCheck.MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
87 };
88
89 final List<String> expectedXpathQueries = Collections.singletonList(
90 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
91 + "[@text='InputXpathDefaultComesLastEmptyCase']]/OBJBLOCK"
92 + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP"
93 + "/LITERAL_DEFAULT"
94 );
95
96 runVerifications(moduleConfig, fileToProcess, expectedViolation,
97 expectedXpathQueries);
98 }
99
100 }