|
|
|
@ -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"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -17,10 +17,13 @@ |
|
|
|
package org.springframework.http.codec.xml; |
|
|
|
package org.springframework.http.codec.xml; |
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import reactor.core.publisher.Flux; |
|
|
|
import reactor.core.publisher.Flux; |
|
|
|
|
|
|
|
import reactor.core.publisher.Mono; |
|
|
|
import reactor.test.StepVerifier; |
|
|
|
import reactor.test.StepVerifier; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.ResolvableType; |
|
|
|
import org.springframework.core.ResolvableType; |
|
|
|
@ -36,6 +39,10 @@ import static org.junit.Assert.assertThat; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo; |
|
|
|
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 Sebastien Deleuze |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Arjen Poutsma |
|
|
|
@ -66,8 +73,8 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void encode() throws Exception { |
|
|
|
public void encode() { |
|
|
|
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar")); |
|
|
|
Mono<Pojo> source = Mono.just(new Pojo("foofoo", "barbar")); |
|
|
|
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, |
|
|
|
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, |
|
|
|
ResolvableType.forClass(Pojo.class), |
|
|
|
ResolvableType.forClass(Pojo.class), |
|
|
|
MediaType.APPLICATION_XML, Collections.emptyMap()); |
|
|
|
MediaType.APPLICATION_XML, Collections.emptyMap()); |
|
|
|
@ -84,8 +91,78 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase { |
|
|
|
DataBufferUtils.release(dataBuffer); |
|
|
|
DataBufferUtils.release(dataBuffer); |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
.expectComplete() |
|
|
|
.verifyComplete(); |
|
|
|
.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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.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")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|