001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStreamReader;
023: import java.io.Reader;
024: import java.io.StringWriter;
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.stream.XMLEventReader;
028: import javax.xml.stream.XMLEventWriter;
029: import javax.xml.stream.XMLInputFactory;
030: import javax.xml.stream.XMLOutputFactory;
031: import javax.xml.stream.XMLStreamReader;
032: import javax.xml.stream.XMLStreamWriter;
033: import javax.xml.transform.Transformer;
034: import javax.xml.transform.TransformerFactory;
035: import javax.xml.transform.dom.DOMResult;
036: import javax.xml.transform.dom.DOMSource;
037: import javax.xml.transform.sax.SAXSource;
038: import javax.xml.transform.stream.StreamResult;
039: import javax.xml.transform.stream.StreamSource;
040:
041: import org.custommonkey.xmlunit.XMLTestCase;
042: import org.custommonkey.xmlunit.XMLUnit;
043: import org.springframework.core.io.ClassPathResource;
044: import org.springframework.core.io.Resource;
045: import org.springframework.util.FileCopyUtils;
046: import org.springframework.xml.sax.SaxUtils;
047: import org.springframework.xml.transform.StaxResult;
048: import org.springframework.xml.transform.StaxSource;
049: import org.springframework.xml.transform.StringResult;
050: import org.w3c.dom.Document;
051: import org.xml.sax.InputSource;
052: import org.xml.sax.XMLReader;
053: import org.xml.sax.helpers.DefaultHandler;
054: import org.xml.sax.helpers.XMLReaderFactory;
055:
056: public abstract class AbstractWebServiceMessageTestCase extends
057: XMLTestCase {
058:
059: protected Transformer transformer;
060:
061: protected WebServiceMessage webServiceMessage;
062:
063: private Resource payload;
064:
065: private String getExpectedString() throws IOException {
066: StringWriter expectedWriter = new StringWriter();
067: FileCopyUtils.copy(new InputStreamReader(payload
068: .getInputStream(), "UTF-8"), expectedWriter);
069: return expectedWriter.toString();
070: }
071:
072: protected final void setUp() throws Exception {
073: TransformerFactory transformerFactory = TransformerFactory
074: .newInstance();
075: transformer = transformerFactory.newTransformer();
076: webServiceMessage = createWebServiceMessage();
077: payload = new ClassPathResource("payload.xml",
078: AbstractWebServiceMessageTestCase.class);
079: XMLUnit.setIgnoreWhitespace(true);
080: }
081:
082: public void testDomPayload() throws Exception {
083: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
084: .newInstance();
085: documentBuilderFactory.setNamespaceAware(true);
086: DocumentBuilder documentBuilder = documentBuilderFactory
087: .newDocumentBuilder();
088: Document payloadDocument = documentBuilder.parse(SaxUtils
089: .createInputSource(payload));
090: DOMSource domSource = new DOMSource(payloadDocument);
091: transformer.transform(domSource, webServiceMessage
092: .getPayloadResult());
093: Document resultDocument = documentBuilder.newDocument();
094: DOMResult domResult = new DOMResult(resultDocument);
095: transformer.transform(webServiceMessage.getPayloadSource(),
096: domResult);
097: assertXMLEqual(payloadDocument, resultDocument);
098: validateMessage();
099: }
100:
101: public void testEventReaderPayload() throws Exception {
102: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
103: XMLEventReader eventReader = inputFactory
104: .createXMLEventReader(payload.getInputStream());
105: StaxSource staxSource = new StaxSource(eventReader);
106: transformer.transform(staxSource, webServiceMessage
107: .getPayloadResult());
108: StringWriter stringWriter = new StringWriter();
109: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
110: XMLEventWriter eventWriter = outputFactory
111: .createXMLEventWriter(stringWriter);
112: StaxResult staxResult = new StaxResult(eventWriter);
113: transformer.transform(webServiceMessage.getPayloadSource(),
114: staxResult);
115: eventWriter.flush();
116: assertXMLEqual(getExpectedString(), stringWriter.toString());
117: validateMessage();
118: }
119:
120: public void testReaderPayload() throws Exception {
121: Reader reader = new InputStreamReader(payload.getInputStream(),
122: "UTF-8");
123: StreamSource streamSource = new StreamSource(reader, payload
124: .getURL().toString());
125: transformer.transform(streamSource, webServiceMessage
126: .getPayloadResult());
127: StringWriter resultWriter = new StringWriter();
128: StreamResult streamResult = new StreamResult(resultWriter);
129: transformer.transform(webServiceMessage.getPayloadSource(),
130: streamResult);
131: assertXMLEqual(getExpectedString(), resultWriter.toString());
132: }
133:
134: public void testSaxPayload() throws Exception {
135: SAXSource saxSource = new SAXSource(SaxUtils
136: .createInputSource(payload));
137: transformer.transform(saxSource, webServiceMessage
138: .getPayloadResult());
139: StringResult stringResult = new StringResult();
140: transformer.transform(webServiceMessage.getPayloadSource(),
141: stringResult);
142: assertXMLEqual(getExpectedString(), stringResult.toString());
143: validateMessage();
144: }
145:
146: public void testStreamPayload() throws Exception {
147: StreamSource streamSource = new StreamSource(payload
148: .getInputStream(), payload.getURL().toString());
149: transformer.transform(streamSource, webServiceMessage
150: .getPayloadResult());
151: ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
152: StreamResult streamResult = new StreamResult(resultStream);
153: transformer.transform(webServiceMessage.getPayloadSource(),
154: streamResult);
155: ByteArrayOutputStream expectedStream = new ByteArrayOutputStream();
156: FileCopyUtils.copy(payload.getInputStream(), expectedStream);
157: assertXMLEqual(expectedStream.toString("UTF-8"), resultStream
158: .toString("UTF-8"));
159: validateMessage();
160: }
161:
162: public void testStreamReaderPayload() throws Exception {
163: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
164: XMLStreamReader streamReader = inputFactory
165: .createXMLStreamReader(payload.getInputStream());
166: StaxSource staxSource = new StaxSource(streamReader);
167: transformer.transform(staxSource, webServiceMessage
168: .getPayloadResult());
169: StringWriter stringWriter = new StringWriter();
170: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
171: XMLStreamWriter streamWriter = outputFactory
172: .createXMLStreamWriter(stringWriter);
173: StaxResult staxResult = new StaxResult(streamWriter);
174: transformer.transform(webServiceMessage.getPayloadSource(),
175: staxResult);
176: streamWriter.flush();
177: assertXMLEqual(getExpectedString(), stringWriter.toString());
178: validateMessage();
179: }
180:
181: private void validateMessage() throws Exception {
182: XMLReader xmlReader = XMLReaderFactory.createXMLReader();
183: xmlReader.setContentHandler(new DefaultHandler());
184: ByteArrayOutputStream os = new ByteArrayOutputStream();
185: webServiceMessage.writeTo(os);
186: ByteArrayInputStream is = new ByteArrayInputStream(os
187: .toByteArray());
188: xmlReader.parse(new InputSource(is));
189: }
190:
191: protected abstract WebServiceMessage createWebServiceMessage()
192: throws Exception;
193: }
|