Add doc & tests to Jaxb2XmlEncoder for collections
Issue: SPR-16363
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,10 +17,13 @@
|
||||
package org.springframework.http.codec.xml;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
@@ -36,6 +39,10 @@ import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElements;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
* @author Arjen Poutsma
|
||||
@@ -66,8 +73,8 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() throws Exception {
|
||||
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
|
||||
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());
|
||||
@@ -84,8 +91,78 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
})
|
||||
.expectComplete()
|
||||
.verify();
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
|
||||
public static class Model {}
|
||||
|
||||
public static class Foo extends Model {
|
||||
|
||||
private String name;
|
||||
|
||||
public Foo(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Bar extends Model {
|
||||
|
||||
private String title;
|
||||
|
||||
public Bar(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
|
||||
@XmlRootElement
|
||||
public static class Container {
|
||||
|
||||
@XmlElements({
|
||||
@XmlElement(name="foo", type=Foo.class),
|
||||
@XmlElement(name="bar", type=Bar.class)
|
||||
})
|
||||
public List<Model> getElements() {
|
||||
return Arrays.asList(new Foo("name1"), new Bar("title1"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user