View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.xml;
6   
7   import com.jcabi.matchers.XhtmlMatchers;
8   import javax.xml.parsers.DocumentBuilderFactory;
9   import org.hamcrest.MatcherAssert;
10  import org.junit.jupiter.api.Test;
11  
12  /**
13   * Test case for {@link DomParser}.
14   * @since 0.1
15   */
16  final class DomParserTest {
17  
18      @Test
19      void parsesIncomingXmlDocument() {
20          final String xml = "<a><b>\u0443\u0440\u0430!</b></a>";
21          final DomParser parser = new DomParser(
22              DocumentBuilderFactory.newInstance(), xml
23          );
24          MatcherAssert.assertThat(
25              "XPath result node does not match with expected",
26              parser.document(),
27              XhtmlMatchers.hasXPath("/a/b")
28          );
29      }
30  
31      @Test
32      void parsesIncomingXmlDocumentComment() {
33          final String xml = "<?xml version='1.0'?><!-- test --><root/>";
34          final DomParser parser = new DomParser(
35              DocumentBuilderFactory.newInstance(), xml
36          );
37          MatcherAssert.assertThat(
38              "XPath result node does not match with expected",
39              parser.document(),
40              XhtmlMatchers.hasXPath("/root")
41          );
42      }
43  
44      @Test
45      @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
46      void allowsValidXmlFormatting() {
47          final String[] texts = {
48              "<?xml version=\"1.0\" encoding='ISO-8895-1'?><a/>",
49              "<:a/>",
50              "<ns:a><ns2:test-me/></ns:a>",
51              "<_a/>",
52              "<\u00c0a/>",
53              "<something>\uFFFD</something>",
54          };
55          for (final String text : texts) {
56              new DomParser(
57                  DocumentBuilderFactory.newInstance(),
58                  text
59              );
60          }
61      }
62  
63  }