1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
68 }
69 }
70
71 }