Jaxb2UnmarshallerTests.java 5.0 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
P
Phillip Webb 已提交
2
 * Copyright 2002-2013 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.oxm.jaxb;

import java.io.StringReader;
P
Phillip Webb 已提交
20

A
Arjen Poutsma 已提交
21 22 23 24 25 26 27 28
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.bind.JAXBElement;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

A
Arjen Poutsma 已提交
29
import org.junit.Test;
A
Arjen Poutsma 已提交
30 31
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
32
import org.springframework.oxm.AbstractUnmarshallerTests;
33
import org.springframework.oxm.Unmarshaller;
34 35
import org.springframework.oxm.jaxb.test.FlightType;
import org.springframework.oxm.jaxb.test.Flights;
A
Arjen Poutsma 已提交
36
import org.springframework.oxm.mime.MimeContainer;
A
Arjen Poutsma 已提交
37 38
import org.springframework.util.xml.StaxUtils;

P
Phillip Webb 已提交
39 40 41
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;

42
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
A
Arjen Poutsma 已提交
43 44 45 46 47 48

	private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
			"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";

	private Jaxb2Marshaller unmarshaller;

49 50
	@Override
	public Unmarshaller createUnmarshaller() throws Exception {
A
Arjen Poutsma 已提交
51
		unmarshaller = new Jaxb2Marshaller();
52
		unmarshaller.setContextPath("org.springframework.oxm.jaxb.test");
A
Arjen Poutsma 已提交
53 54
		unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
		unmarshaller.afterPropertiesSet();
55
		return unmarshaller;
A
Arjen Poutsma 已提交
56 57 58 59 60
	}

	@Test
	public void marshalAttachments() throws Exception {
		unmarshaller = new Jaxb2Marshaller();
61
		unmarshaller.setClassesToBeBound(BinaryObject.class);
A
Arjen Poutsma 已提交
62 63
		unmarshaller.setMtomEnabled(true);
		unmarshaller.afterPropertiesSet();
P
Phillip Webb 已提交
64
		MimeContainer mimeContainer = mock(MimeContainer.class);
A
Arjen Poutsma 已提交
65 66 67 68

		Resource logo = new ClassPathResource("spring-ws.png", getClass());
		DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));

P
Phillip Webb 已提交
69 70 71 72
		given(mimeContainer.isXopPackage()).willReturn(true);
		given(mimeContainer.getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).willReturn(dataHandler);
		given(mimeContainer.getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).willReturn(dataHandler);
		given(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png")).willReturn(dataHandler);
A
Arjen Poutsma 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
		String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
				"<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
				"</bytes>" + "<dataHandler>" +
				"<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
				"</dataHandler>" +
				"<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>" +
				"</binaryObject>";

		StringReader reader = new StringReader(content);
		Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
		assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
		BinaryObject object = (BinaryObject) result;
		assertNotNull("bytes property not set", object.getBytes());
		assertTrue("bytes property not set", object.getBytes().length > 0);
		assertNotNull("datahandler property not set", object.getSwaDataHandler());
	}

90 91
	@Override
	protected void testFlights(Object o) {
A
Arjen Poutsma 已提交
92 93 94 95 96 97
		Flights flights = (Flights) o;
		assertNotNull("Flights is null", flights);
		assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
		testFlight(flights.getFlight().get(0));
	}

98 99
	@Override
	protected void testFlight(Object o) {
A
Arjen Poutsma 已提交
100 101 102 103 104 105
		FlightType flight = (FlightType) o;
		assertNotNull("Flight is null", flight);
		assertEquals("Number is invalid", 42L, flight.getNumber());
	}

	@Test
106
	@Override
A
Arjen Poutsma 已提交
107 108 109 110 111 112 113 114 115 116
	public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
		XMLInputFactory inputFactory = XMLInputFactory.newInstance();
		XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
		streamReader.nextTag(); // skip to flights
		streamReader.nextTag(); // skip to flight
		Source source = StaxUtils.createStaxSource(streamReader);
		JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source);
		FlightType flight = element.getValue();
		testFlight(flight);
	}
A
Arjen Poutsma 已提交
117 118 119


}