Skip to content

Commit

Permalink
feat(#215): Test two more constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 20, 2023
1 parent f680d53 commit 6336b2b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/main/java/com/jcabi/xml/SaxonDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
59 changes: 58 additions & 1 deletion src/test/java/com/jcabi/xml/SaxonDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,64 @@
*/
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}.
* @since 0.28
*/
final class SaxonDocumentTest {

/**
* Default XML content for tests.
*/
private static final String DEFAULT_XML = "<o><o base='a'/></o>";

/**
* 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)
);
}


Expand All @@ -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;
}
}

0 comments on commit 6336b2b

Please sign in to comment.