Various StAX improvements.

This commit is contained in:
Arjen Poutsma
2010-08-27 11:35:27 +00:00
parent b72cca5403
commit 9aafa1c6b2
6 changed files with 139 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,16 +18,21 @@ package org.springframework.util.xml;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import static org.custommonkey.xmlunit.XMLAssert.*;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class StaxSourceTests {
@@ -37,15 +42,20 @@ public class StaxSourceTests {
private XMLInputFactory inputFactory;
private DocumentBuilder documentBuilder;
@Before
public void createsetUp() throws Exception {
public void setUp() throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
inputFactory = XMLInputFactory.newInstance();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
}
@Test
public void streamReaderSource() throws Exception {
public void streamReaderSourceToStreamResult() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader);
assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
@@ -56,7 +66,20 @@ public class StaxSourceTests {
}
@Test
public void eventReaderSource() throws Exception {
public void streamReaderSourceToDOMResult() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader);
assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
assertNull("EventReader returned", source.getXMLEventReader());
Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
Document result = documentBuilder.newDocument();
transformer.transform(source, new DOMResult(result));
assertXMLEqual("Invalid result", expected, result);
}
@Test
public void eventReaderSourceToStreamResult() throws Exception {
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
StaxSource source = new StaxSource(eventReader);
assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
@@ -65,4 +88,17 @@ public class StaxSourceTests {
transformer.transform(source, new StreamResult(writer));
assertXMLEqual("Invalid result", XML, writer.toString());
}
@Test
public void eventReaderSourceToDOMResult() throws Exception {
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
StaxSource source = new StaxSource(eventReader);
assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
assertNull("StreamReader returned", source.getXMLStreamReader());
Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
Document result = documentBuilder.newDocument();
transformer.transform(source, new DOMResult(result));
assertXMLEqual("Invalid result", expected, result);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@ import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.easymock.MockControl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import org.junit.Test;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
@@ -55,6 +55,8 @@ public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
mockControl.setDefaultMatcher(new SaxArgumentMatcher());
ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock();
contentHandlerMock.setDocumentLocator(null);
mockControl.setMatcher(MockControl.ALWAYS_MATCHER);
contentHandlerMock.startDocument();
contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child");