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 static com.puppycrawl.tools.checkstyle.checks.AvoidEscapedUnicodeCharactersCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.AvoidEscapedUnicodeCharactersCheck;
32
33 public class XpathRegressionAvoidEscapedUnicodeCharactersTest extends AbstractXpathTestSupport {
34 private final String checkName = AvoidEscapedUnicodeCharactersCheck.class.getSimpleName();
35
36 @Override
37 protected String getCheckName() {
38 return checkName;
39 }
40
41 @Test
42 public void testDefault() throws Exception {
43 final File fileToProcess = new File(getPath(
44 "InputXpathAvoidEscapedUnicodeCharactersDefault.java"));
45
46 final DefaultConfiguration moduleConfig =
47 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
48
49 final String[] expectedViolation = {
50 "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
51 };
52
53 final List<String> expectedXpathQueries = Arrays.asList(
54 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
55 + "[@text='InputXpathAvoidEscapedUnicodeCharactersDefault']]"
56 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev2']]"
57 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
58 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
59 + "[@text='InputXpathAvoidEscapedUnicodeCharactersDefault']]"
60 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev2']]"
61 + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation,
65 expectedXpathQueries);
66 }
67
68 @Test
69 public void testControlCharacters() throws Exception {
70 final File fileToProcess = new File(getPath(
71 "InputXpathAvoidEscapedUnicodeCharactersControlCharacters.java")
72 );
73
74 final DefaultConfiguration moduleConfig =
75 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
76 moduleConfig.addProperty("allowEscapesForControlCharacters", "true");
77
78 final String[] expectedViolation = {
79 "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
80 };
81
82 final List<String> expectedXpathQueries = Arrays.asList(
83 "/COMPILATION_UNIT/CLASS_DEF[."
84 + "/IDENT[@text="
85 + "'InputXpathAvoidEscapedUnicodeCharactersControlCharacters']]"
86 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
87 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
88 "/COMPILATION_UNIT/CLASS_DEF[."
89 + "/IDENT[@text="
90 + "'InputXpathAvoidEscapedUnicodeCharactersControlCharacters']]"
91 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
92 + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
93 );
94
95 runVerifications(moduleConfig, fileToProcess, expectedViolation,
96 expectedXpathQueries);
97 }
98
99 @Test
100 public void testTailComment() throws Exception {
101 final File fileToProcess = new File(getPath(
102 "InputXpathAvoidEscapedUnicodeCharactersTailComment.java"));
103
104 final DefaultConfiguration moduleConfig =
105 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
106 moduleConfig.addProperty("allowByTailComment", "true");
107
108 final String[] expectedViolation = {
109 "4:45: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
110 };
111
112 final List<String> expectedXpathQueries = Arrays.asList(
113 "/COMPILATION_UNIT/CLASS_DEF[."
114 + "/IDENT[@text="
115 + "'InputXpathAvoidEscapedUnicodeCharactersTailComment']]"
116 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
117 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
118 "/COMPILATION_UNIT/CLASS_DEF[."
119 + "/IDENT[@text="
120 + "'InputXpathAvoidEscapedUnicodeCharactersTailComment']]"
121 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
122 + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
123 );
124
125 runVerifications(moduleConfig, fileToProcess, expectedViolation,
126 expectedXpathQueries);
127 }
128
129 @Test
130 public void testAllCharactersEscaped() throws Exception {
131 final File fileToProcess = new File(getPath(
132 "InputXpathAvoidEscapedUnicodeCharactersAllEscaped.java"));
133
134 final DefaultConfiguration moduleConfig =
135 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
136 moduleConfig.addProperty("allowIfAllCharactersEscaped", "true");
137
138 final String[] expectedViolation = {
139 "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
140 };
141
142 final List<String> expectedXpathQueries = Arrays.asList(
143 "/COMPILATION_UNIT/CLASS_DEF[."
144 + "/IDENT[@text="
145 + "'InputXpathAvoidEscapedUnicodeCharactersAllEscaped']]"
146 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
147 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
148 "/COMPILATION_UNIT/CLASS_DEF[."
149 + "/IDENT[@text="
150 + "'InputXpathAvoidEscapedUnicodeCharactersAllEscaped']]"
151 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
152 + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
153 );
154
155 runVerifications(moduleConfig, fileToProcess, expectedViolation,
156 expectedXpathQueries);
157 }
158
159 @Test
160 public void testNonPrintableCharacters() throws Exception {
161 final File fileToProcess = new File(getPath(
162 "InputXpathAvoidEscapedUnicodeCharactersNonPrintable.java"));
163
164 final DefaultConfiguration moduleConfig =
165 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
166 moduleConfig.addProperty("allowNonPrintableEscapes", "true");
167
168 final String[] expectedViolation = {
169 "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
170 };
171
172 final List<String> expectedXpathQueries = Arrays.asList(
173 "/COMPILATION_UNIT/CLASS_DEF[."
174 + "/IDENT[@text="
175 + "'InputXpathAvoidEscapedUnicodeCharactersNonPrintable']]"
176 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
177 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
178 "/COMPILATION_UNIT/CLASS_DEF[."
179 + "/IDENT[@text="
180 + "'InputXpathAvoidEscapedUnicodeCharactersNonPrintable']]"
181 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
182 + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
183 );
184
185 runVerifications(moduleConfig, fileToProcess, expectedViolation,
186 expectedXpathQueries);
187 }
188 }