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.xmlbeans;
17:
18: import java.io.StringReader;
19: import javax.xml.namespace.QName;
20: import javax.xml.stream.XMLInputFactory;
21: import javax.xml.stream.XMLStreamReader;
22:
23: import org.springframework.oxm.AbstractUnmarshallerTestCase;
24: import org.springframework.oxm.Unmarshaller;
25: import org.springframework.samples.flight.FlightDocument;
26: import org.springframework.samples.flight.FlightType;
27: import org.springframework.samples.flight.FlightsDocument;
28: import org.springframework.samples.flight.FlightsDocument.Flights;
29: import org.springframework.xml.transform.StaxSource;
30: import org.springframework.xml.transform.StringSource;
31:
32: public class XmlBeansUnmarshallerTest extends
33: AbstractUnmarshallerTestCase {
34:
35: protected Unmarshaller createUnmarshaller() throws Exception {
36: return new XmlBeansMarshaller();
37: }
38:
39: protected void testFlights(Object o) {
40: FlightsDocument flightsDocument = (FlightsDocument) o;
41: assertNotNull("FlightsDocument is null", flightsDocument);
42: Flights flights = flightsDocument.getFlights();
43: assertEquals("Invalid amount of flight elements", 1, flights
44: .sizeOfFlightArray());
45: testFlight(flights.getFlightArray(0));
46: }
47:
48: protected void testFlight(Object o) {
49: FlightType flight = null;
50: if (o instanceof FlightType) {
51: flight = (FlightType) o;
52: } else if (o instanceof FlightDocument) {
53: FlightDocument flightDocument = (FlightDocument) o;
54: flight = flightDocument.getFlight();
55: }
56: assertNotNull("Flight is null", flight);
57: assertEquals("Number is invalid", 42L, flight.getNumber());
58: }
59:
60: public void testUnmarshalPartialStaxSourceXmlStreamReader()
61: throws Exception {
62: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
63: XMLStreamReader streamReader = inputFactory
64: .createXMLStreamReader(new StringReader(INPUT_STRING));
65: streamReader.nextTag(); // skip to flights
66: assertEquals("Invalid element",
67: new QName("http://samples.springframework.org/flight",
68: "flights"), streamReader.getName());
69: streamReader.nextTag(); // skip to flight
70: assertEquals("Invalid element", new QName(
71: "http://samples.springframework.org/flight", "flight"),
72: streamReader.getName());
73: StaxSource source = new StaxSource(streamReader);
74: Object flight = unmarshaller.unmarshal(source);
75: testFlight(flight);
76: }
77:
78: public void testValidate() throws Exception {
79: ((XmlBeansMarshaller) unmarshaller).setValidating(true);
80:
81: try {
82: String invalidInput = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">"
83: + "<tns:flight><tns:number>abc</tns:number></tns:flight></tns:flights>";
84: unmarshaller.unmarshal(new StringSource(invalidInput));
85: fail("Expected a XmlBeansValidationFailureException");
86: } catch (XmlBeansValidationFailureException ex) {
87: // expected
88: }
89:
90: }
91:
92: }
|