Migrate parameterized tests in spring-core

This commit migrates parameterized tests in spring-core using the
"composed @ParameterizedTest" approach. This approach is reused in
follow-up commits for the migration of the remaining modules.

For a concrete example, see AbstractDataBufferAllocatingTests and its
subclasses (e.g., DataBufferTests).

Specifically, AbstractDataBufferAllocatingTests declares a custom
@ParameterizedDataBufferAllocatingTest annotation that is
meta-annotated with @ParameterizedTest and
@MethodSource("org.springframework.core.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()").

Individual methods in concrete subclasses are then annotated with
@ParameterizedDataBufferAllocatingTest instead of @ParameterizedTest or
@Test.

The approach makes the migration from JUnit 4 to JUnit Jupiter rather
straightforward; however, there is one major downside. The arguments
for a @ParameterizedTest test method can only be accessed by the test
method itself. It is not possible to access them in an @BeforeEach
method (see https://github.com/junit-team/junit5/issues/944).
Consequently, we are forced to declare the parameters in each such
method and delegate to a custom "setup" method. Although this is a bit
cumbersome, I feel it is currently the best way to achieve fine grained
parameterized tests within our test suite without implementing a custom
TestTemplateInvocationContextProvider for each specific use case.

Once https://github.com/junit-team/junit5/issues/878 is resolved, we
should consider migrating to parameterized test classes.

See gh-23451
This commit is contained in:
Sam Brannen
2019-08-12 13:35:56 +02:00
parent 32cc32f9a7
commit 3df85c783f
9 changed files with 528 additions and 317 deletions

View File

@@ -22,7 +22,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Date;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.tests.sample.objects.TestObject;
@@ -203,7 +203,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
assertThat(names.length).isEqualTo(0);
}
@Ignore("Ignored because Ubuntu packages OpenJDK with debug symbols enabled. See SPR-8078.")
@Disabled("Ignored because Ubuntu packages OpenJDK with debug symbols enabled. See SPR-8078.")
@Test
public void classesWithoutDebugSymbols() throws Exception {
// JDK classes don't have debug information (usually)

View File

@@ -16,27 +16,34 @@
package org.springframework.core.io.buffer;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PoolArenaMetric;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocatorMetric;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.junit.Rule;
import org.junit.rules.Verifier;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
/**
* Base class for tests that read or write data buffers with a rule to check
@@ -44,29 +51,14 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
@RunWith(Parameterized.class)
public abstract class AbstractDataBufferAllocatingTestCase {
public abstract class AbstractDataBufferAllocatingTests {
@Parameterized.Parameter
public DataBufferFactory bufferFactory;
@RegisterExtension
AfterEachCallback leakDetector = context -> verifyAllocations();
@Parameterized.Parameters(name = "{0}")
public static Object[][] dataBufferFactories() {
return new Object[][] {
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))},
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))},
// disable caching for reliable leak detection, see https://github.com/netty/netty/issues/5275
{new NettyDataBufferFactory(new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0, true))},
{new NettyDataBufferFactory(new PooledByteBufAllocator(false, 1, 1, 8192, 11, 0, 0, 0, true))},
{new DefaultDataBufferFactory(true)},
{new DefaultDataBufferFactory(false)}
};
}
@Rule
public final Verifier leakDetector = new LeakDetector();
protected DataBufferFactory bufferFactory;
protected DataBuffer createDataBuffer(int capacity) {
@@ -93,8 +85,7 @@ public abstract class AbstractDataBufferAllocatingTestCase {
protected Consumer<DataBuffer> stringConsumer(String expected) {
return dataBuffer -> {
String value =
DataBufferTestUtils.dumpString(dataBuffer, StandardCharsets.UTF_8);
String value = DataBufferTestUtils.dumpString(dataBuffer, StandardCharsets.UTF_8);
DataBufferUtils.release(dataBuffer);
assertThat(value).isEqualTo(expected);
};
@@ -150,12 +141,29 @@ public abstract class AbstractDataBufferAllocatingTestCase {
}
protected class LeakDetector extends Verifier {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ParameterizedTest(name = "{0}")
@MethodSource("org.springframework.core.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()")
public @interface ParameterizedDataBufferAllocatingTest {
}
@Override
public void verify() {
AbstractDataBufferAllocatingTestCase.this.verifyAllocations();
}
public static Stream<Arguments> dataBufferFactories() {
return Stream.of(
arguments("NettyDataBufferFactory - UnpooledByteBufAllocator - preferDirect = true",
new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))),
arguments("NettyDataBufferFactory - UnpooledByteBufAllocator - preferDirect = false",
new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))),
// disable caching for reliable leak detection, see https://github.com/netty/netty/issues/5275
arguments("NettyDataBufferFactory - PooledByteBufAllocator - preferDirect = true",
new NettyDataBufferFactory(new PooledByteBufAllocator(true, 1, 1, 4096, 2, 0, 0, 0, true))),
arguments("NettyDataBufferFactory - PooledByteBufAllocator - preferDirect = false",
new NettyDataBufferFactory(new PooledByteBufAllocator(false, 1, 1, 4096, 2, 0, 0, 0, true))),
arguments("DefaultDataBufferFactory - preferDirect = true",
new DefaultDataBufferFactory(true)),
arguments("DefaultDataBufferFactory - preferDirect = false",
new DefaultDataBufferFactory(false))
);
}
}

View File

@@ -16,26 +16,26 @@
package org.springframework.core.io.buffer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Arjen Poutsma
* @author Sam Brannen
*/
public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
class DataBufferTests extends AbstractDataBufferAllocatingTests {
@ParameterizedDataBufferAllocatingTest
public void byteCountsAndPositions(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
@Test
public void byteCountsAndPositions() {
DataBuffer buffer = createDataBuffer(2);
assertThat(buffer.readPosition()).isEqualTo(0);
@@ -75,8 +75,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void readPositionSmallerThanZero() {
@ParameterizedDataBufferAllocatingTest
public void readPositionSmallerThanZero(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
try {
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
@@ -87,8 +89,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void readPositionGreaterThanWritePosition() {
@ParameterizedDataBufferAllocatingTest
public void readPositionGreaterThanWritePosition(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
try {
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
@@ -99,8 +103,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void writePositionSmallerThanReadPosition() {
@ParameterizedDataBufferAllocatingTest
public void writePositionSmallerThanReadPosition(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(2);
try {
buffer.write((byte) 'a');
@@ -113,8 +119,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void writePositionGreaterThanCapacity() {
@ParameterizedDataBufferAllocatingTest
public void writePositionGreaterThanCapacity(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
try {
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
@@ -125,8 +133,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void writeAndRead() {
@ParameterizedDataBufferAllocatingTest
public void writeAndRead(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(5);
buffer.write(new byte[]{'a', 'b', 'c'});
@@ -144,8 +154,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void writeNullString() {
@ParameterizedDataBufferAllocatingTest
public void writeNullString(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
try {
assertThatIllegalArgumentException().isThrownBy(() ->
@@ -156,8 +168,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void writeNullCharset() {
@ParameterizedDataBufferAllocatingTest
public void writeNullCharset(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
try {
assertThatIllegalArgumentException().isThrownBy(() ->
@@ -168,8 +182,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void writeEmptyString() {
@ParameterizedDataBufferAllocatingTest
public void writeEmptyString(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
buffer.write("", StandardCharsets.UTF_8);
@@ -178,8 +194,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void writeUtf8String() {
@ParameterizedDataBufferAllocatingTest
public void writeUtf8String(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(6);
buffer.write("Spring", StandardCharsets.UTF_8);
@@ -190,8 +208,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void writeUtf8StringOutGrowsCapacity() {
@ParameterizedDataBufferAllocatingTest
public void writeUtf8StringOutGrowsCapacity(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(5);
buffer.write("Spring €", StandardCharsets.UTF_8);
@@ -202,8 +222,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void writeIsoString() {
@ParameterizedDataBufferAllocatingTest
public void writeIsoString(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(3);
buffer.write("\u00A3", StandardCharsets.ISO_8859_1);
@@ -214,8 +236,9 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void writeMultipleUtf8String() {
@ParameterizedDataBufferAllocatingTest
public void writeMultipleUtf8String(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
buffer.write("abc", StandardCharsets.UTF_8);
@@ -235,8 +258,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void toStringNullCharset() {
@ParameterizedDataBufferAllocatingTest
public void toStringNullCharset(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
try {
assertThatIllegalArgumentException().isThrownBy(() ->
@@ -247,8 +272,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void toStringUtf8() {
@ParameterizedDataBufferAllocatingTest
public void toStringUtf8(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
String spring = "Spring";
byte[] bytes = spring.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = createDataBuffer(bytes.length);
@@ -260,8 +287,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void toStringSection() {
@ParameterizedDataBufferAllocatingTest
public void toStringSection(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
String spring = "Spring";
byte[] bytes = spring.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = createDataBuffer(bytes.length);
@@ -273,8 +302,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void inputStream() throws IOException {
@ParameterizedDataBufferAllocatingTest
public void inputStream(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(4);
buffer.write(new byte[]{'a', 'b', 'c', 'd', 'e'});
buffer.readPosition(1);
@@ -305,8 +336,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void inputStreamReleaseOnClose() throws IOException {
@ParameterizedDataBufferAllocatingTest
public void inputStreamReleaseOnClose(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(3);
byte[] bytes = {'a', 'b', 'c'};
buffer.write(bytes);
@@ -323,12 +356,13 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
inputStream.close();
}
// AbstractDataBufferAllocatingTestCase.LeakDetector will verify the buffer's release
// AbstractDataBufferAllocatingTests.leakDetector will verify the buffer's release
}
@Test
public void outputStream() throws IOException {
@ParameterizedDataBufferAllocatingTest
public void outputStream(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(4);
buffer.write((byte) 'a');
@@ -345,8 +379,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void expand() {
@ParameterizedDataBufferAllocatingTest
public void expand(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
buffer.write((byte) 'a');
assertThat(buffer.capacity()).isEqualTo(1);
@@ -357,8 +393,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void increaseCapacity() {
@ParameterizedDataBufferAllocatingTest
public void increaseCapacity(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
assertThat(buffer.capacity()).isEqualTo(1);
@@ -368,8 +406,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void decreaseCapacityLowReadPosition() {
@ParameterizedDataBufferAllocatingTest
public void decreaseCapacityLowReadPosition(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(2);
buffer.writePosition(2);
buffer.capacity(1);
@@ -378,8 +418,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void decreaseCapacityHighReadPosition() {
@ParameterizedDataBufferAllocatingTest
public void decreaseCapacityHighReadPosition(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(2);
buffer.writePosition(2);
buffer.readPosition(2);
@@ -389,8 +431,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void capacityLessThanZero() {
@ParameterizedDataBufferAllocatingTest
public void capacityLessThanZero(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
try {
assertThatIllegalArgumentException().isThrownBy(() ->
@@ -401,8 +445,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void writeByteBuffer() {
@ParameterizedDataBufferAllocatingTest
public void writeByteBuffer(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer1 = createDataBuffer(1);
buffer1.write((byte) 'a');
ByteBuffer buffer2 = createByteBuffer(2);
@@ -428,8 +474,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
return ByteBuffer.allocate(capacity);
}
@Test
public void writeDataBuffer() {
@ParameterizedDataBufferAllocatingTest
public void writeDataBuffer(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer1 = createDataBuffer(1);
buffer1.write((byte) 'a');
DataBuffer buffer2 = createDataBuffer(2);
@@ -449,8 +497,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer1, buffer2, buffer3);
}
@Test
public void asByteBuffer() {
@ParameterizedDataBufferAllocatingTest
public void asByteBuffer(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(4);
buffer.write(new byte[]{'a', 'b', 'c'});
buffer.read(); // skip a
@@ -468,8 +518,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void asByteBufferIndexLength() {
@ParameterizedDataBufferAllocatingTest
public void asByteBufferIndexLength(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(3);
buffer.write(new byte[]{'a', 'b'});
@@ -486,8 +538,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void byteBufferContainsDataBufferChanges() {
@ParameterizedDataBufferAllocatingTest
public void byteBufferContainsDataBufferChanges(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer dataBuffer = createDataBuffer(1);
ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, 1);
@@ -500,8 +554,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(dataBuffer);
}
@Test
public void dataBufferContainsByteBufferChanges() {
@ParameterizedDataBufferAllocatingTest
public void dataBufferContainsByteBufferChanges(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer dataBuffer = createDataBuffer(1);
ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, 1);
@@ -514,8 +570,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(dataBuffer);
}
@Test
public void emptyAsByteBuffer() {
@ParameterizedDataBufferAllocatingTest
public void emptyAsByteBuffer(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(1);
ByteBuffer result = buffer.asByteBuffer();
@@ -524,8 +582,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void indexOf() {
@ParameterizedDataBufferAllocatingTest
public void indexOf(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(3);
buffer.write(new byte[]{'a', 'b', 'c'});
@@ -544,8 +604,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void lastIndexOf() {
@ParameterizedDataBufferAllocatingTest
public void lastIndexOf(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(3);
buffer.write(new byte[]{'a', 'b', 'c'});
@@ -573,8 +635,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void slice() {
@ParameterizedDataBufferAllocatingTest
public void slice(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(3);
buffer.write(new byte[]{'a', 'b'});
@@ -600,8 +664,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void retainedSlice() {
@ParameterizedDataBufferAllocatingTest
public void retainedSlice(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(3);
buffer.write(new byte[]{'a', 'b'});
@@ -627,8 +693,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer, slice);
}
@Test
public void spr16351() {
@ParameterizedDataBufferAllocatingTest
public void spr16351(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = createDataBuffer(6);
byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f'};
buffer.write(bytes);
@@ -645,8 +713,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void join() {
@ParameterizedDataBufferAllocatingTest
public void join(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer composite = this.bufferFactory.join(Arrays.asList(stringBuffer("a"),
stringBuffer("b"), stringBuffer("c")));
assertThat(composite.readableByteCount()).isEqualTo(3);
@@ -658,8 +728,10 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(composite);
}
@Test
public void getByte() {
@ParameterizedDataBufferAllocatingTest
public void getByte(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer buffer = stringBuffer("abc");
assertThat(buffer.getByte(0)).isEqualTo((byte) 'a');

View File

@@ -35,8 +35,6 @@ import java.util.List;
import java.util.concurrent.CountDownLatch;
import io.netty.buffer.ByteBuf;
import org.junit.Before;
import org.junit.Test;
import org.mockito.stubbing.Answer;
import org.reactivestreams.Subscription;
import reactor.core.publisher.BaseSubscriber;
@@ -59,40 +57,46 @@ import static org.mockito.Mockito.mock;
/**
* @author Arjen Poutsma
* @author Sam Brannen
*/
public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
class DataBufferUtilsTests extends AbstractDataBufferAllocatingTests {
private Resource resource;
private Path tempFile;
private final Resource resource;
private final Path tempFile;
@Before
public void setUp() throws IOException {
DataBufferUtilsTests() throws Exception {
this.resource = new ClassPathResource("DataBufferUtilsTests.txt", getClass());
this.tempFile = Files.createTempFile("DataBufferUtilsTests", null);
}
@Test
public void readInputStream() {
@ParameterizedDataBufferAllocatingTest
public void readInputStream(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> flux = DataBufferUtils.readInputStream(
() -> this.resource.getInputStream(), this.bufferFactory, 3);
() -> this.resource.getInputStream(), super.bufferFactory, 3);
verifyReadData(flux);
}
@Test
public void readByteChannel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readByteChannel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
URI uri = this.resource.getURI();
Flux<DataBuffer> result =
DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ),
this.bufferFactory, 3);
super.bufferFactory, 3);
verifyReadData(result);
}
@Test
public void readByteChannelError() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readByteChannelError(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
ReadableByteChannel channel = mock(ReadableByteChannel.class);
given(channel.read(any()))
.willAnswer(invocation -> {
@@ -104,7 +108,7 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.willThrow(new IOException());
Flux<DataBuffer> result =
DataBufferUtils.readByteChannel(() -> channel, this.bufferFactory, 3);
DataBufferUtils.readByteChannel(() -> channel, super.bufferFactory, 3);
StepVerifier.create(result)
.consumeNextWith(stringConsumer("foo"))
@@ -112,12 +116,14 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(3));
}
@Test
public void readByteChannelCancel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readByteChannelCancel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
URI uri = this.resource.getURI();
Flux<DataBuffer> result =
DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ),
this.bufferFactory, 3);
super.bufferFactory, 3);
StepVerifier.create(result)
.consumeNextWith(stringConsumer("foo"))
@@ -125,22 +131,26 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify();
}
@Test
public void readAsynchronousFileChannel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readAsynchronousFileChannel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
URI uri = this.resource.getURI();
Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel(
() -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ),
this.bufferFactory, 3);
super.bufferFactory, 3);
verifyReadData(flux);
}
@Test
public void readAsynchronousFileChannelPosition() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readAsynchronousFileChannelPosition(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
URI uri = this.resource.getURI();
Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel(
() -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ),
9, this.bufferFactory, 3);
9, super.bufferFactory, 3);
StepVerifier.create(flux)
.consumeNextWith(stringConsumer("qux"))
@@ -148,8 +158,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void readAsynchronousFileChannelError() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readAsynchronousFileChannelError(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
AsynchronousFileChannel channel = mock(AsynchronousFileChannel.class);
willAnswer(invocation -> {
ByteBuffer byteBuffer = invocation.getArgument(0);
@@ -170,7 +182,7 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.given(channel).read(any(), anyLong(), any(), any());
Flux<DataBuffer> result =
DataBufferUtils.readAsynchronousFileChannel(() -> channel, this.bufferFactory, 3);
DataBufferUtils.readAsynchronousFileChannel(() -> channel, super.bufferFactory, 3);
StepVerifier.create(result)
.consumeNextWith(stringConsumer("foo"))
@@ -178,12 +190,14 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(3));
}
@Test
public void readAsynchronousFileChannelCancel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readAsynchronousFileChannelCancel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
URI uri = this.resource.getURI();
Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel(
() -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ),
this.bufferFactory, 3);
super.bufferFactory, 3);
StepVerifier.create(flux)
.consumeNextWith(stringConsumer("foo"))
@@ -191,35 +205,43 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify();
}
@Test // gh-22107
public void readAsynchronousFileChannelCancelWithoutDemand() throws Exception {
@ParameterizedDataBufferAllocatingTest // gh-22107
public void readAsynchronousFileChannelCancelWithoutDemand(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
URI uri = this.resource.getURI();
Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel(
() -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ),
this.bufferFactory, 3);
super.bufferFactory, 3);
BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
flux.subscribe(subscriber);
subscriber.cancel();
}
@Test
public void readPath() throws IOException {
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource.getFile().toPath(), this.bufferFactory, 3);
@ParameterizedDataBufferAllocatingTest
public void readPath(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource.getFile().toPath(), super.bufferFactory, 3);
verifyReadData(flux);
}
@Test
public void readResource() throws Exception {
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource, this.bufferFactory, 3);
@ParameterizedDataBufferAllocatingTest
public void readResource(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource, super.bufferFactory, 3);
verifyReadData(flux);
}
@Test
public void readResourcePosition() throws Exception {
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource, 9, this.bufferFactory, 3);
@ParameterizedDataBufferAllocatingTest
public void readResourcePosition(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource, 9, super.bufferFactory, 3);
StepVerifier.create(flux)
.consumeNextWith(stringConsumer("qux"))
@@ -237,10 +259,12 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(3));
}
@Test
public void readResourcePositionAndTakeUntil() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readResourcePositionAndTakeUntil(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
Resource resource = new ClassPathResource("DataBufferUtilsTests.txt", getClass());
Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, this.bufferFactory, 3);
Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, super.bufferFactory, 3);
flux = DataBufferUtils.takeUntilByteCount(flux, 5);
@@ -252,10 +276,12 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void readByteArrayResourcePositionAndTakeUntil() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readByteArrayResourcePositionAndTakeUntil(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
Resource resource = new ByteArrayResource("foobarbazqux" .getBytes());
Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, this.bufferFactory, 3);
Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, super.bufferFactory, 3);
flux = DataBufferUtils.takeUntilByteCount(flux, 5);
@@ -267,8 +293,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void writeOutputStream() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeOutputStream(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
@@ -282,8 +310,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
os.close();
}
@Test
public void writeWritableByteChannel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeWritableByteChannel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
@@ -297,8 +327,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
channel.close();
}
@Test
public void writeWritableByteChannelErrorInFlux() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeWritableByteChannelErrorInFlux(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException()));
@@ -318,8 +350,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
channel.close();
}
@Test
public void writeWritableByteChannelErrorInWrite() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeWritableByteChannelErrorInWrite(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar);
@@ -344,8 +378,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
channel.close();
}
@Test
public void writeWritableByteChannelCancel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeWritableByteChannelCancel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar);
@@ -366,8 +402,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
flux.subscribe(DataBufferUtils::release);
}
@Test
public void writeAsynchronousFileChannel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeAsynchronousFileChannel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
@@ -396,8 +434,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
assertThat(result).isEqualTo("foobarbazqux");
}
@Test
public void writeAsynchronousFileChannelErrorInFlux() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeAsynchronousFileChannelErrorInFlux(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux =
@@ -419,9 +459,11 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
channel.close();
}
@Test
@ParameterizedDataBufferAllocatingTest
@SuppressWarnings("unchecked")
public void writeAsynchronousFileChannelErrorInWrite() throws Exception {
public void writeAsynchronousFileChannelErrorInWrite(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar);
@@ -459,8 +501,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
channel.close();
}
@Test
public void writeAsynchronousFileChannelCanceled() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void writeAsynchronousFileChannelCanceled(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar);
@@ -482,8 +526,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
flux.subscribe(DataBufferUtils::release);
}
@Test
public void writePath() throws IOException {
@ParameterizedDataBufferAllocatingTest
public void writePath(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar);
@@ -497,14 +543,16 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
assertThat(written).contains("foobar");
}
@Test
public void readAndWriteByteChannel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readAndWriteByteChannel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
Path source = Paths.get(
DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt").toURI());
Flux<DataBuffer> sourceFlux =
DataBufferUtils
.readByteChannel(() -> FileChannel.open(source, StandardOpenOption.READ),
this.bufferFactory, 3);
super.bufferFactory, 3);
Path destination = Files.createTempFile("DataBufferUtilsTests", null);
WritableByteChannel channel = Files.newByteChannel(destination, StandardOpenOption.WRITE);
@@ -529,13 +577,15 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
});
}
@Test
public void readAndWriteAsynchronousFileChannel() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void readAndWriteAsynchronousFileChannel(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
Path source = Paths.get(
DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt").toURI());
Flux<DataBuffer> sourceFlux = DataBufferUtils.readAsynchronousFileChannel(
() -> AsynchronousFileChannel.open(source, StandardOpenOption.READ),
this.bufferFactory, 3);
super.bufferFactory, 3);
Path destination = Files.createTempFile("DataBufferUtilsTests", null);
AsynchronousFileChannel channel =
@@ -568,8 +618,9 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
latch.await();
}
@Test
public void takeUntilByteCount() {
@ParameterizedDataBufferAllocatingTest
public void takeUntilByteCount(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(
Flux.just(stringBuffer("foo"), stringBuffer("bar")), 5L);
@@ -581,8 +632,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void takeUntilByteCountCanceled() {
@ParameterizedDataBufferAllocatingTest
public void takeUntilByteCountCanceled(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> source = Flux.concat(
deferStringBuffer("foo"),
deferStringBuffer("bar")
@@ -596,8 +649,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void takeUntilByteCountError() {
@ParameterizedDataBufferAllocatingTest
public void takeUntilByteCountError(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> source = Flux.concat(
Mono.defer(() -> Mono.just(stringBuffer("foo"))),
Mono.error(new RuntimeException())
@@ -611,8 +666,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void takeUntilByteCountExact() {
@ParameterizedDataBufferAllocatingTest
public void takeUntilByteCountExact(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> source = Flux.concat(
deferStringBuffer("foo"),
deferStringBuffer("bar"),
@@ -628,8 +685,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void skipUntilByteCount() {
@ParameterizedDataBufferAllocatingTest
public void skipUntilByteCount(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> source = Flux.concat(
deferStringBuffer("foo"),
deferStringBuffer("bar"),
@@ -644,8 +703,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void skipUntilByteCountCancelled() {
@ParameterizedDataBufferAllocatingTest
public void skipUntilByteCountCancelled(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> source = Flux.concat(
deferStringBuffer("foo"),
deferStringBuffer("bar")
@@ -658,8 +719,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void skipUntilByteCountErrorInFlux() {
@ParameterizedDataBufferAllocatingTest
public void skipUntilByteCountErrorInFlux(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
Flux<DataBuffer> flux =
Flux.just(foo).concatWith(Mono.error(new RuntimeException()));
@@ -670,8 +733,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void skipUntilByteCountShouldSkipAll() {
@ParameterizedDataBufferAllocatingTest
public void skipUntilByteCountShouldSkipAll(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
@@ -683,8 +748,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify(Duration.ofSeconds(5));
}
@Test
public void releaseConsumer() {
@ParameterizedDataBufferAllocatingTest
public void releaseConsumer(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
@@ -704,8 +771,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
}
}
@Test
public void SPR16070() throws Exception {
@ParameterizedDataBufferAllocatingTest
public void SPR16070(String displayName, DataBufferFactory bufferFactory) throws Exception {
super.bufferFactory = bufferFactory;
ReadableByteChannel channel = mock(ReadableByteChannel.class);
given(channel.read(any()))
.willAnswer(putByte('a'))
@@ -714,7 +783,7 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.willReturn(-1);
Flux<DataBuffer> read =
DataBufferUtils.readByteChannel(() -> channel, this.bufferFactory, 1);
DataBufferUtils.readByteChannel(() -> channel, super.bufferFactory, 1);
StepVerifier.create(read)
.consumeNextWith(stringConsumer("a"))
@@ -733,8 +802,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
};
}
@Test
public void join() {
@ParameterizedDataBufferAllocatingTest
public void join(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
@@ -749,8 +820,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verifyComplete();
}
@Test
public void joinErrors() {
@ParameterizedDataBufferAllocatingTest
public void joinErrors(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException()));
@@ -761,8 +834,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify();
}
@Test
public void joinCanceled() {
@ParameterizedDataBufferAllocatingTest
public void joinCanceled(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> source = Flux.concat(
deferStringBuffer("foo"),
deferStringBuffer("bar"),
@@ -775,8 +850,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify();
}
@Test
public void matcher() {
@ParameterizedDataBufferAllocatingTest
public void matcher(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
@@ -791,8 +868,10 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
release(foo, bar);
}
@Test
public void matcher2() {
@ParameterizedDataBufferAllocatingTest
public void matcher2(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
DataBuffer foo = stringBuffer("fooobar");
byte[] delims = "oo".getBytes(StandardCharsets.UTF_8);

View File

@@ -24,7 +24,7 @@ import static org.springframework.core.io.buffer.DataBufferUtils.release;
/**
* @author Arjen Poutsma
*/
public class LeakAwareDataBufferFactoryTests {
class LeakAwareDataBufferFactoryTests {
private final LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();

View File

@@ -18,57 +18,81 @@ package org.springframework.core.io.buffer;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* @author Arjen Poutsma
* @author Sam Brannen
*/
@RunWith(Parameterized.class)
public class PooledDataBufferTests {
class PooledDataBufferTests {
@Parameterized.Parameter
public DataBufferFactory dataBufferFactory;
@Nested
class UnpooledByteBufAllocatorWithPreferDirectTrueTests implements PooledDataBufferTestingTrait {
@Parameterized.Parameters(name = "{0}")
public static Object[][] buffers() {
return new Object[][]{
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))},
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))},
{new NettyDataBufferFactory(new PooledByteBufAllocator(true))},
{new NettyDataBufferFactory(new PooledByteBufAllocator(false))}};
@Override
public DataBufferFactory createDataBufferFactory() {
return new NettyDataBufferFactory(new UnpooledByteBufAllocator(true));
}
}
private PooledDataBuffer createDataBuffer(int capacity) {
return (PooledDataBuffer) dataBufferFactory.allocateBuffer(capacity);
@Nested
class UnpooledByteBufAllocatorWithPreferDirectFalseTests implements PooledDataBufferTestingTrait {
@Override
public DataBufferFactory createDataBufferFactory() {
return new NettyDataBufferFactory(new UnpooledByteBufAllocator(true));
}
}
@Test
public void retainAndRelease() {
PooledDataBuffer buffer = createDataBuffer(1);
buffer.write((byte) 'a');
@Nested
class PooledByteBufAllocatorWithPreferDirectTrueTests implements PooledDataBufferTestingTrait {
buffer.retain();
boolean result = buffer.release();
assertThat(result).isFalse();
result = buffer.release();
assertThat(result).isTrue();
@Override
public DataBufferFactory createDataBufferFactory() {
return new NettyDataBufferFactory(new PooledByteBufAllocator(true));
}
}
@Test
public void tooManyReleases() {
PooledDataBuffer buffer = createDataBuffer(1);
buffer.write((byte) 'a');
@Nested
class PooledByteBufAllocatorWithPreferDirectFalseTests implements PooledDataBufferTestingTrait {
buffer.release();
assertThatIllegalStateException().isThrownBy(
buffer::release);
@Override
public DataBufferFactory createDataBufferFactory() {
return new NettyDataBufferFactory(new PooledByteBufAllocator(true));
}
}
interface PooledDataBufferTestingTrait {
DataBufferFactory createDataBufferFactory();
default PooledDataBuffer createDataBuffer(int capacity) {
return (PooledDataBuffer) createDataBufferFactory().allocateBuffer(capacity);
}
@Test
default void retainAndRelease() {
PooledDataBuffer buffer = createDataBuffer(1);
buffer.write((byte) 'a');
buffer.retain();
assertThat(buffer.release()).isFalse();
assertThat(buffer.release()).isTrue();
}
@Test
default void tooManyReleases() {
PooledDataBuffer buffer = createDataBuffer(1);
buffer.write((byte) 'a');
buffer.release();
assertThatIllegalStateException().isThrownBy(buffer::release);
}
}
}

View File

@@ -18,20 +18,22 @@ package org.springframework.core.io.buffer.support;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTests;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
* @author Sam Brannen
*/
public class DataBufferTestUtilsTests extends AbstractDataBufferAllocatingTestCase {
class DataBufferTestUtilsTests extends AbstractDataBufferAllocatingTests {
@ParameterizedDataBufferAllocatingTest
public void dumpBytes(String displayName, DataBufferFactory bufferFactory) {
this.bufferFactory = bufferFactory;
@Test
public void dumpBytes() {
DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
byte[] source = {'a', 'b', 'c', 'd'};
buffer.write(source);
@@ -43,8 +45,10 @@ public class DataBufferTestUtilsTests extends AbstractDataBufferAllocatingTestCa
release(buffer);
}
@Test
public void dumpString() {
@ParameterizedDataBufferAllocatingTest
public void dumpString(String displayName, DataBufferFactory bufferFactory) {
this.bufferFactory = bufferFactory;
DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
String source = "abcd";
buffer.write(source.getBytes(StandardCharsets.UTF_8));