01: /*
02: * Copyright 2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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
27: AbstractUnmarshallerTestCase {
28:
29: protected void testFlights(Object o) {
30: Flights flights = (Flights) o;
31: assertNotNull("Flights is null", flights);
32: assertEquals("Invalid amount of flight elements", 1, flights
33: .getFlightCount());
34: testFlight(flights.getFlight()[0]);
35: }
36:
37: protected void testFlight(Object o) {
38: Flight flight = (Flight) o;
39: assertNotNull("Flight is null", flight);
40: assertEquals("Number is invalid", 42L, flight.getNumber());
41: }
42:
43: protected Unmarshaller createUnmarshaller() throws Exception {
44: CastorMarshaller marshaller = new CastorMarshaller();
45: ClassPathResource mappingLocation = new ClassPathResource(
46: "mapping.xml", CastorMarshaller.class);
47: marshaller.setMappingLocation(mappingLocation);
48: marshaller.afterPropertiesSet();
49: return marshaller;
50: }
51:
52: public void testUnmarshalTargetClass() throws Exception {
53: CastorMarshaller unmarshaller = new CastorMarshaller();
54: unmarshaller.setTargetClass(Flights.class);
55: unmarshaller.afterPropertiesSet();
56: StreamSource source = new StreamSource(
57: new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
58: Object flights = unmarshaller.unmarshal(source);
59: testFlights(flights);
60: }
61:
62: public void testSetBothTargetClassAndMapping() throws IOException {
63: try {
64: CastorMarshaller marshaller = new CastorMarshaller();
65: marshaller.setMappingLocation(new ClassPathResource(
66: "mapping.xml", CastorMarshaller.class));
67: marshaller.setTargetClass(getClass());
68: marshaller.afterPropertiesSet();
69: fail("IllegalArgumentException expected");
70: } catch (IllegalArgumentException ex) {
71: // expected
72: }
73: }
74:
75: }
|