Add and use AbstractEncoderTestCase
Introduce new base test case for encoder tests, and use it. Issue: SPR-17449
This commit is contained in:
@@ -20,38 +20,64 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import static java.util.Collections.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.http.MediaType.*;
|
||||
import static org.springframework.http.codec.json.Jackson2JsonEncoder.*;
|
||||
import static org.springframework.http.codec.json.JacksonViewBean.*;
|
||||
import org.springframework.util.MimeType;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.codec.AbstractEncoderTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
import org.springframework.http.codec.ServerSentEvent;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
|
||||
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
|
||||
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_XML;
|
||||
import static org.springframework.http.codec.json.Jackson2JsonEncoder.JSON_VIEW_HINT;
|
||||
import static org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1;
|
||||
import static org.springframework.http.codec.json.JacksonViewBean.MyJacksonView3;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class Jackson2JsonEncoderTests extends AbstractEncoderTestCase<Object, Jackson2JsonEncoder> {
|
||||
|
||||
private final Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
|
||||
|
||||
public Jackson2JsonEncoderTests() {
|
||||
super(new Jackson2JsonEncoder(), ResolvableType.forClass(Pojo.class),
|
||||
APPLICATION_STREAM_JSON, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Flux<Object> input() {
|
||||
return Flux.just(new Pojo("foo", "bar"),
|
||||
new Pojo("foofoo", "barbar"),
|
||||
new Pojo("foofoofoo", "barbarbar"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<Consumer<DataBuffer>> outputConsumers() {
|
||||
return Stream.<Consumer<DataBuffer>>builder()
|
||||
.add(resultConsumer("{\"foo\":\"foo\",\"bar\":\"bar\"}\n"))
|
||||
.add(resultConsumer("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n"))
|
||||
.add(resultConsumer("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
@@ -94,7 +120,7 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() throws Exception {
|
||||
public void encodeNonStream() {
|
||||
Flux<Pojo> source = Flux.just(
|
||||
new Pojo("foo", "bar"),
|
||||
new Pojo("foofoo", "barbar"),
|
||||
@@ -104,43 +130,29 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(stringConsumer("[" +
|
||||
.consumeNextWith(resultConsumer("[" +
|
||||
"{\"foo\":\"foo\",\"bar\":\"bar\"}," +
|
||||
"{\"foo\":\"foofoo\",\"bar\":\"barbar\"}," +
|
||||
"{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]"))
|
||||
"{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeWithType() throws Exception {
|
||||
public void encodeWithType() {
|
||||
Flux<ParentClass> source = Flux.just(new Foo(), new Bar());
|
||||
ResolvableType type = ResolvableType.forClass(ParentClass.class);
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(stringConsumer("[{\"type\":\"foo\"},{\"type\":\"bar\"}]"))
|
||||
.consumeNextWith(resultConsumer("[{\"type\":\"foo\"},{\"type\":\"bar\"}]")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAsStream() throws Exception {
|
||||
Flux<Pojo> source = Flux.just(
|
||||
new Pojo("foo", "bar"),
|
||||
new Pojo("foofoo", "barbar"),
|
||||
new Pojo("foofoofoo", "barbarbar")
|
||||
);
|
||||
ResolvableType type = ResolvableType.forClass(Pojo.class);
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, APPLICATION_STREAM_JSON, emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(stringConsumer("{\"foo\":\"foo\",\"bar\":\"bar\"}\n"))
|
||||
.consumeNextWith(stringConsumer("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n"))
|
||||
.consumeNextWith(stringConsumer("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n"))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // SPR-15727
|
||||
public void encodeAsStreamWithCustomStreamingType() throws Exception {
|
||||
public void encodeAsStreamWithCustomStreamingType() {
|
||||
MediaType fooMediaType = new MediaType("application", "foo");
|
||||
MediaType barMediaType = new MediaType("application", "bar");
|
||||
this.encoder.setStreamingMediaTypes(Arrays.asList(fooMediaType, barMediaType));
|
||||
@@ -153,14 +165,17 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, barMediaType, emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(stringConsumer("{\"foo\":\"foo\",\"bar\":\"bar\"}\n"))
|
||||
.consumeNextWith(stringConsumer("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n"))
|
||||
.consumeNextWith(stringConsumer("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n"))
|
||||
.consumeNextWith(resultConsumer("{\"foo\":\"foo\",\"bar\":\"bar\"}\n")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.consumeNextWith(resultConsumer("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.consumeNextWith(resultConsumer("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldLevelJsonView() throws Exception {
|
||||
public void fieldLevelJsonView() {
|
||||
JacksonViewBean bean = new JacksonViewBean();
|
||||
bean.setWithView1("with");
|
||||
bean.setWithView2("with");
|
||||
@@ -171,12 +186,13 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null, hints);
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(stringConsumer("{\"withView1\":\"with\"}"))
|
||||
.consumeNextWith(resultConsumer("{\"withView1\":\"with\"}")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classLevelJsonView() throws Exception {
|
||||
public void classLevelJsonView() {
|
||||
JacksonViewBean bean = new JacksonViewBean();
|
||||
bean.setWithView1("with");
|
||||
bean.setWithView2("with");
|
||||
@@ -187,12 +203,12 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null, hints);
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(stringConsumer("{\"withoutView\":\"without\"}"))
|
||||
.consumeNextWith(resultConsumer("{\"withoutView\":\"without\"}")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
|
||||
private static class ParentClass {
|
||||
}
|
||||
|
||||
|
||||
@@ -19,14 +19,17 @@ package org.springframework.http.codec.json;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.codec.AbstractEncoderTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
@@ -35,9 +38,7 @@ import org.springframework.http.codec.ServerSentEvent;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.http.MediaType.APPLICATION_XML;
|
||||
|
||||
/**
|
||||
@@ -45,13 +46,55 @@ import static org.springframework.http.MediaType.APPLICATION_XML;
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class Jackson2SmileEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class Jackson2SmileEncoderTests extends AbstractEncoderTestCase<Object, Jackson2SmileEncoder> {
|
||||
|
||||
private final static MimeType SMILE_MIME_TYPE = new MimeType("application", "x-jackson-smile");
|
||||
private final static MimeType STREAM_SMILE_MIME_TYPE = new MimeType("application", "stream+x-jackson-smile");
|
||||
|
||||
private final Jackson2SmileEncoder encoder = new Jackson2SmileEncoder();
|
||||
|
||||
private final ObjectMapper mapper = Jackson2ObjectMapperBuilder.smile().build();
|
||||
|
||||
private Pojo pojo1 = new Pojo("foo", "bar");
|
||||
|
||||
private Pojo pojo2 = new Pojo("foofoo", "barbar");
|
||||
|
||||
private Pojo pojo3 = new Pojo("foofoofoo", "barbarbar");
|
||||
|
||||
public Jackson2SmileEncoderTests() {
|
||||
super(new Jackson2SmileEncoder(), ResolvableType.forClass(Pojo.class),
|
||||
STREAM_SMILE_MIME_TYPE, null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Flux<Object> input() {
|
||||
return Flux.just(this.pojo1, this.pojo2, this.pojo3);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<Consumer<DataBuffer>> outputConsumers() {
|
||||
return Stream.<Consumer<DataBuffer>>builder()
|
||||
.add(pojoConsumer(this.pojo1))
|
||||
.add(pojoConsumer(this.pojo2))
|
||||
.add(pojoConsumer(this.pojo3))
|
||||
.build();
|
||||
}
|
||||
|
||||
public Consumer<DataBuffer> pojoConsumer(Pojo expected) {
|
||||
return dataBuffer -> {
|
||||
try {
|
||||
Pojo actual = this.mapper.reader().forType(Pojo.class)
|
||||
.readValue(DataBufferTestUtils.dumpBytes(dataBuffer));
|
||||
assertEquals(expected, actual);
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
@@ -74,48 +117,33 @@ public class Jackson2SmileEncoderTests extends AbstractDataBufferAllocatingTestC
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() throws Exception {
|
||||
Flux<Pojo> source = Flux.just(
|
||||
new Pojo("foo", "bar"),
|
||||
new Pojo("foofoo", "barbar"),
|
||||
new Pojo("foofoofoo", "barbarbar")
|
||||
);
|
||||
ResolvableType type = ResolvableType.forClass(Pojo.class);
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, emptyMap());
|
||||
public void encodeNonStream() {
|
||||
Flux<DataBuffer> output = this.encoder.encode(input(), this.bufferFactory, elementType,
|
||||
null, null);
|
||||
|
||||
ObjectMapper mapper = Jackson2ObjectMapperBuilder.smile().build();
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(dataBuffer -> readPojo(mapper, List.class, dataBuffer))
|
||||
.consumeNextWith(dataBuffer -> {
|
||||
try {
|
||||
CollectionType type = mapper.getTypeFactory()
|
||||
.constructCollectionType(List.class, Pojo.class);
|
||||
List<Pojo> value = mapper.reader().forType(type)
|
||||
.readValue(dataBuffer.asInputStream());
|
||||
assertEquals(3, value.size());
|
||||
assertEquals(pojo1, value.get(0));
|
||||
assertEquals(pojo2, value.get(1));
|
||||
assertEquals(pojo3, value.get(2));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
finally {
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAsStream() throws Exception {
|
||||
Flux<Pojo> source = Flux.just(
|
||||
new Pojo("foo", "bar"),
|
||||
new Pojo("foofoo", "barbar"),
|
||||
new Pojo("foofoofoo", "barbarbar")
|
||||
);
|
||||
ResolvableType type = ResolvableType.forClass(Pojo.class);
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, STREAM_SMILE_MIME_TYPE, emptyMap());
|
||||
|
||||
ObjectMapper mapper = Jackson2ObjectMapperBuilder.smile().build();
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(dataBuffer -> readPojo(mapper, Pojo.class, dataBuffer))
|
||||
.consumeNextWith(dataBuffer -> readPojo(mapper, Pojo.class, dataBuffer))
|
||||
.consumeNextWith(dataBuffer -> readPojo(mapper, Pojo.class, dataBuffer))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
public <T> T readPojo(ObjectMapper mapper, Class<T> valueType, DataBuffer dataBuffer) {
|
||||
try {
|
||||
T value = mapper.reader().forType(valueType).readValue(DataBufferTestUtils.dumpBytes(dataBuffer));
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
return value;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,23 +18,20 @@ package org.springframework.http.codec.protobuf;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.protobuf.Message;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.codec.AbstractEncoderTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.protobuf.Msg;
|
||||
import org.springframework.protobuf.SecondMsg;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.core.ResolvableType.forClass;
|
||||
|
||||
@@ -43,13 +40,44 @@ import static org.springframework.core.ResolvableType.forClass;
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ProtobufEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class ProtobufEncoderTests extends AbstractEncoderTestCase<Message, ProtobufEncoder> {
|
||||
|
||||
private final static MimeType PROTOBUF_MIME_TYPE = new MimeType("application", "x-protobuf");
|
||||
|
||||
private final Msg testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
|
||||
private Msg msg1 =
|
||||
Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
|
||||
|
||||
private final ProtobufEncoder encoder = new ProtobufEncoder();
|
||||
private Msg msg2 =
|
||||
Msg.newBuilder().setFoo("Bar").setBlah(SecondMsg.newBuilder().setBlah(456).build()).build();
|
||||
|
||||
public ProtobufEncoderTests() {
|
||||
super(new ProtobufEncoder(), Msg.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Flux<Message> input() {
|
||||
return Flux.just(this.msg1, this.msg2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<Consumer<DataBuffer>> outputConsumers() {
|
||||
return Stream.<Consumer<DataBuffer>>builder()
|
||||
.add(resultConsumer(this.msg1))
|
||||
.add(resultConsumer(this.msg2))
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
protected final Consumer<DataBuffer> resultConsumer(Msg msg) {
|
||||
return dataBuffer -> {
|
||||
try {
|
||||
assertEquals(msg, Msg.parseDelimitedFrom(dataBuffer.asInputStream()));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
@@ -60,63 +88,4 @@ public class ProtobufEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
assertFalse(this.encoder.canEncode(forClass(Object.class), PROTOBUF_MIME_TYPE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
Mono<Message> message = Mono.just(this.testMsg);
|
||||
ResolvableType elementType = forClass(Msg.class);
|
||||
Flux<DataBuffer> output = this.encoder.encode(message, this.bufferFactory, elementType, PROTOBUF_MIME_TYPE, emptyMap());
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(dataBuffer -> {
|
||||
try {
|
||||
assertEquals(this.testMsg, Msg.parseFrom(dataBuffer.asInputStream()));
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeError() {
|
||||
Flux<Msg> messages = Flux.just(this.testMsg)
|
||||
.concatWith(Flux.error(new RuntimeException()));
|
||||
|
||||
ResolvableType elementType = forClass(Msg.class);
|
||||
Flux<DataBuffer> output = this.encoder.encode(messages, this.bufferFactory, elementType, PROTOBUF_MIME_TYPE, emptyMap());
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(DataBufferUtils::release)
|
||||
.expectError(RuntimeException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeStream() {
|
||||
Msg testMsg2 = Msg.newBuilder().setFoo("Bar").setBlah(SecondMsg.newBuilder().setBlah(456).build()).build();
|
||||
Flux<Message> messages = Flux.just(this.testMsg, testMsg2);
|
||||
ResolvableType elementType = forClass(Msg.class);
|
||||
Flux<DataBuffer> output = this.encoder.encode(messages, this.bufferFactory, elementType, PROTOBUF_MIME_TYPE, emptyMap());
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(dataBuffer -> {
|
||||
try {
|
||||
assertEquals(this.testMsg, Msg.parseDelimitedFrom(dataBuffer.asInputStream()));
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
})
|
||||
.consumeNextWith(dataBuffer -> {
|
||||
try {
|
||||
assertEquals(testMsg2, Msg.parseDelimitedFrom(dataBuffer.asInputStream()));
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,21 +18,19 @@ package org.springframework.http.codec.xml;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElements;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.codec.AbstractEncoderTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
@@ -44,10 +42,11 @@ import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
|
||||
* @author Sebastien Deleuze
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private final Jaxb2XmlEncoder encoder = new Jaxb2XmlEncoder();
|
||||
public class Jaxb2XmlEncoderTests extends AbstractEncoderTestCase<Object, Jaxb2XmlEncoder> {
|
||||
|
||||
public Jaxb2XmlEncoderTests() {
|
||||
super(new Jaxb2XmlEncoder(), Pojo.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
@@ -69,60 +68,22 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.NONE, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
Mono<Pojo> source = Mono.just(new Pojo("foofoo", "barbar"));
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
|
||||
ResolvableType.forClass(Pojo.class),
|
||||
MediaType.APPLICATION_XML, Collections.emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(dataBuffer -> {
|
||||
try {
|
||||
String s = DataBufferTestUtils
|
||||
.dumpString(dataBuffer, StandardCharsets.UTF_8);
|
||||
assertThat(s, isSimilarTo("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
|
||||
"<pojo><bar>barbar</bar><foo>foofoo</foo></pojo>"));
|
||||
}
|
||||
finally {
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
})
|
||||
.verifyComplete();
|
||||
@Override
|
||||
protected Flux<Object> input() {
|
||||
return Flux.just(new Container());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeError() {
|
||||
Flux<Pojo> source = Flux.error(RuntimeException::new);
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
|
||||
ResolvableType.forClass(Pojo.class),
|
||||
MediaType.APPLICATION_XML, Collections.emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.expectError(RuntimeException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeElementsWithCommonType() {
|
||||
Mono<Container> source = Mono.just(new Container());
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
|
||||
ResolvableType.forClass(Pojo.class),
|
||||
MediaType.APPLICATION_XML, Collections.emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.consumeNextWith(dataBuffer -> {
|
||||
try {
|
||||
String s = DataBufferTestUtils
|
||||
.dumpString(dataBuffer, StandardCharsets.UTF_8);
|
||||
assertThat(s, isSimilarTo("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
|
||||
"<container><foo><name>name1</name></foo><bar><title>title1</title></bar></container>"));
|
||||
}
|
||||
finally {
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
@Override
|
||||
protected Stream<Consumer<DataBuffer>> outputConsumers() {
|
||||
return Stream.<Consumer<DataBuffer>>builder()
|
||||
.add(dataBuffer -> {
|
||||
String s = DataBufferTestUtils
|
||||
.dumpString(dataBuffer, StandardCharsets.UTF_8);
|
||||
assertThat(s,
|
||||
isSimilarTo("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
|
||||
"<container><foo><name>name1</name></foo><bar><title>title1</title></bar></container>"));
|
||||
})
|
||||
.verifyComplete();
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user