View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 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.Assertions;
11  import org.junit.jupiter.api.Test;
12  
13  /**
14   * Test case for {@link DomParser}.
15   * @since 0.1
16   */
17  @SuppressWarnings("PMD.UnnecessaryLocalRule")
18  final class DomParserTest {
19  
20      @Test
21      void parsesIncomingXmlDocument() {
22          final String xml = "<a><b>\u0443\u0440\u0430!</b></a>";
23          final DomParser parser = new DomParser(
24              DocumentBuilderFactory.newInstance(), xml
25          );
26          MatcherAssert.assertThat(
27              "XPath result node does not match with expected",
28              parser.document(),
29              XhtmlMatchers.hasXPath("/a/b")
30          );
31      }
32  
33      @Test
34      void parsesIncomingXmlDocumentComment() {
35          final String xml = "<?xml version='1.0'?><!-- test --><root/>";
36          final DomParser parser = new DomParser(
37              DocumentBuilderFactory.newInstance(), xml
38          );
39          MatcherAssert.assertThat(
40              "XPath result node does not match with expected",
41              parser.document(),
42              XhtmlMatchers.hasXPath("/root")
43          );
44      }
45  
46      @Test
47      void allowsValidXmlFormatting() {
48          final String[] texts = {
49              "<?xml version=\"1.0\" encoding='ISO-8895-1'?><a/>",
50              "<:a/>",
51              "<ns:a><ns2:test-me/></ns:a>",
52              "<_a/>",
53              "<\u00c0a/>",
54              "<something>\uFFFD</something>",
55          };
56          for (final String text : texts) {
57              Assertions.assertDoesNotThrow(
58                  () -> new DomParser(
59                      DocumentBuilderFactory.newInstance(),
60                      text
61                  ),
62                  () -> String.format("Should accept valid XML: %s", text)
63              );
64          }
65      }
66  
67  }