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.xpath;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.utils.XpathUtil.getXpathItems;
24
25 import java.io.File;
26 import java.util.List;
27
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30
31 import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
32 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
33 import com.puppycrawl.tools.checkstyle.JavaParser;
34 import com.puppycrawl.tools.checkstyle.api.DetailAST;
35 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36 import net.sf.saxon.om.AxisInfo;
37 import net.sf.saxon.om.NodeInfo;
38 import net.sf.saxon.tree.iter.AxisIterator;
39 import net.sf.saxon.tree.iter.EmptyIterator;
40
41 public class RootNodeTest extends AbstractPathTestSupport {
42
43 private static RootNode rootNode;
44
45 @Override
46 protected String getPackageLocation() {
47 return "com/puppycrawl/tools/checkstyle/xpath/xpathmapper";
48 }
49
50 @BeforeEach
51 public void init() throws Exception {
52 final File file = new File(getPath("InputXpathMapperAst.java"));
53 final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
54 rootNode = new RootNode(rootAst);
55 }
56
57 @Test
58 public void testCompareOrder() {
59 try {
60 rootNode.compareOrder(null);
61 assertWithMessage("Exception is excepted").fail();
62 }
63 catch (UnsupportedOperationException ex) {
64 assertWithMessage("Invalid exception message")
65 .that(ex.getMessage())
66 .isEqualTo("Operation is not supported");
67 }
68 }
69
70 @Test
71 public void testXpath() throws Exception {
72 final String xpath = "/";
73 final List<NodeInfo> nodes = getXpathItems(xpath, rootNode);
74 assertWithMessage("Invalid number of nodes")
75 .that(nodes)
76 .hasSize(1);
77 final NodeInfo firstNode = nodes.get(0);
78 assertWithMessage("Should return true, because selected node is RootNode")
79 .that(firstNode)
80 .isInstanceOf(RootNode.class);
81 assertWithMessage("Result node should have same reference as expected")
82 .that(rootNode)
83 .isEqualTo(firstNode);
84 }
85
86 @Test
87 public void testGetDepth() {
88 assertWithMessage("Root node depth should be 0")
89 .that(rootNode.getDepth())
90 .isEqualTo(0);
91 }
92
93 @Test
94 public void testGetTokenType() {
95 assertWithMessage("Invalid token type")
96 .that(rootNode.getTokenType())
97 .isEqualTo(TokenTypes.COMPILATION_UNIT);
98 }
99
100 @Test
101 public void testGetLineNumber() {
102 assertWithMessage("Invalid line number")
103 .that(rootNode.getLineNumber())
104 .isEqualTo(1);
105 }
106
107 @Test
108 public void testGetColumnNumber() {
109 assertWithMessage("Invalid column number")
110 .that(rootNode.getColumnNumber())
111 .isEqualTo(0);
112 }
113
114
115
116
117
118
119 @Test
120 public void testNonRealGetColumnNumber() {
121 final DetailAstImpl nonRealNode = new DetailAstImpl();
122 nonRealNode.setType(TokenTypes.PACKAGE_DEF);
123 nonRealNode.setLineNo(555);
124 nonRealNode.setColumnNo(888);
125
126 final RootNode nonRealRootNode = new RootNode(nonRealNode);
127 assertWithMessage("Invalid column number")
128 .that(nonRealRootNode.getColumnNumber())
129 .isEqualTo(888);
130 }
131
132 @Test
133 public void testGetLocalPart() {
134 assertWithMessage("Invalid local part")
135 .that(rootNode.getLocalPart())
136 .isEqualTo("ROOT");
137 }
138
139 @Test
140 public void testIterate() {
141 try (AxisIterator following = rootNode.iterateAxis(AxisInfo.FOLLOWING)) {
142 assertWithMessage("Result iterator does not match expected")
143 .that(following)
144 .isEqualTo(EmptyIterator.ofNodes());
145 }
146 try (AxisIterator followingSibling = rootNode.iterateAxis(AxisInfo.FOLLOWING_SIBLING)) {
147 assertWithMessage("Result iterator does not match expected")
148 .that(followingSibling)
149 .isEqualTo(EmptyIterator.ofNodes());
150 }
151 try (AxisIterator preceding = rootNode.iterateAxis(AxisInfo.PRECEDING)) {
152 assertWithMessage("Result iterator does not match expected")
153 .that(preceding)
154 .isEqualTo(EmptyIterator.ofNodes());
155 }
156 try (AxisIterator precedingSibling = rootNode.iterateAxis(AxisInfo.PRECEDING_SIBLING)) {
157 assertWithMessage("Result iterator does not match expected")
158 .that(precedingSibling)
159 .isEqualTo(EmptyIterator.ofNodes());
160 }
161 try (AxisIterator parent = rootNode.iterateAxis(AxisInfo.PARENT)) {
162 assertWithMessage("Result iterator does not match expected")
163 .that(parent)
164 .isEqualTo(EmptyIterator.ofNodes());
165 }
166 try (AxisIterator parentNull = rootNode.iterateAxis(AxisInfo.PARENT, null)) {
167 assertWithMessage("Result iterator does not match expected")
168 .that(parentNull)
169 .isEqualTo(EmptyIterator.ofNodes());
170 }
171 }
172
173 @Test
174 public void testRootWithNullDetailAst() {
175 final RootNode emptyRootNode = new RootNode(null);
176 assertWithMessage("Empty node should not have children")
177 .that(emptyRootNode.hasChildNodes())
178 .isFalse();
179
180 try (AxisIterator iterator = emptyRootNode.iterateAxis(AxisInfo.DESCENDANT)) {
181 assertWithMessage("Result iterator does not match expected")
182 .that(iterator)
183 .isEqualTo(EmptyIterator.ofNodes());
184 }
185 try (AxisIterator iterator = emptyRootNode.iterateAxis(AxisInfo.CHILD)) {
186 assertWithMessage("Result iterator does not match expected")
187 .that(iterator)
188 .isEqualTo(EmptyIterator.ofNodes());
189 }
190 }
191
192 @Test
193 public void testGetStringValue() {
194 try {
195 rootNode.getStringValue();
196 assertWithMessage("Exception is excepted").fail();
197 }
198 catch (UnsupportedOperationException ex) {
199 assertWithMessage("Invalid exception message")
200 .that(ex.getMessage())
201 .isEqualTo("Operation is not supported");
202 }
203 }
204
205 @Test
206 public void testGetAttributeValue() {
207 try {
208 rootNode.getAttributeValue("", "");
209 assertWithMessage("Exception is excepted").fail();
210 }
211 catch (UnsupportedOperationException ex) {
212 assertWithMessage("Invalid exception message")
213 .that(ex.getMessage())
214 .isEqualTo("Operation is not supported");
215 }
216 }
217
218 @Test
219 public void testGetDeclaredNamespaces() {
220 try {
221 rootNode.getDeclaredNamespaces(null);
222 assertWithMessage("Exception is excepted").fail();
223 }
224 catch (UnsupportedOperationException ex) {
225 assertWithMessage("Invalid exception message")
226 .that(ex.getMessage())
227 .isEqualTo("Operation is not supported");
228 }
229 }
230
231 @Test
232 public void testIsId() {
233 try {
234 rootNode.isId();
235 assertWithMessage("Exception is excepted").fail();
236 }
237 catch (UnsupportedOperationException ex) {
238 assertWithMessage("Invalid exception message")
239 .that(ex.getMessage())
240 .isEqualTo("Operation is not supported");
241 }
242 }
243
244 @Test
245 public void testIsIdref() {
246 try {
247 rootNode.isIdref();
248 assertWithMessage("Exception is excepted").fail();
249 }
250 catch (UnsupportedOperationException ex) {
251 assertWithMessage("Invalid exception message")
252 .that(ex.getMessage())
253 .isEqualTo("Operation is not supported");
254 }
255 }
256
257 @Test
258 public void testIsNilled() {
259 try {
260 rootNode.isNilled();
261 assertWithMessage("Exception is excepted").fail();
262 }
263 catch (UnsupportedOperationException ex) {
264 assertWithMessage("Invalid exception message")
265 .that(ex.getMessage())
266 .isEqualTo("Operation is not supported");
267 }
268 }
269
270 @Test
271 public void testIsStreamed() {
272 try {
273 rootNode.isStreamed();
274 assertWithMessage("Exception is excepted").fail();
275 }
276 catch (UnsupportedOperationException ex) {
277 assertWithMessage("Invalid exception message")
278 .that(ex.getMessage())
279 .isEqualTo("Operation is not supported");
280 }
281 }
282
283 @Test
284 public void testGetConfiguration() {
285 try {
286 rootNode.getConfiguration();
287 assertWithMessage("Exception is excepted").fail();
288 }
289 catch (UnsupportedOperationException ex) {
290 assertWithMessage("Invalid exception message")
291 .that(ex.getMessage())
292 .isEqualTo("Operation is not supported");
293 }
294 }
295
296 @Test
297 public void testSetSystemId() {
298 try {
299 rootNode.setSystemId("1");
300 assertWithMessage("Exception is excepted").fail();
301 }
302 catch (UnsupportedOperationException ex) {
303 assertWithMessage("Invalid exception message")
304 .that(ex.getMessage())
305 .isEqualTo("Operation is not supported");
306 }
307 }
308
309 @Test
310 public void testGetSystemId() {
311 try {
312 rootNode.getSystemId();
313 assertWithMessage("Exception is excepted").fail();
314 }
315 catch (UnsupportedOperationException ex) {
316 assertWithMessage("Invalid exception message")
317 .that(ex.getMessage())
318 .isEqualTo("Operation is not supported");
319 }
320 }
321
322 @Test
323 public void testGetPublicId() {
324 try {
325 rootNode.getPublicId();
326 assertWithMessage("Exception is excepted").fail();
327 }
328 catch (UnsupportedOperationException ex) {
329 assertWithMessage("Invalid exception message")
330 .that(ex.getMessage())
331 .isEqualTo("Operation is not supported");
332 }
333 }
334
335 @Test
336 public void testBaseUri() {
337 try {
338 rootNode.getBaseURI();
339 assertWithMessage("Exception is excepted").fail();
340 }
341 catch (UnsupportedOperationException ex) {
342 assertWithMessage("Invalid exception message")
343 .that(ex.getMessage())
344 .isEqualTo("Operation is not supported");
345 }
346 }
347
348 @Test
349 public void testSaveLocation() {
350 try {
351 rootNode.saveLocation();
352 assertWithMessage("Exception is excepted").fail();
353 }
354 catch (UnsupportedOperationException ex) {
355 assertWithMessage("Invalid exception message")
356 .that(ex.getMessage())
357 .isEqualTo("Operation is not supported");
358 }
359 }
360
361 @Test
362 public void testGetStringValueCs() {
363 try {
364 rootNode.getUnicodeStringValue();
365 assertWithMessage("Exception is excepted").fail();
366 }
367 catch (UnsupportedOperationException ex) {
368 assertWithMessage("Invalid exception message")
369 .that(ex.getMessage())
370 .isEqualTo("Operation is not supported");
371 }
372 }
373
374 @Test
375 public void testFingerprint() {
376 try {
377 rootNode.getFingerprint();
378 assertWithMessage("Exception is excepted").fail();
379 }
380 catch (UnsupportedOperationException ex) {
381 assertWithMessage("Invalid exception message")
382 .that(ex.getMessage())
383 .isEqualTo("Operation is not supported");
384 }
385 }
386
387 @Test
388 public void testGetDisplayName() {
389 try {
390 rootNode.getDisplayName();
391 assertWithMessage("Exception is excepted").fail();
392 }
393 catch (UnsupportedOperationException ex) {
394 assertWithMessage("Invalid exception message")
395 .that(ex.getMessage())
396 .isEqualTo("Operation is not supported");
397 }
398 }
399
400 @Test
401 public void testGetPrefix() {
402 try {
403 rootNode.getPrefix();
404 assertWithMessage("Exception is excepted").fail();
405 }
406 catch (UnsupportedOperationException ex) {
407 assertWithMessage("Invalid exception message")
408 .that(ex.getMessage())
409 .isEqualTo("Operation is not supported");
410 }
411 }
412
413 @Test
414 public void testGetSchemaType() {
415 try {
416 rootNode.getSchemaType();
417 assertWithMessage("Exception is excepted").fail();
418 }
419 catch (UnsupportedOperationException ex) {
420 assertWithMessage("Invalid exception message")
421 .that(ex.getMessage())
422 .isEqualTo("Operation is not supported");
423 }
424 }
425
426 @Test
427 public void testAtomize() {
428 try {
429 rootNode.atomize();
430 assertWithMessage("Exception is excepted").fail();
431 }
432 catch (UnsupportedOperationException ex) {
433 assertWithMessage("Invalid exception message")
434 .that(ex.getMessage())
435 .isEqualTo("Operation is not supported");
436 }
437 }
438
439 @Test
440 public void testGenerateId() {
441 try {
442 rootNode.generateId(null);
443 assertWithMessage("Exception is excepted").fail();
444 }
445 catch (UnsupportedOperationException ex) {
446 assertWithMessage("Invalid exception message")
447 .that(ex.getMessage())
448 .isEqualTo("Operation is not supported");
449 }
450 }
451
452 @Test
453 public void testCopy() {
454 try {
455 rootNode.copy(null, -1, null);
456 assertWithMessage("Exception is excepted").fail();
457 }
458 catch (UnsupportedOperationException ex) {
459 assertWithMessage("Invalid exception message")
460 .that(ex.getMessage())
461 .isEqualTo("Operation is not supported");
462 }
463 }
464
465 @Test
466 public void testGetAllNamespaces() {
467 try {
468 rootNode.getAllNamespaces();
469 assertWithMessage("Exception is excepted").fail();
470 }
471 catch (UnsupportedOperationException ex) {
472 assertWithMessage("Invalid exception message")
473 .that(ex.getMessage())
474 .isEqualTo("Operation is not supported");
475 }
476 }
477
478 @Test
479 public void testSameNodeInfo() {
480 assertWithMessage("Should return true, because object is being compared to itself")
481 .that(rootNode.isSameNodeInfo(rootNode))
482 .isTrue();
483 assertWithMessage("Should return false, because object does not equal null")
484 .that(rootNode.isSameNodeInfo(null))
485 .isFalse();
486 }
487 }