1   /*
2    * Copyright 2005 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  package org.springframework.oxm.xmlbeans;
17  
18  import java.io.ByteArrayOutputStream;
19  import javax.xml.transform.stream.StreamResult;
20  
21  import org.apache.xmlbeans.XmlObject;
22  import org.springframework.oxm.AbstractMarshallerTestCase;
23  import org.springframework.oxm.Marshaller;
24  import org.springframework.samples.flight.FlightType;
25  import org.springframework.samples.flight.FlightsDocument;
26  import org.springframework.samples.flight.FlightsDocument.Flights;
27  
28  public class XmlBeansMarshallerTest extends AbstractMarshallerTestCase {
29  
30      protected Marshaller createMarshaller() throws Exception {
31          return new XmlBeansMarshaller();
32      }
33  
34      public void testMarshalNonXmlObject() throws Exception {
35          try {
36              marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream()));
37              fail("XmlBeansMarshaller did not throw ClassCastException for non-XmlObject");
38          }
39          catch (ClassCastException e) {
40              // Expected behavior
41          }
42      }
43  
44      protected Object createFlights() {
45          FlightsDocument flightsDocument = FlightsDocument.Factory.newInstance();
46          Flights flights = flightsDocument.addNewFlights();
47          FlightType flightType = flights.addNewFlight();
48          flightType.setNumber(42L);
49          return flightsDocument;
50      }
51  
52      public void testSupports() throws Exception {
53          assertTrue("XmlBeansMarshaller does not support XmlObject", marshaller.supports(XmlObject.class));
54          assertFalse("XmlBeansMarshaller supports other objects", marshaller.supports(Object.class));
55          assertTrue("XmlBeansMarshaller does not support FlightsDocument", marshaller.supports(FlightsDocument.class));
56          assertTrue("XmlBeansMarshaller does not support Flights", marshaller.supports(Flights.class));
57          assertTrue("XmlBeansMarshaller does not support FlightType", marshaller.supports(FlightType.class));
58      }
59  }