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 public 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 "14:25: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
89 "16: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 "13:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
99 "16: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 "15:5: " + getCheckMessage(MatchXpathCheck.MSG_KEY),
109 "22: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 "14:5: Parameterized constructors are not allowed",
162 "17: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 getExpectedThrowable(IllegalStateException.class,
185 () -> matchXpathCheck.setQuery("!@#%^"));
186 }
187
188 @Test
189 public void testEvaluationException() {
190 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
191 matchXpathCheck.setQuery("count(*) div 0");
192
193 final DetailAstImpl detailAST = new DetailAstImpl();
194 detailAST.setType(TokenTypes.CLASS_DEF);
195 detailAST.setText("Class Def");
196 detailAST.setLineNo(0);
197 detailAST.setColumnNo(0);
198
199 getExpectedThrowable(IllegalStateException.class,
200 () -> matchXpathCheck.beginTree(detailAST));
201 }
202
203 @Test
204 public void testGetDefaultTokens() {
205 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
206 assertWithMessage("Expected empty array")
207 .that(matchXpathCheck.getDefaultTokens())
208 .isEmpty();
209 }
210
211 @Test
212 public void testGetAcceptableTokens() {
213 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
214 assertWithMessage("Expected empty array")
215 .that(matchXpathCheck.getAcceptableTokens())
216 .isEmpty();
217 }
218
219 @Test
220 public void testGetRequiredTokens() {
221 final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
222 assertWithMessage("Expected empty array")
223 .that(matchXpathCheck.getRequiredTokens())
224 .isEmpty();
225 }
226
227 @Test
228 public void testMatchXpathWithFailedEvaluation() {
229 final CheckstyleException ex = getExpectedThrowable(CheckstyleException.class,
230 () -> verifyWithInlineConfigParser(getPath("InputMatchXpath5.java")));
231 assertThat(ex.getCause().getMessage())
232 .isEqualTo("Evaluation of Xpath query failed: count(*) div 0");
233 }
234 }