001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.oxm.jaxb;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.StringWriter;
021: import java.util.Collections;
022: import javax.activation.DataHandler;
023: import javax.activation.FileDataSource;
024: import javax.xml.bind.JAXBElement;
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.stream.XMLEventWriter;
028: import javax.xml.stream.XMLOutputFactory;
029: import javax.xml.stream.XMLStreamWriter;
030: import javax.xml.transform.Result;
031: import javax.xml.transform.dom.DOMResult;
032: import javax.xml.transform.sax.SAXResult;
033: import javax.xml.transform.stream.StreamResult;
034:
035: import org.custommonkey.xmlunit.XMLTestCase;
036: import static org.easymock.EasyMock.*;
037: import org.springframework.core.io.ClassPathResource;
038: import org.springframework.core.io.Resource;
039: import org.springframework.oxm.XmlMappingException;
040: import org.springframework.oxm.jaxb2.FlightType;
041: import org.springframework.oxm.jaxb2.Flights;
042: import org.springframework.oxm.mime.MimeContainer;
043: import org.springframework.util.FileCopyUtils;
044: import org.springframework.xml.transform.StaxResult;
045: import org.springframework.xml.transform.StringResult;
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048: import org.w3c.dom.Text;
049: import org.xml.sax.Attributes;
050: import org.xml.sax.ContentHandler;
051: import org.xml.sax.Locator;
052:
053: public class Jaxb2MarshallerTest extends XMLTestCase {
054:
055: private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb2";
056:
057: private static final String EXPECTED_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">"
058: + "<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
059:
060: private Jaxb2Marshaller marshaller;
061:
062: private Flights flights;
063:
064: protected void setUp() throws Exception {
065: marshaller = new Jaxb2Marshaller();
066: marshaller.setContextPath(CONTEXT_PATH);
067: marshaller.afterPropertiesSet();
068: FlightType flight = new FlightType();
069: flight.setNumber(42L);
070: flights = new Flights();
071: flights.getFlight().add(flight);
072: }
073:
074: public void testMarshalDOMResult() throws Exception {
075: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
076: .newInstance();
077: DocumentBuilder builder = documentBuilderFactory
078: .newDocumentBuilder();
079: Document document = builder.newDocument();
080: DOMResult domResult = new DOMResult(document);
081: marshaller.marshal(flights, domResult);
082: Document expected = builder.newDocument();
083: Element flightsElement = expected.createElementNS(
084: "http://samples.springframework.org/flight",
085: "tns:flights");
086: expected.appendChild(flightsElement);
087: Element flightElement = expected.createElementNS(
088: "http://samples.springframework.org/flight",
089: "tns:flight");
090: flightsElement.appendChild(flightElement);
091: Element numberElement = expected.createElementNS(
092: "http://samples.springframework.org/flight",
093: "tns:number");
094: flightElement.appendChild(numberElement);
095: Text text = expected.createTextNode("42");
096: numberElement.appendChild(text);
097: assertXMLEqual("Marshaller writes invalid DOMResult", expected,
098: document);
099: }
100:
101: public void testMarshalStreamResultWriter() throws Exception {
102: StringWriter writer = new StringWriter();
103: StreamResult result = new StreamResult(writer);
104: marshaller.marshal(flights, result);
105: assertXMLEqual("Marshaller writes invalid StreamResult",
106: EXPECTED_STRING, writer.toString());
107: }
108:
109: public void testMarshalStreamResultOutputStream() throws Exception {
110: ByteArrayOutputStream os = new ByteArrayOutputStream();
111: StreamResult result = new StreamResult(os);
112: marshaller.marshal(flights, result);
113: assertXMLEqual("Marshaller writes invalid StreamResult",
114: EXPECTED_STRING, new String(os.toByteArray(), "UTF-8"));
115: }
116:
117: public void testMarshalStaxResultXMLStreamWriter() throws Exception {
118: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
119: StringWriter writer = new StringWriter();
120: XMLStreamWriter streamWriter = outputFactory
121: .createXMLStreamWriter(writer);
122: StaxResult result = new StaxResult(streamWriter);
123: marshaller.marshal(flights, result);
124: assertXMLEqual("Marshaller writes invalid StreamResult",
125: EXPECTED_STRING, writer.toString());
126: }
127:
128: public void testMarshalStaxResultXMLEventWriter() throws Exception {
129: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
130: StringWriter writer = new StringWriter();
131: XMLEventWriter eventWriter = outputFactory
132: .createXMLEventWriter(writer);
133: StaxResult result = new StaxResult(eventWriter);
134: marshaller.marshal(flights, result);
135: assertXMLEqual("Marshaller writes invalid StreamResult",
136: EXPECTED_STRING, writer.toString());
137: }
138:
139: public void testProperties() throws Exception {
140: Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
141: marshaller.setContextPath(CONTEXT_PATH);
142: marshaller.setMarshallerProperties(Collections.singletonMap(
143: javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT,
144: Boolean.TRUE));
145: marshaller.afterPropertiesSet();
146: }
147:
148: public void testNoContextPathOrClassesToBeBound() throws Exception {
149: try {
150: Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
151: marshaller.afterPropertiesSet();
152: fail("Should have thrown an IllegalArgumentException");
153: } catch (IllegalArgumentException e) {
154: }
155: }
156:
157: public void testInvalidContextPath() throws Exception {
158: try {
159: Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
160: marshaller.setContextPath("ab");
161: marshaller.afterPropertiesSet();
162: fail("Should have thrown an XmlMappingException");
163: } catch (XmlMappingException ex) {
164: }
165: }
166:
167: public void testMarshalInvalidClass() throws Exception {
168: Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
169: marshaller
170: .setClassesToBeBound(new Class[] { FlightType.class });
171: marshaller.afterPropertiesSet();
172: Result result = new StreamResult(new StringWriter());
173: Flights flights = new Flights();
174: try {
175: marshaller.marshal(flights, result);
176: fail("Should have thrown an MarshallingFailureException");
177: } catch (XmlMappingException ex) {
178: // expected
179: }
180: }
181:
182: public void testMarshalSaxResult() throws Exception {
183: ContentHandler handlerMock = createStrictMock(ContentHandler.class);
184: handlerMock.setDocumentLocator(isA(Locator.class));
185: handlerMock.startDocument();
186: handlerMock.startPrefixMapping("",
187: "http://samples.springframework.org/flight");
188: handlerMock.startElement(
189: eq("http://samples.springframework.org/flight"),
190: eq("flights"), eq("flights"), isA(Attributes.class));
191: handlerMock.startElement(
192: eq("http://samples.springframework.org/flight"),
193: eq("flight"), eq("flight"), isA(Attributes.class));
194: handlerMock.startElement(
195: eq("http://samples.springframework.org/flight"),
196: eq("number"), eq("number"), isA(Attributes.class));
197: handlerMock.characters(isA(char[].class), eq(0), eq(2));
198: handlerMock.endElement(
199: "http://samples.springframework.org/flight", "number",
200: "number");
201: handlerMock.endElement(
202: "http://samples.springframework.org/flight", "flight",
203: "flight");
204: handlerMock.endElement(
205: "http://samples.springframework.org/flight", "flights",
206: "flights");
207: handlerMock.endPrefixMapping("");
208: handlerMock.endDocument();
209: replay(handlerMock);
210:
211: SAXResult result = new SAXResult(handlerMock);
212: marshaller.marshal(flights, result);
213: verify(handlerMock);
214: }
215:
216: public void testSupports() throws Exception {
217: assertTrue("Jaxb2Marshaller does not support Flights",
218: marshaller.supports(Flights.class));
219: assertTrue("Jaxb2Marshaller does not support JAXBElement",
220: marshaller.supports(JAXBElement.class));
221: }
222:
223: public void testMarshalAttachments() throws Exception {
224: marshaller = new Jaxb2Marshaller();
225: marshaller
226: .setClassesToBeBound(new Class[] { BinaryObject.class });
227: marshaller.setMtomEnabled(true);
228: marshaller.afterPropertiesSet();
229: MimeContainer mimeContainer = createMock(MimeContainer.class);
230:
231: Resource logo = new ClassPathResource("spring-ws.png",
232: getClass());
233: DataHandler dataHandler = new DataHandler(new FileDataSource(
234: logo.getFile()));
235:
236: expect(mimeContainer.convertToXopPackage()).andReturn(true);
237: mimeContainer.addAttachment(isA(String.class),
238: isA(DataHandler.class));
239: expectLastCall().times(3);
240:
241: replay(mimeContainer);
242: byte[] bytes = FileCopyUtils.copyToByteArray(logo
243: .getInputStream());
244: BinaryObject object = new BinaryObject(bytes, dataHandler);
245: Result result = new StringResult();
246: marshaller.marshal(object, result, mimeContainer);
247: verify(mimeContainer);
248: assertTrue("No XML written", result.toString().length() > 0);
249: }
250: }
|