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