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.jaxb;
17  
18  import java.util.Collections;
19  
20  import org.springframework.oxm.Marshaller;
21  import org.springframework.oxm.XmlMappingException;
22  import org.springframework.oxm.jaxb1.FlightType;
23  import org.springframework.oxm.jaxb1.Flights;
24  import org.springframework.oxm.jaxb1.FlightsType;
25  import org.springframework.oxm.jaxb1.impl.FlightTypeImpl;
26  import org.springframework.oxm.jaxb1.impl.FlightsImpl;
27  
28  public class Jaxb1MarshallerTest extends AbstractJaxbMarshallerTestCase {
29  
30      private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb1";
31  
32      protected final Marshaller createMarshaller() throws Exception {
33          Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
34          marshaller.setContextPaths(new String[]{CONTEXT_PATH});
35          marshaller.afterPropertiesSet();
36          return marshaller;
37      }
38  
39      protected Object createFlights() {
40          FlightType flight = new FlightTypeImpl();
41          flight.setNumber(42L);
42          Flights flights = new FlightsImpl();
43          flights.getFlight().add(flight);
44          return flights;
45      }
46  
47      public void testProperties() throws Exception {
48          Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
49          marshaller.setContextPath(CONTEXT_PATH);
50          marshaller.setMarshallerProperties(
51                  Collections.singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE));
52          marshaller.afterPropertiesSet();
53      }
54  
55      public void testNoContextPath() throws Exception {
56          try {
57              Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
58              marshaller.afterPropertiesSet();
59              fail("Should have thrown an IllegalArgumentException");
60          }
61          catch (IllegalArgumentException e) {
62          }
63      }
64  
65      public void testInvalidContextPath() throws Exception {
66          try {
67              Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
68              marshaller.setContextPath("ab");
69              marshaller.afterPropertiesSet();
70              fail("Should have thrown an XmlMappingException");
71          }
72          catch (XmlMappingException ex) {
73          }
74      }
75  
76      public void testSupports() throws Exception {
77          assertTrue("Jaxb1Marshaller does not support Flights", marshaller.supports(Flights.class));
78          assertFalse("Jaxb1Marshaller supports FlightsType", marshaller.supports(FlightsType.class));
79      }
80  
81  
82  }