Skip to content

Commit

Permalink
support java.nio.file.Path as input source
Browse files Browse the repository at this point in the history
closes #196
  • Loading branch information
bodewig committed Oct 17, 2020
1 parent 5bb7a26 commit c92c958
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Expand Up @@ -28,6 +28,9 @@
select them.
[#197](https://github.com/xmlunit/xmlunit/issues/197)

* `Input` builder now supports `java.nio.file.Path`
[#196](https://github.com/xmlunit/xmlunit/issues/196)

## XMLUnit for Java 2.7.0 - /Released 2020-05-12/

This version contains a backwards incompatible change to the
Expand Down
14 changes: 13 additions & 1 deletion xmlunit-core/src/main/java/org/xmlunit/builder/Input.java
Expand Up @@ -25,6 +25,7 @@
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;

import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
Expand Down Expand Up @@ -113,7 +114,7 @@ void setSystemId(String id) {
* Return the matching Builder for the supported types: {@link Source}, {@link Builder}, {@link Document}, {@link Node},
* byte[] (XML as byte[]), {@link String} (XML as String), {@link File} (contains XML),
* {@link URL} (to an XML-Document), {@link URI} (to an XML-Document), {@link InputStream},
* {@link ReadableByteChannel},
* {@link ReadableByteChannel}, {@link Path},
* Jaxb-{@link Object} (marshal-able with {@link javax.xml.bind.JAXB}.marshal(...))
*/
public static Builder from(Object object) {
Expand All @@ -140,6 +141,8 @@ public static Builder from(Object object) {
xml = Input.fromStream((InputStream) object);
} else if (object instanceof ReadableByteChannel) {
xml = Input.fromChannel((ReadableByteChannel) object);
} else if (object instanceof Path) {
xml = Input.fromPath((Path) object);
} else {
// assume it is a JaxB-Object.
xml = Input.fromJaxb(object);
Expand Down Expand Up @@ -259,6 +262,15 @@ public static Builder fromURI(String uri) {
}
}

/**
* Build a Source from a Path.
* @param path a Path
* @since XMLUnit 2.8.0
*/
public static Builder fromPath(final Path path) {
return fromURI(path.toUri());
}

/**
* Builds {@link Source}s by transforming other sources.
*/
Expand Down
8 changes: 8 additions & 0 deletions xmlunit-core/src/test/java/org/xmlunit/builder/InputTest.java
Expand Up @@ -86,6 +86,12 @@ private static Document parse(Source s) throws Exception {
}
}

@Test public void shouldParseAnExistingFileFromPath() throws Exception {
Source s = Input.fromPath(new File(TestResources.ANIMAL_FILE).toPath()).build();
allIsWellFor(s);
assertEquals(toFileUri(TestResources.ANIMAL_FILE), s.getSystemId());
}

@Test public void shouldParseString() throws Exception {
allIsWellFor(Input.fromString(new String(readTestFile(), "UTF-8"))
.build());
Expand Down Expand Up @@ -158,6 +164,8 @@ private static Document parse(Source s) throws Exception {
FileChannel fc = is.getChannel()) {
allIsWellFor(Input.from(fc).build());
}
// from Path
allIsWellFor(Input.from(new File(TestResources.ANIMAL_FILE).toPath()).build());
}

@Test(expected = IllegalArgumentException.class)
Expand Down

0 comments on commit c92c958

Please sign in to comment.