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 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("mapping.xml", CastorMarshaller.class);
31          marshaller.setMappingLocation(mappingLocation);
32          marshaller.afterPropertiesSet();
33          return marshaller;
34      }
35  
36      protected Object createFlights() {
37          Flight flight = new Flight();
38          flight.setNumber(42L);
39          Flights flights = new Flights();
40          flights.addFlight(flight);
41          return flights;
42      }
43  
44      public void testMarshalSaxResult() throws Exception {
45          MockControl handlerControl = MockControl.createControl(ContentHandler.class);
46          ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
47          handlerMock.startDocument();
48          handlerMock.startPrefixMapping("tns", "http://samples.springframework.org/flight");
49          handlerMock.startElement("http://samples.springframework.org/flight", "flights", "tns:flights", null);
50          handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
51          handlerMock.startElement("http://samples.springframework.org/flight", "flight", "tns:flight", null);
52          handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
53          handlerMock.startElement("http://samples.springframework.org/flight", "number", "tns:number", null);
54          handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
55          handlerMock.characters(new char[]{'4', '2'}, 0, 2);
56          handlerControl.setMatcher(MockControl.ARRAY_MATCHER);
57          handlerMock.endElement("http://samples.springframework.org/flight", "number", "tns:number");
58          handlerMock.endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
59          handlerMock.endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
60          handlerMock.endPrefixMapping("tns");
61          handlerMock.endDocument();
62  
63          handlerControl.replay();
64          SAXResult result = new SAXResult(handlerMock);
65          marshaller.marshal(flights, result);
66          handlerControl.verify();
67      }
68  
69      public void testSupports() throws Exception {
70          assertTrue("CastorMarshaller does not support Flights", marshaller.supports(Flights.class));
71          assertTrue("CastorMarshaller does not support Flight", marshaller.supports(Flight.class));
72      }
73  
74  
75  }