1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter;
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.checks.coding.WhenShouldBeUsedCheck;
30
31 public class XpathRegressionWhenShouldBeUsedTest
32 extends AbstractXpathTestSupport {
33
34 @Override
35 protected String getCheckName() {
36 return WhenShouldBeUsedCheck.class.getSimpleName();
37 }
38
39 @Test
40 public void testSimple() throws Exception {
41 final File fileToProcess =
42 new File(getNonCompilablePath(
43 "InputXpathWhenShouldBeUsedSimple.java"));
44
45 final DefaultConfiguration moduleConfig =
46 createModuleConfig(WhenShouldBeUsedCheck.class);
47 final String[] expectedViolation = {
48 "7:13: " + getCheckMessage(WhenShouldBeUsedCheck.class,
49 WhenShouldBeUsedCheck.MSG_KEY),
50 };
51
52 final List<String> expectedXpathQueries = Arrays.asList(
53 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedSimple']]"
54 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/SWITCH_RULE"
55 + "[./LITERAL_CASE/PATTERN_VARIABLE_DEF/IDENT[@text='s']]",
56 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedSimple']]"
57 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
58 + "LITERAL_SWITCH/SWITCH_RULE/LITERAL_CASE"
59 );
60 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
61 }
62
63 @Test
64 public void testNested() throws Exception {
65 final File fileToProcess =
66 new File(getNonCompilablePath(
67 "InputXpathWhenShouldBeUsedNested.java"));
68
69 final DefaultConfiguration moduleConfig =
70 createModuleConfig(WhenShouldBeUsedCheck.class);
71 final String[] expectedViolation = {
72 "10:21: " + getCheckMessage(WhenShouldBeUsedCheck.class,
73 WhenShouldBeUsedCheck.MSG_KEY),
74 };
75
76 final List<String> expectedXpathQueries = Arrays.asList(
77 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedNested']]"
78 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
79 + "VARIABLE_DEF[./IDENT[@text='x']]/ASSIGN/EXPR/LITERAL_SWITCH"
80 + "/SWITCH_RULE/SLIST/"
81 + "LITERAL_SWITCH/SWITCH_RULE[./LITERAL_CASE/PATTERN_VARIABLE_DEF/"
82 + "IDENT[@text='_']]",
83 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathWhenShouldBeUsedNested']]"
84 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
85 + "[./IDENT[@text='x']]/ASSIGN/EXPR/LITERAL_SWITCH/"
86 + "SWITCH_RULE/SLIST/"
87 + "LITERAL_SWITCH/SWITCH_RULE/LITERAL_CASE"
88 );
89 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
90 }
91 }