Working on JAXP 1.4 support.

This commit is contained in:
Arjen Poutsma
2007-12-20 02:39:52 +00:00
parent 6a0bd01081
commit 5841e69872
4 changed files with 190 additions and 574 deletions

View File

@@ -1,69 +0,0 @@
/*
* Copyright 2007 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.
* 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 org.springframework.xml.transform;
import java.io.OutputStream;
import java.io.Writer;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
import org.xml.sax.ext.LexicalHandler;
/**
* Strategy interface for dealing with various {@link Result} implementations. By using this interface, in combination
* with {@link TraxUtils#handleResult(Result, ResultHandler)} , implementations are freed from <code>instanceof</code>
* checks typically seen in TrAX code.
*
* @author Arjen Poutsma
* @see TraxUtils#handleResult(Result, ResultHandler)
* @since 1.5.0
*/
public interface ResultHandler {
/**
* Handles a {@link DOMSource} with a {@link Node}.
* <p/>
* In practice, node is typically a {@link Document}, a {@link DocumentFragment}, or a {@link Element}. In other
* words, a node that accepts children.
*/
void domResult(Node node);
/** Handles a {@link SAXResult} with a {@link ContentHandler} and possibly a {@link LexicalHandler}. */
void saxResult(ContentHandler contentHandler, LexicalHandler lexicalHandler);
/** Handles a StAX Result with an {@link XMLEventWriter}, either in a {@link StaxResult} or a {@link StAXResult}. */
void staxResult(XMLEventWriter eventWriter);
/** Handles a StAX Result with an {@link XMLStreamWriter}, either in a {@link StaxResult} or a {@link StAXResult}. */
void staxResult(XMLStreamWriter streamWriter);
/** Handles a {@link StreamResult} with an {@link OutputStream}. */
void streamResult(OutputStream outputStream);
/** Handles a {@link StreamResult} with an {@link Writer}. */
void streamResult(Writer writer);
}

View File

@@ -1,62 +0,0 @@
/*
* Copyright 2007 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.
* 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 org.springframework.xml.transform;
import java.io.InputStream;
import java.io.Reader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
/**
* Strategy interface for dealing with various {@link Source} implementations. By using this interface, in combination
* with {@link TraxUtils#handleSource(Source, SourceHandler)} , implementations are freed from <code>instanceof</code>
* checks typically seen in TrAX code.
*
* @author Arjen Poutsma
* @see TraxUtils#handleSource(Source, SourceHandler)
* @since 1.5.0
*/
public interface SourceHandler {
/** Handles a {@link DOMSource} with a {@link Node}. */
void domSource(Node node);
/** Handles a {@link SAXSource} with an {@link XMLReader} and an {@link InputSource}. */
void saxSource(XMLReader xmlReader, InputSource inputSource);
/** Handles a StAX Source with an {@link XMLEventReader}, either in a {@link StaxSource} or a {@link StAXSource}. */
void staxSource(XMLEventReader eventReader);
/** Handles a StAX Source with an {@link XMLStreamReader}, either in a {@link StaxSource} or a {@link StAXSource}. */
void staxSource(XMLStreamReader streamReader);
/** Handles a {@link StreamSource} with an {@link InputStream}. */
void streamSource(InputStream inputStream);
/** Handles a {@link StreamSource} with an {@link Reader}. */
void streamSource(Reader reader);
}

View File

@@ -16,9 +16,7 @@
package org.springframework.xml.transform;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamException;
@@ -26,21 +24,8 @@ import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.springframework.util.Assert;
import org.springframework.xml.JaxpVersion;
@@ -59,6 +44,122 @@ public abstract class TraxUtils {
documentBuilderFactory.setNamespaceAware(true);
}
/**
* Indicates whether the given {@link Source} is a StAX Source.
*
* @return <code>true</code> if <code>source</code> is a Spring-WS {@link StaxSource} or JAXP 1.4 {@link
* StAXSource}; <code>false</code> otherwise.
*/
public static boolean isStaxSource(Source source) {
if (source instanceof StaxSource) {
return true;
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.isStaxSource(source);
}
else {
return false;
}
}
/**
* Indicates whether the given {@link Result} is a StAX Result.
*
* @return <code>true</code> if <code>result</code> is a Spring-WS {@link StaxResult} or JAXP 1.4 {@link
* StAXResult}; <code>false</code> otherwise.
*/
public static boolean isStaxResult(Result result) {
if (result instanceof StaxResult) {
return true;
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.isStaxResult(result);
}
else {
return false;
}
}
/**
* Returns the {@link XMLStreamReader} for the given StAX Source.
*
* @param source a Spring-WS {@link StaxSource} or {@link StAXSource}
* @return the {@link XMLStreamReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxSource} or {@link
* StAXSource}
*/
public static XMLStreamReader getXMLStreamReader(Source source) {
if (source instanceof StaxSource) {
return ((StaxSource) source).getXMLStreamReader();
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.getXMLStreamReader(source);
}
else {
throw new IllegalArgumentException("Source '" + source + "' is neither StaxSource nor StAXSource");
}
}
/**
* Returns the {@link XMLEventReader} for the given StAX Source.
*
* @param source a Spring-WS {@link StaxSource} or {@link StAXSource}
* @return the {@link XMLEventReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxSource} or {@link
* StAXSource}
*/
public static XMLEventReader getXMLEventReader(Source source) {
if (source instanceof StaxSource) {
return ((StaxSource) source).getXMLEventReader();
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.getXMLEventReader(source);
}
else {
throw new IllegalArgumentException("Source '" + source + "' is neither StaxSource nor StAXSource");
}
}
/**
* Returns the {@link XMLStreamWriter} for the given StAX Result.
*
* @param result a Spring-WS {@link StaxResult} or {@link StAXResult}
* @return the {@link XMLStreamReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxResult} or {@link
* StAXResult}
*/
public static XMLStreamWriter getXMLStreamWriter(Result result) {
if (result instanceof StaxResult) {
return ((StaxResult) result).getXMLStreamWriter();
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.getXMLStreamWriter(result);
}
else {
throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
}
}
/**
* Returns the {@link XMLEventWriter} for the given StAX Result.
*
* @param result a Spring-WS {@link StaxResult} or {@link StAXResult}
* @return the {@link XMLStreamReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxResult} or {@link
* StAXResult}
*/
public static XMLEventWriter getXMLEventWriter(Result result) {
if (result instanceof StaxResult) {
return ((StaxResult) result).getXMLEventWriter();
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.getXMLEventWriter(result);
}
else {
throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
}
}
/**
* Creates a StAX {@link Source} for the given {@link XMLStreamReader}. Returns a {@link StAXSource} under JAXP 1.4
* or higher, or a {@link StaxSource} otherwise.
@@ -92,198 +193,6 @@ public abstract class TraxUtils {
}
}
/**
* Handles the given {@link Source} by calling one of the methods on the given {@link SourceHandler}.
*
* @param source the source to handle
* @param handler the handler
*/
public static void handleSource(Source source, SourceHandler handler) {
if (source instanceof DOMSource) {
handleDomSource((DOMSource) source, handler);
}
else if (isStaxSource(source)) {
handleStaxSource(source, handler);
}
else if (source instanceof SAXSource) {
handleSaxSource((SAXSource) source, handler);
}
else if (source instanceof StreamSource) {
handleStreamSource((StreamSource) source, handler);
}
else {
throw new IllegalArgumentException("Unknown Source type: " + source.getClass());
}
}
private static boolean isStaxSource(Source source) {
if (source instanceof StaxSource) {
return true;
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.isStaxSource(source);
}
else {
return false;
}
}
private static void handleDomSource(DOMSource domSource, SourceHandler handler) {
if (domSource.getNode() == null) {
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
domSource.setNode(documentBuilder.newDocument());
}
catch (ParserConfigurationException ex) {
throw new IllegalStateException("Could not create document for DOMSource: " + ex.getMessage(), ex);
}
}
handler.domSource(domSource.getNode());
}
private static void handleStaxSource(Source source, SourceHandler handler) {
XMLStreamReader streamReader = null;
XMLEventReader eventReader = null;
if (source instanceof StaxSource) {
StaxSource staxSource = (StaxSource) source;
streamReader = staxSource.getXMLStreamReader();
eventReader = staxSource.getXMLEventReader();
}
else if (JaxpVersion.isAtLeastJaxp14()) {
streamReader = Jaxp14StaxHandler.getXMLStreamReader(source);
eventReader = Jaxp14StaxHandler.getXMLEventReader(source);
}
if (streamReader != null) {
handler.staxSource(streamReader);
}
else if (eventReader != null) {
handler.staxSource(eventReader);
}
else {
throw new IllegalArgumentException("StAX Source contains neither XMLStreamReader nor XMLEventReader");
}
}
private static void handleSaxSource(SAXSource saxSource, SourceHandler handler) {
if (saxSource.getXMLReader() == null) {
try {
saxSource.setXMLReader(XMLReaderFactory.createXMLReader());
}
catch (SAXException ex) {
throw new IllegalStateException("Could not create XMLReader for SAXSource: " + ex.getMessage(), ex);
}
}
if (saxSource.getInputSource() == null) {
saxSource.setInputSource(new InputSource());
}
handler.saxSource(saxSource.getXMLReader(), saxSource.getInputSource());
}
private static void handleStreamSource(StreamSource streamSource, SourceHandler handler) {
if (streamSource.getInputStream() != null) {
handler.streamSource(streamSource.getInputStream());
}
else if (streamSource.getReader() != null) {
handler.streamSource(streamSource.getReader());
}
else {
throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader");
}
}
/**
* Handles the given {@link Result} by calling one of the methods on the given {@link ResultHandler}.
*
* @param result the result to handle
* @param handler the handler
*/
public static void handleResult(Result result, ResultHandler handler) {
if (result instanceof DOMResult) {
handleDomResult((DOMResult) result, handler);
}
else if (isStaxResult(result)) {
handleStaxResult(result, handler);
}
else if (result instanceof SAXResult) {
handleSaxResult((SAXResult) result, handler);
}
else if (result instanceof StreamResult) {
handleStreamResult((StreamResult) result, handler);
}
else {
throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
}
}
private static boolean isStaxResult(Result result) {
if (result instanceof StaxResult) {
return true;
}
else if (JaxpVersion.isAtLeastJaxp14()) {
return Jaxp14StaxHandler.isStaxResult(result);
}
else {
return false;
}
}
private static void handleDomResult(DOMResult domResult, ResultHandler handler) {
if (domResult.getNode() == null) {
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
domResult.setNode(documentBuilder.newDocument());
}
catch (ParserConfigurationException ex) {
throw new IllegalStateException("Could not create document for DOMResult: " + ex.getMessage(), ex);
}
}
handler.domResult(domResult.getNode());
}
private static void handleStaxResult(Result result, ResultHandler handler) {
XMLStreamWriter streamWriter = null;
XMLEventWriter eventWriter = null;
if (result instanceof StaxResult) {
StaxResult staxResult = (StaxResult) result;
streamWriter = staxResult.getXMLStreamWriter();
eventWriter = staxResult.getXMLEventWriter();
}
else if (JaxpVersion.isAtLeastJaxp14()) {
streamWriter = Jaxp14StaxHandler.getXMLStreamWriter(result);
eventWriter = Jaxp14StaxHandler.getXMLEventWriter(result);
}
if (streamWriter != null) {
handler.staxResult(streamWriter);
}
else if (eventWriter != null) {
handler.staxResult(eventWriter);
}
else {
throw new IllegalArgumentException("StAX Result contains neither XMLStreamWriter nor XMLEventWriter");
}
}
private static void handleSaxResult(SAXResult saxResult, ResultHandler handler) {
ContentHandler contentHandler = saxResult.getHandler();
if (contentHandler == null) {
contentHandler = new DefaultHandler();
}
LexicalHandler lexicalHandler = saxResult.getLexicalHandler();
handler.saxResult(contentHandler, lexicalHandler);
}
private static void handleStreamResult(StreamResult streamResult, ResultHandler handler) {
if (streamResult.getOutputStream() != null) {
handler.streamResult(streamResult.getOutputStream());
}
else if (streamResult.getWriter() != null) {
handler.streamResult(streamResult.getWriter());
}
else {
throw new IllegalArgumentException("StreamResult contains neither OutputStream nor Writer");
}
}
/** Inner class to avoid a static JAXP 1.4 dependency. */
private static class Jaxp14StaxHandler {

View File

@@ -16,16 +16,8 @@
package org.springframework.xml.transform;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
@@ -45,31 +37,53 @@ import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.custommonkey.xmlunit.XMLTestCase;
import org.easymock.MockControl;
import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.DefaultHandler2;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class TraxUtilsTest extends XMLTestCase {
private SourceHandler sourceHandlerMock;
public void testIsStaxSourceInvalid() throws Exception {
assertFalse("A StAX Source", TraxUtils.isStaxSource(new DOMSource()));
assertFalse("A StAX Source", TraxUtils.isStaxSource(new SAXSource()));
assertFalse("A StAX Source", TraxUtils.isStaxSource(new StreamSource()));
}
private MockControl sourceHandlerControl;
public void testIsStaxSource() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String expected = "<element/>";
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
StaxSource source = new StaxSource(streamReader);
private MockControl resultHandlerControl;
assertTrue("Not a StAX Source", TraxUtils.isStaxSource(source));
}
private ResultHandler resultHandlerMock;
public void testIsStaxSourceJaxp14() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String expected = "<element/>";
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
StAXSource source = new StAXSource(streamReader);
protected void setUp() throws Exception {
sourceHandlerControl = MockControl.createControl(SourceHandler.class);
sourceHandlerMock = (SourceHandler) sourceHandlerControl.getMock();
resultHandlerControl = MockControl.createControl(ResultHandler.class);
resultHandlerMock = (ResultHandler) resultHandlerControl.getMock();
assertTrue("Not a StAX Source", TraxUtils.isStaxSource(source));
}
public void testIsStaxResultInvalid() throws Exception {
assertFalse("A StAX Result", TraxUtils.isStaxResult(new DOMResult()));
assertFalse("A StAX Result", TraxUtils.isStaxResult(new SAXResult()));
assertFalse("A StAX Result", TraxUtils.isStaxResult(new StreamResult()));
}
public void testIsStaxResult() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
StaxResult result = new StaxResult(streamWriter);
assertTrue("Not a StAX Result", TraxUtils.isStaxResult(result));
}
public void testIsStaxResultJaxp14() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
StAXResult result = new StAXResult(streamWriter);
assertTrue("Not a StAX Result", TraxUtils.isStaxResult(result));
}
public void testCreateStaxSourceStreamReader() throws Exception {
@@ -100,255 +114,79 @@ public class TraxUtilsTest extends XMLTestCase {
assertXMLEqual(expected, result.toString());
}
public void testHandleDomSource() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
DOMSource domSource = new DOMSource(document);
sourceHandlerMock.domSource(domSource.getNode());
sourceHandlerControl.replay();
TraxUtils.handleSource(domSource, sourceHandlerMock);
sourceHandlerControl.verify();
}
public void testHandleEmptyDomSource() throws Exception {
DOMSource domSource = new DOMSource();
sourceHandlerMock.domSource(domSource.getNode());
sourceHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
sourceHandlerControl.replay();
TraxUtils.handleSource(domSource, sourceHandlerMock);
sourceHandlerControl.verify();
}
public void testHandlerJaxp14StaxSourceStreamReader() throws Exception {
public void testGetXMLStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("<element/>"));
String expected = "<element/>";
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
StAXSource staxSource = new StAXSource(streamReader);
sourceHandlerMock.staxSource(streamReader);
sourceHandlerControl.replay();
StaxSource source = new StaxSource(streamReader);
TraxUtils.handleSource(staxSource, sourceHandlerMock);
sourceHandlerControl.verify();
assertEquals("Invalid XMLStreamReader", streamReader, TraxUtils.getXMLStreamReader(source));
}
public void testHandlerJaxp14StaxSourceEventReader() throws Exception {
public void testGetXMLStreamReaderJaxp14() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("<element/>"));
String expected = "<element/>";
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
StAXSource staxSource = new StAXSource(eventReader);
sourceHandlerMock.staxSource(eventReader);
sourceHandlerControl.replay();
StAXSource source = new StAXSource(streamReader);
TraxUtils.handleSource(staxSource, sourceHandlerMock);
sourceHandlerControl.verify();
assertEquals("Invalid XMLStreamReader", streamReader, TraxUtils.getXMLStreamReader(source));
}
public void testHandlerStaxSourceStreamReader() throws Exception {
public void testGetXMLEventReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("<element/>"));
String expected = "<element/>";
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(expected));
StaxSource staxSource = new StaxSource(streamReader);
sourceHandlerMock.staxSource(streamReader);
sourceHandlerControl.replay();
StaxSource source = new StaxSource(eventReader);
TraxUtils.handleSource(staxSource, sourceHandlerMock);
sourceHandlerControl.verify();
assertEquals("Invalid XMLEventReader", eventReader, TraxUtils.getXMLEventReader(source));
}
public void testHandlerStaxSourceEventReader() throws Exception {
public void testGetXMLEventReaderJaxp14() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("<element/>"));
String expected = "<element/>";
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(expected));
StaxSource staxSource = new StaxSource(eventReader);
sourceHandlerMock.staxSource(eventReader);
sourceHandlerControl.replay();
StAXSource source = new StAXSource(eventReader);
TraxUtils.handleSource(staxSource, sourceHandlerMock);
sourceHandlerControl.verify();
assertEquals("Invalid XMLEventReader", eventReader, TraxUtils.getXMLEventReader(source));
}
public void testHandlerSaxSource() throws Exception {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
InputSource inputSource = new InputSource(new StringReader("<element/>"));
SAXSource saxSource = new SAXSource(xmlReader, inputSource);
sourceHandlerMock.saxSource(xmlReader, inputSource);
sourceHandlerControl.replay();
TraxUtils.handleSource(saxSource, sourceHandlerMock);
sourceHandlerControl.verify();
}
public void testHandlerEmptySaxSource() throws Exception {
SAXSource saxSource = new SAXSource();
sourceHandlerMock.saxSource(null, null);
sourceHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
sourceHandlerControl.replay();
TraxUtils.handleSource(saxSource, sourceHandlerMock);
sourceHandlerControl.verify();
}
public void testHandleStreamSourceInputStream() throws Exception {
InputStream inputStream = new ByteArrayInputStream("<element/>".getBytes("UTF-8"));
StreamSource streamSource = new StreamSource(inputStream);
sourceHandlerMock.streamSource(inputStream);
sourceHandlerControl.replay();
TraxUtils.handleSource(streamSource, sourceHandlerMock);
sourceHandlerControl.verify();
}
public void testHandleStreamSourceReader() throws Exception {
Reader reader = new StringReader("<element/>");
StreamSource streamSource = new StreamSource(reader);
sourceHandlerMock.streamSource(reader);
sourceHandlerControl.replay();
TraxUtils.handleSource(streamSource, sourceHandlerMock);
sourceHandlerControl.verify();
}
public void testHandleDomResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
DOMResult domResult = new DOMResult(document);
resultHandlerMock.domResult(domResult.getNode());
resultHandlerControl.replay();
TraxUtils.handleResult(domResult, resultHandlerMock);
resultHandlerControl.verify();
}
public void testHandleEmptyDomResult() throws Exception {
DOMResult domResult = new DOMResult();
resultHandlerMock.domResult(domResult.getNode());
resultHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
resultHandlerControl.replay();
TraxUtils.handleResult(domResult, resultHandlerMock);
resultHandlerControl.verify();
}
public void testHandlerJaxp14StaxResultStreamReader() throws Exception {
public void testGetXMLStreamWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
StAXResult stAXResult = new StAXResult(streamWriter);
resultHandlerMock.staxResult(streamWriter);
resultHandlerControl.replay();
StaxResult result = new StaxResult(streamWriter);
TraxUtils.handleResult(stAXResult, resultHandlerMock);
resultHandlerControl.verify();
assertEquals("Invalid XMLStreamWriter", streamWriter, TraxUtils.getXMLStreamWriter(result));
}
public void testHandlerJaxp14StaxResultEventReader() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
StAXResult stAXResult = new StAXResult(eventWriter);
resultHandlerMock.staxResult(eventWriter);
resultHandlerControl.replay();
TraxUtils.handleResult(stAXResult, resultHandlerMock);
resultHandlerControl.verify();
}
public void testHandlerStaxResultStreamReader() throws Exception {
public void testGetXMLStreamWriterJaxp14() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
StaxResult staxResult = new StaxResult(streamWriter);
resultHandlerMock.staxResult(streamWriter);
resultHandlerControl.replay();
StAXResult result = new StAXResult(streamWriter);
TraxUtils.handleResult(staxResult, resultHandlerMock);
resultHandlerControl.verify();
assertEquals("Invalid XMLStreamWriter", streamWriter, TraxUtils.getXMLStreamWriter(result));
}
public void testHandlerStaxResultEventReader() throws Exception {
public void testGetXMLEventWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
StaxResult staxResult = new StaxResult(eventWriter);
resultHandlerMock.staxResult(eventWriter);
resultHandlerControl.replay();
StaxResult result = new StaxResult(eventWriter);
TraxUtils.handleResult(staxResult, resultHandlerMock);
resultHandlerControl.verify();
assertEquals("Invalid XMLStreamWriter", eventWriter, TraxUtils.getXMLEventWriter(result));
}
public void testHandlerSaxResult() throws Exception {
ContentHandler contentHandler = new DefaultHandler();
LexicalHandler lexicalHandler = new DefaultHandler2();
public void testGetXMLEventWriterJaxp14() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
SAXResult saxResult = new SAXResult(contentHandler);
saxResult.setLexicalHandler(lexicalHandler);
resultHandlerMock.saxResult(contentHandler, lexicalHandler);
resultHandlerControl.replay();
StAXResult result = new StAXResult(eventWriter);
TraxUtils.handleResult(saxResult, resultHandlerMock);
resultHandlerControl.verify();
assertEquals("Invalid XMLEventWriter", eventWriter, TraxUtils.getXMLEventWriter(result));
}
public void testHandlerEmptySaxResult() throws Exception {
SAXResult saxResult = new SAXResult();
resultHandlerMock.saxResult(null, null);
resultHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
resultHandlerControl.replay();
TraxUtils.handleResult(saxResult, resultHandlerMock);
resultHandlerControl.verify();
}
public void testHandleStreamResultOutputStream() throws Exception {
OutputStream outputStream = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(outputStream);
resultHandlerMock.streamResult(outputStream);
resultHandlerControl.replay();
TraxUtils.handleResult(streamResult, resultHandlerMock);
resultHandlerControl.verify();
}
public void testHandleStreamResultWriter() throws Exception {
Writer writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
resultHandlerMock.streamResult(writer);
resultHandlerControl.replay();
TraxUtils.handleResult(streamResult, resultHandlerMock);
resultHandlerControl.verify();
}
}