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 javax.xml.transform.sax.SAXResult;
19:
20: import org.easymock.MockControl;
21: import org.springframework.core.io.ClassPathResource;
22: import org.springframework.oxm.AbstractMarshallerTestCase;
23: import org.springframework.oxm.Marshaller;
24: import org.xml.sax.ContentHandler;
25:
26: public class CastorMarshallerTest extends AbstractMarshallerTestCase {
27:
28: protected Marshaller createMarshaller() throws Exception {
29: CastorMarshaller marshaller = new CastorMarshaller();
30: ClassPathResource mappingLocation = new ClassPathResource(
31: "mapping.xml", CastorMarshaller.class);
32: marshaller.setMappingLocation(mappingLocation);
33: marshaller.afterPropertiesSet();
34: return marshaller;
35: }
36:
37: protected Object createFlights() {
38: Flight flight = new Flight();
39: flight.setNumber(42L);
40: Flights flights = new Flights();
41: flights.addFlight(flight);
42: return flights;
43: }
44:
45: public void testMarshalSaxResult() throws Exception {
46: MockControl handlerControl = MockControl
47: .createControl(ContentHandler.class);
48: ContentHandler handlerMock = (ContentHandler) handlerControl
49: .getMock();
50: handlerMock.startDocument();
51: handlerMock.startPrefixMapping("tns",
52: "http://samples.springframework.org/flight");
53: handlerMock.startElement(
54: "http://samples.springframework.org/flight", "flights",
55: "tns:flights", null);
56: handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
57: handlerMock.startElement(
58: "http://samples.springframework.org/flight", "flight",
59: "tns:flight", null);
60: handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
61: handlerMock.startElement(
62: "http://samples.springframework.org/flight", "number",
63: "tns:number", null);
64: handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
65: handlerMock.characters(new char[] { '4', '2' }, 0, 2);
66: handlerControl.setMatcher(MockControl.ARRAY_MATCHER);
67: handlerMock.endElement(
68: "http://samples.springframework.org/flight", "number",
69: "tns:number");
70: handlerMock.endElement(
71: "http://samples.springframework.org/flight", "flight",
72: "tns:flight");
73: handlerMock.endElement(
74: "http://samples.springframework.org/flight", "flights",
75: "tns:flights");
76: handlerMock.endPrefixMapping("tns");
77: handlerMock.endDocument();
78:
79: handlerControl.replay();
80: SAXResult result = new SAXResult(handlerMock);
81: marshaller.marshal(flights, result);
82: handlerControl.verify();
83: }
84:
85: }
|