From c92c9585173461af520624f3af7a59f41f897cf1 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Sat, 17 Oct 2020 17:01:53 +0200 Subject: [PATCH] support java.nio.file.Path as input source closes #196 --- RELEASE_NOTES.md | 3 +++ .../src/main/java/org/xmlunit/builder/Input.java | 14 +++++++++++++- .../test/java/org/xmlunit/builder/InputTest.java | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 28fe2213..8c9af748 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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 diff --git a/xmlunit-core/src/main/java/org/xmlunit/builder/Input.java b/xmlunit-core/src/main/java/org/xmlunit/builder/Input.java index 4f487f3a..27d9c5a7 100644 --- a/xmlunit-core/src/main/java/org/xmlunit/builder/Input.java +++ b/xmlunit-core/src/main/java/org/xmlunit/builder/Input.java @@ -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; @@ -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) { @@ -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); @@ -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. */ diff --git a/xmlunit-core/src/test/java/org/xmlunit/builder/InputTest.java b/xmlunit-core/src/test/java/org/xmlunit/builder/InputTest.java index b6c4e7f1..e767f388 100644 --- a/xmlunit-core/src/test/java/org/xmlunit/builder/InputTest.java +++ b/xmlunit-core/src/test/java/org/xmlunit/builder/InputTest.java @@ -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()); @@ -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)