001: /*
002: * Copyright 2005 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: package org.springframework.oxm;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.StringWriter;
020: import javax.xml.parsers.DocumentBuilder;
021: import javax.xml.parsers.DocumentBuilderFactory;
022: import javax.xml.stream.XMLEventWriter;
023: import javax.xml.stream.XMLOutputFactory;
024: import javax.xml.stream.XMLStreamWriter;
025: import javax.xml.transform.dom.DOMResult;
026: import javax.xml.transform.stream.StreamResult;
027:
028: import org.custommonkey.xmlunit.XMLTestCase;
029: import org.custommonkey.xmlunit.XMLUnit;
030: import org.springframework.xml.transform.StaxResult;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Text;
034: import org.w3c.dom.Attr;
035:
036: public abstract class AbstractMarshallerTestCase extends XMLTestCase {
037:
038: protected Marshaller marshaller;
039:
040: protected Object flights;
041:
042: protected static final String EXPECTED_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">"
043: + "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
044:
045: protected final void setUp() throws Exception {
046: marshaller = createMarshaller();
047: flights = createFlights();
048: XMLUnit.setIgnoreWhitespace(true);
049: }
050:
051: protected abstract Marshaller createMarshaller() throws Exception;
052:
053: protected abstract Object createFlights();
054:
055: public void testMarshalDOMResult() throws Exception {
056: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
057: .newInstance();
058: documentBuilderFactory.setNamespaceAware(true);
059: DocumentBuilder builder = documentBuilderFactory
060: .newDocumentBuilder();
061: Document result = builder.newDocument();
062: DOMResult domResult = new DOMResult(result);
063: marshaller.marshal(flights, domResult);
064: Document expected = builder.newDocument();
065: Element flightsElement = expected.createElementNS(
066: "http://samples.springframework.org/flight",
067: "tns:flights");
068: Attr namespace = expected.createAttributeNS(
069: "http://www.w3.org/2000/xmlns/", "xmlns:tns");
070: namespace
071: .setNodeValue("http://samples.springframework.org/flight");
072: flightsElement.setAttributeNode(namespace);
073: expected.appendChild(flightsElement);
074: Element flightElement = expected.createElementNS(
075: "http://samples.springframework.org/flight",
076: "tns:flight");
077: flightsElement.appendChild(flightElement);
078: Element numberElement = expected.createElementNS(
079: "http://samples.springframework.org/flight",
080: "tns:number");
081: flightElement.appendChild(numberElement);
082: Text text = expected.createTextNode("42");
083: numberElement.appendChild(text);
084: assertXMLEqual("Marshaller writes invalid DOMResult", expected,
085: result);
086: }
087:
088: public void testMarshalStreamResultWriter() throws Exception {
089: StringWriter writer = new StringWriter();
090: StreamResult result = new StreamResult(writer);
091: marshaller.marshal(flights, result);
092: assertXMLEqual("Marshaller writes invalid StreamResult",
093: EXPECTED_STRING, writer.toString());
094: }
095:
096: public void testMarshalStreamResultOutputStream() throws Exception {
097: ByteArrayOutputStream os = new ByteArrayOutputStream();
098: StreamResult result = new StreamResult(os);
099: marshaller.marshal(flights, result);
100: assertXMLEqual("Marshaller writes invalid StreamResult",
101: EXPECTED_STRING, new String(os.toByteArray(), "UTF-8"));
102: }
103:
104: public void testMarshalStaxResultXMLStreamWriter() throws Exception {
105: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
106: StringWriter writer = new StringWriter();
107: XMLStreamWriter streamWriter = outputFactory
108: .createXMLStreamWriter(writer);
109: StaxResult result = new StaxResult(streamWriter);
110: marshaller.marshal(flights, result);
111: assertXMLEqual("Marshaller writes invalid StreamResult",
112: EXPECTED_STRING, writer.toString());
113: }
114:
115: public void testMarshalStaxResultXMLEventWriter() throws Exception {
116: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
117: StringWriter writer = new StringWriter();
118: XMLEventWriter eventWriter = outputFactory
119: .createXMLEventWriter(writer);
120: StaxResult result = new StaxResult(eventWriter);
121: marshaller.marshal(flights, result);
122: assertXMLEqual("Marshaller writes invalid StreamResult",
123: EXPECTED_STRING, writer.toString());
124: }
125: }
|