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.IllegalTokenTextCheck;
32
33 public class XpathRegressionIllegalTokenTextTest extends AbstractXpathTestSupport {
34
35 private final String checkName = IllegalTokenTextCheck.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/illegaltokentext";
45 }
46
47 @Test
48 public void testField() throws Exception {
49 final File fileToProcess =
50 new File(getPath("InputXpathIllegalTokenTextField.java"));
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(IllegalTokenTextCheck.class);
53 moduleConfig.addProperty("format", "12345");
54 moduleConfig.addProperty("tokens", "NUM_INT");
55 final String[] expectedViolation = {
56 "4:33: " + getCheckMessage(IllegalTokenTextCheck.class,
57 IllegalTokenTextCheck.MSG_KEY, "12345"),
58 };
59 final List<String> expectedXpathQueries = Arrays.asList(
60 "/COMPILATION_UNIT"
61 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextField']]"
62 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='illegalNumber']]"
63 + "/ASSIGN/EXPR[./NUM_INT[@text='12345']]",
64 "/COMPILATION_UNIT"
65 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextField']]"
66 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='illegalNumber']]"
67 + "/ASSIGN/EXPR/NUM_INT[@text='12345']"
68 );
69
70 runVerifications(moduleConfig, fileToProcess, expectedViolation,
71 expectedXpathQueries);
72 }
73
74 @Test
75 public void testMethod() throws Exception {
76 final File fileToProcess =
77 new File(getPath("InputXpathIllegalTokenTextMethod.java"));
78 final DefaultConfiguration moduleConfig =
79 createModuleConfig(IllegalTokenTextCheck.class);
80 moduleConfig.addProperty("format", "forbiddenText");
81 moduleConfig.addProperty("tokens", "STRING_LITERAL");
82 final String[] expectedViolation = {
83 "5:32: " + getCheckMessage(IllegalTokenTextCheck.class,
84 IllegalTokenTextCheck.MSG_KEY, "forbiddenText"),
85 };
86 final List<String> expectedXpathQueries = Arrays.asList(
87 "/COMPILATION_UNIT"
88 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextMethod']]"
89 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myMethod']]"
90 + "/SLIST/VARIABLE_DEF[./IDENT[@text='illegalString']]"
91 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='forbiddenText']]",
92 "/COMPILATION_UNIT"
93 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextMethod']]"
94 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myMethod']]"
95 + "/SLIST/VARIABLE_DEF[./IDENT[@text='illegalString']]"
96 + "/ASSIGN/EXPR/STRING_LITERAL[@text='forbiddenText']"
97 );
98
99 runVerifications(moduleConfig, fileToProcess, expectedViolation,
100 expectedXpathQueries);
101 }
102
103 @Test
104 public void testInterface() throws Exception {
105 final File fileToProcess =
106 new File(getPath("InputXpathIllegalTokenTextInterface.java"));
107 final DefaultConfiguration moduleConfig =
108 createModuleConfig(IllegalTokenTextCheck.class);
109 moduleConfig.addProperty("format", "invalidIdentifier");
110 moduleConfig.addProperty("tokens", "IDENT");
111 final String[] expectedViolation = {
112 "4:10: " + getCheckMessage(IllegalTokenTextCheck.class,
113 IllegalTokenTextCheck.MSG_KEY, "invalidIdentifier"),
114 };
115 final List<String> expectedXpathQueries = Collections.singletonList(
116 "/COMPILATION_UNIT"
117 + "/INTERFACE_DEF[./IDENT[@text='InputXpathIllegalTokenTextInterface']]"
118 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='invalidIdentifier']"
119 );
120
121 runVerifications(moduleConfig, fileToProcess, expectedViolation,
122 expectedXpathQueries);
123 }
124
125 }