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 java.io.ByteArrayInputStream;
8   import java.io.File;
9   import java.io.IOException;
10  import java.net.URI;
11  import java.net.URISyntaxException;
12  import java.net.URL;
13  import java.nio.charset.Charset;
14  import java.nio.charset.StandardCharsets;
15  import java.nio.file.Files;
16  import java.nio.file.Path;
17  import org.hamcrest.MatcherAssert;
18  import org.hamcrest.Matchers;
19  import org.junit.jupiter.api.Test;
20  import org.junit.jupiter.api.io.TempDir;
21  
22  /**
23   * Test case for {@link SaxonDocument}.
24   * @since 0.28
25   */
26  final class SaxonDocumentTest {
27  
28      /**
29       * Default XML content for tests.
30       */
31      private static final String DEFAULT_XML = "<o><o base='a'/></o>";
32  
33      /**
34       * Default assertion message that will be used in many test assertions.
35       */
36      private static final String ASSERTION_MSG = "Can't create Saxon XML document from '%s'";
37  
38      @Test
39      void createsFromFile(@TempDir final Path temp) throws IOException {
40          final Path xml = SaxonDocumentTest.xmlFile(temp);
41          final File file = xml.toFile();
42          MatcherAssert.assertThat(
43              String.format(SaxonDocumentTest.ASSERTION_MSG, file),
44              new SaxonDocument(file).xpath("//o[@base]"),
45              Matchers.hasSize(1)
46          );
47      }
48  
49      @Test
50      void createsFromPath(@TempDir final Path temp) throws IOException {
51          final Path xml = SaxonDocumentTest.xmlFile(temp);
52          MatcherAssert.assertThat(
53              String.format(SaxonDocumentTest.ASSERTION_MSG, xml),
54              new SaxonDocument(xml).xpath("//o[@base]"),
55              Matchers.hasSize(1)
56          );
57      }
58  
59      @Test
60      void createsFromByteArray() {
61          final Charset utf = StandardCharsets.UTF_8;
62          final byte[] bytes = SaxonDocumentTest.DEFAULT_XML.getBytes(utf);
63          MatcherAssert.assertThat(
64              String.format(SaxonDocumentTest.ASSERTION_MSG, new String(bytes, utf)),
65              new SaxonDocument(bytes).xpath("//o[@base]"),
66              Matchers.hasSize(1)
67          );
68      }
69  
70      @Test
71      void createsFromUrl() throws IOException {
72          final URL resource = this.getClass().getResource("simple.xml");
73          MatcherAssert.assertThat(
74              String.format(SaxonDocumentTest.ASSERTION_MSG, resource),
75              new SaxonDocument(resource).xpath("//simple"),
76              Matchers.hasSize(1)
77          );
78      }
79  
80      @Test
81      void createsFromUri() throws URISyntaxException, IOException {
82          final URI resource = this.getClass().getResource("simple.xml").toURI();
83          MatcherAssert.assertThat(
84              String.format(SaxonDocumentTest.ASSERTION_MSG, resource),
85              new SaxonDocument(resource).xpath("//simple"),
86              Matchers.hasSize(1)
87          );
88      }
89  
90      @Test
91      void createsFromStream() {
92          MatcherAssert.assertThat(
93              String.format(SaxonDocumentTest.ASSERTION_MSG, SaxonDocumentTest.DEFAULT_XML),
94              new SaxonDocument(
95                  new ByteArrayInputStream(
96                      SaxonDocumentTest.DEFAULT_XML.getBytes(StandardCharsets.UTF_8)
97                  )
98              ).xpath("//o[@base]"),
99              Matchers.hasSize(1)
100         );
101     }
102 
103     @Test
104     void findsXpathWithConcatFunctionThatReturnsSeveralItems() {
105         MatcherAssert.assertThat(
106             "SaxonDocument can handle XPath 2.0 feature - XPath evaluation of concat method, but it can't",
107             new SaxonDocument(
108                 "<o><o base='a' ver='1'/><o base='b' ver='2'/></o>"
109             ).xpath("//o[@base and @ver]/concat(@base,'|',@ver)"),
110             Matchers.hasItems("a|1", "b|2")
111         );
112     }
113 
114     @Test
115     void findsXpathWithStringJoinFunctionThatReturnsSeveralItems() {
116         MatcherAssert.assertThat(
117             "SaxonDocument can handle XPath 2.0 feature - XPath evaluation of string-join method, but it can't",
118             new SaxonDocument(
119                 "<o><o base='a'/><o base='b' ver='2'/><o base='c'/></o>"
120             ).xpath("//o[@base]/string-join((@base,@ver),'|')"),
121             Matchers.hasItems("a", "b|2", "c")
122         );
123     }
124 
125     /**
126      * Creates XML file.
127      * @param temp Temporary directory where file will be created
128      * @return Path to created file
129      * @throws IOException If something goes wrong
130      */
131     private static Path xmlFile(final Path temp) throws IOException {
132         final Path xml = temp.resolve("test.xml");
133         Files.write(xml, SaxonDocumentTest.DEFAULT_XML.getBytes(StandardCharsets.UTF_8));
134         return xml;
135     }
136 }