From: <bo...@us...> - 2010-04-29 08:58:12
|
Revision: 366 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=366&view=rev Author: bodewig Date: 2010-04-29 08:58:05 +0000 (Thu, 29 Apr 2010) Log Message: ----------- take advantage of Connvert.toInputSource Modified Paths: -------------- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java Added Paths: ----------- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/Resources.java Added: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/Resources.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/Resources.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/Resources.java 2010-04-29 08:58:05 UTC (rev 366) @@ -0,0 +1,21 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit; + +public final class Resources { + public static final String ANIMAL_FILE = "src/tests/resources/test1.xml"; + + private Resources() { } + +} Property changes on: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/Resources.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java 2010-04-29 08:48:24 UTC (rev 365) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java 2010-04-29 08:58:05 UTC (rev 366) @@ -22,50 +22,42 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Source; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.sax.SAXSource; import org.w3c.dom.Document; +import net.sf.xmlunit.Resources; +import net.sf.xmlunit.util.Convert; +import org.junit.Test; + import static org.hamcrest.core.Is.*; import static org.hamcrest.core.IsInstanceOf.*; import static org.hamcrest.core.IsNull.*; import static org.junit.Assert.*; -import org.junit.Test; public class InputTest { - private static final String TEST_FILE = "src/tests/resources/test1.xml"; - private static Document parse(Source s) throws Exception { DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - return b.parse(SAXSource.sourceToInputSource(s)); + return b.parse(Convert.toInputSource(s)); } @Test public void shouldParseADocument() throws Exception { - Document d = parse(Input.fromFile(TEST_FILE).build()); - // it looks as if SAXSource.sourceToInputSource cannot deal - // with a DOMSource, so we cannot use the parse method + Document d = parse(Input.fromFile(Resources.ANIMAL_FILE).build()); Source s = Input.fromDocument(d).build(); - assertThat(s, instanceOf(DOMSource.class)); - Object o = ((DOMSource) s).getNode(); - assertThat(o, instanceOf(Document.class)); - Document d2 = (Document) o; - assertThat(d2, notNullValue()); - assertThat(d2.getDocumentElement().getTagName(), is("animal")); + allIsWellFor(s); } @Test public void shouldParseAnExistingFileByName() throws Exception { - allIsWellFor(Input.fromFile(TEST_FILE).build()); + allIsWellFor(Input.fromFile(Resources.ANIMAL_FILE).build()); } @Test public void shouldParseAnExistingFileByFile() throws Exception { - allIsWellFor(Input.fromFile(new File(TEST_FILE)).build()); + allIsWellFor(Input.fromFile(new File(Resources.ANIMAL_FILE)).build()); } @Test public void shouldParseAnExistingFileFromStream() throws Exception { FileInputStream is = null; try { - is = new FileInputStream(TEST_FILE); + is = new FileInputStream(Resources.ANIMAL_FILE); allIsWellFor(Input.fromStream(is).build()); } finally { if (is != null) { @@ -77,7 +69,7 @@ @Test public void shouldParseAnExistingFileFromReader() throws Exception { FileReader r = null; try { - r = new FileReader(TEST_FILE); + r = new FileReader(Resources.ANIMAL_FILE); allIsWellFor(Input.fromReader(r).build()); } finally { if (r != null) { @@ -96,15 +88,15 @@ } @Test public void shouldParseFileFromURIString() throws Exception { - allIsWellFor(Input.fromURI("file:" + TEST_FILE).build()); + allIsWellFor(Input.fromURI("file:" + Resources.ANIMAL_FILE).build()); } @Test public void shouldParseFileFromURI() throws Exception { - allIsWellFor(Input.fromURI(new URI("file:" + TEST_FILE)).build()); + allIsWellFor(Input.fromURI(new URI("file:" + Resources.ANIMAL_FILE)).build()); } @Test public void shouldParseFileFromURL() throws Exception { - allIsWellFor(Input.fromURL(new URL("file:" + TEST_FILE)).build()); + allIsWellFor(Input.fromURL(new URL("file:" + Resources.ANIMAL_FILE)).build()); } @Test public void shouldParseATransformationFromSource() throws Exception { @@ -113,13 +105,7 @@ .withStylesheet(Input.fromFile("src/tests/resources/animal.xsl") .build()) .build(); - // again, transformed is a DOMSource, cannot use parse() - assertThat(s, instanceOf(DOMSource.class)); - Object o = ((DOMSource) s).getNode(); - assertThat(o, instanceOf(Document.class)); - Document d2 = (Document) o; - assertThat(d2, notNullValue()); - assertThat(d2.getDocumentElement().getTagName(), is("furry")); + allIsWellFor(s, "furry"); } @Test public void shouldParseATransformationFromBuilder() throws Exception { @@ -127,24 +113,23 @@ Source s = Input.byTransforming(input) .withStylesheet(Input.fromFile("src/tests/resources/animal.xsl")) .build(); - // again, transformed is a DOMSource, cannot use parse() - assertThat(s, instanceOf(DOMSource.class)); - Object o = ((DOMSource) s).getNode(); - assertThat(o, instanceOf(Document.class)); - Document d2 = (Document) o; - assertThat(d2, notNullValue()); - assertThat(d2.getDocumentElement().getTagName(), is("furry")); + allIsWellFor(s, "furry"); } private static void allIsWellFor(Source s) throws Exception { + allIsWellFor(s, "animal"); + } + + private static void allIsWellFor(Source s, String rootElementName) + throws Exception { assertThat(s, notNullValue()); Document d = parse(s); assertThat(d, notNullValue()); - assertThat(d.getDocumentElement().getTagName(), is("animal")); + assertThat(d.getDocumentElement().getTagName(), is(rootElementName)); } private static byte[] readTestFile() throws Exception { - FileInputStream is = new FileInputStream(TEST_FILE); + FileInputStream is = new FileInputStream(Resources.ANIMAL_FILE); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read = -1; Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java 2010-04-29 08:48:24 UTC (rev 365) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java 2010-04-29 08:58:05 UTC (rev 366) @@ -21,6 +21,7 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamSource; +import net.sf.xmlunit.Resources; import org.hamcrest.core.IsNull; import org.junit.Test; import org.w3c.dom.Document; @@ -31,8 +32,6 @@ public class ConvertTest { - private static final String TEST_FILE = "src/tests/resources/test1.xml"; - private static void convertAndAssert(Source s) throws Exception { DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); @@ -42,18 +41,18 @@ } @Test public void streamSourceToInputSource() throws Exception { - convertAndAssert(new StreamSource(new File(TEST_FILE))); + convertAndAssert(new StreamSource(new File(Resources.ANIMAL_FILE))); } @Test public void domSourceToInputSource() throws Exception { DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document d = b.parse(new File(TEST_FILE)); + Document d = b.parse(new File(Resources.ANIMAL_FILE)); convertAndAssert(new DOMSource(d)); } @Test public void saxSourceToInputSource() throws Exception { - InputSource s = new InputSource(new FileInputStream(TEST_FILE)); + InputSource s = new InputSource(new FileInputStream(Resources.ANIMAL_FILE)); convertAndAssert(new SAXSource(s)); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |