From 1c628293a2eda10ab07d77f4d2ae832d134c578c Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 8 Aug 2018 17:04:25 +0200 Subject: [PATCH] Add doc & tests to Jaxb2XmlEncoder for collections Issue: SPR-16363 --- .../http/codec/xml/Jaxb2XmlEncoder.java | 6 +- .../http/codec/xml/Jaxb2XmlEncoderTests.java | 87 +++++++++++++++++-- 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java index fc5617848ce..331f5e05b00 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java @@ -40,7 +40,11 @@ import org.springframework.util.MimeType; import org.springframework.util.MimeTypeUtils; /** - * Encode from {@code Object} stream to a byte stream containing XML elements. + * Encode from single value to a byte stream containing XML elements. + * + *

{@link javax.xml.bind.annotation.XmlElements @XmlElements} and + * {@link javax.xml.bind.annotation.XmlElement @XmlElement} can be used to specify how + * collections should be marshalled. * * @author Sebastien Deleuze * @author Arjen Poutsma diff --git a/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlEncoderTests.java b/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlEncoderTests.java index 40563c3d2e9..7295d173d2c 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlEncoderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlEncoderTests.java @@ -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 source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar")); + public void encode() { + Mono source = Mono.just(new Pojo("foofoo", "barbar")); Flux 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 source = Mono.just(new Container()); + Flux 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("" + + "name1title1")); + } + 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 getElements() { + return Arrays.asList(new Foo("name1"), new Bar("title1")); + } } }