Jaxb2EncoderTests.java 3.0 KB
Newer Older
S
Sebastien Deleuze 已提交
1
/*
2
 * Copyright 2002-2016 the original author or authors.
S
Sebastien Deleuze 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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.
 */

17
package org.springframework.core.codec.support;
S
Sebastien Deleuze 已提交
18

A
Arjen Poutsma 已提交
19
import java.io.IOException;
S
Sebastien Deleuze 已提交
20 21
import java.nio.charset.StandardCharsets;

22
import org.junit.Before;
S
Sebastien Deleuze 已提交
23
import org.junit.Test;
A
Arjen Poutsma 已提交
24
import org.xml.sax.SAXException;
25
import reactor.core.publisher.Flux;
26
import reactor.core.test.TestSubscriber;
S
Sebastien Deleuze 已提交
27

A
Arjen Poutsma 已提交
28
import org.springframework.core.ResolvableType;
29 30
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.DataBuffer;
A
Arjen Poutsma 已提交
31
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
32
import org.springframework.core.io.buffer.support.DataBufferUtils;
S
Sebastien Deleuze 已提交
33 34
import org.springframework.http.MediaType;

A
Arjen Poutsma 已提交
35 36
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.custommonkey.xmlunit.XMLAssert.fail;
37 38
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
39

S
Sebastien Deleuze 已提交
40 41
/**
 * @author Sebastien Deleuze
A
Arjen Poutsma 已提交
42
 * @author Arjen Poutsma
S
Sebastien Deleuze 已提交
43
 */
44
public class Jaxb2EncoderTests extends AbstractDataBufferAllocatingTestCase {
S
Sebastien Deleuze 已提交
45

46 47 48 49
	private Jaxb2Encoder encoder;

	@Before
	public void createEncoder() {
50
		this.encoder = new Jaxb2Encoder();
51
	}
S
Sebastien Deleuze 已提交
52 53 54

	@Test
	public void canEncode() {
55
		assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
A
Arjen Poutsma 已提交
56
				MediaType.APPLICATION_XML));
57
		assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
A
Arjen Poutsma 已提交
58
				MediaType.TEXT_XML));
59
		assertFalse(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
A
Arjen Poutsma 已提交
60 61
				MediaType.APPLICATION_JSON));

62
		assertTrue(this.encoder.canEncode(
A
Arjen Poutsma 已提交
63 64 65
				ResolvableType.forClass(Jaxb2DecoderTests.TypePojo.class),
				MediaType.APPLICATION_XML));

66
		assertFalse(this.encoder.canEncode(ResolvableType.forClass(getClass()),
A
Arjen Poutsma 已提交
67
				MediaType.APPLICATION_XML));
S
Sebastien Deleuze 已提交
68 69 70
	}

	@Test
71
	public void encode() {
72
		Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
73 74 75 76 77
		Flux<DataBuffer> output = this.encoder
				.encode(source, this.allocator, ResolvableType.forClass(Pojo.class),
						MediaType.APPLICATION_XML);
		TestSubscriber<DataBuffer> testSubscriber = new TestSubscriber<>();
		testSubscriber.bindTo(output).assertValuesWith(dataBuffer -> {
A
Arjen Poutsma 已提交
78
			try {
79 80
				String s = DataBufferTestUtils
						.dumpString(dataBuffer, StandardCharsets.UTF_8);
A
Arjen Poutsma 已提交
81 82 83 84 85
				assertXMLEqual("<pojo><bar>barbar</bar><foo>foofoo</foo></pojo>", s);
			}
			catch (SAXException | IOException e) {
				fail(e.getMessage());
			}
86 87 88
			finally {
				DataBufferUtils.release(dataBuffer);
			}
A
Arjen Poutsma 已提交
89
		});
S
Sebastien Deleuze 已提交
90 91 92
	}

}