1   /*
2    * Copyright 2005 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.stream.StreamResult;
27  
28  import org.custommonkey.xmlunit.XMLTestCase;
29  import org.custommonkey.xmlunit.XMLUnit;
30  import org.springframework.xml.transform.StaxResult;
31  import org.w3c.dom.Document;
32  import org.w3c.dom.Element;
33  import org.w3c.dom.Text;
34  import org.w3c.dom.Attr;
35  
36  public abstract class AbstractMarshallerTestCase extends XMLTestCase {
37  
38      protected Marshaller marshaller;
39  
40      protected Object flights;
41  
42      protected static final String EXPECTED_STRING =
43              "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
44                      "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
45  
46      protected final void setUp() throws Exception {
47          marshaller = createMarshaller();
48          flights = createFlights();
49          XMLUnit.setIgnoreWhitespace(true);
50      }
51  
52      protected abstract Marshaller createMarshaller() throws Exception;
53  
54      protected abstract Object createFlights();
55  
56      public void testMarshalDOMResult() throws Exception {
57          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
58          documentBuilderFactory.setNamespaceAware(true);
59          DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
60          Document result = builder.newDocument();
61          DOMResult domResult = new DOMResult(result);
62          marshaller.marshal(flights, domResult);
63          Document expected = builder.newDocument();
64          Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
65          Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
66          namespace.setNodeValue("http://samples.springframework.org/flight");
67          flightsElement.setAttributeNode(namespace);
68          expected.appendChild(flightsElement);
69          Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
70          flightsElement.appendChild(flightElement);
71          Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
72          flightElement.appendChild(numberElement);
73          Text text = expected.createTextNode("42");
74          numberElement.appendChild(text);
75          assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
76      }
77  
78      public void testMarshalStreamResultWriter() throws Exception {
79          StringWriter writer = new StringWriter();
80          StreamResult result = new StreamResult(writer);
81          marshaller.marshal(flights, result);
82          assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
83      }
84  
85      public void testMarshalStreamResultOutputStream() throws Exception {
86          ByteArrayOutputStream os = new ByteArrayOutputStream();
87          StreamResult result = new StreamResult(os);
88          marshaller.marshal(flights, result);
89          assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING,
90                  new String(os.toByteArray(), "UTF-8"));
91      }
92  
93      public void testMarshalStaxResultXMLStreamWriter() throws Exception {
94          XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
95          StringWriter writer = new StringWriter();
96          XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
97          StaxResult result = new StaxResult(streamWriter);
98          marshaller.marshal(flights, result);
99          assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
100     }
101 
102     public void testMarshalStaxResultXMLEventWriter() throws Exception {
103         XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
104         StringWriter writer = new StringWriter();
105         XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
106         StaxResult result = new StaxResult(eventWriter);
107         marshaller.marshal(flights, result);
108         assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
109     }
110 }