From 6336b2b3cf8aa74742c0965881e327771d0bba77 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Thu, 20 Jul 2023 14:16:59 +0300 Subject: [PATCH] feat(#215): Test two more constructors --- .../java/com/jcabi/xml/SaxonDocument.java | 24 +++++--- .../java/com/jcabi/xml/SaxonDocumentTest.java | 59 ++++++++++++++++++- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/jcabi/xml/SaxonDocument.java b/src/main/java/com/jcabi/xml/SaxonDocument.java index 58ad4cf..ab737c8 100644 --- a/src/main/java/com/jcabi/xml/SaxonDocument.java +++ b/src/main/java/com/jcabi/xml/SaxonDocument.java @@ -101,22 +101,30 @@ public SaxonDocument(final byte[] data) { this(SaxonDocument.node(new String(data, StandardCharsets.UTF_8))); } - public SaxonDocument(final Node node) { - this(SaxonDocument.node(node.getTextContent())); - } - - public SaxonDocument(final Source source) { - this(SaxonDocument.node(new StreamSource(source.getSystemId()))); - } - + /** + * Public constructor from XML saved in a filesystem. + * @param path Path to XML file in a filesystem. + */ public SaxonDocument(final Path path) { this(path.toFile()); } + /** + * Public constructor from XML saved in a filesystem. + * @param file XML file in a filesystem. + */ public SaxonDocument(final File file) { this(SaxonDocument.node(new StreamSource(file))); } + public SaxonDocument(final Source source) { + this(SaxonDocument.node(new StreamSource(source.getSystemId()))); + } + + public SaxonDocument(final Node node) { + this(SaxonDocument.node(node.getTextContent())); + } + public SaxonDocument(final URL url) { this.xdm = null; } diff --git a/src/test/java/com/jcabi/xml/SaxonDocumentTest.java b/src/test/java/com/jcabi/xml/SaxonDocumentTest.java index dfee5d3..6f221da 100644 --- a/src/test/java/com/jcabi/xml/SaxonDocumentTest.java +++ b/src/test/java/com/jcabi/xml/SaxonDocumentTest.java @@ -29,9 +29,17 @@ */ package com.jcabi.xml; +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; /** * Test case for {@link SaxonDocument}. @@ -39,9 +47,46 @@ */ final class SaxonDocumentTest { + /** + * Default XML content for tests. + */ + private static final String DEFAULT_XML = ""; + + /** + * Default assertion message that will be used in many test assertions. + */ + private static final String ASSERTION_MSG = "Can't create Saxon XML document from '%s'"; + @Test - void createsFromFile(){ + void createsFromFile(@TempDir final Path temp) throws IOException { + final Path xml = SaxonDocumentTest.xmlFile(temp); + final File file = xml.toFile(); + MatcherAssert.assertThat( + String.format(SaxonDocumentTest.ASSERTION_MSG, file), + new SaxonDocument(file).xpath("//o[@base]"), + Matchers.hasSize(1) + ); + } + @Test + void createsFromPath(@TempDir final Path temp) throws IOException { + final Path xml = SaxonDocumentTest.xmlFile(temp); + MatcherAssert.assertThat( + String.format(SaxonDocumentTest.ASSERTION_MSG, xml), + new SaxonDocument(xml).xpath("//o[@base]"), + Matchers.hasSize(1) + ); + } + + @Test + void createsFromByteArray() { + final Charset utf = StandardCharsets.UTF_8; + final byte[] bytes = SaxonDocumentTest.DEFAULT_XML.getBytes(utf); + MatcherAssert.assertThat( + String.format(SaxonDocumentTest.ASSERTION_MSG, new String(bytes, utf)), + new SaxonDocument(bytes).xpath("//o[@base]"), + Matchers.hasSize(1) + ); } @@ -66,4 +111,16 @@ void findsXpathWithStringJoinFunctionThatReturnsSeveralItems() { Matchers.hasItems("a", "b|2", "c") ); } + + /** + * Creates XML file. + * @param temp Temporary directory where file will be created + * @return Path to created file + * @throws IOException If something goes wrong + */ + private static Path xmlFile(final Path temp) throws IOException { + final Path xml = temp.resolve("test.xml"); + Files.write(xml, SaxonDocumentTest.DEFAULT_XML.getBytes(StandardCharsets.UTF_8)); + return xml; + } }