This commit is contained in:
Arjen Poutsma
2008-07-22 10:49:53 +00:00
parent eaba5accd6
commit 389fe5dd13
2 changed files with 445 additions and 0 deletions

View File

@@ -16,8 +16,14 @@
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;
@@ -26,6 +32,7 @@ import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
@@ -39,8 +46,16 @@ 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.w3c.dom.Element;
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 {
@@ -204,4 +219,214 @@ public class TraxUtilsTest extends XMLTestCase {
document.appendChild(element);
assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(element)));
}
public void testDoWithDomSource() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
mock.domSource(document);
control.replay();
TraxUtils.doWithSource(new DOMSource(document), mock);
control.verify();
}
public void testDoWithDomResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
mock.domResult(document);
control.replay();
TraxUtils.doWithResult(new DOMResult(document), mock);
control.verify();
}
public void testDoWithSaxSource() throws Exception {
XMLReader reader = XMLReaderFactory.createXMLReader();
InputSource inputSource = new InputSource();
MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
mock.saxSource(reader, inputSource);
control.replay();
TraxUtils.doWithSource(new SAXSource(reader, inputSource), mock);
control.verify();
}
public void testDoWithSaxResult() throws Exception {
ContentHandler contentHandler = new DefaultHandler();
LexicalHandler lexicalHandler = new DefaultHandler2();
MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
mock.saxResult(contentHandler, lexicalHandler);
control.replay();
SAXResult result = new SAXResult(contentHandler);
result.setLexicalHandler(lexicalHandler);
TraxUtils.doWithResult(result, mock);
control.verify();
}
public void testDoWithStaxSourceEventReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("<element/>"));
MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
mock.staxSource(eventReader);
control.replay();
TraxUtils.doWithSource(new StaxSource(eventReader), mock);
control.verify();
}
public void testDoWithStaxResultEventWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
mock.staxResult(eventWriter);
control.replay();
TraxUtils.doWithResult(new StaxResult(eventWriter), mock);
control.verify();
}
public void testDoWithStaxSourceStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("<element/>"));
MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
mock.staxSource(streamReader);
control.replay();
TraxUtils.doWithSource(new StaxSource(streamReader), mock);
control.verify();
}
public void testDoWithStaxResultStreamWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
mock.staxResult(streamWriter);
control.replay();
TraxUtils.doWithResult(new StaxResult(streamWriter), mock);
control.verify();
}
public void testDoWithStreamSourceInputStream() throws Exception {
byte[] xml = "<element/>".getBytes("UTF-8");
InputStream inputStream = new ByteArrayInputStream(xml);
MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
mock.streamSource(inputStream);
control.replay();
TraxUtils.doWithSource(new StreamSource(inputStream), mock);
control.verify();
}
public void testDoWithStreamResultOutputStream() throws Exception {
OutputStream outputStream = new ByteArrayOutputStream();
MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
mock.streamResult(outputStream);
control.replay();
TraxUtils.doWithResult(new StreamResult(outputStream), mock);
control.verify();
}
public void testDoWithStreamSourceReader() throws Exception {
String xml = "<element/>";
Reader reader = new StringReader(xml);
MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
mock.streamSource(reader);
control.replay();
TraxUtils.doWithSource(new StreamSource(reader), mock);
control.verify();
}
public void testDoWithStreamResultWriter() throws Exception {
Writer writer = new StringWriter();
MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
mock.streamResult(writer);
control.replay();
TraxUtils.doWithResult(new StreamResult(writer), mock);
control.verify();
}
public void testDoWithInvalidSource() throws Exception {
Source source = new Source() {
public void setSystemId(String systemId) {
}
public String getSystemId() {
return null;
}
};
try {
TraxUtils.doWithSource(source, null);
fail("IllegalArgumentException expected");
}
catch (IllegalArgumentException ex) {
// expected
}
}
public void testDoWithInvalidResult() throws Exception {
Result result = new Result() {
public void setSystemId(String systemId) {
}
public String getSystemId() {
return null;
}
};
try {
TraxUtils.doWithResult(result, null);
fail("IllegalArgumentException expected");
}
catch (IllegalArgumentException ex) {
// expected
}
}
}