001: /*
002: * Copyright 2006 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:
017: package org.springframework.oxm.jaxb;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.StringReader;
021: import javax.activation.DataHandler;
022: import javax.activation.FileDataSource;
023: import javax.xml.bind.JAXBElement;
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.stream.XMLEventReader;
027: import javax.xml.stream.XMLInputFactory;
028: import javax.xml.stream.XMLStreamReader;
029: import javax.xml.transform.Source;
030: import javax.xml.transform.dom.DOMSource;
031: import javax.xml.transform.sax.SAXSource;
032: import javax.xml.transform.stream.StreamSource;
033:
034: import junit.framework.TestCase;
035: import static org.easymock.EasyMock.*;
036: import org.springframework.core.io.ClassPathResource;
037: import org.springframework.core.io.Resource;
038: import org.springframework.oxm.jaxb2.FlightType;
039: import org.springframework.oxm.jaxb2.Flights;
040: import org.springframework.oxm.mime.MimeContainer;
041: import org.springframework.xml.transform.StaxSource;
042: import org.springframework.xml.transform.StringSource;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045: import org.w3c.dom.Text;
046: import org.xml.sax.InputSource;
047: import org.xml.sax.XMLReader;
048: import org.xml.sax.helpers.XMLReaderFactory;
049:
050: public class Jaxb2UnmarshallerTest extends TestCase {
051:
052: private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">"
053: + "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
054:
055: private Jaxb2Marshaller unmarshaller;
056:
057: protected void setUp() throws Exception {
058: unmarshaller = new Jaxb2Marshaller();
059: unmarshaller.setContextPath("org.springframework.oxm.jaxb2");
060: unmarshaller.setSchema(new ClassPathResource(
061: "org/springframework/oxm/flight.xsd"));
062: unmarshaller.afterPropertiesSet();
063: }
064:
065: public void testUnmarshalDomSource() throws Exception {
066: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
067: .newDocumentBuilder();
068: Document document = builder.newDocument();
069: Element flightsElement = document.createElementNS(
070: "http://samples.springframework.org/flight",
071: "tns:flights");
072: document.appendChild(flightsElement);
073: Element flightElement = document.createElementNS(
074: "http://samples.springframework.org/flight",
075: "tns:flight");
076: flightsElement.appendChild(flightElement);
077: Element numberElement = document.createElementNS(
078: "http://samples.springframework.org/flight",
079: "tns:number");
080: flightElement.appendChild(numberElement);
081: Text text = document.createTextNode("42");
082: numberElement.appendChild(text);
083: DOMSource source = new DOMSource(document);
084: Object flights = unmarshaller.unmarshal(source);
085: testFlights(flights);
086: }
087:
088: public void testUnmarshalStreamSourceReader() throws Exception {
089: StreamSource source = new StreamSource(new StringReader(
090: INPUT_STRING));
091: Object flights = unmarshaller.unmarshal(source);
092: testFlights(flights);
093: }
094:
095: public void testUnmarshalStreamSourceInputStream() throws Exception {
096: StreamSource source = new StreamSource(
097: new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
098: Object flights = unmarshaller.unmarshal(source);
099: testFlights(flights);
100: }
101:
102: public void testUnmarshalSAXSource() throws Exception {
103: XMLReader reader = XMLReaderFactory.createXMLReader();
104: SAXSource source = new SAXSource(reader, new InputSource(
105: new StringReader(INPUT_STRING)));
106: Object flights = unmarshaller.unmarshal(source);
107: testFlights(flights);
108: }
109:
110: public void testUnmarshalStaxSourceXmlStreamReader()
111: throws Exception {
112: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
113: XMLStreamReader streamReader = inputFactory
114: .createXMLStreamReader(new StringReader(INPUT_STRING));
115: StaxSource source = new StaxSource(streamReader);
116: Object flights = unmarshaller.unmarshal(source);
117: testFlights(flights);
118: }
119:
120: public void testUnmarshalStaxSourceXmlEventReader()
121: throws Exception {
122: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
123: XMLEventReader eventReader = inputFactory
124: .createXMLEventReader(new StringReader(INPUT_STRING));
125: StaxSource source = new StaxSource(eventReader);
126: Object flights = unmarshaller.unmarshal(source);
127: testFlights(flights);
128: }
129:
130: public void testMarshalAttachments() throws Exception {
131: unmarshaller = new Jaxb2Marshaller();
132: unmarshaller
133: .setClassesToBeBound(new Class[] { BinaryObject.class });
134: unmarshaller.setMtomEnabled(true);
135: unmarshaller.afterPropertiesSet();
136: MimeContainer mimeContainer = createMock(MimeContainer.class);
137:
138: Resource logo = new ClassPathResource("spring-ws.png",
139: getClass());
140: DataHandler dataHandler = new DataHandler(new FileDataSource(
141: logo.getFile()));
142:
143: expect(mimeContainer.isXopPackage()).andReturn(true);
144: expect(
145: mimeContainer
146: .getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>"))
147: .andReturn(dataHandler);
148: expect(
149: mimeContainer
150: .getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>"))
151: .andReturn(dataHandler);
152: expect(
153: mimeContainer
154: .getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png"))
155: .andReturn(dataHandler);
156: replay(mimeContainer);
157: String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>"
158: + "<bytes>"
159: + "<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>"
160: + "</bytes>"
161: + "<dataHandler>"
162: + "<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>"
163: + "</dataHandler>"
164: + "<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>"
165: + "</binaryObject>";
166:
167: Source source = new StringSource(content);
168: Object result = unmarshaller.unmarshal(source, mimeContainer);
169: assertTrue("Result is not a BinaryObject",
170: result instanceof BinaryObject);
171: verify(mimeContainer);
172: BinaryObject object = (BinaryObject) result;
173: assertNotNull("bytes property not set", object.getBytes());
174: assertTrue("bytes property not set",
175: object.getBytes().length > 0);
176: assertNotNull("datahandler property not set", object
177: .getSwaDataHandler());
178: }
179:
180: private void testFlights(Object o) {
181: Flights flights = (Flights) o;
182: assertNotNull("Flights is null", flights);
183: assertEquals("Invalid amount of flight elements", 1, flights
184: .getFlight().size());
185: testFlight(flights.getFlight().get(0));
186: }
187:
188: private void testFlight(Object o) {
189: FlightType flight = (FlightType) o;
190: assertNotNull("Flight is null", flight);
191: assertEquals("Number is invalid", 42L, flight.getNumber());
192: }
193:
194: public void testUnmarshalPartialStaxSourceXmlStreamReader()
195: throws Exception {
196: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
197: XMLStreamReader streamReader = inputFactory
198: .createXMLStreamReader(new StringReader(INPUT_STRING));
199: streamReader.nextTag(); // skip to flights
200: streamReader.nextTag(); // skip to flight
201: StaxSource source = new StaxSource(streamReader);
202: JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller
203: .unmarshal(source);
204: FlightType flight = element.getValue();
205: testFlight(flight);
206: }
207:
208: }
|