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.castor;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.IOException;
20  import javax.xml.transform.stream.StreamSource;
21  
22  import org.springframework.core.io.ClassPathResource;
23  import org.springframework.oxm.AbstractUnmarshallerTestCase;
24  import org.springframework.oxm.Unmarshaller;
25  
26  public class CastorUnmarshallerTest extends AbstractUnmarshallerTestCase {
27  
28      protected void testFlights(Object o) {
29          Flights flights = (Flights) o;
30          assertNotNull("Flights is null", flights);
31          assertEquals("Invalid amount of flight elements", 1, flights.getFlightCount());
32          testFlight(flights.getFlight()[0]);
33      }
34  
35      protected void testFlight(Object o) {
36          Flight flight = (Flight) o;
37          assertNotNull("Flight is null", flight);
38          assertEquals("Number is invalid", 42L, flight.getNumber());
39      }
40  
41      protected Unmarshaller createUnmarshaller() throws Exception {
42          CastorMarshaller marshaller = new CastorMarshaller();
43          ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
44          marshaller.setMappingLocation(mappingLocation);
45          marshaller.afterPropertiesSet();
46          return marshaller;
47      }
48  
49      public void testUnmarshalTargetClass() throws Exception {
50          CastorMarshaller unmarshaller = new CastorMarshaller();
51          unmarshaller.setTargetClass(Flights.class);
52          unmarshaller.afterPropertiesSet();
53          StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
54          Object flights = unmarshaller.unmarshal(source);
55          testFlights(flights);
56      }
57  
58      public void testSetBothTargetClassAndMapping() throws IOException {
59          try {
60              CastorMarshaller marshaller = new CastorMarshaller();
61              marshaller.setMappingLocation(new ClassPathResource("mapping.xml", CastorMarshaller.class));
62              marshaller.setTargetClass(getClass());
63              marshaller.afterPropertiesSet();
64              fail("IllegalArgumentException expected");
65          }
66          catch (IllegalArgumentException ex) {
67              // expected
68          }
69      }
70  
71  }