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.IllegalTokenCheck;
32
33 public class XpathRegressionIllegalTokenTest extends AbstractXpathTestSupport {
34
35 private final String checkName = IllegalTokenCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Override
43 public String getPackageLocation() {
44 return "org/checkstyle/suppressionxpathfilter/coding/illegaltoken";
45 }
46
47 @Test
48 public void testLabel() throws Exception {
49 final File fileToProcess =
50 new File(getPath("InputXpathIllegalTokenLabel.java"));
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(IllegalTokenCheck.class);
53 final String[] expectedViolation = {
54 "5:10: " + getCheckMessage(IllegalTokenCheck.class,
55 IllegalTokenCheck.MSG_KEY, "outer:"),
56 };
57 final List<String> expectedXpathQueries = Collections.singletonList(
58 "/COMPILATION_UNIT"
59 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenLabel']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest']]"
61 + "/SLIST/LABELED_STAT[./IDENT[@text='outer']]"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation,
65 expectedXpathQueries);
66 }
67
68 @Test
69 public void testNative() throws Exception {
70 final File fileToProcess =
71 new File(getPath("InputXpathIllegalTokenNative.java"));
72 final DefaultConfiguration moduleConfig =
73 createModuleConfig(IllegalTokenCheck.class);
74
75 moduleConfig.addProperty("tokens", "LITERAL_NATIVE");
76
77 final String[] expectedViolation = {
78 "4:10: " + getCheckMessage(IllegalTokenCheck.class,
79 IllegalTokenCheck.MSG_KEY, "native"),
80 };
81 final List<String> expectedXpathQueries = Collections.singletonList(
82 "/COMPILATION_UNIT"
83 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenNative']]"
84 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest']]"
85 + "/MODIFIERS/LITERAL_NATIVE"
86 );
87
88 runVerifications(moduleConfig, fileToProcess, expectedViolation,
89 expectedXpathQueries);
90 }
91
92 @Test
93 public void testPostIncrement() throws Exception {
94 final File fileToProcess =
95 new File(getPath("InputXpathIllegalTokenPostIncrement.java"));
96 final DefaultConfiguration moduleConfig =
97 createModuleConfig(IllegalTokenCheck.class);
98
99 moduleConfig.addProperty("tokens", "POST_INC");
100
101 final String[] expectedViolation = {
102 "8:10: " + getCheckMessage(IllegalTokenCheck.class,
103 IllegalTokenCheck.MSG_KEY, "++"),
104 };
105 final List<String> expectedXpathQueries = Arrays.asList(
106 "/COMPILATION_UNIT"
107 + "/ENUM_DEF[./IDENT[@text='InputXpathIllegalTokenPostIncrement']]"
108 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
109 + "/SLIST/EXPR",
110 "/COMPILATION_UNIT"
111 + "/ENUM_DEF[./IDENT[@text='InputXpathIllegalTokenPostIncrement']]"
112 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
113 + "/SLIST/EXPR/POST_INC[./IDENT[@text='i']]"
114 );
115
116 runVerifications(moduleConfig, fileToProcess, expectedViolation,
117 expectedXpathQueries);
118 }
119 }