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.xstream;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.StringReader;
21  import java.util.Properties;
22  
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  import javax.xml.stream.XMLInputFactory;
26  import javax.xml.stream.XMLStreamReader;
27  import javax.xml.transform.dom.DOMSource;
28  import javax.xml.transform.stream.StreamSource;
29  
30  import junit.framework.TestCase;
31  import org.w3c.dom.Document;
32  import org.xml.sax.InputSource;
33  
34  import org.springframework.xml.transform.StaxSource;
35  
36  public class XStreamUnmarshallerTest extends TestCase {
37  
38      protected static final String INPUT_STRING = "<flight><flightNumber>42</flightNumber></flight>";
39  
40      private XStreamMarshaller unmarshaller;
41  
42      protected void setUp() throws Exception {
43          unmarshaller = new XStreamMarshaller();
44          Properties aliases = new Properties();
45          aliases.setProperty("flight", Flight.class.getName());
46          unmarshaller.setAliases(aliases);
47      }
48  
49      private void testFlight(Object o) {
50          assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
51          Flight flight = (Flight) o;
52          assertNotNull("Flight is null", flight);
53          assertEquals("Number is invalid", 42L, flight.getFlightNumber());
54      }
55  
56      public void testUnmarshalDomSource() throws Exception {
57          DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
58          Document document = builder.parse(new InputSource(new StringReader(INPUT_STRING)));
59          DOMSource source = new DOMSource(document);
60          Object flight = unmarshaller.unmarshal(source);
61          testFlight(flight);
62      }
63  
64      public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
65          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
66          XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
67          StaxSource source = new StaxSource(streamReader);
68          Object flights = unmarshaller.unmarshal(source);
69          testFlight(flights);
70      }
71  
72      public void testUnmarshalStreamSourceInputStream() throws Exception {
73          StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
74          Object flights = unmarshaller.unmarshal(source);
75          testFlight(flights);
76      }
77  
78      public void testUnmarshalStreamSourceReader() throws Exception {
79          StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
80          Object flights = unmarshaller.unmarshal(source);
81          testFlight(flights);
82      }
83  }
84