001: /*
002: * Copyright 2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.oxm;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.StringReader;
020: import javax.xml.namespace.QName;
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.stream.XMLEventReader;
024: import javax.xml.stream.XMLInputFactory;
025: import javax.xml.stream.XMLStreamReader;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.sax.SAXSource;
028: import javax.xml.transform.stream.StreamSource;
029:
030: import junit.framework.TestCase;
031: import org.springframework.xml.transform.StaxSource;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.Text;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.XMLReader;
037: import org.xml.sax.helpers.XMLReaderFactory;
038:
039: public abstract class AbstractUnmarshallerTestCase extends TestCase {
040:
041: protected Unmarshaller unmarshaller;
042:
043: protected static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">"
044: + "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
045:
046: protected final void setUp() throws Exception {
047: unmarshaller = createUnmarshaller();
048: }
049:
050: protected abstract Unmarshaller createUnmarshaller()
051: throws Exception;
052:
053: protected abstract void testFlights(Object o);
054:
055: protected abstract void testFlight(Object o);
056:
057: public void testUnmarshalDomSource() throws Exception {
058: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
059: .newDocumentBuilder();
060: Document document = builder.newDocument();
061: Element flightsElement = document.createElementNS(
062: "http://samples.springframework.org/flight",
063: "tns:flights");
064: document.appendChild(flightsElement);
065: Element flightElement = document.createElementNS(
066: "http://samples.springframework.org/flight",
067: "tns:flight");
068: flightsElement.appendChild(flightElement);
069: Element numberElement = document.createElementNS(
070: "http://samples.springframework.org/flight",
071: "tns:number");
072: flightElement.appendChild(numberElement);
073: Text text = document.createTextNode("42");
074: numberElement.appendChild(text);
075: DOMSource source = new DOMSource(document);
076: Object flights = unmarshaller.unmarshal(source);
077: testFlights(flights);
078: }
079:
080: public void testUnmarshalStreamSourceReader() throws Exception {
081: StreamSource source = new StreamSource(new StringReader(
082: INPUT_STRING));
083: Object flights = unmarshaller.unmarshal(source);
084: testFlights(flights);
085: }
086:
087: public void testUnmarshalStreamSourceInputStream() throws Exception {
088: StreamSource source = new StreamSource(
089: new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
090: Object flights = unmarshaller.unmarshal(source);
091: testFlights(flights);
092: }
093:
094: public void testUnmarshalSAXSource() throws Exception {
095: XMLReader reader = XMLReaderFactory.createXMLReader();
096: SAXSource source = new SAXSource(reader, new InputSource(
097: new StringReader(INPUT_STRING)));
098: Object flights = unmarshaller.unmarshal(source);
099: testFlights(flights);
100: }
101:
102: public void testUnmarshalStaxSourceXmlStreamReader()
103: throws Exception {
104: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
105: XMLStreamReader streamReader = inputFactory
106: .createXMLStreamReader(new StringReader(INPUT_STRING));
107: StaxSource source = new StaxSource(streamReader);
108: Object flights = unmarshaller.unmarshal(source);
109: testFlights(flights);
110: }
111:
112: public void testUnmarshalStaxSourceXmlEventReader()
113: throws Exception {
114: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
115: XMLEventReader eventReader = inputFactory
116: .createXMLEventReader(new StringReader(INPUT_STRING));
117: StaxSource source = new StaxSource(eventReader);
118: Object flights = unmarshaller.unmarshal(source);
119: testFlights(flights);
120: }
121:
122: public void testUnmarshalPartialStaxSourceXmlStreamReader()
123: throws Exception {
124: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
125: XMLStreamReader streamReader = inputFactory
126: .createXMLStreamReader(new StringReader(INPUT_STRING));
127: streamReader.nextTag(); // skip to flights
128: assertEquals("Invalid element",
129: new QName("http://samples.springframework.org/flight",
130: "flights"), streamReader.getName());
131: streamReader.nextTag(); // skip to flight
132: assertEquals("Invalid element", new QName(
133: "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: }
|