1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.oxm.xstream;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.StringReader;
21 import java.util.Properties;
22
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.stream.XMLInputFactory;
26 import javax.xml.stream.XMLStreamReader;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamSource;
29
30 import junit.framework.TestCase;
31 import org.w3c.dom.Document;
32 import org.xml.sax.InputSource;
33
34 import org.springframework.xml.transform.StaxSource;
35
36 public class XStreamUnmarshallerTest extends TestCase {
37
38 protected static final String INPUT_STRING = "<flight><flightNumber>42</flightNumber></flight>";
39
40 private XStreamMarshaller unmarshaller;
41
42 protected void setUp() throws Exception {
43 unmarshaller = new XStreamMarshaller();
44 Properties aliases = new Properties();
45 aliases.setProperty("flight", Flight.class.getName());
46 unmarshaller.setAliases(aliases);
47 }
48
49 private void testFlight(Object o) {
50 assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
51 Flight flight = (Flight) o;
52 assertNotNull("Flight is null", flight);
53 assertEquals("Number is invalid", 42L, flight.getFlightNumber());
54 }
55
56 public void testUnmarshalDomSource() throws Exception {
57 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
58 Document document = builder.parse(new InputSource(new StringReader(INPUT_STRING)));
59 DOMSource source = new DOMSource(document);
60 Object flight = unmarshaller.unmarshal(source);
61 testFlight(flight);
62 }
63
64 public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
65 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
66 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
67 StaxSource source = new StaxSource(streamReader);
68 Object flights = unmarshaller.unmarshal(source);
69 testFlight(flights);
70 }
71
72 public void testUnmarshalStreamSourceInputStream() throws Exception {
73 StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
74 Object flights = unmarshaller.unmarshal(source);
75 testFlight(flights);
76 }
77
78 public void testUnmarshalStreamSourceReader() throws Exception {
79 StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
80 Object flights = unmarshaller.unmarshal(source);
81 testFlight(flights);
82 }
83 }
84