1   /*
2    * Copyright 2006 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  
17  package org.springframework.oxm.xstream;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.StringWriter;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.Properties;
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.stream.XMLEventWriter;
27  import javax.xml.stream.XMLOutputFactory;
28  import javax.xml.stream.XMLStreamWriter;
29  import javax.xml.transform.dom.DOMResult;
30  import javax.xml.transform.sax.SAXResult;
31  import javax.xml.transform.stream.StreamResult;
32  
33  import com.thoughtworks.xstream.converters.Converter;
34  import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
35  import org.custommonkey.xmlunit.XMLTestCase;
36  import org.easymock.MockControl;
37  import org.springframework.xml.transform.StaxResult;
38  import org.springframework.xml.transform.StringResult;
39  import org.springframework.xml.transform.StringSource;
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Element;
42  import org.w3c.dom.Text;
43  import org.xml.sax.ContentHandler;
44  
45  public class XStreamMarshallerTest extends XMLTestCase {
46  
47      private static final String EXPECTED_STRING = "<flight><flightNumber>42</flightNumber></flight>";
48  
49      private XStreamMarshaller marshaller;
50  
51      private Flight flight;
52  
53      protected void setUp() throws Exception {
54          marshaller = new XStreamMarshaller();
55          Properties aliases = new Properties();
56          aliases.setProperty("flight", Flight.class.getName());
57          marshaller.setAliases(aliases);
58          flight = new Flight();
59          flight.setFlightNumber(42L);
60      }
61  
62      public void testMarshalDOMResult() throws Exception {
63          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
64          DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
65          Document document = builder.newDocument();
66          DOMResult domResult = new DOMResult(document);
67          marshaller.marshal(flight, domResult);
68          Document expected = builder.newDocument();
69          Element flightElement = expected.createElement("flight");
70          expected.appendChild(flightElement);
71          Element numberElement = expected.createElement("flightNumber");
72          flightElement.appendChild(numberElement);
73          Text text = expected.createTextNode("42");
74          numberElement.appendChild(text);
75          assertXMLEqual("Marshaller writes invalid DOMResult", expected, document);
76      }
77  
78      public void testMarshalStreamResultWriter() throws Exception {
79          StringWriter writer = new StringWriter();
80          StreamResult result = new StreamResult(writer);
81          marshaller.marshal(flight, 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(flight, result);
89          String s = new String(os.toByteArray(), "UTF-8");
90          assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, s);
91      }
92  
93      public void testMarshalSaxResult() throws Exception {
94          MockControl handlerControl = MockControl.createStrictControl(ContentHandler.class);
95          handlerControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
96          ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
97          handlerMock.startDocument();
98          handlerMock.startElement("", "flight", "flight", null);
99          handlerMock.startElement("", "number", "number", null);
100         handlerMock.characters(new char[]{'4', '2'}, 0, 2);
101         handlerMock.endElement("", "number", "number");
102         handlerMock.endElement("", "flight", "flight");
103         handlerMock.endDocument();
104 
105         handlerControl.replay();
106         SAXResult result = new SAXResult(handlerMock);
107         marshaller.marshal(flight, result);
108         handlerControl.verify();
109     }
110 
111     public void testMarshalStaxResultXMLStreamWriter() throws Exception {
112         XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
113         StringWriter writer = new StringWriter();
114         XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
115         StaxResult result = new StaxResult(streamWriter);
116         marshaller.marshal(flight, result);
117         assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
118     }
119 
120     public void testMarshalStaxResultXMLEventWriter() throws Exception {
121         XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
122         StringWriter writer = new StringWriter();
123         XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
124         StaxResult result = new StaxResult(eventWriter);
125         marshaller.marshal(flight, result);
126         assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
127     }
128 
129     public void testConverters() throws Exception {
130         marshaller.setConverters(new Converter[]{new EncodedByteArrayConverter()});
131         byte[] buf = new byte[]{0x1, 0x2};
132         StringResult result = new StringResult();
133         marshaller.marshal(buf, result);
134         assertXMLEqual("<byte-array>AQI=</byte-array>", result.toString());
135         StringSource source = new StringSource(result.toString());
136         byte[] bufResult = (byte[]) marshaller.unmarshal(source);
137         assertTrue("Invalid result", Arrays.equals(buf, bufResult));
138     }
139 
140     public void testUseAttributesFor() throws Exception {
141         marshaller.setUseAttributeForTypes(new Class[]{Long.TYPE});
142         StringResult result = new StringResult();
143         marshaller.marshal(flight, result);
144         String expected = "<flight flightNumber=\"42\" />";
145         assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
146     }
147 
148     public void testUseAttributesForStringClassMap() throws Exception {
149         marshaller.setUseAttributeFor(Collections.singletonMap("flightNumber", Long.TYPE));
150         StringResult result = new StringResult();
151         marshaller.marshal(flight, result);
152         String expected = "<flight flightNumber=\"42\" />";
153         assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
154     }
155 
156     public void testUseAttributesForClassStringMap() throws Exception {
157         marshaller.setUseAttributeFor(Collections.singletonMap(Flight.class, "flightNumber"));
158         StringResult result = new StringResult();
159         marshaller.marshal(flight, result);
160         String expected = "<flight flightNumber=\"42\" />";
161         assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
162     }
163 
164 
165 }