From: <bo...@us...> - 2009-05-20 14:09:49
|
Revision: 328 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=328&view=rev Author: bodewig Date: 2009-05-20 14:09:41 +0000 (Wed, 20 May 2009) Log Message: ----------- Java tests for input builder Modified Paths: -------------- trunk/xmlunit/build.xml Added Paths: ----------- trunk/xmlunit/lib/junit-4.6.jar trunk/xmlunit/src/tests/java-core/ trunk/xmlunit/src/tests/java-core/net/ trunk/xmlunit/src/tests/java-core/net/sf/ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java Modified: trunk/xmlunit/build.xml =================================================================== --- trunk/xmlunit/build.xml 2009-05-18 16:03:06 UTC (rev 327) +++ trunk/xmlunit/build.xml 2009-05-20 14:09:41 UTC (rev 328) @@ -30,7 +30,8 @@ <property name="lib.dir" value="${build.dir}/lib"/> <property name="core.out.dir" value="${build.dir}/core-classes"/> <property name="legacy.out.dir" value="${build.dir}/legacy-classes"/> - <property name="test.out.dir" value="${build.dir}/test-classes"/> + <property name="coretest.out.dir" value="${build.dir}/core-test-classes"/> + <property name="legacytest.out.dir" value="${build.dir}/legacy-test-classes"/> <property name="userguide.out.dir" value="${build.dir}/ug-classes"/> <property name="test.report.dir" value="${build.dir}/test-report"/> <property name="dist.dir" value="${build.dir}/dist"/> @@ -56,7 +57,8 @@ <mkdir dir="${lib.dir}"/> <mkdir dir="${core.out.dir}"/> <mkdir dir="${legacy.out.dir}"/> - <mkdir dir="${test.out.dir}"/> + <mkdir dir="${coretest.out.dir}"/> + <mkdir dir="${legacytest.out.dir}"/> <mkdir dir="${test.report.dir}"/> <mkdir dir="${dist.dir}"/> <mkdir dir="${docs.dir}"/> @@ -69,7 +71,8 @@ <fileset dir="${lib.dir}"/> <fileset dir="${core.out.dir}"/> <fileset dir="${legacy.out.dir}"/> - <fileset dir="${test.out.dir}"/> + <fileset dir="${coretest.out.dir}"/> + <fileset dir="${legacytest.out.dir}"/> <fileset dir="${test.report.dir}"/> <fileset dir="${dist.dir}"/> <fileset dir="${docs.dir}"/> @@ -86,55 +89,71 @@ </javac> </target> - <target name="compile" depends="compile-core" + <target name="compile-legacy" depends="compile-core" description="compiles legacy sources and tests"> <javac srcdir="${src.dir}/java-legacy" destdir="${legacy.out.dir}" debug="${javac.debug}" target="${javac.target}" source="${javac.source}"> <classpath> <pathelement location="${core.out.dir}"/> <pathelement path="${java.class.path}"/> - <fileset dir="lib" includes="*.jar"/> + <fileset dir="lib" includes="junit-3*.jar"/> </classpath> <exclude name="**/*XPathRegexAssert.java" unless="regexp.present"/> </javac> - <javac srcdir="${test.dir}/java-legacy" destdir="${test.out.dir}" + </target> + + <target name="compile-legacy-tests" depends="compile-legacy" + description="Compiles the test for XMLUnit 1.x"> + <javac srcdir="${test.dir}/java-legacy" destdir="${legacytest.out.dir}" debug="${javac.debug}" target="${javac.target}" source="${javac.source}"> <classpath> <pathelement location="${core.out.dir}"/> <pathelement location="${legacy.out.dir}"/> <pathelement path="${java.class.path}"/> - <fileset dir="lib" includes="*.jar"/> + <fileset dir="lib" includes="junit-3*.jar"/> </classpath> <exclude name="**/*XPathRegexAssert.java" unless="regexp.present"/> </javac> </target> - <target name="test" depends="compile" - description="runs the tests"> + <target name="compile-core-tests" depends="compile-core" + description="Compiles the test for XMLUnit2"> + <javac srcdir="${test.dir}/java-core" destdir="${coretest.out.dir}" + debug="${javac.debug}" target="${javac.target}" + source="${javac.source}"> + <classpath> + <pathelement location="${core.out.dir}"/> + <pathelement path="${java.class.path}"/> + <fileset dir="lib" includes="junit-4*.jar"/> + </classpath> + </javac> + </target> + + <target name="compile" depends="compile-core,compile-legacy"/> + <target name="compile-tests" + depends="compile-core-tests,compile-legacy-tests"/> + + <target name="test" depends="compile-tests" description="runs the tests"> <junit printsummary="yes" haltonfailure="no" fork="${junit.fork}" forkMode="perBatch" failureproperty="tests.failed"> <sysproperty key="basedir" value="${basedir}"/> <sysproperty key="user.dir" value="${basedir}"/> - <!-- - <sysproperty key="javax.xml.parsers.DocumentBuilderFactory" - value="org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"/> - <sysproperty key="javax.xml.parsers.SAXParserFactory" - value="org.apache.xerces.jaxp.SAXParserFactoryImpl"/> - <sysproperty key="javax.xml.transform.TransformerFactory" - value="org.apache.xalan.processor.TransformerFactoryImpl"/> - --> <classpath> <pathelement location="${core.out.dir}"/> <pathelement location="${legacy.out.dir}"/> - <pathelement location="${test.out.dir}"/> + <pathelement location="${legacytest.out.dir}"/> + <pathelement location="${coretest.out.dir}"/> <pathelement path="${java.class.path}"/> - <fileset dir="lib" includes="*.jar"/> + <fileset dir="lib" includes="junit-4*.jar"/> </classpath> <formatter type="xml"/> <batchtest todir="${test.report.dir}"> <fileset dir="${test.dir}/java-legacy"> <include name="**/test_*.java"/> </fileset> + <fileset dir="${test.dir}/java-core"> + <include name="**/*Test.java"/> + </fileset> </batchtest> </junit> Added: trunk/xmlunit/lib/junit-4.6.jar =================================================================== (Binary files differ) Property changes on: trunk/xmlunit/lib/junit-4.6.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java 2009-05-20 14:09:41 UTC (rev 328) @@ -0,0 +1,144 @@ +/* + 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.builder; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.net.URL; +import java.net.URI; +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 static org.hamcrest.core.Is.*; +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)); + } + + @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 + Source s = Input.fromDocument(d).build(); + assertThat(s, is(DOMSource.class)); + Object o = ((DOMSource) s).getNode(); + assertThat(o, is(Document.class)); + Document d2 = (Document) o; + assertThat(d2, notNullValue()); + assertThat(d2.getDocumentElement().getTagName(), is("animal")); + } + + @Test public void shouldParseAnExistingFileByName() throws Exception { + allIsWellFor(Input.fromFile(TEST_FILE).build()); + } + + @Test public void shouldParseAnExistingFileByFile() throws Exception { + allIsWellFor(Input.fromFile(new File(TEST_FILE)).build()); + } + + @Test public void shouldParseAnExistingFileFromStream() throws Exception { + FileInputStream is = null; + try { + is = new FileInputStream(TEST_FILE); + allIsWellFor(Input.fromStream(is).build()); + } finally { + if (is != null) { + is.close(); + } + } + } + + @Test public void shouldParseAnExistingFileFromReader() throws Exception { + FileReader r = null; + try { + r = new FileReader(TEST_FILE); + allIsWellFor(Input.fromReader(r).build()); + } finally { + if (r != null) { + r.close(); + } + } + } + + @Test public void shouldParseString() throws Exception { + allIsWellFor(Input.fromMemory(new String(readTestFile(), "UTF-8")) + .build()); + } + + @Test public void shouldParseBytes() throws Exception { + allIsWellFor(Input.fromMemory(readTestFile()).build()); + } + + @Test public void shouldParseFileFromURIString() throws Exception { + allIsWellFor(Input.fromURI("file:" + TEST_FILE).build()); + } + + @Test public void shouldParseFileFromURI() throws Exception { + allIsWellFor(Input.fromURI(new URI("file:" + TEST_FILE)).build()); + } + + @Test public void shouldParseFileFromURL() throws Exception { + allIsWellFor(Input.fromURL(new URL("file:" + TEST_FILE)).build()); + } + + @Test public void shouldParseATransformation() throws Exception { + Source input = Input.fromMemory("<animal>furry</animal>").build(); + Source s = Input.byTransforming(input) + .withStylesheet(Input.fromFile("src/tests/resources/animal.xsl") + .build()) + .build(); + // again, transformed is a DOMSource, cannot use parse() + assertThat(s, is(DOMSource.class)); + Object o = ((DOMSource) s).getNode(); + assertThat(o, is(Document.class)); + Document d2 = (Document) o; + assertThat(d2, notNullValue()); + assertThat(d2.getDocumentElement().getTagName(), is("furry")); + } + + private static void allIsWellFor(Source s) throws Exception { + assertThat(s, notNullValue()); + Document d = parse(s); + assertThat(d, notNullValue()); + assertThat(d.getDocumentElement().getTagName(), is("animal")); + } + + private static byte[] readTestFile() throws Exception { + FileInputStream is = new FileInputStream(TEST_FILE); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int read = -1; + while ((read = is.read(buffer)) >= 0) { + if (read > 0) { + bos.write(buffer, 0, read); + } + } + is.close(); + return bos.toByteArray(); + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/builder/InputTest.java ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |