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.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.stax.StAXSource;
29  import javax.xml.transform.stream.StreamSource;
30  
31  import junit.framework.TestCase;
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  import org.springframework.xml.transform.StaxSource;
40  
41  public abstract class AbstractUnmarshallerTestCase extends TestCase {
42  
43      protected Unmarshaller unmarshaller;
44  
45      protected static final String INPUT_STRING =
46              "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
47                      "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
48  
49      protected final void setUp() throws Exception {
50          unmarshaller = createUnmarshaller();
51      }
52  
53      protected abstract Unmarshaller createUnmarshaller() throws Exception;
54  
55      protected abstract void testFlights(Object o);
56  
57      protected abstract void testFlight(Object o);
58  
59      public void testUnmarshalDomSource() throws Exception {
60          DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
61          Document document = builder.newDocument();
62          Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
63          document.appendChild(flightsElement);
64          Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
65          flightsElement.appendChild(flightElement);
66          Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
67          flightElement.appendChild(numberElement);
68          Text text = document.createTextNode("42");
69          numberElement.appendChild(text);
70          DOMSource source = new DOMSource(document);
71          Object flights = unmarshaller.unmarshal(source);
72          testFlights(flights);
73      }
74  
75      public void testUnmarshalStreamSourceReader() throws Exception {
76          StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
77          Object flights = unmarshaller.unmarshal(source);
78          testFlights(flights);
79      }
80  
81      public void testUnmarshalStreamSourceInputStream() throws Exception {
82          StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
83          Object flights = unmarshaller.unmarshal(source);
84          testFlights(flights);
85      }
86  
87      public void testUnmarshalSAXSource() throws Exception {
88          XMLReader reader = XMLReaderFactory.createXMLReader();
89          SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
90          Object flights = unmarshaller.unmarshal(source);
91          testFlights(flights);
92      }
93  
94      public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
95          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
96          XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
97          StaxSource source = new StaxSource(streamReader);
98          Object flights = unmarshaller.unmarshal(source);
99          testFlights(flights);
100     }
101 
102     public void testUnmarshalStaxSourceXmlEventReader() throws Exception {
103         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
104         XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
105         StaxSource source = new StaxSource(eventReader);
106         Object flights = unmarshaller.unmarshal(source);
107         testFlights(flights);
108     }
109 
110     public void testUnmarshalJaxp14StaxSourceXmlStreamReader() throws Exception {
111         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
112         XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
113         StAXSource source = new StAXSource(streamReader);
114         Object flights = unmarshaller.unmarshal(source);
115         testFlights(flights);
116     }
117 
118     public void testUnmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
119         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
120         XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
121         StAXSource source = new StAXSource(eventReader);
122         Object flights = unmarshaller.unmarshal(source);
123         testFlights(flights);
124     }
125 
126     public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
127         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
128         XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
129         streamReader.nextTag(); // skip to flights
130         assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"),
131                 streamReader.getName());
132         streamReader.nextTag(); // skip to flight
133         assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"),
134                 streamReader.getName());
135         StaxSource source = new StaxSource(streamReader);
136         Object flight = unmarshaller.unmarshal(source);
137         testFlight(flight);
138     }
139 }