1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.oxm;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.StringWriter;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.stream.XMLEventWriter;
23 import javax.xml.stream.XMLOutputFactory;
24 import javax.xml.stream.XMLStreamWriter;
25 import javax.xml.transform.dom.DOMResult;
26 import javax.xml.transform.stax.StAXResult;
27 import javax.xml.transform.stream.StreamResult;
28
29 import org.custommonkey.xmlunit.XMLTestCase;
30 import org.custommonkey.xmlunit.XMLUnit;
31 import org.w3c.dom.Attr;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Text;
35
36 import org.springframework.xml.transform.StaxResult;
37
38 public abstract class AbstractMarshallerTestCase extends XMLTestCase {
39
40 protected Marshaller marshaller;
41
42 protected Object flights;
43
44 protected static final String EXPECTED_STRING =
45 "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
46 "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
47
48 protected final void setUp() throws Exception {
49 marshaller = createMarshaller();
50 flights = createFlights();
51 XMLUnit.setIgnoreWhitespace(true);
52 }
53
54 protected abstract Marshaller createMarshaller() throws Exception;
55
56 protected abstract Object createFlights();
57
58 public void testMarshalDOMResult() throws Exception {
59 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
60 documentBuilderFactory.setNamespaceAware(true);
61 DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
62 Document result = builder.newDocument();
63 DOMResult domResult = new DOMResult(result);
64 marshaller.marshal(flights, domResult);
65 Document expected = builder.newDocument();
66 Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
67 Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
68 namespace.setNodeValue("http://samples.springframework.org/flight");
69 flightsElement.setAttributeNode(namespace);
70 expected.appendChild(flightsElement);
71 Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
72 flightsElement.appendChild(flightElement);
73 Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
74 flightElement.appendChild(numberElement);
75 Text text = expected.createTextNode("42");
76 numberElement.appendChild(text);
77 assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
78 }
79
80 public void testMarshalStreamResultWriter() throws Exception {
81 StringWriter writer = new StringWriter();
82 StreamResult result = new StreamResult(writer);
83 marshaller.marshal(flights, result);
84 assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
85 }
86
87 public void testMarshalStreamResultOutputStream() throws Exception {
88 ByteArrayOutputStream os = new ByteArrayOutputStream();
89 StreamResult result = new StreamResult(os);
90 marshaller.marshal(flights, result);
91 assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING,
92 new String(os.toByteArray(), "UTF-8"));
93 }
94
95 public void testMarshalStaxResultStreamWriter() throws Exception {
96 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
97 StringWriter writer = new StringWriter();
98 XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
99 StaxResult result = new StaxResult(streamWriter);
100 marshaller.marshal(flights, result);
101 assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
102 }
103
104 public void testMarshalStaxResultEventWriter() throws Exception {
105 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
106 StringWriter writer = new StringWriter();
107 XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
108 StaxResult result = new StaxResult(eventWriter);
109 marshaller.marshal(flights, result);
110 assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
111 }
112
113 public void testMarshalJaxp14StaxResultStreamWriter() throws Exception {
114 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
115 StringWriter writer = new StringWriter();
116 XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
117 StAXResult result = new StAXResult(streamWriter);
118 marshaller.marshal(flights, result);
119 assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
120 }
121
122 public void testMarshalJaxp14StaxResultEventWriter() throws Exception {
123 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
124 StringWriter writer = new StringWriter();
125 XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
126 StAXResult result = new StAXResult(eventWriter);
127 marshaller.marshal(flights, result);
128 assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
129 }
130 }