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.List;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28 import com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck;
29
30 public class XpathRegressionDescendantTokenTest extends AbstractXpathTestSupport {
31
32 private final String checkName = DescendantTokenCheck.class.getSimpleName();
33
34 @Override
35 protected String getCheckName() {
36 return checkName;
37 }
38
39 @Test
40 public void testSwitchNoDefault() throws Exception {
41 final File fileToProcess =
42 new File(getPath("InputXpathDescendantTokenSwitchNoDefault.java"));
43
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(DescendantTokenCheck.class);
46 moduleConfig.addProperty("tokens", "LITERAL_SWITCH");
47 moduleConfig.addProperty("maximumDepth", "2");
48 moduleConfig.addProperty("limitedTokens", "LITERAL_DEFAULT");
49 moduleConfig.addProperty("minimumNumber", "1");
50
51 final String[] expected = {
52 "7:9: " + getCheckMessage(DescendantTokenCheck.class,
53 DescendantTokenCheck.MSG_KEY_MIN,
54 0, 1, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
55 };
56
57 final List<String> expectedXpathQueries = List.of(
58 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
59 + "[@text='InputXpathDescendantTokenSwitchNoDefault']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT"
61 + "[@text='testMethod1']]/SLIST/LITERAL_SWITCH"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
65 }
66
67 @Test
68 public void testSwitchTooManyCases() throws Exception {
69 final File fileToProcess =
70 new File(getPath("InputXpathDescendantTokenSwitchTooManyCases.java"));
71
72 final DefaultConfiguration moduleConfig =
73 createModuleConfig(DescendantTokenCheck.class);
74 moduleConfig.addProperty("tokens", "LITERAL_SWITCH");
75 moduleConfig.addProperty("limitedTokens", "LITERAL_CASE");
76 moduleConfig.addProperty("maximumDepth", "2");
77 moduleConfig.addProperty("maximumNumber", "1");
78
79 final String[] expected = {
80 "8:13: " + getCheckMessage(DescendantTokenCheck.class,
81 DescendantTokenCheck.MSG_KEY_MAX,
82 2, 1, "LITERAL_SWITCH", "LITERAL_CASE"),
83 };
84
85 final List<String> expectedXpathQueries = List.of(
86 "/COMPILATION_UNIT/CLASS_DEF"
87 + "[./IDENT[@text='InputXpathDescendantTokenSwitchTooManyCases']]"
88 + "/OBJBLOCK/METHOD_DEF[./IDENT"
89 + "[@text='testMethod1']]/SLIST/VARIABLE_DEF[./IDENT[@text='switchLogic']]"
90 + "/ASSIGN/LAMBDA/SLIST/LITERAL_SWITCH"
91 );
92
93 runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
94 }
95
96 @Test
97 public void testNestedSwitch() throws Exception {
98 final File fileToProcess =
99 new File(getPath("InputXpathDescendantTokenNestedSwitch.java"));
100
101 final DefaultConfiguration moduleConfig =
102 createModuleConfig(DescendantTokenCheck.class);
103
104 moduleConfig.addProperty("tokens", "CASE_GROUP");
105 moduleConfig.addProperty("limitedTokens", "LITERAL_SWITCH");
106 moduleConfig.addProperty("maximumNumber", "0");
107
108 final String[] expected = {
109 "12:13: " + getCheckMessage(DescendantTokenCheck.class,
110 DescendantTokenCheck.MSG_KEY_MAX,
111 1, 0, "CASE_GROUP", "LITERAL_SWITCH"),
112 };
113
114 final List<String> expectedXpathQueries = List.of(
115 "/COMPILATION_UNIT/CLASS_DEF["
116 + "./IDENT[@text='InputXpathDescendantTokenNestedSwitch']]/OBJBLOCK/"
117 + "METHOD_DEF[./IDENT[@text='testMethod1']]/SLIST/LITERAL_SWITCH/"
118 + "CASE_GROUP[./LITERAL_CASE/EXPR/NUM_INT[@text='2']]",
119 "/COMPILATION_UNIT/CLASS_DEF["
120 + "./IDENT[@text='InputXpathDescendantTokenNestedSwitch']]/OBJBLOCK/"
121 + "METHOD_DEF[./IDENT[@text='testMethod1']]/SLIST/LITERAL_SWITCH/"
122 + "CASE_GROUP/LITERAL_CASE"
123 );
124
125 runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
126 }
127 }
128