Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix xml stream writer test on windows, replaced xml-matcher with xmlunit #1329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions deegree-core/deegree-core-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
<dependency>
<groupId>org.xmlmatchers</groupId>
<artifactId>xml-matchers</artifactId>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
</dependency>
</dependencies>
</project>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,30 @@
----------------------------------------------------------------------------*/
package org.deegree.filter.xml;

import static org.apache.commons.io.IOUtils.toInputStream;
import static org.junit.Assert.assertThat;
import static org.xmlmatchers.XmlMatchers.isSimilarTo;
import static org.xmlmatchers.transform.XmlConverters.the;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.deegree.commons.xml.stax.IndentingXMLStreamWriter;
import org.deegree.filter.Filter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.xmlunit.matchers.CompareMatcher;

import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.deegree.commons.xml.stax.IndentingXMLStreamWriter;
import org.deegree.filter.Filter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.io.IOUtils.toInputStream;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz</a>
Expand All @@ -78,7 +77,7 @@ public Filter200XMLEncoderParameterizedTest( String testName, String filterUnder

@Parameters
public static List<Object[]> data()
throws IOException {
throws IOException {
List<Object[]> filterTests = new ArrayList<Object[]>();
filterTests.add( new Object[] { "testfilter1.xml", asString( "v200/testfilter1.xml" ) } );
filterTests.add( new Object[] { "testfilter3.xml", asString( "v200/testfilter3.xml" ) } );
Expand All @@ -99,7 +98,7 @@ public static List<Object[]> data()

@Test
public void testExport()
throws Exception {
throws Exception {
Filter filter = parseFilter( filterUnderTest );

ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand All @@ -108,18 +107,20 @@ public void testExport()
Filter200XMLEncoder.export( filter, writer );
out.close();

assertThat( "Failed test: " + testName, the( bos.toString() ), isSimilarTo( the( filterUnderTest ) ) );
assertThat( "Failed test: " + testName, bos.toString(),
CompareMatcher.isSimilarTo( filterUnderTest ).ignoreWhitespace() );
}

private static String asString( String filterResource )
throws IOException {
throws IOException {
InputStream resourceAsStream = Filter200XMLEncoderParameterizedTest.class.getResourceAsStream( filterResource );
return IOUtils.toString( resourceAsStream );
return IOUtils.toString( resourceAsStream, UTF_8 );
}

private Filter parseFilter( String filterAsString )
throws XMLStreamException, FactoryConfigurationError {
XMLStreamReader in = XMLInputFactory.newInstance().createXMLStreamReader( toInputStream( filterAsString ) );
throws XMLStreamException, FactoryConfigurationError {
XMLStreamReader in = XMLInputFactory.newInstance().createXMLStreamReader(
toInputStream( filterAsString, UTF_8 ) );
in.nextTag();
return Filter200XMLDecoder.parse( in );
}
Expand Down

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions deegree-core/deegree-core-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,13 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,27 @@
----------------------------------------------------------------------------*/
package org.deegree.commons.xml.stax;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.io.IOUtils;
import org.deegree.commons.xml.NamespaceBindings;
import org.deegree.commons.xml.XMLAdapter;
import org.deegree.commons.xml.XPath;
import org.junit.Assert;
import org.junit.Test;
import org.xmlunit.matchers.CompareMatcher;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;

/**
*
* @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
* @author last edited by: $Author: stranger $
*
* @version $Revision: $, $Date: $
*/
public class FilteringXMLStreamWriterTest {
Expand All @@ -70,14 +67,15 @@ public class FilteringXMLStreamWriterTest {
private final static String alles = "http://www.deegree.org/alles";

private static final NamespaceBindings nsBindings = new NamespaceBindings();

static {
nsBindings.addNamespace( "app", app );
nsBindings.addNamespace( "nix", nix );
nsBindings.addNamespace( "alles", alles );
}

private void writeDocument( XMLStreamWriter writer )
throws XMLStreamException {
throws XMLStreamException {
writer.writeStartDocument();
writer.setPrefix( "app", app );
writer.setPrefix( "nix", nix );
Expand All @@ -104,7 +102,7 @@ private void writeDocument( XMLStreamWriter writer )
}

private XMLStreamWriter getWriter( List<String> paths, OutputStream stream )
throws Exception {
throws Exception {
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter( stream );
writer = new IndentingXMLStreamWriter( writer );
List<XPath> xpaths = new ArrayList<XPath>();
Expand All @@ -117,70 +115,73 @@ private XMLStreamWriter getWriter( List<String> paths, OutputStream stream )

@Test
public void testFilteringOneXPath()
throws Exception {
List<String> list = new ArrayList<String>();
throws Exception {
List<String> list = new ArrayList<>();
list.add( "/app:a/app:b/nix:c/app:b" );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLStreamWriter writer = getWriter( list, bos );
writeDocument( writer );
byte[] actual = bos.toByteArray();
byte[] expected = IOUtils.toByteArray( FilteringXMLStreamWriterTest.class.getResourceAsStream( "filteringxpathone.xml" ) );
Assert.assertArrayEquals( expected, actual );
String actual = bos.toString();
String expected = IOUtils.toString( FilteringXMLStreamWriterTest.class.getResourceAsStream(
"filteringxpathone.xml" ), UTF_8 );
assertThat( actual, CompareMatcher.isSimilarTo( expected ).ignoreWhitespace() );
}

@Test
public void testFilteringMultipleXPaths()
throws Exception {
List<String> list = new ArrayList<String>();
throws Exception {
List<String> list = new ArrayList<>();
list.add( "/app:a/app:b/nix:c/app:d" );
list.add( "/app:a/app:b/nix:c/app:b" );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLStreamWriter writer = getWriter( list, bos );
writeDocument( writer );
byte[] actual = bos.toByteArray();
byte[] expected = IOUtils.toByteArray( FilteringXMLStreamWriterTest.class.getResourceAsStream( "filteringxpathmultiple.xml" ) );
Assert.assertArrayEquals( expected, actual );
String actual = bos.toString();
String expected = IOUtils.toString( FilteringXMLStreamWriterTest.class.getResourceAsStream(
"filteringxpathmultiple.xml" ), UTF_8 );
assertThat( actual, CompareMatcher.isSimilarTo( expected ).ignoreWhitespace() );
}

@Test
public void testFilteringMultipleXPathsWithText()
throws Exception {
List<String> list = new ArrayList<String>();
throws Exception {
List<String> list = new ArrayList<>();
list.add( "/app:a/app:b/nix:c/alles:e" );
list.add( "/app:a/app:b/nix:c/app:b" );
list.add( "/app:a/app:b/nix:c/falsch:d" );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLStreamWriter writer = getWriter( list, bos );
writeDocument( writer );
byte[] actual = bos.toByteArray();
byte[] expected = IOUtils.toByteArray( FilteringXMLStreamWriterTest.class.getResourceAsStream( "filteringxpathmultiplewithtext.xml" ) );
Assert.assertArrayEquals( expected, actual );
String actual = bos.toString();
String expected = IOUtils.toString( FilteringXMLStreamWriterTest.class.getResourceAsStream(
"filteringxpathmultiplewithtext.xml" ), UTF_8 );
assertThat( actual, CompareMatcher.isSimilarTo( expected ).ignoreWhitespace() );
}

@Test(expected = XMLStreamException.class)
public void testFilteringOneXPathWithoutMatchingRootElement()
throws Exception {
List<String> list = new ArrayList<String>();
throws Exception {
List<String> list = new ArrayList<>();
list.add( "/ap:a/app:c" );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLStreamWriter writer = getWriter( list, bos );
writeDocument( writer );
byte[] actual = bos.toByteArray();
Assert.assertEquals( 0, actual.length );
}

@Test
public void testFilteringXPathSetPrefixBug()
throws Exception {
final XMLAdapter input = new XMLAdapter( FilteringXMLStreamWriterTest.class.getResourceAsStream( "filtering_xpath_set_prefix.xml" ) );
final List<String> list = new ArrayList<String>();
throws Exception {
final XMLAdapter input = new XMLAdapter(
FilteringXMLStreamWriterTest.class.getResourceAsStream( "filtering_xpath_set_prefix.xml" ) );
final List<String> list = new ArrayList<>();
list.add( "/app:a/nix:d" );
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final XMLStreamWriter writer = getWriter( list, bos );
input.getRootElement().serialize( writer );
writer.close();
byte[] actual = bos.toByteArray();
byte[] expected = IOUtils.toByteArray( FilteringXMLStreamWriterTest.class.getResourceAsStream( "filtering_xpath_set_prefix_expected.xml" ) );
Assert.assertArrayEquals( expected, actual );
String actual = bos.toString();
String expected = IOUtils.toString( FilteringXMLStreamWriterTest.class.getResourceAsStream(
"filtering_xpath_set_prefix_expected.xml" ), UTF_8 );
assertThat( actual, CompareMatcher.isSimilarTo( expected ).ignoreWhitespace() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.xmlmatchers</groupId>
<artifactId>xml-matchers</artifactId>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
</dependency>
</dependencies>
</project>
Loading