1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.oxm.jaxb;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.StringReader;
21 import javax.activation.DataHandler;
22 import javax.activation.FileDataSource;
23 import javax.xml.bind.JAXBElement;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.stream.XMLEventReader;
27 import javax.xml.stream.XMLInputFactory;
28 import javax.xml.stream.XMLStreamReader;
29 import javax.xml.transform.Source;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.sax.SAXSource;
32 import javax.xml.transform.stream.StreamSource;
33
34 import junit.framework.TestCase;
35 import static org.easymock.EasyMock.*;
36 import org.springframework.core.io.ClassPathResource;
37 import org.springframework.core.io.Resource;
38 import org.springframework.oxm.jaxb2.FlightType;
39 import org.springframework.oxm.jaxb2.Flights;
40 import org.springframework.oxm.mime.MimeContainer;
41 import org.springframework.xml.transform.StaxSource;
42 import org.springframework.xml.transform.StringSource;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
45 import org.w3c.dom.Text;
46 import org.xml.sax.InputSource;
47 import org.xml.sax.XMLReader;
48 import org.xml.sax.helpers.XMLReaderFactory;
49
50 public class Jaxb2UnmarshallerTest extends TestCase {
51
52 private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
53 "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
54
55 private Jaxb2Marshaller unmarshaller;
56
57 protected void setUp() throws Exception {
58 unmarshaller = new Jaxb2Marshaller();
59 unmarshaller.setContextPath("org.springframework.oxm.jaxb2");
60 unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
61 unmarshaller.afterPropertiesSet();
62 }
63
64 public void testUnmarshalDomSource() throws Exception {
65 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
66 Document document = builder.newDocument();
67 Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
68 document.appendChild(flightsElement);
69 Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
70 flightsElement.appendChild(flightElement);
71 Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
72 flightElement.appendChild(numberElement);
73 Text text = document.createTextNode("42");
74 numberElement.appendChild(text);
75 DOMSource source = new DOMSource(document);
76 Object flights = unmarshaller.unmarshal(source);
77 testFlights(flights);
78 }
79
80 public void testUnmarshalStreamSourceReader() throws Exception {
81 StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
82 Object flights = unmarshaller.unmarshal(source);
83 testFlights(flights);
84 }
85
86 public void testUnmarshalStreamSourceInputStream() throws Exception {
87 StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
88 Object flights = unmarshaller.unmarshal(source);
89 testFlights(flights);
90 }
91
92 public void testUnmarshalSAXSource() throws Exception {
93 XMLReader reader = XMLReaderFactory.createXMLReader();
94 SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
95 Object flights = unmarshaller.unmarshal(source);
96 testFlights(flights);
97 }
98
99 public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
100 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
101 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
102 StaxSource source = new StaxSource(streamReader);
103 Object flights = unmarshaller.unmarshal(source);
104 testFlights(flights);
105 }
106
107 public void testUnmarshalStaxSourceXmlEventReader() throws Exception {
108 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
109 XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
110 StaxSource source = new StaxSource(eventReader);
111 Object flights = unmarshaller.unmarshal(source);
112 testFlights(flights);
113 }
114
115 public void testMarshalAttachments() throws Exception {
116 unmarshaller = new Jaxb2Marshaller();
117 unmarshaller.setClassesToBeBound(new Class[]{BinaryObject.class});
118 unmarshaller.setMtomEnabled(true);
119 unmarshaller.afterPropertiesSet();
120 MimeContainer mimeContainer = createMock(MimeContainer.class);
121
122 Resource logo = new ClassPathResource("spring-ws.png", getClass());
123 DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
124
125 expect(mimeContainer.isXopPackage()).andReturn(true);
126 expect(mimeContainer.getAttachment(
127 "<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>"))
128 .andReturn(dataHandler);
129 expect(mimeContainer.getAttachment(
130 "<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>"))
131 .andReturn(dataHandler);
132 expect(mimeContainer.getAttachment("[email protected]"))
133 .andReturn(dataHandler);
134 replay(mimeContainer);
135 String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
136 "<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
137 "</bytes>" + "<dataHandler>" +
138 "<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
139 "</dataHandler>" +
140 "<swaDataHandler>[email protected]</swaDataHandler>" +
141 "</binaryObject>";
142
143 Source source = new StringSource(content);
144 Object result = unmarshaller.unmarshal(source, mimeContainer);
145 assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
146 verify(mimeContainer);
147 BinaryObject object = (BinaryObject) result;
148 assertNotNull("bytes property not set", object.getBytes());
149 assertTrue("bytes property not set", object.getBytes().length > 0);
150 assertNotNull("datahandler property not set", object.getSwaDataHandler());
151 }
152
153 private void testFlights(Object o) {
154 Flights flights = (Flights) o;
155 assertNotNull("Flights is null", flights);
156 assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
157 testFlight(flights.getFlight().get(0));
158 }
159
160 private void testFlight(Object o) {
161 FlightType flight = (FlightType) o;
162 assertNotNull("Flight is null", flight);
163 assertEquals("Number is invalid", 42L, flight.getNumber());
164 }
165
166 public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
167 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
168 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
169 streamReader.nextTag();
170 streamReader.nextTag();
171 StaxSource source = new StaxSource(streamReader);
172 JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source);
173 FlightType flight = element.getValue();
174 testFlight(flight);
175 }
176
177
178 }