From b89a807d15fd925c21d3c875836145cd5302c8c9 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Thu, 27 May 2021 00:22:02 -0700 Subject: [PATCH] Migrate buffer tests to JUnit 5 (#11305) Motivation: JUnit 5 is more expressive, extensible, and composable in many ways, and it's better able to run tests in parallel. Modifications: Use JUnit5 in tests Result: Related to https://github.com/netty/netty/issues/10757 --- .../buffer/AbstractByteBufAllocatorTest.java | 10 +- .../io/netty/buffer/AbstractByteBufTest.java | 1768 +++++++++++++---- .../buffer/AbstractCompositeByteBufTest.java | 109 +- .../buffer/AbstractPooledByteBufTest.java | 23 +- .../AbstractReferenceCountedByteBufTest.java | 65 +- .../buffer/AdvancedLeakAwareByteBufTest.java | 4 +- .../buffer/BigEndianCompositeByteBufTest.java | 7 +- .../buffer/BigEndianDirectByteBufTest.java | 6 +- .../buffer/BigEndianHeapByteBufTest.java | 24 +- .../BigEndianUnsafeDirectByteBufTest.java | 8 +- ...ndianUnsafeNoCleanerDirectByteBufTest.java | 11 +- .../io/netty/buffer/ByteBufAllocatorTest.java | 4 +- .../netty/buffer/ByteBufDerivationTest.java | 2 +- .../io/netty/buffer/ByteBufStreamTest.java | 24 +- .../java/io/netty/buffer/ByteBufUtilTest.java | 413 ++-- .../io/netty/buffer/ByteProcessorTest.java | 4 +- .../io/netty/buffer/ConsolidationTest.java | 4 +- .../buffer/DefaultByteBufHolderTest.java | 7 +- .../netty/buffer/DuplicatedByteBufTest.java | 15 +- .../io/netty/buffer/EmptyByteBufTest.java | 10 +- .../buffer/FixedCompositeByteBufTest.java | 241 ++- .../buffer/LittleEndianDirectByteBufTest.java | 3 +- .../buffer/LittleEndianHeapByteBufTest.java | 2 +- .../LittleEndianUnsafeDirectByteBufTest.java | 8 +- ...ndianUnsafeNoCleanerDirectByteBufTest.java | 10 +- .../netty/buffer/LongPriorityQueueTest.java | 2 +- .../java/io/netty/buffer/PoolArenaTest.java | 46 +- ...oledAlignedBigEndianDirectByteBufTest.java | 10 +- .../PooledBigEndianDirectByteBufTest.java | 2 +- .../buffer/PooledByteBufAllocatorTest.java | 29 +- .../PooledLittleEndianDirectByteBufTest.java | 2 +- .../PooledLittleEndianHeapByteBufTest.java | 2 +- .../io/netty/buffer/ReadOnlyByteBufTest.java | 137 +- .../buffer/ReadOnlyByteBufferBufTest.java | 4 +- .../ReadOnlyDirectByteBufferBufTest.java | 199 +- ...ReadOnlyUnsafeDirectByteBufferBufTest.java | 14 +- .../buffer/RetainedDuplicatedByteBufTest.java | 2 +- .../buffer/RetainedSlicedByteBufTest.java | 5 +- .../buffer/SimpleLeakAwareByteBufTest.java | 14 +- .../SimpleLeakAwareCompositeByteBufTest.java | 16 +- .../io/netty/buffer/SlicedByteBufTest.java | 194 +- .../java/io/netty/buffer/UnpooledTest.java | 47 +- .../buffer/UnreleaseableByteBufTest.java | 10 +- .../netty/buffer/UnsafeByteBufUtilTest.java | 112 +- .../WrappedUnpooledUnsafeByteBufTest.java | 191 +- .../BitapSearchProcessorFactoryTest.java | 14 +- .../search/MultiSearchProcessorTest.java | 4 +- .../buffer/search/SearchProcessorTest.java | 109 +- 48 files changed, 2740 insertions(+), 1207 deletions(-) diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java index 2a1822bd45..e2f3ab72ef 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java @@ -16,11 +16,11 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public abstract class AbstractByteBufAllocatorTest extends ByteBufAllocatorTest { @@ -108,7 +108,7 @@ public abstract class AbstractByteBufAllocatorTest expectedException) { + final ByteBuf releasedBuffer = releasedBuffer(); + assertThrows(expectedException, new Executable() { + @Override + public void execute() { + releasedBuffer.internalNioBuffer(releasedBuffer.readerIndex(), 1); + } + }); + } + + @Test public void testNioBuffersAfterRelease() { - releasedBuffer().nioBuffers(); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().nioBuffers(); + } + }); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testNioBuffersAfterRelease2() { - releasedBuffer().nioBuffers(0, 1); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().nioBuffers(0, 1); + } + }); } @Test @@ -3382,14 +4190,24 @@ public abstract class AbstractByteBufTest { } } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testSliceAfterRelease() { - releasedBuffer().slice(); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().slice(); + } + }); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testSliceAfterRelease2() { - releasedBuffer().slice(0, 1); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().slice(0, 1); + } + }); } private static void assertSliceFailAfterRelease(ByteBuf... bufs) { @@ -3447,14 +4265,24 @@ public abstract class AbstractByteBufTest { assertSliceFailAfterRelease(buf, buf2, buf3); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testRetainedSliceAfterRelease() { - releasedBuffer().retainedSlice(); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().retainedSlice(); + } + }); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testRetainedSliceAfterRelease2() { - releasedBuffer().retainedSlice(0, 1); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().retainedSlice(0, 1); + } + }); } private static void assertRetainedSliceFailAfterRelease(ByteBuf... bufs) { @@ -3512,14 +4340,24 @@ public abstract class AbstractByteBufTest { assertRetainedSliceFailAfterRelease(buf, buf2, buf3); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testDuplicateAfterRelease() { - releasedBuffer().duplicate(); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().duplicate(); + } + }); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testRetainedDuplicateAfterRelease() { - releasedBuffer().retainedDuplicate(); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + releasedBuffer().retainedDuplicate(); + } + }); } private static void assertDuplicateFailAfterRelease(ByteBuf... bufs) { @@ -3608,14 +4446,24 @@ public abstract class AbstractByteBufTest { assertEquals(0, buf.refCnt()); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testReadSliceOutOfBounds() { - testReadSliceOutOfBounds(false); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testReadSliceOutOfBounds(false); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testReadRetainedSliceOutOfBounds() { - testReadSliceOutOfBounds(true); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testReadSliceOutOfBounds(true); + } + }); } private void testReadSliceOutOfBounds(boolean retainedSlice) { @@ -3664,24 +4512,44 @@ public abstract class AbstractByteBufTest { } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetUsAsciiCharSequenceNoExpand() { - testSetCharSequenceNoExpand(CharsetUtil.US_ASCII); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSetCharSequenceNoExpand(CharsetUtil.US_ASCII); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetUtf8CharSequenceNoExpand() { - testSetCharSequenceNoExpand(CharsetUtil.UTF_8); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSetCharSequenceNoExpand(CharsetUtil.UTF_8); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetIso88591CharSequenceNoExpand() { - testSetCharSequenceNoExpand(CharsetUtil.ISO_8859_1); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSetCharSequenceNoExpand(CharsetUtil.ISO_8859_1); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetUtf16CharSequenceNoExpand() { - testSetCharSequenceNoExpand(CharsetUtil.UTF_16); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSetCharSequenceNoExpand(CharsetUtil.UTF_16); + } + }); } private void testSetCharSequenceNoExpand(Charset charset) { @@ -3764,44 +4632,84 @@ public abstract class AbstractByteBufTest { buf.release(); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testRetainedSliceIndexOutOfBounds() { - testSliceOutOfBounds(true, true, true); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(true, true, true); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testRetainedSliceLengthOutOfBounds() { - testSliceOutOfBounds(true, true, false); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(true, true, false); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testMixedSliceAIndexOutOfBounds() { - testSliceOutOfBounds(true, false, true); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(true, false, true); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testMixedSliceALengthOutOfBounds() { - testSliceOutOfBounds(true, false, false); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(true, false, false); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testMixedSliceBIndexOutOfBounds() { - testSliceOutOfBounds(false, true, true); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(false, true, true); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testMixedSliceBLengthOutOfBounds() { - testSliceOutOfBounds(false, true, false); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(false, true, false); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSliceIndexOutOfBounds() { - testSliceOutOfBounds(false, false, true); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(false, false, true); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSliceLengthOutOfBounds() { - testSliceOutOfBounds(false, false, false); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + testSliceOutOfBounds(false, false, false); + } + }); } @Test @@ -4057,14 +4965,24 @@ public abstract class AbstractByteBufTest { testDuplicateCapacityChange(true); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSliceCapacityChange() { - testSliceCapacityChange(false); + assertThrows(UnsupportedOperationException.class, new Executable() { + @Override + public void execute() { + testSliceCapacityChange(false); + } + }); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testRetainedSliceCapacityChange() { - testSliceCapacityChange(true); + assertThrows(UnsupportedOperationException.class, new Executable() { + @Override + public void execute() { + testSliceCapacityChange(true); + } + }); } @Test @@ -4698,15 +5616,20 @@ public abstract class AbstractByteBufTest { } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testGetBytesByteBuffer() { byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; // Ensure destination buffer is bigger then what is in the ByteBuf. - ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); - ByteBuf buffer = newBuffer(bytes.length); + final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); + final ByteBuf buffer = newBuffer(bytes.length); try { buffer.writeBytes(bytes); - buffer.getBytes(buffer.readerIndex(), nioBuffer); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + buffer.getBytes(buffer.readerIndex(), nioBuffer); + } + }); } finally { buffer.release(); } @@ -4870,25 +5793,35 @@ public abstract class AbstractByteBufTest { } } - @Test(expected = IllegalArgumentException.class) + @Test public void testCapacityEnforceMaxCapacity() { - ByteBuf buffer = newBuffer(3, 13); + final ByteBuf buffer = newBuffer(3, 13); assertEquals(13, buffer.maxCapacity()); assertEquals(3, buffer.capacity()); try { - buffer.capacity(14); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + buffer.capacity(14); + } + }); } finally { buffer.release(); } } - @Test(expected = IllegalArgumentException.class) + @Test public void testCapacityNegative() { - ByteBuf buffer = newBuffer(3, 13); + final ByteBuf buffer = newBuffer(3, 13); assertEquals(13, buffer.maxCapacity()); assertEquals(3, buffer.capacity()); try { - buffer.capacity(-1); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + buffer.capacity(-1); + } + }); } finally { buffer.release(); } @@ -4922,12 +5855,12 @@ public abstract class AbstractByteBufTest { } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testReaderIndexLargerThanWriterIndex() { String content1 = "hello"; String content2 = "world"; int length = content1.length() + content2.length(); - ByteBuf buffer = newBuffer(length); + final ByteBuf buffer = newBuffer(length); buffer.setIndex(0, 0); buffer.writeCharSequence(content1, CharsetUtil.US_ASCII); buffer.markWriterIndex(); @@ -4937,7 +5870,12 @@ public abstract class AbstractByteBufTest { assertTrue(buffer.readerIndex() <= buffer.writerIndex()); try { - buffer.resetWriterIndex(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + buffer.resetWriterIndex(); + } + }); } finally { buffer.release(); } diff --git a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java index 4af2e2efbd..d4bbf30ce8 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java @@ -18,8 +18,9 @@ package io.netty.buffer; import io.netty.util.ReferenceCountUtil; import io.netty.util.internal.ObjectUtil; import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Test; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -39,14 +40,15 @@ import static io.netty.util.internal.EmptyArrays.EMPTY_BYTES; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * An abstract test class for composite channel buffers @@ -63,7 +65,7 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest { @Override protected ByteBuf newBuffer(int length, int maxCapacity) { - Assume.assumeTrue(maxCapacity == Integer.MAX_VALUE); + Assumptions.assumeTrue(maxCapacity == Integer.MAX_VALUE); List buffers = new ArrayList(); for (int i = 0; i < length + 45; i += 45) { @@ -1253,33 +1255,43 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest { cbuf.release(); } - @Test(expected = ConcurrentModificationException.class) + @Test public void testIteratorConcurrentModificationAdd() { CompositeByteBuf cbuf = newCompositeBuffer(); cbuf.addComponent(EMPTY_BUFFER); - Iterator it = cbuf.iterator(); + final Iterator it = cbuf.iterator(); cbuf.addComponent(EMPTY_BUFFER); assertTrue(it.hasNext()); try { - it.next(); + assertThrows(ConcurrentModificationException.class, new Executable() { + @Override + public void execute() { + it.next(); + } + }); } finally { cbuf.release(); } } - @Test(expected = ConcurrentModificationException.class) + @Test public void testIteratorConcurrentModificationRemove() { CompositeByteBuf cbuf = newCompositeBuffer(); cbuf.addComponent(EMPTY_BUFFER); - Iterator it = cbuf.iterator(); + final Iterator it = cbuf.iterator(); cbuf.removeComponent(0); assertTrue(it.hasNext()); try { - it.next(); + assertThrows(ConcurrentModificationException.class, new Executable() { + @Override + public void execute() { + it.next(); + } + }); } finally { cbuf.release(); } @@ -1564,52 +1576,67 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest { assertTrue(cbuf.release()); } - @Test(expected = IllegalArgumentException.class) + @Test public void testOverflowWhileAddingComponent() { int capacity = 1024 * 1024; // 1MB - ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity); - CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE); + final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity); + final CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE); try { - for (int i = 0; i >= 0; i += buffer.readableBytes()) { - ByteBuf duplicate = buffer.duplicate(); - compositeByteBuf.addComponent(duplicate); - duplicate.retain(); - } + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + for (int i = 0; i >= 0; i += buffer.readableBytes()) { + ByteBuf duplicate = buffer.duplicate(); + compositeByteBuf.addComponent(duplicate); + duplicate.retain(); + } + } + }); } finally { compositeByteBuf.release(); } } - @Test(expected = IllegalArgumentException.class) + @Test public void testOverflowWhileAddingComponentsViaVarargs() { int capacity = 1024 * 1024; // 1MB - ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity); - CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE); + final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity); + final CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE); try { - for (int i = 0; i >= 0; i += buffer.readableBytes()) { - ByteBuf duplicate = buffer.duplicate(); - compositeByteBuf.addComponents(duplicate); - duplicate.retain(); - } + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + for (int i = 0; i >= 0; i += buffer.readableBytes()) { + ByteBuf duplicate = buffer.duplicate(); + compositeByteBuf.addComponents(duplicate); + duplicate.retain(); + } + } + }); } finally { compositeByteBuf.release(); } } - @Test(expected = IllegalArgumentException.class) + @Test public void testOverflowWhileAddingComponentsViaIterable() { int capacity = 1024 * 1024; // 1MB - ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity); - CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE); + final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity); + final CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE); try { - for (int i = 0; i >= 0; i += buffer.readableBytes()) { - ByteBuf duplicate = buffer.duplicate(); - compositeByteBuf.addComponents(Collections.singletonList(duplicate)); - duplicate.retain(); - } + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + for (int i = 0; i >= 0; i += buffer.readableBytes()) { + ByteBuf duplicate = buffer.duplicate(); + compositeByteBuf.addComponents(Collections.singletonList(duplicate)); + duplicate.retain(); + } + } + }); } finally { compositeByteBuf.release(); } diff --git a/buffer/src/test/java/io/netty/buffer/AbstractPooledByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractPooledByteBufTest.java index 54797b2564..a4a0e61583 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractPooledByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractPooledByteBufTest.java @@ -15,15 +15,16 @@ */ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class AbstractPooledByteBufTest extends AbstractByteBufTest { @@ -51,12 +52,16 @@ public abstract class AbstractPooledByteBufTest extends AbstractByteBufTest { buf.release(); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void ensureWritableWithNotEnoughSpaceShouldThrow() { - ByteBuf buf = newBuffer(1, 10); + final ByteBuf buf = newBuffer(1, 10); try { - buf.ensureWritable(11); - fail(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + buf.ensureWritable(11); + } + }); } finally { buf.release(); } diff --git a/buffer/src/test/java/io/netty/buffer/AbstractReferenceCountedByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractReferenceCountedByteBufTest.java index 6df1787281..8525f670ff 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractReferenceCountedByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractReferenceCountedByteBufTest.java @@ -16,7 +16,8 @@ package io.netty.buffer; import io.netty.util.IllegalReferenceCountException; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.io.IOException; import java.io.InputStream; @@ -27,33 +28,49 @@ import java.nio.channels.FileChannel; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class AbstractReferenceCountedByteBufTest { - @Test(expected = IllegalReferenceCountException.class) + @Test public void testRetainOverflow() { - AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); + final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); referenceCounted.setRefCnt(Integer.MAX_VALUE); assertEquals(Integer.MAX_VALUE, referenceCounted.refCnt()); - referenceCounted.retain(); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + referenceCounted.retain(); + } + }); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testRetainOverflow2() { - AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); + final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); assertEquals(1, referenceCounted.refCnt()); - referenceCounted.retain(Integer.MAX_VALUE); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + referenceCounted.retain(Integer.MAX_VALUE); + } + }); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testReleaseOverflow() { - AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); + final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); referenceCounted.setRefCnt(0); assertEquals(0, referenceCounted.refCnt()); - referenceCounted.release(Integer.MAX_VALUE); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + referenceCounted.release(Integer.MAX_VALUE); + } + }); } @Test @@ -68,20 +85,30 @@ public class AbstractReferenceCountedByteBufTest { } } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testRetainResurrect() { - AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); + final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); assertTrue(referenceCounted.release()); assertEquals(0, referenceCounted.refCnt()); - referenceCounted.retain(); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + referenceCounted.retain(); + } + }); } - @Test(expected = IllegalReferenceCountException.class) + @Test public void testRetainResurrect2() { - AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); + final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted(); assertTrue(referenceCounted.release()); assertEquals(0, referenceCounted.refCnt()); - referenceCounted.retain(2); + assertThrows(IllegalReferenceCountException.class, new Executable() { + @Override + public void execute() { + referenceCounted.retain(2); + } + }); } private static AbstractReferenceCountedByteBuf newReferenceCounted() { diff --git a/buffer/src/test/java/io/netty/buffer/AdvancedLeakAwareByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AdvancedLeakAwareByteBufTest.java index 9c828e4d86..e79b35b277 100644 --- a/buffer/src/test/java/io/netty/buffer/AdvancedLeakAwareByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AdvancedLeakAwareByteBufTest.java @@ -16,9 +16,9 @@ package io.netty.buffer; import static io.netty.buffer.Unpooled.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import org.junit.Test; +import org.junit.jupiter.api.Test; import io.netty.util.CharsetUtil; import io.netty.util.ResourceLeakTracker; diff --git a/buffer/src/test/java/io/netty/buffer/BigEndianCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/BigEndianCompositeByteBufTest.java index e4b5827292..50d3e12d97 100644 --- a/buffer/src/test/java/io/netty/buffer/BigEndianCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/BigEndianCompositeByteBufTest.java @@ -15,8 +15,7 @@ */ package io.netty.buffer; - -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Tests big-endian composite channel buffers @@ -27,9 +26,9 @@ public class BigEndianCompositeByteBufTest extends AbstractCompositeByteBufTest } @Override - @Test(expected = UnsupportedOperationException.class) + @Test public void testInternalNioBufferAfterRelease() { - super.testInternalNioBufferAfterRelease(); + testInternalNioBufferAfterRelease0(UnsupportedOperationException.class); } } diff --git a/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java index 70f2f33f4d..e7a1463074 100644 --- a/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java @@ -15,11 +15,13 @@ */ package io.netty.buffer; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.ByteOrder; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Tests big-endian direct channel buffers diff --git a/buffer/src/test/java/io/netty/buffer/BigEndianHeapByteBufTest.java b/buffer/src/test/java/io/netty/buffer/BigEndianHeapByteBufTest.java index b54e9a9824..e9accda9b0 100644 --- a/buffer/src/test/java/io/netty/buffer/BigEndianHeapByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/BigEndianHeapByteBufTest.java @@ -15,9 +15,11 @@ */ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Tests big-endian heap channel buffers @@ -31,13 +33,23 @@ public class BigEndianHeapByteBufTest extends AbstractByteBufTest { return buffer; } - @Test(expected = NullPointerException.class) + @Test public void shouldNotAllowNullInConstructor1() { - new UnpooledHeapByteBuf(null, new byte[1], 0); + assertThrows(NullPointerException.class, new Executable() { + @Override + public void execute() { + new UnpooledHeapByteBuf(null, new byte[1], 0); + } + }); } - @Test(expected = NullPointerException.class) + @Test public void shouldNotAllowNullInConstructor2() { - new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT, null, 0); + assertThrows(NullPointerException.class, new Executable() { + @Override + public void execute() { + new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT, null, 0); + } + }); } } diff --git a/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java index bfc82f6729..a2de7f653b 100644 --- a/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java @@ -17,15 +17,15 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Before; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; public class BigEndianUnsafeDirectByteBufTest extends BigEndianDirectByteBufTest { - @Before + @BeforeEach @Override public void init() { - Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe()); + Assumptions.assumeTrue(PlatformDependent.hasUnsafe(), "sun.misc.Unsafe not found, skip tests"); super.init(); } diff --git a/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeNoCleanerDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeNoCleanerDirectByteBufTest.java index 1c06d9f91f..19c053e459 100644 --- a/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeNoCleanerDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeNoCleanerDirectByteBufTest.java @@ -15,18 +15,17 @@ */ package io.netty.buffer; - import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Before; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; public class BigEndianUnsafeNoCleanerDirectByteBufTest extends BigEndianDirectByteBufTest { - @Before + @BeforeEach @Override public void init() { - Assume.assumeTrue("java.nio.DirectByteBuffer.(long, int) not found, skip tests", - PlatformDependent.useDirectBufferNoCleaner()); + Assumptions.assumeTrue(PlatformDependent.useDirectBufferNoCleaner(), + "java.nio.DirectByteBuffer.(long, int) not found, skip tests"); super.init(); } diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java index 64c288303a..b538953c76 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java @@ -15,9 +15,9 @@ */ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public abstract class ByteBufAllocatorTest { diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java index 5cec06c323..ab4a59c3c7 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java @@ -16,7 +16,7 @@ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.ByteOrder; import java.util.Random; diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufStreamTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufStreamTest.java index 87b7c14a65..8c3375b74e 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufStreamTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufStreamTest.java @@ -15,13 +15,22 @@ */ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.io.EOFException; +import java.io.IOException; import java.nio.charset.Charset; import static io.netty.util.internal.EmptyArrays.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * Tests channel buffer streams @@ -279,15 +288,20 @@ public class ByteBufStreamTest { in2.close(); } - @Test(expected = EOFException.class) + @Test public void testReadByteLengthRespected() throws Exception { // case1 ByteBuf buf = Unpooled.buffer(16); buf.writeBytes(new byte[] { 1, 2, 3, 4, 5, 6 }); - ByteBufInputStream in = new ByteBufInputStream(buf, 0); + final ByteBufInputStream in = new ByteBufInputStream(buf, 0); try { - in.readByte(); + assertThrows(EOFException.class, new Executable() { + @Override + public void execute() throws IOException { + in.readBoolean(); + } + }); } finally { buf.release(); in.close(); diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java index fd6aba2948..7bbdbe70cb 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java @@ -17,9 +17,10 @@ package io.netty.buffer; import io.netty.util.AsciiString; import io.netty.util.CharsetUtil; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.nio.ByteOrder; import java.nio.charset.Charset; @@ -35,23 +36,22 @@ import static io.netty.buffer.Unpooled.unreleasableBuffer; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.*; -import static org.junit.Assume.assumeThat; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assumptions.assumeTrue; -@RunWith(Parameterized.class) public class ByteBufUtilTest { + private static final String PARAMETERIZED_NAME = "bufferType = {0}"; private enum BufferType { DIRECT_UNPOOLED, DIRECT_POOLED, HEAP_POOLED, HEAP_UNPOOLED } - private final BufferType bufferType; - - public ByteBufUtilTest(BufferType bufferType) { - this.bufferType = bufferType; - } - - private ByteBuf buffer(int capacity) { + private ByteBuf buffer(BufferType bufferType, int capacity) { switch (bufferType) { case DIRECT_UNPOOLED: @@ -67,7 +67,6 @@ public class ByteBufUtilTest { } } - @Parameterized.Parameters(name = "bufferType = {0}") public static Collection noUnsafe() { return Arrays.asList(new Object[][] { { BufferType.DIRECT_POOLED }, @@ -99,14 +98,24 @@ public class ByteBufUtilTest { } } - @Test(expected = IllegalArgumentException.class) + @Test public void decodeHexDumpWithOddLength() { - ByteBufUtil.decodeHexDump("abc"); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() throws Throwable { + ByteBufUtil.decodeHexDump("abc"); + } + }); } - @Test(expected = IllegalArgumentException.class) + @Test public void decodeHexDumpWithInvalidChar() { - ByteBufUtil.decodeHexDump("fg"); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() throws Throwable { + ByteBufUtil.decodeHexDump("fg"); + } + }); } @Test @@ -160,10 +169,10 @@ public class ByteBufUtilTest { Math.max(b1.length, b2.length) * 2)); } - @Test (expected = IllegalArgumentException.class) + @Test public void notEqualsBufferUnderflow() { - byte[] b1 = new byte[8]; - byte[] b2 = new byte[16]; + final byte[] b1 = new byte[8]; + final byte[] b2 = new byte[16]; Random rand = new Random(); rand.nextBytes(b1); rand.nextBytes(b2); @@ -171,23 +180,28 @@ public class ByteBufUtilTest { final int iB2 = iB1 + b1.length; final int length = b1.length - iB1; System.arraycopy(b1, iB1, b2, iB2, length - 1); - assertFalse(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, - -1)); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, -1); + } + }); } @SuppressWarnings("deprecation") - @Test - public void writeShortBE() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void writeShortBE(BufferType bufferType) { int expected = 0x1234; - ByteBuf buf = buffer(2).order(ByteOrder.BIG_ENDIAN); + ByteBuf buf = buffer(bufferType, 2).order(ByteOrder.BIG_ENDIAN); ByteBufUtil.writeShortBE(buf, expected); assertEquals(expected, buf.readShort()); buf.resetReaderIndex(); assertEquals(ByteBufUtil.swapShort((short) expected), buf.readShortLE()); buf.release(); - buf = buffer(2).order(ByteOrder.LITTLE_ENDIAN); + buf = buffer(bufferType, 2).order(ByteOrder.LITTLE_ENDIAN); ByteBufUtil.writeShortBE(buf, expected); assertEquals(ByteBufUtil.swapShort((short) expected), buf.readShortLE()); buf.resetReaderIndex(); @@ -216,18 +230,19 @@ public class ByteBufUtilTest { } @SuppressWarnings("deprecation") - @Test - public void writeMediumBE() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void writeMediumBE(BufferType bufferType) { int mediumValue = 0x123456; - ByteBuf buf = buffer(4).order(ByteOrder.BIG_ENDIAN); + ByteBuf buf = buffer(bufferType, 4).order(ByteOrder.BIG_ENDIAN); ByteBufUtil.writeMediumBE(buf, mediumValue); assertEquals(mediumValue, buf.readMedium()); buf.resetReaderIndex(); assertEquals(ByteBufUtil.swapMedium(mediumValue), buf.readMediumLE()); buf.release(); - buf = buffer(4).order(ByteOrder.LITTLE_ENDIAN); + buf = buffer(bufferType, 4).order(ByteOrder.LITTLE_ENDIAN); ByteBufUtil.writeMediumBE(buf, mediumValue); assertEquals(ByteBufUtil.swapMedium(mediumValue), buf.readMediumLE()); buf.resetReaderIndex(); @@ -235,12 +250,13 @@ public class ByteBufUtilTest { buf.release(); } - @Test - public void testWriteUsAscii() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUsAscii(BufferType bufferType) { String usAscii = "NettyRocks"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeAscii(buf2, usAscii); assertEquals(buf, buf2); @@ -249,12 +265,13 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUsAsciiSwapped() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUsAsciiSwapped(BufferType bufferType) { String usAscii = "NettyRocks"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); - SwappedByteBuf buf2 = new SwappedByteBuf(buffer(16)); + SwappedByteBuf buf2 = new SwappedByteBuf(buffer(bufferType, 16)); ByteBufUtil.writeAscii(buf2, usAscii); assertEquals(buf, buf2); @@ -263,13 +280,14 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUsAsciiWrapped() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUsAsciiWrapped(BufferType bufferType) { String usAscii = "NettyRocks"; - ByteBuf buf = unreleasableBuffer(buffer(16)); + ByteBuf buf = unreleasableBuffer(buffer(bufferType, 16)); assertWrapped(buf); buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); - ByteBuf buf2 = unreleasableBuffer(buffer(16)); + ByteBuf buf2 = unreleasableBuffer(buffer(bufferType, 16)); assertWrapped(buf2); ByteBufUtil.writeAscii(buf2, usAscii); @@ -279,13 +297,14 @@ public class ByteBufUtilTest { buf2.unwrap().release(); } - @Test - public void testWriteUsAsciiComposite() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUsAsciiComposite(BufferType bufferType) { String usAscii = "NettyRocks"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); ByteBuf buf2 = Unpooled.compositeBuffer().addComponent( - buffer(8)).addComponent(buffer(24)); + buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)); // write some byte so we start writing with an offset. buf2.writeByte(1); ByteBufUtil.writeAscii(buf2, usAscii); @@ -297,13 +316,14 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUsAsciiCompositeWrapped() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUsAsciiCompositeWrapped(BufferType bufferType) { String usAscii = "NettyRocks"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent( - buffer(8)).addComponent(buffer(24))); + buffer(bufferType, 8)).addComponent(buffer(bufferType, 24))); // write some byte so we start writing with an offset. buf2.writeByte(1); ByteBufUtil.writeAscii(buf2, usAscii); @@ -315,12 +335,13 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8(BufferType bufferType) { String usAscii = "Some UTF-8 like äÄ∏ŒŒ"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, usAscii); assertEquals(buf, buf2); @@ -329,13 +350,14 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8Composite() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8Composite(BufferType bufferType) { String utf8 = "Some UTF-8 like äÄ∏ŒŒ"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8)); ByteBuf buf2 = Unpooled.compositeBuffer().addComponent( - buffer(8)).addComponent(buffer(24)); + buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)); // write some byte so we start writing with an offset. buf2.writeByte(1); ByteBufUtil.writeUtf8(buf2, utf8); @@ -347,13 +369,14 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8CompositeWrapped() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8CompositeWrapped(BufferType bufferType) { String utf8 = "Some UTF-8 like äÄ∏ŒŒ"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8)); ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent( - buffer(8)).addComponent(buffer(24))); + buffer(bufferType, 8)).addComponent(buffer(bufferType, 24))); // write some byte so we start writing with an offset. buf2.writeByte(1); ByteBufUtil.writeUtf8(buf2, utf8); @@ -365,8 +388,9 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8Surrogates() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8Surrogates(BufferType bufferType) { // leading surrogate + trailing surrogate String surrogateString = new StringBuilder(2) .append('a') @@ -374,9 +398,9 @@ public class ByteBufUtilTest { .append('\uDC00') .append('b') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -386,16 +410,17 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8InvalidOnlyTrailingSurrogate() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidOnlyTrailingSurrogate(BufferType bufferType) { String surrogateString = new StringBuilder(2) .append('a') .append('\uDC00') .append('b') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -405,16 +430,17 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8InvalidOnlyLeadingSurrogate() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidOnlyLeadingSurrogate(BufferType bufferType) { String surrogateString = new StringBuilder(2) .append('a') .append('\uD800') .append('b') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -424,17 +450,18 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8InvalidSurrogatesSwitched() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidSurrogatesSwitched(BufferType bufferType) { String surrogateString = new StringBuilder(2) .append('a') .append('\uDC00') .append('\uD800') .append('b') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -444,17 +471,18 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8InvalidTwoLeadingSurrogates() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidTwoLeadingSurrogates(BufferType bufferType) { String surrogateString = new StringBuilder(2) .append('a') .append('\uD800') .append('\uD800') .append('b') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -463,17 +491,18 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8InvalidTwoTrailingSurrogates() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidTwoTrailingSurrogates(BufferType bufferType) { String surrogateString = new StringBuilder(2) .append('a') .append('\uDC00') .append('\uDC00') .append('b') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -483,14 +512,15 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8InvalidEndOnLeadingSurrogate() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidEndOnLeadingSurrogate(BufferType bufferType) { String surrogateString = new StringBuilder(2) .append('\uD800') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -500,14 +530,15 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8InvalidEndOnTrailingSurrogate() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidEndOnTrailingSurrogate(BufferType bufferType) { String surrogateString = new StringBuilder(2) .append('\uDC00') .toString(); - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, surrogateString); assertEquals(buf, buf2); @@ -517,13 +548,14 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUsAsciiString() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUsAsciiString(BufferType bufferType) { AsciiString usAscii = new AsciiString("NettyRocks"); int expectedCapacity = usAscii.length(); - ByteBuf buf = buffer(expectedCapacity); + ByteBuf buf = buffer(bufferType, expectedCapacity); buf.writeBytes(usAscii.toString().getBytes(CharsetUtil.US_ASCII)); - ByteBuf buf2 = buffer(expectedCapacity); + ByteBuf buf2 = buffer(bufferType, expectedCapacity); ByteBufUtil.writeAscii(buf2, usAscii); assertEquals(buf, buf2); @@ -532,13 +564,14 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8Wrapped() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8Wrapped(BufferType bufferType) { String usAscii = "Some UTF-8 like äÄ∏ŒŒ"; - ByteBuf buf = unreleasableBuffer(buffer(16)); + ByteBuf buf = unreleasableBuffer(buffer(bufferType, 16)); assertWrapped(buf); buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = unreleasableBuffer(buffer(16)); + ByteBuf buf2 = unreleasableBuffer(buffer(bufferType, 16)); assertWrapped(buf2); ByteBufUtil.writeUtf8(buf2, usAscii); @@ -552,12 +585,13 @@ public class ByteBufUtilTest { assertTrue(buf instanceof WrappedByteBuf); } - @Test - public void testWriteUtf8Subsequence() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8Subsequence(BufferType bufferType) { String usAscii = "Some UTF-8 like äÄ∏ŒŒ"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.substring(5, 18).getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, usAscii, 5, 18); assertEquals(buf, buf2); @@ -566,12 +600,13 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testWriteUtf8SubsequenceSplitSurrogate() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8SubsequenceSplitSurrogate(BufferType bufferType) { String usAscii = "\uD800\uDC00"; // surrogate pair: one code point, two chars - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.substring(0, 1).getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); ByteBufUtil.writeUtf8(buf2, usAscii, 0, 1); assertEquals(buf, buf2); @@ -580,12 +615,13 @@ public class ByteBufUtilTest { buf2.release(); } - @Test - public void testReserveAndWriteUtf8Subsequence() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testReserveAndWriteUtf8Subsequence(BufferType bufferType) { String usAscii = "Some UTF-8 like äÄ∏ŒŒ"; - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); buf.writeBytes(usAscii.substring(5, 18).getBytes(CharsetUtil.UTF_8)); - ByteBuf buf2 = buffer(16); + ByteBuf buf2 = buffer(bufferType, 16); int count = ByteBufUtil.reserveAndWriteUtf8(buf2, usAscii, 5, 18, 16); assertEquals(buf, buf2); @@ -610,9 +646,9 @@ public class ByteBufUtilTest { int invoke(Object... args); } - private void testInvalidSubsequences(TestMethod method) { + private void testInvalidSubsequences(BufferType bufferType, TestMethod method) { for (int [] range : INVALID_RANGES) { - ByteBuf buf = buffer(16); + ByteBuf buf = buffer(bufferType, 16); try { method.invoke(buf, "Some UTF-8 like äÄ∏ŒŒ", range[0], range[1]); fail("Did not throw IndexOutOfBoundsException for range (" + range[0] + ", " + range[1] + ")"); @@ -625,9 +661,10 @@ public class ByteBufUtilTest { } } - @Test - public void testWriteUtf8InvalidSubsequences() { - testInvalidSubsequences(new TestMethod() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testWriteUtf8InvalidSubsequences(BufferType bufferType) { + testInvalidSubsequences(bufferType, new TestMethod() { @Override public int invoke(Object... args) { return ByteBufUtil.writeUtf8((ByteBuf) args[0], (String) args[1], @@ -636,9 +673,10 @@ public class ByteBufUtilTest { }); } - @Test - public void testReserveAndWriteUtf8InvalidSubsequences() { - testInvalidSubsequences(new TestMethod() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testReserveAndWriteUtf8InvalidSubsequences(BufferType bufferType) { + testInvalidSubsequences(bufferType, new TestMethod() { @Override public int invoke(Object... args) { return ByteBufUtil.reserveAndWriteUtf8((ByteBuf) args[0], (String) args[1], @@ -647,14 +685,16 @@ public class ByteBufUtilTest { }); } - @Test - public void testUtf8BytesInvalidSubsequences() { - testInvalidSubsequences(new TestMethod() { - @Override - public int invoke(Object... args) { - return ByteBufUtil.utf8Bytes((String) args[1], (Integer) args[2], (Integer) args[3]); - } - }); + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testUtf8BytesInvalidSubsequences(BufferType bufferType) { + testInvalidSubsequences(bufferType, + new TestMethod() { + @Override + public int invoke(Object... args) { + return ByteBufUtil.utf8Bytes((String) args[1], (Integer) args[2], (Integer) args[3]); + } + }); } @Test @@ -673,21 +713,23 @@ public class ByteBufUtilTest { buffer.release(); } - @Test - public void testToStringDoesNotThrowIndexOutOfBounds() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testToStringDoesNotThrowIndexOutOfBounds(BufferType bufferType) { CompositeByteBuf buffer = Unpooled.compositeBuffer(); try { byte[] bytes = "1234".getBytes(CharsetUtil.UTF_8); - buffer.addComponent(buffer(bytes.length).writeBytes(bytes)); - buffer.addComponent(buffer(bytes.length).writeBytes(bytes)); + buffer.addComponent(buffer(bufferType, bytes.length).writeBytes(bytes)); + buffer.addComponent(buffer(bufferType, bytes.length).writeBytes(bytes)); assertEquals("1234", buffer.toString(bytes.length, bytes.length, CharsetUtil.UTF_8)); } finally { buffer.release(); } } - @Test - public void testIsTextWithUtf8() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testIsTextWithUtf8(BufferType bufferType) { byte[][] validUtf8Bytes = { "netty".getBytes(CharsetUtil.UTF_8), {(byte) 0x24}, @@ -700,7 +742,7 @@ public class ByteBufUtilTest { (byte) 0xF0, (byte) 0x90, (byte) 0x8D, (byte) 0x88} // multiple characters }; for (byte[] bytes : validUtf8Bytes) { - assertIsText(bytes, true, CharsetUtil.UTF_8); + assertIsText(bufferType, bytes, true, CharsetUtil.UTF_8); } byte[][] invalidUtf8Bytes = { {(byte) 0x80}, @@ -716,31 +758,34 @@ public class ByteBufUtilTest { {(byte) 0xED, (byte) 0xAF, (byte) 0x80} // out of upper bound }; for (byte[] bytes : invalidUtf8Bytes) { - assertIsText(bytes, false, CharsetUtil.UTF_8); + assertIsText(bufferType, bytes, false, CharsetUtil.UTF_8); } } - @Test - public void testIsTextWithoutOptimization() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testIsTextWithoutOptimization(BufferType bufferType) { byte[] validBytes = {(byte) 0x01, (byte) 0xD8, (byte) 0x37, (byte) 0xDC}; byte[] invalidBytes = {(byte) 0x01, (byte) 0xD8}; - assertIsText(validBytes, true, CharsetUtil.UTF_16LE); - assertIsText(invalidBytes, false, CharsetUtil.UTF_16LE); + assertIsText(bufferType, validBytes, true, CharsetUtil.UTF_16LE); + assertIsText(bufferType, invalidBytes, false, CharsetUtil.UTF_16LE); } - @Test - public void testIsTextWithAscii() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testIsTextWithAscii(BufferType bufferType) { byte[] validBytes = {(byte) 0x00, (byte) 0x01, (byte) 0x37, (byte) 0x7F}; byte[] invalidBytes = {(byte) 0x80, (byte) 0xFF}; - assertIsText(validBytes, true, CharsetUtil.US_ASCII); - assertIsText(invalidBytes, false, CharsetUtil.US_ASCII); + assertIsText(bufferType, validBytes, true, CharsetUtil.US_ASCII); + assertIsText(bufferType, invalidBytes, false, CharsetUtil.US_ASCII); } - @Test - public void testIsTextWithInvalidIndexAndLength() { - ByteBuf buffer = buffer(4); + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testIsTextWithInvalidIndexAndLength(BufferType bufferType) { + ByteBuf buffer = buffer(bufferType, 4); try { buffer.writeBytes(new byte[4]); int[][] validIndexLengthPairs = { @@ -772,33 +817,37 @@ public class ByteBufUtilTest { } } - @Test - public void testUtf8Bytes() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testUtf8Bytes(BufferType bufferType) { final String s = "Some UTF-8 like äÄ∏ŒŒ"; - checkUtf8Bytes(s); + checkUtf8Bytes(bufferType, s); } - @Test - public void testUtf8BytesWithSurrogates() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testUtf8BytesWithSurrogates(BufferType bufferType) { final String s = "a\uD800\uDC00b"; - checkUtf8Bytes(s); + checkUtf8Bytes(bufferType, s); } - @Test - public void testUtf8BytesWithNonSurrogates3Bytes() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testUtf8BytesWithNonSurrogates3Bytes(BufferType bufferType) { final String s = "a\uE000b"; - checkUtf8Bytes(s); + checkUtf8Bytes(bufferType, s); } - @Test - public void testUtf8BytesWithNonSurrogatesNonAscii() { + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testUtf8BytesWithNonSurrogatesNonAscii(BufferType bufferType) { final char nonAscii = (char) 0x81; final String s = "a" + nonAscii + "b"; - checkUtf8Bytes(s); + checkUtf8Bytes(bufferType, s); } - private void checkUtf8Bytes(final CharSequence charSequence) { - final ByteBuf buf = buffer(ByteBufUtil.utf8MaxBytes(charSequence)); + private void checkUtf8Bytes(BufferType bufferType, final CharSequence charSequence) { + final ByteBuf buf = buffer(bufferType, ByteBufUtil.utf8MaxBytes(charSequence)); try { final int writtenBytes = ByteBufUtil.writeUtf8(buf, charSequence); final int utf8Bytes = ByteBufUtil.utf8Bytes(charSequence); @@ -808,8 +857,8 @@ public class ByteBufUtilTest { } } - private void assertIsText(byte[] bytes, boolean expected, Charset charset) { - ByteBuf buffer = buffer(bytes.length); + private void assertIsText(BufferType bufferType, byte[] bytes, boolean expected, Charset charset) { + ByteBuf buffer = buffer(bufferType, bytes.length); try { buffer.writeBytes(bytes); assertEquals(expected, ByteBufUtil.isText(buffer, charset)); @@ -818,9 +867,10 @@ public class ByteBufUtilTest { } } - @Test - public void testIsTextMultiThreaded() throws Throwable { - assumeThat(bufferType, is(BufferType.HEAP_UNPOOLED)); + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testIsTextMultiThreaded(BufferType bufferType) throws Throwable { + assumeTrue(bufferType == BufferType.HEAP_UNPOOLED); final ByteBuf buffer = Unpooled.copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1); try { @@ -859,9 +909,10 @@ public class ByteBufUtilTest { } } - @Test - public void testGetBytes() { - final ByteBuf buf = buffer(4); + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testGetBytes(BufferType bufferType) { + final ByteBuf buf = buffer(bufferType, 4); try { checkGetBytes(buf); } finally { @@ -869,10 +920,11 @@ public class ByteBufUtilTest { } } - @Test - public void testGetBytesHeapWithNonZeroArrayOffset() { - assumeThat(bufferType, is(BufferType.HEAP_UNPOOLED)); - final ByteBuf buf = buffer(5); + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testGetBytesHeapWithNonZeroArrayOffset(BufferType bufferType) { + assumeTrue(bufferType == BufferType.HEAP_UNPOOLED); + final ByteBuf buf = buffer(bufferType, 5); try { buf.setByte(0, 0x05); @@ -889,10 +941,11 @@ public class ByteBufUtilTest { } } - @Test - public void testGetBytesHeapWithArrayLengthGreaterThanCapacity() { - assumeThat(bufferType, is(BufferType.HEAP_UNPOOLED)); - final ByteBuf buf = buffer(5); + @ParameterizedTest(name = PARAMETERIZED_NAME) + @MethodSource("noUnsafe") + public void testGetBytesHeapWithArrayLengthGreaterThanCapacity(BufferType bufferType) { + assumeTrue(bufferType == BufferType.HEAP_UNPOOLED); + final ByteBuf buf = buffer(bufferType, 5); try { buf.setByte(4, 0x05); diff --git a/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java b/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java index ac09d3b233..01c7b7f82f 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java @@ -16,11 +16,11 @@ package io.netty.buffer; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import io.netty.util.ByteProcessor; import io.netty.util.CharsetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ByteProcessorTest { @Test diff --git a/buffer/src/test/java/io/netty/buffer/ConsolidationTest.java b/buffer/src/test/java/io/netty/buffer/ConsolidationTest.java index 2bd94096d7..6d304ccae7 100644 --- a/buffer/src/test/java/io/netty/buffer/ConsolidationTest.java +++ b/buffer/src/test/java/io/netty/buffer/ConsolidationTest.java @@ -16,10 +16,10 @@ package io.netty.buffer; import io.netty.util.CharsetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static io.netty.buffer.Unpooled.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Tests buffer consolidation diff --git a/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java b/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java index 8c490d7c96..2a623aa0ee 100644 --- a/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java +++ b/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java @@ -15,9 +15,12 @@ */ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class DefaultByteBufHolderTest { diff --git a/buffer/src/test/java/io/netty/buffer/DuplicatedByteBufTest.java b/buffer/src/test/java/io/netty/buffer/DuplicatedByteBufTest.java index bc01d80e36..201d1ed532 100644 --- a/buffer/src/test/java/io/netty/buffer/DuplicatedByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/DuplicatedByteBufTest.java @@ -15,9 +15,11 @@ */ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Tests duplicated channel buffers @@ -40,9 +42,14 @@ public class DuplicatedByteBufTest extends AbstractByteBufTest { buf.release(); } - @Test(expected = NullPointerException.class) + @Test public void shouldNotAllowNullInConstructor() { - new DuplicatedByteBuf(null); + assertThrows(NullPointerException.class, new Executable() { + @Override + public void execute() { + new DuplicatedByteBuf(null); + } + }); } // See https://github.com/netty/netty/issues/1800 diff --git a/buffer/src/test/java/io/netty/buffer/EmptyByteBufTest.java b/buffer/src/test/java/io/netty/buffer/EmptyByteBufTest.java index 42aea1edd2..3ea80124bf 100644 --- a/buffer/src/test/java/io/netty/buffer/EmptyByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/EmptyByteBufTest.java @@ -12,14 +12,18 @@ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. - */package io.netty.buffer; + */ +package io.netty.buffer; import io.netty.util.CharsetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class EmptyByteBufTest { diff --git a/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java index 8c5d4a60fd..df53c10d81 100644 --- a/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java @@ -15,9 +15,9 @@ */ package io.netty.buffer; - -import org.junit.Assume; -import org.junit.Test; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -28,7 +28,12 @@ import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; import static io.netty.buffer.Unpooled.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class FixedCompositeByteBufTest { @@ -36,147 +41,207 @@ public class FixedCompositeByteBufTest { return new FixedCompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, buffers); } - @Test(expected = ReadOnlyBufferException.class) + @Test public void testSetBoolean() { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setBoolean(0, true); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setBoolean(0, true); + } + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) + @Test public void testSetByte() { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setByte(0, 1); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setByte(0, 1); + } + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) + @Test public void testSetBytesWithByteBuf() { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); - ByteBuf src = wrappedBuffer(new byte[4]); + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + final ByteBuf src = wrappedBuffer(new byte[4]); try { - buf.setBytes(0, src); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setBytes(0, src); + } + }); } finally { buf.release(); src.release(); } } - @Test(expected = ReadOnlyBufferException.class) + @Test public void testSetBytesWithByteBuffer() { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setBytes(0, ByteBuffer.wrap(new byte[4])); - } finally { - buf.release(); - } - } - - @Test(expected = ReadOnlyBufferException.class) - public void testSetBytesWithInputStream() throws IOException { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); - try { - buf.setBytes(0, new ByteArrayInputStream(new byte[4]), 4); - } finally { - buf.release(); - } - } - - @Test(expected = ReadOnlyBufferException.class) - public void testSetBytesWithChannel() throws IOException { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); - try { - buf.setBytes(0, new ScatteringByteChannel() { + assertThrows(ReadOnlyBufferException.class, new Executable() { @Override - public long read(ByteBuffer[] dsts, int offset, int length) { - return 0; + public void execute() { + buf.setBytes(0, ByteBuffer.wrap(new byte[4])); } - - @Override - public long read(ByteBuffer[] dsts) { - return 0; - } - - @Override - public int read(ByteBuffer dst) { - return 0; - } - - @Override - public boolean isOpen() { - return true; - } - - @Override - public void close() { - } - }, 4); + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) - public void testSetChar() throws IOException { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + @Test + public void testSetBytesWithInputStream() { + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setChar(0, 'b'); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() throws IOException { + buf.setBytes(0, new ByteArrayInputStream(new byte[4]), 4); + } + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) - public void testSetDouble() throws IOException { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + @Test + public void testSetBytesWithChannel() { + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setDouble(0, 1); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() throws IOException { + buf.setBytes(0, new ScatteringByteChannel() { + @Override + public long read(ByteBuffer[] dsts, int offset, int length) { + return 0; + } + + @Override + public long read(ByteBuffer[] dsts) { + return 0; + } + + @Override + public int read(ByteBuffer dst) { + return 0; + } + + @Override + public boolean isOpen() { + return true; + } + + @Override + public void close() { + } + }, 4); + } + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) - public void testSetFloat() throws IOException { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + @Test + public void testSetChar() { + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setFloat(0, 1); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setChar(0, 'b'); + } + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) + @Test + public void testSetDouble() { + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + try { + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setDouble(0, 1); + } + }); + } finally { + buf.release(); + } + } + + @Test + public void testSetFloat() { + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + try { + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setFloat(0, 1); + } + }); + } finally { + buf.release(); + } + } + + @Test public void testSetInt() throws IOException { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setInt(0, 1); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setInt(0, 1); + } + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) + @Test public void testSetLong() { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setLong(0, 1); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setLong(0, 1); + } + }); } finally { buf.release(); } } - @Test(expected = ReadOnlyBufferException.class) - public void testSetMedium() throws IOException { - ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); + @Test + public void testSetMedium() { + final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8])); try { - buf.setMedium(0, 1); + assertThrows(ReadOnlyBufferException.class, new Executable() { + @Override + public void execute() { + buf.setMedium(0, 1); + } + }); } finally { buf.release(); } @@ -394,7 +459,7 @@ public class FixedCompositeByteBufTest { @Test public void testHasMemoryAddressWhenEmpty() { - Assume.assumeTrue(EMPTY_BUFFER.hasMemoryAddress()); + Assumptions.assumeTrue(EMPTY_BUFFER.hasMemoryAddress()); ByteBuf buf = newBuffer(new ByteBuf[0]); assertTrue(buf.hasMemoryAddress()); assertEquals(EMPTY_BUFFER.memoryAddress(), buf.memoryAddress()); @@ -441,15 +506,19 @@ public class FixedCompositeByteBufTest { buf.release(); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testHasNoArrayWhenMultipleBuffers() { ByteBuf buf1 = buffer(10); ByteBuf buf2 = buffer(10); - ByteBuf buf = newBuffer(buf1, buf2); + final ByteBuf buf = newBuffer(buf1, buf2); assertFalse(buf.hasArray()); try { - buf.array(); - fail(); + assertThrows(UnsupportedOperationException.class, new Executable() { + @Override + public void execute() { + buf.array(); + } + }); } finally { buf.release(); } diff --git a/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java index 40fa35a176..6fa0ed0161 100644 --- a/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java @@ -15,7 +15,8 @@ */ package io.netty.buffer; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import java.nio.ByteOrder; diff --git a/buffer/src/test/java/io/netty/buffer/LittleEndianHeapByteBufTest.java b/buffer/src/test/java/io/netty/buffer/LittleEndianHeapByteBufTest.java index ddb20b05f2..077873425c 100644 --- a/buffer/src/test/java/io/netty/buffer/LittleEndianHeapByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/LittleEndianHeapByteBufTest.java @@ -15,7 +15,7 @@ */ package io.netty.buffer; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.nio.ByteOrder; diff --git a/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java index c0a4e6c8cf..57eff37ba4 100644 --- a/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java @@ -16,15 +16,15 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Before; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; public class LittleEndianUnsafeDirectByteBufTest extends LittleEndianDirectByteBufTest { - @Before + @BeforeEach @Override public void init() { - Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe()); + Assumptions.assumeTrue(PlatformDependent.hasUnsafe(), "sun.misc.Unsafe not found, skip tests"); super.init(); } diff --git a/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeNoCleanerDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeNoCleanerDirectByteBufTest.java index 51394c586b..6396148e07 100644 --- a/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeNoCleanerDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeNoCleanerDirectByteBufTest.java @@ -16,16 +16,16 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Before; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; public class LittleEndianUnsafeNoCleanerDirectByteBufTest extends LittleEndianDirectByteBufTest { - @Before + @BeforeEach @Override public void init() { - Assume.assumeTrue("java.nio.DirectByteBuffer.(long, int) not found, skip tests", - PlatformDependent.useDirectBufferNoCleaner()); + Assumptions.assumeTrue(PlatformDependent.useDirectBufferNoCleaner(), + "java.nio.DirectByteBuffer.(long, int) not found, skip tests"); super.init(); } diff --git a/buffer/src/test/java/io/netty/buffer/LongPriorityQueueTest.java b/buffer/src/test/java/io/netty/buffer/LongPriorityQueueTest.java index 25e7680f3d..b2d1c93b55 100644 --- a/buffer/src/test/java/io/netty/buffer/LongPriorityQueueTest.java +++ b/buffer/src/test/java/io/netty/buffer/LongPriorityQueueTest.java @@ -32,7 +32,7 @@ class LongPriorityQueueTest { final LongPriorityQueue pq = new LongPriorityQueue(); assertThrows(IllegalArgumentException.class, new Executable() { @Override - public void execute() throws Throwable { + public void execute() { pq.offer(LongPriorityQueue.NO_VALUE); } }); diff --git a/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java b/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java index a598c910f3..cfa151b52e 100644 --- a/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java +++ b/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java @@ -17,13 +17,13 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.ByteBuffer; -import static org.junit.Assert.*; -import static org.junit.Assume.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class PoolArenaTest { @@ -38,7 +38,7 @@ public class PoolArenaTest { int[] reqCapacities = {0, 15, 510, 1024, 1023, 1025}; int[] expectedResult = {16, 16, 512, 1024, 1024, 1280}; for (int i = 0; i < reqCapacities.length; i ++) { - Assert.assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i]))); + assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i]))); } } @@ -48,7 +48,7 @@ public class PoolArenaTest { int[] reqCapacities = {0, 15, 510, 1024, 1023, 1025}; int[] expectedResult = {16, 64, 512, 1024, 1024, 1280}; for (int i = 0; i < reqCapacities.length; i ++) { - Assert.assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i]))); + assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i]))); } } @@ -58,9 +58,9 @@ public class PoolArenaTest { for (int sz = 0; sz <= CHUNK_SIZE; sz++) { int sizeIdx = arena.size2SizeIdx(sz); - Assert.assertTrue(sz <= arena.sizeIdx2size(sizeIdx)); + assertTrue(sz <= arena.sizeIdx2size(sizeIdx)); if (sizeIdx > 0) { - Assert.assertTrue(sz > arena.sizeIdx2size(sizeIdx - 1)); + assertTrue(sz > arena.sizeIdx2size(sizeIdx - 1)); } } } @@ -74,15 +74,15 @@ public class PoolArenaTest { int maxPages = CHUNK_SIZE >> pageShifts; for (int pages = 1; pages <= maxPages; pages++) { int pageIdxFloor = arena.pages2pageIdxFloor(pages); - Assert.assertTrue(pages << pageShifts >= arena.pageIdx2size(pageIdxFloor)); + assertTrue(pages << pageShifts >= arena.pageIdx2size(pageIdxFloor)); if (pageIdxFloor > 0 && pages < maxPages) { - Assert.assertTrue(pages << pageShifts < arena.pageIdx2size(pageIdxFloor + 1)); + assertTrue(pages << pageShifts < arena.pageIdx2size(pageIdxFloor + 1)); } int pageIdxCeiling = arena.pages2pageIdx(pages); - Assert.assertTrue(pages << pageShifts <= arena.pageIdx2size(pageIdxCeiling)); + assertTrue(pages << pageShifts <= arena.pageIdx2size(pageIdxCeiling)); if (pageIdxCeiling > 0) { - Assert.assertTrue(pages << pageShifts > arena.pageIdx2size(pageIdxCeiling - 1)); + assertTrue(pages << pageShifts > arena.pageIdx2size(pageIdxCeiling - 1)); } } } @@ -122,24 +122,24 @@ public class PoolArenaTest { // create normal buffer final ByteBuf b2 = allocator.directBuffer(8192 * 5); - Assert.assertNotNull(b1); - Assert.assertNotNull(b2); + assertNotNull(b1); + assertNotNull(b2); // then release buffer to deallocated memory while threadlocal cache has been disabled // allocations counter value must equals deallocations counter value - Assert.assertTrue(b1.release()); - Assert.assertTrue(b2.release()); + assertTrue(b1.release()); + assertTrue(b2.release()); - Assert.assertTrue(allocator.directArenas().size() >= 1); + assertTrue(allocator.directArenas().size() >= 1); final PoolArenaMetric metric = allocator.directArenas().get(0); - Assert.assertEquals(2, metric.numDeallocations()); - Assert.assertEquals(2, metric.numAllocations()); + assertEquals(2, metric.numDeallocations()); + assertEquals(2, metric.numAllocations()); - Assert.assertEquals(1, metric.numSmallDeallocations()); - Assert.assertEquals(1, metric.numSmallAllocations()); - Assert.assertEquals(1, metric.numNormalDeallocations()); - Assert.assertEquals(1, metric.numNormalAllocations()); + assertEquals(1, metric.numSmallDeallocations()); + assertEquals(1, metric.numSmallAllocations()); + assertEquals(1, metric.numNormalDeallocations()); + assertEquals(1, metric.numNormalAllocations()); } @Test diff --git a/buffer/src/test/java/io/netty/buffer/PooledAlignedBigEndianDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/PooledAlignedBigEndianDirectByteBufTest.java index 6ac7740a19..5ee3de6f12 100644 --- a/buffer/src/test/java/io/netty/buffer/PooledAlignedBigEndianDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/PooledAlignedBigEndianDirectByteBufTest.java @@ -15,18 +15,18 @@ */ package io.netty.buffer; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import java.nio.ByteOrder; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertSame; public class PooledAlignedBigEndianDirectByteBufTest extends PooledBigEndianDirectByteBufTest { private static final int directMemoryCacheAlignment = 1; private static PooledByteBufAllocator allocator; - @BeforeClass + @BeforeAll public static void setUpAllocator() { allocator = new PooledByteBufAllocator( true, @@ -40,7 +40,7 @@ public class PooledAlignedBigEndianDirectByteBufTest extends PooledBigEndianDire directMemoryCacheAlignment); } - @AfterClass + @AfterAll public static void releaseAllocator() { allocator = null; } diff --git a/buffer/src/test/java/io/netty/buffer/PooledBigEndianDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/PooledBigEndianDirectByteBufTest.java index e0ff404a89..0494f77459 100644 --- a/buffer/src/test/java/io/netty/buffer/PooledBigEndianDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/PooledBigEndianDirectByteBufTest.java @@ -17,7 +17,7 @@ package io.netty.buffer; import java.nio.ByteOrder; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertSame; /** * Tests big-endian direct channel buffers diff --git a/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java b/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java index dafa83d2b1..ef3a73d778 100644 --- a/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java +++ b/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java @@ -20,8 +20,8 @@ import io.netty.util.concurrent.FastThreadLocal; import io.netty.util.concurrent.FastThreadLocalThread; import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.SystemPropertyUtil; -import org.junit.Assume; -import org.junit.Test; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -32,14 +32,15 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.LockSupport; +import org.junit.jupiter.api.Timeout; import static io.netty.buffer.PoolChunk.runOffset; import static io.netty.buffer.PoolChunk.runPages; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class PooledByteBufAllocatorTest extends AbstractByteBufAllocatorTest { @@ -144,13 +145,13 @@ public class PooledByteBufAllocatorTest extends AbstractByteBufAllocatorTest clazz = leakClass(); @@ -46,14 +46,14 @@ public class SimpleLeakAwareByteBufTest extends BigEndianHeapByteBufTest { return new SimpleLeakAwareByteBuf(buffer, tracker); } - @Before + @BeforeEach @Override public void init() { super.init(); trackers.clear(); } - @After + @AfterEach @Override public void dispose() { super.dispose(); diff --git a/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java index 40a7d03c21..43369c5fcf 100644 --- a/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java @@ -18,17 +18,17 @@ package io.netty.buffer; import io.netty.util.ByteProcessor; import io.netty.util.ResourceLeakTracker; import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.ArrayDeque; import java.util.Queue; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; public class SimpleLeakAwareCompositeByteBufTest extends WrappedCompositeByteBufTest { @@ -47,14 +47,14 @@ public class SimpleLeakAwareCompositeByteBufTest extends WrappedCompositeByteBuf return new SimpleLeakAwareCompositeByteBuf(buffer, tracker); } - @Before + @BeforeEach @Override public void init() { super.init(); trackers.clear(); } - @After + @AfterEach @Override public void dispose() { super.dispose(); diff --git a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java index e3199b53f3..e6e20ff277 100644 --- a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java @@ -16,16 +16,18 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Ignore; -import org.junit.Test; + +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.nio.ByteBuffer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests sliced channel buffers @@ -34,7 +36,7 @@ public class SlicedByteBufTest extends AbstractByteBufTest { @Override protected final ByteBuf newBuffer(int length, int maxCapacity) { - Assume.assumeTrue(maxCapacity == Integer.MAX_VALUE); + Assumptions.assumeTrue(maxCapacity == Integer.MAX_VALUE); int offset = length == 0 ? 0 : PlatformDependent.threadLocalRandom().nextInt(length); ByteBuf buffer = Unpooled.buffer(length * 2); ByteBuf slice = newSlice(buffer, offset, length); @@ -54,69 +56,124 @@ public class SlicedByteBufTest extends AbstractByteBufTest { buf.release(); } - @Test(expected = NullPointerException.class) + @Test public void shouldNotAllowNullInConstructor() { - new SlicedByteBuf(null, 0, 0); + assertThrows(NullPointerException.class, new Executable() { + @Override + public void execute() { + new SlicedByteBuf(null, 0, 0); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testInternalNioBuffer() { - super.testInternalNioBuffer(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testInternalNioBuffer(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testDuplicateReadGatheringByteChannelMultipleThreads() throws Exception { - super.testDuplicateReadGatheringByteChannelMultipleThreads(); + public void testDuplicateReadGatheringByteChannelMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + SlicedByteBufTest.super.testDuplicateReadGatheringByteChannelMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testSliceReadGatheringByteChannelMultipleThreads() throws Exception { - super.testSliceReadGatheringByteChannelMultipleThreads(); + public void testSliceReadGatheringByteChannelMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + SlicedByteBufTest.super.testSliceReadGatheringByteChannelMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testDuplicateReadOutputStreamMultipleThreads() throws Exception { - super.testDuplicateReadOutputStreamMultipleThreads(); + public void testDuplicateReadOutputStreamMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + SlicedByteBufTest.super.testDuplicateReadOutputStreamMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testSliceReadOutputStreamMultipleThreads() throws Exception { - super.testSliceReadOutputStreamMultipleThreads(); + public void testSliceReadOutputStreamMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + SlicedByteBufTest.super.testSliceReadOutputStreamMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testDuplicateBytesInArrayMultipleThreads() throws Exception { - super.testDuplicateBytesInArrayMultipleThreads(); + public void testDuplicateBytesInArrayMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + SlicedByteBufTest.super.testDuplicateBytesInArrayMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testSliceBytesInArrayMultipleThreads() throws Exception { - super.testSliceBytesInArrayMultipleThreads(); + public void testSliceBytesInArrayMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + SlicedByteBufTest.super.testSliceBytesInArrayMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testNioBufferExposeOnlyRegion() { - super.testNioBufferExposeOnlyRegion(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testNioBufferExposeOnlyRegion(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testGetReadOnlyDirectDst() { - super.testGetReadOnlyDirectDst(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testGetReadOnlyDirectDst(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testGetReadOnlyHeapDst() { - super.testGetReadOnlyHeapDst(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testGetReadOnlyHeapDst(); + } + }); } @Test @@ -143,12 +200,12 @@ public class SlicedByteBufTest extends AbstractByteBufTest { // Ignore for SlicedByteBuf } - @Ignore("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run") + @Disabled("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run") @Override public void testDuplicateCapacityChange() { } - @Ignore("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run") + @Disabled("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run") @Override public void testRetainedDuplicateCapacityChange() { } @@ -201,41 +258,66 @@ public class SlicedByteBufTest extends AbstractByteBufTest { } @Override - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testGetBytesByteBuffer() { byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; // Ensure destination buffer is bigger then what is wrapped in the ByteBuf. - ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); - ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bytes).slice(0, bytes.length - 1); + final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); + final ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bytes).slice(0, bytes.length - 1); try { - wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer); + } + }); } finally { wrappedBuffer.release(); } } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteUsAsciiCharSequenceExpand() { - super.testWriteUsAsciiCharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testWriteUsAsciiCharSequenceExpand(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteUtf8CharSequenceExpand() { - super.testWriteUtf8CharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testWriteUtf8CharSequenceExpand(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteIso88591CharSequenceExpand() { - super.testWriteIso88591CharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testWriteIso88591CharSequenceExpand(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteUtf16CharSequenceExpand() { - super.testWriteUtf16CharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + SlicedByteBufTest.super.testWriteUtf16CharSequenceExpand(); + } + }); } @Test @@ -254,14 +336,18 @@ public class SlicedByteBufTest extends AbstractByteBufTest { slice.release(); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void ensureWritableWithNotEnoughSpaceShouldThrow() { - ByteBuf slice = newBuffer(10); + final ByteBuf slice = newBuffer(10); ByteBuf unwrapped = slice.unwrap(); unwrapped.writerIndex(unwrapped.writerIndex() + 5); try { - slice.ensureWritable(1); - fail(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + slice.ensureWritable(1); + } + }); } finally { slice.release(); } diff --git a/buffer/src/test/java/io/netty/buffer/UnpooledTest.java b/buffer/src/test/java/io/netty/buffer/UnpooledTest.java index 2ba56211aa..0d1fc2131a 100644 --- a/buffer/src/test/java/io/netty/buffer/UnpooledTest.java +++ b/buffer/src/test/java/io/netty/buffer/UnpooledTest.java @@ -16,7 +16,8 @@ package io.netty.buffer; import io.netty.util.CharsetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.mockito.Mockito; import java.io.InputStream; @@ -31,7 +32,12 @@ import java.util.Map.Entry; import static io.netty.buffer.Unpooled.*; import static io.netty.util.internal.EmptyArrays.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * Tests channel buffers @@ -694,11 +700,16 @@ public class UnpooledTest { wrapped.release(); } - @Test(expected = IllegalArgumentException.class) + @Test public void skipBytesNegativeLength() { - ByteBuf buf = buffer(8); + final ByteBuf buf = buffer(8); try { - buf.skipBytes(-1); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() throws Throwable { + buf.skipBytes(-1); + } + }); } finally { buf.release(); } @@ -722,27 +733,37 @@ public class UnpooledTest { assertEquals(0, wrapped.refCnt()); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testGetBytesByteBuffer() { byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; // Ensure destination buffer is bigger then what is wrapped in the ByteBuf. - ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); - ByteBuf wrappedBuffer = wrappedBuffer(bytes); + final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); + final ByteBuf wrappedBuffer = wrappedBuffer(bytes); try { - wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Throwable { + wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer); + } + }); } finally { wrappedBuffer.release(); } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testGetBytesByteBuffer2() { byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; // Ensure destination buffer is bigger then what is wrapped in the ByteBuf. - ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); - ByteBuf wrappedBuffer = wrappedBuffer(bytes, 0, bytes.length); + final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); + final ByteBuf wrappedBuffer = wrappedBuffer(bytes, 0, bytes.length); try { - wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Throwable { + wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer); + } + }); } finally { wrappedBuffer.release(); } diff --git a/buffer/src/test/java/io/netty/buffer/UnreleaseableByteBufTest.java b/buffer/src/test/java/io/netty/buffer/UnreleaseableByteBufTest.java index aef641badc..9ee22db4e7 100644 --- a/buffer/src/test/java/io/netty/buffer/UnreleaseableByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/UnreleaseableByteBufTest.java @@ -15,13 +15,13 @@ */ package io.netty.buffer; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static io.netty.buffer.Unpooled.buffer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; public class UnreleaseableByteBufTest { diff --git a/buffer/src/test/java/io/netty/buffer/UnsafeByteBufUtilTest.java b/buffer/src/test/java/io/netty/buffer/UnsafeByteBufUtilTest.java index e5cd26d578..307e7ee09c 100644 --- a/buffer/src/test/java/io/netty/buffer/UnsafeByteBufUtilTest.java +++ b/buffer/src/test/java/io/netty/buffer/UnsafeByteBufUtilTest.java @@ -16,21 +16,23 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.nio.ByteBuffer; import static io.netty.util.internal.PlatformDependent.directBufferAddress; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class UnsafeByteBufUtilTest { - @Before + @BeforeEach public void checkHasUnsafe() { - Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe()); + Assumptions.assumeTrue(PlatformDependent.hasUnsafe(), "sun.misc.Unsafe not found, skip tests"); } @Test @@ -49,7 +51,7 @@ public class UnsafeByteBufUtilTest { byte[] check = new byte[length]; targetBuffer.getBytes(0, check, 0, length); - assertArrayEquals("The byte array's copy does not equal the original", testData, check); + assertArrayEquals(testData, check, "The byte array's copy does not equal the original"); } finally { targetBuffer.release(); } @@ -82,7 +84,7 @@ public class UnsafeByteBufUtilTest { byte[] check = new byte[length]; targetBuffer.getBytes(0, check, 0, length); - assertArrayEquals("The byte array's copy does not equal the original", testData, check); + assertArrayEquals(testData, check, "The byte array's copy does not equal the original"); } finally { targetBuffer.release(); b1.release(); @@ -105,7 +107,7 @@ public class UnsafeByteBufUtilTest { final byte[] check = new byte[length]; targetBuffer.getBytes(0, check, 0, length); - assertArrayEquals("The byte array's copy does not equal the original", testData, check); + assertArrayEquals(testData, check, "The byte array's copy does not equal the original"); } finally { targetBuffer.release(); } @@ -135,60 +137,100 @@ public class UnsafeByteBufUtilTest { } } - @Test(expected = NullPointerException.class) + @Test public void testSetBytesWithNullByteArray() { final UnpooledByteBufAllocator alloc = new UnpooledByteBufAllocator(true); final UnpooledDirectByteBuf targetBuffer = new UnpooledDirectByteBuf(alloc, 8, 8); try { - UnsafeByteBufUtil.setBytes(targetBuffer, - directBufferAddress(targetBuffer.nioBuffer()), 0, (byte[]) null, 0, 8); + assertThrows(NullPointerException.class, new Executable() { + @Override + public void execute() { + UnsafeByteBufUtil.setBytes(targetBuffer, + directBufferAddress(targetBuffer.nioBuffer()), 0, (byte[]) null, 0, 8); + } + }); } finally { targetBuffer.release(); } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetBytesOutOfBounds() { - // negative index - testSetBytesOutOfBounds0(4, 4, -1, 0, 4); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + // negative index + testSetBytesOutOfBounds0(4, 4, -1, 0, 4); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetBytesOutOfBounds2() { - // negative length - testSetBytesOutOfBounds0(4, 4, 0, 0, -1); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + // negative length + testSetBytesOutOfBounds0(4, 4, 0, 0, -1); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetBytesOutOfBounds3() { - // buffer length oversize - testSetBytesOutOfBounds0(4, 8, 0, 0, 5); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + // buffer length oversize + testSetBytesOutOfBounds0(4, 8, 0, 0, 5); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetBytesOutOfBounds4() { - // buffer length oversize - testSetBytesOutOfBounds0(4, 4, 3, 0, 3); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + // buffer length oversize + testSetBytesOutOfBounds0(4, 4, 3, 0, 3); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetBytesOutOfBounds5() { - // negative srcIndex - testSetBytesOutOfBounds0(4, 4, 0, -1, 4); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + // negative srcIndex + testSetBytesOutOfBounds0(4, 4, 0, -1, 4); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetBytesOutOfBounds6() { - // src length oversize - testSetBytesOutOfBounds0(8, 4, 0, 0, 5); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + // src length oversize + testSetBytesOutOfBounds0(8, 4, 0, 0, 5); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void testSetBytesOutOfBounds7() { - // src length oversize - testSetBytesOutOfBounds0(4, 4, 0, 1, 4); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + // src length oversize + testSetBytesOutOfBounds0(4, 4, 0, 1, 4); + } + }); } private static void testSetBytesOutOfBounds0(int lengthOfBuffer, diff --git a/buffer/src/test/java/io/netty/buffer/WrappedUnpooledUnsafeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/WrappedUnpooledUnsafeByteBufTest.java index fe92ef4eb7..5e845e07ef 100644 --- a/buffer/src/test/java/io/netty/buffer/WrappedUnpooledUnsafeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/WrappedUnpooledUnsafeByteBufTest.java @@ -16,134 +16,227 @@ package io.netty.buffer; import io.netty.util.internal.PlatformDependent; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +import static org.junit.jupiter.api.Assertions.assertThrows; public class WrappedUnpooledUnsafeByteBufTest extends BigEndianUnsafeDirectByteBufTest { - @Before + @BeforeEach @Override public void init() { - Assume.assumeTrue("PlatformDependent.useDirectBufferNoCleaner() returned false, skip tests", - PlatformDependent.useDirectBufferNoCleaner()); + Assumptions.assumeTrue(PlatformDependent.useDirectBufferNoCleaner(), + "PlatformDependent.useDirectBufferNoCleaner() returned false, skip tests"); super.init(); } @Override protected ByteBuf newBuffer(int length, int maxCapacity) { - Assume.assumeTrue(maxCapacity == Integer.MAX_VALUE); + Assumptions.assumeTrue(maxCapacity == Integer.MAX_VALUE); return new WrappedUnpooledUnsafeDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, PlatformDependent.allocateMemory(length), length, true); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testInternalNioBuffer() { - super.testInternalNioBuffer(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testInternalNioBuffer(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testDuplicateReadGatheringByteChannelMultipleThreads() throws Exception { - super.testDuplicateReadGatheringByteChannelMultipleThreads(); + public void testDuplicateReadGatheringByteChannelMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + WrappedUnpooledUnsafeByteBufTest.super.testDuplicateReadGatheringByteChannelMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testSliceReadGatheringByteChannelMultipleThreads() throws Exception { - super.testSliceReadGatheringByteChannelMultipleThreads(); + public void testSliceReadGatheringByteChannelMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + WrappedUnpooledUnsafeByteBufTest.super.testSliceReadGatheringByteChannelMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testDuplicateReadOutputStreamMultipleThreads() throws Exception { - super.testDuplicateReadOutputStreamMultipleThreads(); + public void testDuplicateReadOutputStreamMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + WrappedUnpooledUnsafeByteBufTest.super.testDuplicateReadOutputStreamMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testSliceReadOutputStreamMultipleThreads() throws Exception { - super.testSliceReadOutputStreamMultipleThreads(); + public void testSliceReadOutputStreamMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + WrappedUnpooledUnsafeByteBufTest.super.testSliceReadOutputStreamMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testDuplicateBytesInArrayMultipleThreads() throws Exception { - super.testDuplicateBytesInArrayMultipleThreads(); + public void testDuplicateBytesInArrayMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + WrappedUnpooledUnsafeByteBufTest.super.testDuplicateBytesInArrayMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override - public void testSliceBytesInArrayMultipleThreads() throws Exception { - super.testSliceBytesInArrayMultipleThreads(); + public void testSliceBytesInArrayMultipleThreads() { + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() throws Exception { + WrappedUnpooledUnsafeByteBufTest.super.testSliceBytesInArrayMultipleThreads(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testNioBufferExposeOnlyRegion() { - super.testNioBufferExposeOnlyRegion(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testNioBufferExposeOnlyRegion(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testGetReadOnlyDirectDst() { - super.testGetReadOnlyDirectDst(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testGetReadOnlyDirectDst(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testGetReadOnlyHeapDst() { - super.testGetReadOnlyHeapDst(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testGetReadOnlyHeapDst(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testReadBytes() { - super.testReadBytes(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testReadBytes(); + } + }); } - @Test(expected = IllegalArgumentException.class) + @Test @Override public void testDuplicateCapacityChange() { - super.testDuplicateCapacityChange(); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testDuplicateCapacityChange(); + } + }); } - @Test(expected = IllegalArgumentException.class) + @Test @Override public void testRetainedDuplicateCapacityChange() { - super.testRetainedDuplicateCapacityChange(); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testRetainedDuplicateCapacityChange(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testLittleEndianWithExpand() { - super.testLittleEndianWithExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testLittleEndianWithExpand(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteUsAsciiCharSequenceExpand() { - super.testWriteUsAsciiCharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testWriteUsAsciiCharSequenceExpand(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteUtf8CharSequenceExpand() { - super.testWriteUtf8CharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testWriteUtf8CharSequenceExpand(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteIso88591CharSequenceExpand() { - super.testWriteIso88591CharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testWriteIso88591CharSequenceExpand(); + } + }); } - @Test(expected = IndexOutOfBoundsException.class) + @Test @Override public void testWriteUtf16CharSequenceExpand() { - super.testWriteUtf16CharSequenceExpand(); + assertThrows(IndexOutOfBoundsException.class, new Executable() { + @Override + public void execute() { + WrappedUnpooledUnsafeByteBufTest.super.testWriteUtf16CharSequenceExpand(); + } + }); } @Test diff --git a/buffer/src/test/java/io/netty/buffer/search/BitapSearchProcessorFactoryTest.java b/buffer/src/test/java/io/netty/buffer/search/BitapSearchProcessorFactoryTest.java index cacc893b0f..42b6849884 100644 --- a/buffer/src/test/java/io/netty/buffer/search/BitapSearchProcessorFactoryTest.java +++ b/buffer/src/test/java/io/netty/buffer/search/BitapSearchProcessorFactoryTest.java @@ -15,7 +15,10 @@ */ package io.netty.buffer.search; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +import static org.junit.jupiter.api.Assertions.assertThrows; public class BitapSearchProcessorFactoryTest { @@ -24,9 +27,14 @@ public class BitapSearchProcessorFactoryTest { new BitapSearchProcessorFactory(new byte[64]); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRejectTooLongNeedle() { - new BitapSearchProcessorFactory(new byte[65]); + assertThrows(IllegalArgumentException.class, new Executable() { + @Override + public void execute() { + new BitapSearchProcessorFactory(new byte[65]); + } + }); } } diff --git a/buffer/src/test/java/io/netty/buffer/search/MultiSearchProcessorTest.java b/buffer/src/test/java/io/netty/buffer/search/MultiSearchProcessorTest.java index d373d13801..1a489822ba 100644 --- a/buffer/src/test/java/io/netty/buffer/search/MultiSearchProcessorTest.java +++ b/buffer/src/test/java/io/netty/buffer/search/MultiSearchProcessorTest.java @@ -18,9 +18,9 @@ package io.netty.buffer.search; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class MultiSearchProcessorTest { diff --git a/buffer/src/test/java/io/netty/buffer/search/SearchProcessorTest.java b/buffer/src/test/java/io/netty/buffer/search/SearchProcessorTest.java index f46a6f7692..b24caf0d8a 100644 --- a/buffer/src/test/java/io/netty/buffer/search/SearchProcessorTest.java +++ b/buffer/src/test/java/io/netty/buffer/search/SearchProcessorTest.java @@ -18,17 +18,13 @@ package io.netty.buffer.search; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameter; -import org.junit.runners.Parameterized.Parameters; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import java.util.Arrays; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; -@RunWith(Parameterized.class) public class SearchProcessorTest { private enum Algorithm { @@ -53,54 +49,48 @@ public class SearchProcessorTest { abstract SearchProcessorFactory newFactory(byte[] needle); } - @Parameters(name = "{0} algorithm") - public static Object[] algorithms() { - return Algorithm.values(); - } - - @Parameter - public Algorithm algorithm; - - @Test - public void testSearch() { + @ParameterizedTest + @EnumSource(Algorithm.class) + public void testSearch(Algorithm algorithm) { final ByteBuf haystack = Unpooled.copiedBuffer("abc☺", CharsetUtil.UTF_8); - assertEquals(0, haystack.forEachByte(factory("a").newSearchProcessor())); - assertEquals(1, haystack.forEachByte(factory("ab").newSearchProcessor())); - assertEquals(2, haystack.forEachByte(factory("abc").newSearchProcessor())); - assertEquals(5, haystack.forEachByte(factory("abc☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("abc☺☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("abc☺x").newSearchProcessor())); + assertEquals(0, haystack.forEachByte(factory(algorithm, "a").newSearchProcessor())); + assertEquals(1, haystack.forEachByte(factory(algorithm, "ab").newSearchProcessor())); + assertEquals(2, haystack.forEachByte(factory(algorithm, "abc").newSearchProcessor())); + assertEquals(5, haystack.forEachByte(factory(algorithm, "abc☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "abc☺☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "abc☺x").newSearchProcessor())); - assertEquals(1, haystack.forEachByte(factory("b").newSearchProcessor())); - assertEquals(2, haystack.forEachByte(factory("bc").newSearchProcessor())); - assertEquals(5, haystack.forEachByte(factory("bc☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("bc☺☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("bc☺x").newSearchProcessor())); + assertEquals(1, haystack.forEachByte(factory(algorithm, "b").newSearchProcessor())); + assertEquals(2, haystack.forEachByte(factory(algorithm, "bc").newSearchProcessor())); + assertEquals(5, haystack.forEachByte(factory(algorithm, "bc☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "bc☺☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "bc☺x").newSearchProcessor())); - assertEquals(2, haystack.forEachByte(factory("c").newSearchProcessor())); - assertEquals(5, haystack.forEachByte(factory("c☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("c☺☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("c☺x").newSearchProcessor())); + assertEquals(2, haystack.forEachByte(factory(algorithm, "c").newSearchProcessor())); + assertEquals(5, haystack.forEachByte(factory(algorithm, "c☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "c☺☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "c☺x").newSearchProcessor())); - assertEquals(5, haystack.forEachByte(factory("☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("☺☺").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("☺x").newSearchProcessor())); + assertEquals(5, haystack.forEachByte(factory(algorithm, "☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "☺☺").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "☺x").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("z").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("aa").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("ba").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("abcd").newSearchProcessor())); - assertEquals(-1, haystack.forEachByte(factory("abcde").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "z").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "aa").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "ba").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "abcd").newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, "abcde").newSearchProcessor())); haystack.release(); } - @Test - public void testRepeating() { + @ParameterizedTest + @EnumSource(Algorithm.class) + public void testRepeating(Algorithm algorithm) { final ByteBuf haystack = Unpooled.copiedBuffer("abcababc", CharsetUtil.UTF_8); final int length = haystack.readableBytes(); - SearchProcessor processor = factory("ab").newSearchProcessor(); + SearchProcessor processor = factory(algorithm, "ab").newSearchProcessor(); assertEquals(1, haystack.forEachByte(processor)); assertEquals(4, haystack.forEachByte(2, length - 2, processor)); @@ -110,11 +100,12 @@ public class SearchProcessorTest { haystack.release(); } - @Test - public void testOverlapping() { + @ParameterizedTest + @EnumSource(Algorithm.class) + public void testOverlapping(Algorithm algorithm) { final ByteBuf haystack = Unpooled.copiedBuffer("ababab", CharsetUtil.UTF_8); final int length = haystack.readableBytes(); - SearchProcessor processor = factory("bab").newSearchProcessor(); + SearchProcessor processor = factory(algorithm, "bab").newSearchProcessor(); assertEquals(3, haystack.forEachByte(processor)); assertEquals(5, haystack.forEachByte(4, length - 4, processor)); @@ -123,8 +114,9 @@ public class SearchProcessorTest { haystack.release(); } - @Test - public void testLongInputs() { + @ParameterizedTest + @EnumSource(Algorithm.class) + public void testLongInputs(Algorithm algorithm) { final int haystackLen = 1024; final int needleLen = 64; @@ -133,21 +125,22 @@ public class SearchProcessorTest { final ByteBuf haystack = Unpooled.copiedBuffer(haystackBytes); // 00000...00001 final byte[] needleBytes = new byte[needleLen]; // 000...000 - assertEquals(needleLen - 1, haystack.forEachByte(factory(needleBytes).newSearchProcessor())); + assertEquals(needleLen - 1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor())); needleBytes[needleLen - 1] = 1; // 000...001 - assertEquals(haystackLen - 1, haystack.forEachByte(factory(needleBytes).newSearchProcessor())); + assertEquals(haystackLen - 1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor())); needleBytes[needleLen - 1] = 2; // 000...002 - assertEquals(-1, haystack.forEachByte(factory(needleBytes).newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor())); needleBytes[needleLen - 1] = 0; needleBytes[0] = 1; // 100...000 - assertEquals(-1, haystack.forEachByte(factory(needleBytes).newSearchProcessor())); + assertEquals(-1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor())); } - @Test - public void testUniqueLen64Substrings() { + @ParameterizedTest + @EnumSource(Algorithm.class) + public void testUniqueLen64Substrings(Algorithm algorithm) { final byte[] haystackBytes = new byte[32 * 65]; // 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, ... int pos = 0; for (int i = 1; i <= 64; i++) { @@ -159,16 +152,16 @@ public class SearchProcessorTest { for (int start = 0; start < haystackBytes.length - 64; start++) { final byte[] needle = Arrays.copyOfRange(haystackBytes, start, start + 64); - assertEquals(start + 63, haystack.forEachByte(factory(needle).newSearchProcessor())); + assertEquals(start + 63, haystack.forEachByte(factory(algorithm, needle).newSearchProcessor())); } } - private SearchProcessorFactory factory(byte[] needle) { + private SearchProcessorFactory factory(Algorithm algorithm, byte[] needle) { return algorithm.newFactory(needle); } - private SearchProcessorFactory factory(String needle) { - return factory(needle.getBytes(CharsetUtil.UTF_8)); + private SearchProcessorFactory factory(Algorithm algorithm, String needle) { + return factory(algorithm, needle.getBytes(CharsetUtil.UTF_8)); } }