Improve decodeToMono support

This commit ensures uniform support for decodeToMono across the various
byte and String related decoders.

Issue: SPR-16253
This commit is contained in:
Rossen Stoyanchev
2017-12-14 16:59:34 -05:00
parent 8e253a316d
commit f4d8c7cc2b
9 changed files with 174 additions and 57 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -21,6 +21,7 @@ import java.util.Collections;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
@@ -39,6 +40,7 @@ public class ByteArrayDecoderTests extends AbstractDataBufferAllocatingTestCase
private final ByteArrayDecoder decoder = new ByteArrayDecoder();
@Test
public void canDecode() {
assertTrue(this.decoder.canDecode(ResolvableType.forClass(byte[].class),
@@ -65,4 +67,19 @@ public class ByteArrayDecoderTests extends AbstractDataBufferAllocatingTestCase
.verify();
}
@Test
public void decodeToMono() {
DataBuffer fooBuffer = stringBuffer("foo");
DataBuffer barBuffer = stringBuffer("bar");
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Mono<byte[]> output = this.decoder.decodeToMono(source,
ResolvableType.forClassWithGenerics(Publisher.class, byte[].class),
null, Collections.emptyMap());
StepVerifier.create(output)
.consumeNextWith(bytes -> assertArrayEquals("foobar".getBytes(), bytes))
.expectComplete()
.verify();
}
}

View File

@@ -22,6 +22,7 @@ import java.util.Collections;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
@@ -63,4 +64,19 @@ public class ByteBufferDecoderTests extends AbstractDataBufferAllocatingTestCase
.expectComplete()
.verify();
}
@Test
public void decodeToMono() {
DataBuffer fooBuffer = stringBuffer("foo");
DataBuffer barBuffer = stringBuffer("bar");
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Mono<ByteBuffer> output = this.decoder.decodeToMono(source,
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
null, Collections.emptyMap());
StepVerifier.create(output)
.expectNext(ByteBuffer.wrap("foobar".getBytes()))
.expectComplete()
.verify();
}
}

View File

@@ -17,15 +17,19 @@
package org.springframework.core.codec;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collections;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import org.springframework.util.MimeTypeUtils;
import static org.junit.Assert.*;
@@ -53,11 +57,25 @@ public class DataBufferDecoderTests extends AbstractDataBufferAllocatingTestCase
DataBuffer barBuffer = stringBuffer("bar");
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Flux<DataBuffer> output = this.decoder.decode(source,
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
ResolvableType.forClassWithGenerics(Publisher.class, DataBuffer.class),
null, Collections.emptyMap());
assertSame(source, output);
release(fooBuffer, barBuffer);
}
}
@Test
public void decodeToMono() {
DataBuffer fooBuffer = stringBuffer("foo");
DataBuffer barBuffer = stringBuffer("bar");
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Mono<DataBuffer> output = this.decoder.decodeToMono(source,
ResolvableType.forClassWithGenerics(Publisher.class, DataBuffer.class),
null, Collections.emptyMap());
DataBuffer outputBuffer = output.block(Duration.ofSeconds(5));
assertEquals("foobar", DataBufferTestUtils.dumpString(outputBuffer, StandardCharsets.UTF_8));
release(outputBuffer);
}}