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.ByteArrayInputStream;
19 import java.io.StringReader;
20 import javax.xml.namespace.QName;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.stream.XMLEventReader;
24 import javax.xml.stream.XMLInputFactory;
25 import javax.xml.stream.XMLStreamReader;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.sax.SAXSource;
28 import javax.xml.transform.stream.StreamSource;
29
30 import junit.framework.TestCase;
31 import org.springframework.xml.transform.StaxSource;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Text;
35 import org.xml.sax.InputSource;
36 import org.xml.sax.XMLReader;
37 import org.xml.sax.helpers.XMLReaderFactory;
38
39 public abstract class AbstractUnmarshallerTestCase extends TestCase {
40
41 protected Unmarshaller unmarshaller;
42
43 protected static final String INPUT_STRING =
44 "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
45 "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
46
47 protected final void setUp() throws Exception {
48 unmarshaller = createUnmarshaller();
49 }
50
51 protected abstract Unmarshaller createUnmarshaller() throws Exception;
52
53 protected abstract void testFlights(Object o);
54
55 protected abstract void testFlight(Object o);
56
57 public void testUnmarshalDomSource() throws Exception {
58 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
59 Document document = builder.newDocument();
60 Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
61 document.appendChild(flightsElement);
62 Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
63 flightsElement.appendChild(flightElement);
64 Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
65 flightElement.appendChild(numberElement);
66 Text text = document.createTextNode("42");
67 numberElement.appendChild(text);
68 DOMSource source = new DOMSource(document);
69 Object flights = unmarshaller.unmarshal(source);
70 testFlights(flights);
71 }
72
73 public void testUnmarshalStreamSourceReader() throws Exception {
74 StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
75 Object flights = unmarshaller.unmarshal(source);
76 testFlights(flights);
77 }
78
79 public void testUnmarshalStreamSourceInputStream() throws Exception {
80 StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
81 Object flights = unmarshaller.unmarshal(source);
82 testFlights(flights);
83 }
84
85 public void testUnmarshalSAXSource() throws Exception {
86 XMLReader reader = XMLReaderFactory.createXMLReader();
87 SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
88 Object flights = unmarshaller.unmarshal(source);
89 testFlights(flights);
90 }
91
92 public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
93 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
94 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
95 StaxSource source = new StaxSource(streamReader);
96 Object flights = unmarshaller.unmarshal(source);
97 testFlights(flights);
98 }
99
100 public void testUnmarshalStaxSourceXmlEventReader() throws Exception {
101 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
102 XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
103 StaxSource source = new StaxSource(eventReader);
104 Object flights = unmarshaller.unmarshal(source);
105 testFlights(flights);
106 }
107
108 public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
109 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
110 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
111 streamReader.nextTag();
112 assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"),
113 streamReader.getName());
114 streamReader.nextTag();
115 assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"),
116 streamReader.getName());
117 StaxSource source = new StaxSource(streamReader);
118 Object flight = unmarshaller.unmarshal(source);
119 testFlight(flight);
120 }
121 }