1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import static com.google.common.truth.Truth.assertThat;
23 import static com.google.common.truth.Truth.assertWithMessage;
24 import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
30 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33
34 public class MatchXpathCheckTest
35 extends AbstractModuleTestSupport {
36
37 @Override
38 protected String getPackageLocation() {
39 return "com/puppycrawl/tools/checkstyle/checks/coding/matchxpath";
40 }
41
42 @Test
43 public void testCheckWithEmptyQuery()
44 throws Exception {
45 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
46 verifyWithInlineConfigParser(
47 getPath("InputMatchXpath.java"), expected);
48 }
49
50 @Test
51 public void testNoStackoverflowError()
52 throws Exception {
53 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
54 verifyWithInlineConfigParser(
55 getPath("InputMatchXpathNoStackoverflowError.java"), expected);
56 }
57
58 @Test
59 public void testCheckWithImplicitEmptyQuery()
60 throws Exception {
61 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
62 verifyWithInlineConfigParser(
63 getPath("InputMatchXpath2.java"), expected);
64 }
65
66 @Test
67 public void testCheckWithMatchingMethodNames()
68 throws Exception {
69 final String[] expected = {
70 "11:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
71 "13:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
72 };
73 verifyWithInlineConfigParser(
74 getPath("InputMatchXpath3.java"), expected);
75 }
76
77 @Test
78 public void testCheckWithNoMatchingMethodName()
79 throws Exception {
80 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81 verifyWithInlineConfigParser(
82 getPath("InputMatchXpath4.java"), expected);
83 }
84
85 @Test
86 public void testCheckWithSingleLineCommentsStartsWithSpace() throws Exception {
87 final String[] expected = {
88 "13:25: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
89 "14:27: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
90 };
91 verifyWithInlineConfigParser(
92 getPath("InputMatchXpathSingleLineComments.java"), expected);
93 }
94
95 @Test
96 public void testCheckWithBlockComments() throws Exception {
97 final String[] expected = {
98 "12:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
99 "14:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
100 };
101 verifyWithInlineConfigParser(
102 getPath("InputMatchXpathBlockComments.java"), expected);
103 }
104
105 @Test
106 public void testCheckWithMultilineComments() throws Exception {
107 final String[] expected = {
108 "14:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
109 "20:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
110 };
111 verifyWithInlineConfigParser(
112 getPath("InputMatchXpathMultilineComments.java"), expected);
113 }
114
115 @Test
116 public void testCheckWithDoubleBraceInitialization()
117 throws Exception {
118 final String[] expected = {
119 "18:35: Do not use double-brace initialization",
120 };
121 verifyWithInlineConfigParser(
122 getPath("InputMatchXpathDoubleBrace.java"), expected);
123 }
124
125 @Test
126 public void testImitateIllegalThrowsCheck()
127 throws Exception {
128 final String[] expected = {
129 "13:25: Illegal throws statement",
130 "15:25: Illegal throws statement",
131 "16:25: Illegal throws statement",
132 };
133 verifyWithInlineConfigParser(
134 getPath("InputMatchXpathIllegalThrows.java"), expected);
135 }
136
137 @Test
138 public void testImitateExecutableStatementCountCheck()
139 throws Exception {
140 final String[] expected = {
141 "25:5: Executable number of statements exceed threshold",
142 };
143 verifyWithInlineConfigParser(
144 getPath("InputMatchXpathExecutableStatementCount.java"), expected);
145 }
146
147 @Test
148 public void testForbidPrintStackTrace()
149 throws Exception {
150 final String[] expected = {
151 "18:27: printStackTrace() method calls are forbidden",
152 };
153 verifyWithInlineConfigParser(
154 getPath("InputMatchXpathForbidPrintStackTrace.java"), expected);
155 }
156
157 @Test
158 public void testForbidParameterizedConstructor()
159 throws Exception {
160 final String[] expected = {
161 "13:5: Parameterized constructors are not allowed",
162 "15:5: Parameterized constructors are not allowed",
163 };
164 verifyWithInlineConfigParser(
165 getPath("InputMatchXpathForbidParameterizedConstructor.java"),
166 expected);
167 }
168
169 @Test
170 public void testAvoidInstanceCreationWithoutVar()
171 throws Exception {
172 final String[] expected = {
173 "13:9: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
174 };
175 verifyWithInlineConfigParser(
176 getPath("InputMatchXpathAvoidInstanceCreationWithoutVar.java"),
177 expected);
178 }
179
180 @Test
181 public void testInvalidQuery() {
182 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
183
184 try {
185 matchXpathCheck.setQuery("!@#%^");
186 assertWithMessage("Exception was expected").fail();
187 }
188 catch (IllegalStateException ignored) {
189
190 }
191 }
192
193 @Test
194 public void testEvaluationException() {
195 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
196 matchXpathCheck.setQuery("count(*) div 0");
197
198 final DetailAstImpl detailAST = new DetailAstImpl();
199 detailAST.setType(TokenTypes.CLASS_DEF);
200 detailAST.setText("Class Def");
201 detailAST.setLineNo(0);
202 detailAST.setColumnNo(0);
203
204 try {
205 matchXpathCheck.beginTree(detailAST);
206 assertWithMessage("Exception was expected").fail();
207 }
208 catch (IllegalStateException ignored) {
209
210 }
211 }
212
213 @Test
214 public void testGetDefaultTokens() {
215 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
216 assertWithMessage("Expected empty array")
217 .that(matchXpathCheck.getDefaultTokens())
218 .isEmpty();
219 }
220
221 @Test
222 public void testGetAcceptableTokens() {
223 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
224 assertWithMessage("Expected empty array")
225 .that(matchXpathCheck.getAcceptableTokens())
226 .isEmpty();
227 }
228
229 @Test
230 public void testGetRequiredTokens() {
231 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
232 assertWithMessage("Expected empty array")
233 .that(matchXpathCheck.getRequiredTokens())
234 .isEmpty();
235 }
236
237 @Test
238 public void testMatchXpathWithFailedEvaluation() {
239 final CheckstyleException ex = getExpectedThrowable(CheckstyleException.class,
240 () -> verifyWithInlineConfigParser(getPath("InputMatchXpath5.java")));
241 assertThat(ex.getCause().getMessage())
242 .isEqualTo("Evaluation of Xpath query failed: count(*) div 0");
243 }
244 }