Jaxb2UnmarshallerTests.java 6.0 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
S
Sam Brannen 已提交
2
 * Copyright 2002-2015 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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;

19 20
import java.io.File;
import java.io.IOException;
A
Arjen Poutsma 已提交
21
import java.io.StringReader;
S
Sam Brannen 已提交
22

A
Arjen Poutsma 已提交
23 24 25 26 27 28 29 30
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 已提交
31
import org.junit.Test;
32

A
Arjen Poutsma 已提交
33 34
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
35
import org.springframework.oxm.AbstractUnmarshallerTests;
36 37
import org.springframework.oxm.jaxb.test.FlightType;
import org.springframework.oxm.jaxb.test.Flights;
A
Arjen Poutsma 已提交
38
import org.springframework.oxm.mime.MimeContainer;
A
Arjen Poutsma 已提交
39 40
import org.springframework.util.xml.StaxUtils;

41 42 43
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;

44 45 46
/**
 * @author Arjen Poutsma
 * @author Biju Kunjummen
S
Sam Brannen 已提交
47
 * @author Sam Brannen
48
 */
S
Sam Brannen 已提交
49
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marshaller> {
A
Arjen Poutsma 已提交
50 51 52 53

	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>";

54
	@Override
S
Sam Brannen 已提交
55 56
	protected Jaxb2Marshaller createUnmarshaller() throws Exception {
		Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
57
		unmarshaller.setContextPath("org.springframework.oxm.jaxb.test");
A
Arjen Poutsma 已提交
58 59
		unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
		unmarshaller.afterPropertiesSet();
60
		return unmarshaller;
A
Arjen Poutsma 已提交
61 62
	}

S
Sam Brannen 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	@Override
	protected void testFlights(Object o) {
		Flights flights = (Flights) o;
		assertNotNull("Flights is null", flights);
		assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
		testFlight(flights.getFlight().get(0));
	}

	@Override
	protected void testFlight(Object o) {
		FlightType flight = (FlightType) o;
		assertNotNull("Flight is null", flight);
		assertEquals("Number is invalid", 42L, flight.getNumber());
	}

A
Arjen Poutsma 已提交
78 79 80
	@Test
	public void marshalAttachments() throws Exception {
		unmarshaller = new Jaxb2Marshaller();
81
		unmarshaller.setClassesToBeBound(BinaryObject.class);
A
Arjen Poutsma 已提交
82 83
		unmarshaller.setMtomEnabled(true);
		unmarshaller.afterPropertiesSet();
P
Phillip Webb 已提交
84
		MimeContainer mimeContainer = mock(MimeContainer.class);
A
Arjen Poutsma 已提交
85 86 87 88

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

P
Phillip Webb 已提交
89 90 91 92
		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 已提交
93
		String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
S
Spring Operator 已提交
94
				"<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='https://www.w3.org/2004/08/xop/include'/>" +
A
Arjen Poutsma 已提交
95
				"</bytes>" + "<dataHandler>" +
S
Spring Operator 已提交
96
				"<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='https://www.w3.org/2004/08/xop/include'/>" +
A
Arjen Poutsma 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
				"</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());
	}

	@Test
111
	@Override
112
	@SuppressWarnings("unchecked")
A
Arjen Poutsma 已提交
113 114 115 116 117 118 119 120 121 122
	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 已提交
123

124 125 126 127 128 129 130 131 132 133 134 135 136
	@Test
	@SuppressWarnings("unchecked")
	public void unmarshalAnXmlReferingToAWrappedXmlElementDecl() throws Exception {
		// SPR-10714
		unmarshaller = new Jaxb2Marshaller();
		unmarshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" });
		unmarshaller.afterPropertiesSet();
		Source source = new StreamSource(new StringReader(
				"<brand-airplane><name>test</name></brand-airplane>"));
		JAXBElement<Airplane> airplane = (JAXBElement<Airplane>) unmarshaller.unmarshal(source);
		assertEquals("Unmarshalling via explicit @XmlRegistry tag should return correct type",
				"test", airplane.getValue().getName());
	}
A
Arjen Poutsma 已提交
137

138 139 140 141 142 143 144 145 146
	@Test
	public void unmarshalFile() throws IOException {
		Resource resource = new ClassPathResource("jaxb2.xml", getClass());
		File file = resource.getFile();

		Flights f = (Flights) unmarshaller.unmarshal(new StreamSource(file));
		testFlights(f);
	}

A
Arjen Poutsma 已提交
147
}