From bd8b2519b8476aac16ae1b3cf455ccef5fe480fe Mon Sep 17 00:00:00 2001 From: Riley Park Date: Wed, 26 May 2021 04:46:15 -0700 Subject: [PATCH] Migrate codec tests to JUnit 5 (#11306) 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 --- .../handler/codec/ByteToMessageCodecTest.java | 15 ++- .../codec/ByteToMessageDecoderTest.java | 18 +-- .../codec/CharSequenceValueConverterTest.java | 13 +- .../handler/codec/CodecOutputListTest.java | 7 +- .../codec/DatagramPacketDecoderTest.java | 16 +-- .../codec/DatagramPacketEncoderTest.java | 12 +- .../handler/codec/DateFormatterTest.java | 5 +- .../handler/codec/DefaultHeadersTest.java | 29 ++--- .../codec/DelimiterBasedFrameDecoderTest.java | 5 +- .../netty/handler/codec/EmptyHeadersTest.java | 117 +++++++++--------- .../LengthFieldBasedFrameDecoderTest.java | 33 ++--- .../codec/LineBasedFrameDecoderTest.java | 8 +- .../handler/codec/MessageAggregatorTest.java | 6 +- .../codec/MessageToMessageEncoderTest.java | 12 +- .../codec/ReplayingDecoderByteBufTest.java | 5 +- .../handler/codec/ReplayingDecoderTest.java | 15 ++- .../handler/codec/base64/Base64Test.java | 11 +- .../codec/bytes/ByteArrayDecoderTest.java | 6 +- .../codec/bytes/ByteArrayEncoderTest.java | 10 +- .../compression/AbstractDecoderTest.java | 58 +++++---- .../compression/AbstractEncoderTest.java | 43 +++---- .../compression/AbstractIntegrationTest.java | 14 ++- .../codec/compression/BrotliDecoderTest.java | 37 +++--- .../compression/ByteBufChecksumTest.java | 8 +- .../codec/compression/Bzip2DecoderTest.java | 76 ++++-------- .../codec/compression/Bzip2EncoderTest.java | 6 +- .../compression/Bzip2IntegrationTest.java | 2 +- .../compression/FastLzIntegrationTest.java | 3 +- .../compression/Lz4FrameDecoderTest.java | 57 +++------ .../compression/Lz4FrameEncoderTest.java | 64 +++++----- .../codec/compression/LzfDecoderTest.java | 21 ++-- .../codec/compression/LzfEncoderTest.java | 4 +- .../compression/LzmaFrameEncoderTest.java | 17 +-- .../compression/SnappyFrameDecoderTest.java | 66 +++++----- .../compression/SnappyFrameEncoderTest.java | 8 +- .../compression/SnappyIntegrationTest.java | 2 +- .../handler/codec/compression/SnappyTest.java | 41 +++--- .../frame/DelimiterBasedFrameDecoderTest.java | 6 +- .../LengthFieldBasedFrameDecoderTest.java | 7 +- .../codec/frame/LengthFieldPrependerTest.java | 13 +- .../codec/json/JsonObjectDecoderTest.java | 29 ++--- ...tractCompatibleMarshallingDecoderTest.java | 10 +- ...tractCompatibleMarshallingEncoderTest.java | 6 +- .../marshalling/AbstractMarshallingTest.java | 9 +- .../RiverMarshallingDecoderTest.java | 3 +- .../SerialMarshallingDecoderTest.java | 3 +- .../ProtobufVarint32FrameDecoderTest.java | 9 +- ...tobufVarint32LengthFieldPrependerTest.java | 9 +- .../CompactObjectSerializationTest.java | 6 +- .../CompatibleObjectEncoderTest.java | 6 +- .../handler/codec/string/LineEncoderTest.java | 13 +- .../codec/string/StringEncoderTest.java | 12 +- .../codec/xml/XmlFrameDecoderTest.java | 26 ++-- 53 files changed, 524 insertions(+), 513 deletions(-) diff --git a/codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java b/codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java index f70ff0d5ea..b48fda86a1 100644 --- a/codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java +++ b/codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java @@ -20,20 +20,23 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; -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.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ByteToMessageCodecTest { - @Test(expected = IllegalStateException.class) + @Test public void testSharable() { - new InvalidByteToMessageCodec(); + assertThrows(IllegalStateException.class, InvalidByteToMessageCodec::new); } - @Test(expected = IllegalStateException.class) + @Test public void testSharable2() { - new InvalidByteToMessageCodec2(); + assertThrows(IllegalStateException.class, InvalidByteToMessageCodec2::new); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java index 0dd736677e..cbb1af85da 100644 --- a/codec/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java @@ -27,7 +27,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.socket.ChannelInputShutdownEvent; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; @@ -35,12 +35,12 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicBoolean; import static io.netty.buffer.Unpooled.wrappedBuffer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -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.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class ByteToMessageDecoderTest { @@ -163,8 +163,8 @@ public class ByteToMessageDecoderTest { } private static void assertCumulationReleased(ByteBuf byteBuf) { - assertTrue("unexpected value: " + byteBuf, - byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0); + assertTrue(byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0, + "unexpected value: " + byteBuf); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/CharSequenceValueConverterTest.java b/codec/src/test/java/io/netty/handler/codec/CharSequenceValueConverterTest.java index 597c2ab3b7..05eab1c14c 100644 --- a/codec/src/test/java/io/netty/handler/codec/CharSequenceValueConverterTest.java +++ b/codec/src/test/java/io/netty/handler/codec/CharSequenceValueConverterTest.java @@ -15,11 +15,12 @@ package io.netty.handler.codec; import io.netty.util.AsciiString; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class CharSequenceValueConverterTest { @@ -36,9 +37,9 @@ public class CharSequenceValueConverterTest { assertEquals(127, converter.convertToByte(AsciiString.of("127"))); } - @Test(expected = NumberFormatException.class) + @Test public void testByteFromEmptyAsciiString() { - converter.convertToByte(AsciiString.EMPTY_STRING); + assertThrows(NumberFormatException.class, () ->converter.convertToByte(AsciiString.EMPTY_STRING)); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/CodecOutputListTest.java b/codec/src/test/java/io/netty/handler/codec/CodecOutputListTest.java index c086d18740..5da3395994 100644 --- a/codec/src/test/java/io/netty/handler/codec/CodecOutputListTest.java +++ b/codec/src/test/java/io/netty/handler/codec/CodecOutputListTest.java @@ -15,10 +15,11 @@ */ package io.netty.handler.codec; -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.assertTrue; public class CodecOutputListTest { diff --git a/codec/src/test/java/io/netty/handler/codec/DatagramPacketDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/DatagramPacketDecoderTest.java index d10da91cd2..98b4522a26 100644 --- a/codec/src/test/java/io/netty/handler/codec/DatagramPacketDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/DatagramPacketDecoderTest.java @@ -23,29 +23,29 @@ import io.netty.channel.socket.DatagramPacket; import io.netty.handler.codec.string.StringDecoder; import io.netty.util.CharsetUtil; import io.netty.util.internal.SocketUtils; -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.net.InetSocketAddress; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -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.assertTrue; public class DatagramPacketDecoderTest { private EmbeddedChannel channel; - @Before + @BeforeEach public void setUp() { channel = new EmbeddedChannel( new DatagramPacketDecoder( new StringDecoder(CharsetUtil.UTF_8))); } - @After + @AfterEach public void tearDown() { assertFalse(channel.finish()); } diff --git a/codec/src/test/java/io/netty/handler/codec/DatagramPacketEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/DatagramPacketEncoderTest.java index 0b69e0778c..caffb6db96 100644 --- a/codec/src/test/java/io/netty/handler/codec/DatagramPacketEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/DatagramPacketEncoderTest.java @@ -24,27 +24,27 @@ import io.netty.channel.socket.DatagramPacket; import io.netty.handler.codec.string.StringEncoder; import io.netty.util.CharsetUtil; import io.netty.util.internal.SocketUtils; -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.net.InetSocketAddress; import java.util.List; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class DatagramPacketEncoderTest { private EmbeddedChannel channel; - @Before + @BeforeEach public void setUp() { channel = new EmbeddedChannel( new DatagramPacketEncoder( new StringEncoder(CharsetUtil.UTF_8))); } - @After + @AfterEach public void tearDown() { assertFalse(channel.finish()); } diff --git a/codec/src/test/java/io/netty/handler/codec/DateFormatterTest.java b/codec/src/test/java/io/netty/handler/codec/DateFormatterTest.java index 07c7857334..e7b3d1af4c 100644 --- a/codec/src/test/java/io/netty/handler/codec/DateFormatterTest.java +++ b/codec/src/test/java/io/netty/handler/codec/DateFormatterTest.java @@ -15,13 +15,14 @@ */ package io.netty.handler.codec; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Calendar; import java.util.Date; -import static org.junit.Assert.*; import static io.netty.handler.codec.DateFormatter.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; public class DateFormatterTest { /** diff --git a/codec/src/test/java/io/netty/handler/codec/DefaultHeadersTest.java b/codec/src/test/java/io/netty/handler/codec/DefaultHeadersTest.java index ff645fade3..da4a1456f8 100644 --- a/codec/src/test/java/io/netty/handler/codec/DefaultHeadersTest.java +++ b/codec/src/test/java/io/netty/handler/codec/DefaultHeadersTest.java @@ -16,7 +16,7 @@ package io.netty.handler.codec; import io.netty.util.AsciiString; import io.netty.util.HashingStrategy; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Iterator; @@ -30,13 +30,14 @@ import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -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.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +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 for {@link DefaultHeaders}. @@ -165,13 +166,13 @@ public class DefaultHeadersTest { assertFalse(itr.hasNext()); } - @Test(expected = IllegalStateException.class) + @Test public void valuesItrRemoveThrowsWhenEmpty() { TestDefaultHeaders headers = newInstance(); assertEquals(0, headers.size()); assertTrue(headers.isEmpty()); Iterator itr = headers.valueIterator(of("name")); - itr.remove(); + assertThrows(IllegalStateException.class, itr::remove); } @Test @@ -462,11 +463,11 @@ public class DefaultHeadersTest { assertEquals(h2, h2); } - @Test(expected = NoSuchElementException.class) + @Test public void iterateEmptyHeadersShouldThrow() { Iterator> iterator = newInstance().iterator(); assertFalse(iterator.hasNext()); - iterator.next(); + assertThrows(NoSuchElementException.class, iterator::next); } @Test @@ -569,10 +570,10 @@ public class DefaultHeadersTest { assertEquals(headers1, expected); } - @Test(expected = IllegalArgumentException.class) + @Test public void testAddSelf() { TestDefaultHeaders headers = newInstance(); - headers.add(headers); + assertThrows(IllegalArgumentException.class, () -> headers.add(headers)); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java index 1444af07f0..d3dd830be1 100644 --- a/codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java @@ -20,11 +20,12 @@ import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; import io.netty.util.ReferenceCountUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.charset.Charset; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; public class DelimiterBasedFrameDecoderTest { diff --git a/codec/src/test/java/io/netty/handler/codec/EmptyHeadersTest.java b/codec/src/test/java/io/netty/handler/codec/EmptyHeadersTest.java index 303d2aeafc..5d6c8c38fa 100644 --- a/codec/src/test/java/io/netty/handler/codec/EmptyHeadersTest.java +++ b/codec/src/test/java/io/netty/handler/codec/EmptyHeadersTest.java @@ -14,149 +14,150 @@ */ package io.netty.handler.codec; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Collections; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -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.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; public class EmptyHeadersTest { private static final TestEmptyHeaders HEADERS = new TestEmptyHeaders(); - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddStringValue() { - HEADERS.add("name", "value"); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.add("name", "value")); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddStringValues() { - HEADERS.add("name", "value1", "value2"); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.add("name", "value1", "value2")); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddStringValuesIterable() { - HEADERS.add("name", Arrays.asList("value1", "value2")); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.add("name", Arrays.asList("value1", "value2"))); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddBoolean() { - HEADERS.addBoolean("name", true); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addBoolean("name", true)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddByte() { - HEADERS.addByte("name", (byte) 1); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addByte("name", (byte) 1)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddChar() { - HEADERS.addChar("name", 'a'); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addChar("name", 'a')); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddDouble() { - HEADERS.addDouble("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addDouble("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddFloat() { - HEADERS.addFloat("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addFloat("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddInt() { - HEADERS.addInt("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addInt("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddLong() { - HEADERS.addLong("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addLong("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddShort() { - HEADERS.addShort("name", (short) 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addShort("name", (short) 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testAddTimeMillis() { - HEADERS.addTimeMillis("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.addTimeMillis("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetStringValue() { - HEADERS.set("name", "value"); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.set("name", "value")); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetStringValues() { - HEADERS.set("name", "value1", "value2"); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.set("name", "value1", "value2")); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetStringValuesIterable() { - HEADERS.set("name", Arrays.asList("value1", "value2")); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.set("name", Arrays.asList("value1", "value2"))); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetBoolean() { - HEADERS.setBoolean("name", true); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setBoolean("name", true)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetByte() { - HEADERS.setByte("name", (byte) 1); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setByte("name", (byte) 1)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetChar() { - HEADERS.setChar("name", 'a'); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setChar("name", 'a')); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetDouble() { - HEADERS.setDouble("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setDouble("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetFloat() { - HEADERS.setFloat("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setFloat("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetInt() { - HEADERS.setInt("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setInt("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetLong() { - HEADERS.setLong("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setLong("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetShort() { - HEADERS.setShort("name", (short) 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setShort("name", (short) 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetTimeMillis() { - HEADERS.setTimeMillis("name", 0); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setTimeMillis("name", 0)); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSetAll() { - HEADERS.setAll(new TestEmptyHeaders()); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.setAll(new TestEmptyHeaders())); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testSet() { - HEADERS.set(new TestEmptyHeaders()); + assertThrows(UnsupportedOperationException.class, () -> HEADERS.set(new TestEmptyHeaders())); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/LengthFieldBasedFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/LengthFieldBasedFrameDecoderTest.java index 79bf7cc4a5..afc86b34af 100644 --- a/codec/src/test/java/io/netty/handler/codec/LengthFieldBasedFrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/LengthFieldBasedFrameDecoderTest.java @@ -18,9 +18,12 @@ package io.netty.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class LengthFieldBasedFrameDecoderTest { @@ -36,19 +39,19 @@ public class LengthFieldBasedFrameDecoderTest { EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4)); try { channel.writeInbound(buf); - Assert.fail(); + fail(); } catch (TooLongFrameException e) { // expected } - Assert.assertTrue(channel.finish()); + assertTrue(channel.finish()); ByteBuf b = channel.readInbound(); - Assert.assertEquals(5, b.readableBytes()); - Assert.assertEquals(1, b.readInt()); - Assert.assertEquals('a', b.readByte()); + assertEquals(5, b.readableBytes()); + assertEquals(1, b.readInt()); + assertEquals('a', b.readByte()); b.release(); - Assert.assertNull(channel.readInbound()); + assertNull(channel.readInbound()); channel.finish(); } @@ -64,21 +67,21 @@ public class LengthFieldBasedFrameDecoderTest { EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4)); try { channel.writeInbound(buf.readRetainedSlice(14)); - Assert.fail(); + fail(); } catch (TooLongFrameException e) { // expected } - Assert.assertTrue(channel.writeInbound(buf.readRetainedSlice(buf.readableBytes()))); + assertTrue(channel.writeInbound(buf.readRetainedSlice(buf.readableBytes()))); - Assert.assertTrue(channel.finish()); + assertTrue(channel.finish()); ByteBuf b = channel.readInbound(); - Assert.assertEquals(5, b.readableBytes()); - Assert.assertEquals(1, b.readInt()); - Assert.assertEquals('a', b.readByte()); + assertEquals(5, b.readableBytes()); + assertEquals(1, b.readInt()); + assertEquals('a', b.readByte()); b.release(); - Assert.assertNull(channel.readInbound()); + assertNull(channel.readInbound()); channel.finish(); buf.release(); diff --git a/codec/src/test/java/io/netty/handler/codec/LineBasedFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/LineBasedFrameDecoderTest.java index 59609c994f..82ff201ca3 100644 --- a/codec/src/test/java/io/netty/handler/codec/LineBasedFrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/LineBasedFrameDecoderTest.java @@ -19,12 +19,16 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; import io.netty.util.ReferenceCountUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static io.netty.buffer.Unpooled.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; -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.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class LineBasedFrameDecoderTest { @Test diff --git a/codec/src/test/java/io/netty/handler/codec/MessageAggregatorTest.java b/codec/src/test/java/io/netty/handler/codec/MessageAggregatorTest.java index 6092f8a637..dcfc92f2c4 100644 --- a/codec/src/test/java/io/netty/handler/codec/MessageAggregatorTest.java +++ b/codec/src/test/java/io/netty/handler/codec/MessageAggregatorTest.java @@ -17,9 +17,11 @@ package io.netty.handler.codec; import io.netty.channel.ChannelHandler; -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.assertTrue; import static org.mockito.Mockito.*; import io.netty.buffer.ByteBuf; diff --git a/codec/src/test/java/io/netty/handler/codec/MessageToMessageEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/MessageToMessageEncoderTest.java index d3b61dd979..79947371c8 100644 --- a/codec/src/test/java/io/netty/handler/codec/MessageToMessageEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/MessageToMessageEncoderTest.java @@ -20,18 +20,20 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; + +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 java.util.List; - public class MessageToMessageEncoderTest { /** * Test-case for https://github.com/netty/netty/issues/1656 */ - @Test(expected = EncoderException.class) + @Test public void testException() { EmbeddedChannel channel = new EmbeddedChannel(new MessageToMessageEncoder() { @Override @@ -39,7 +41,7 @@ public class MessageToMessageEncoderTest { throw new Exception(); } }); - channel.writeOutbound(new Object()); + assertThrows(EncoderException.class, () -> channel.writeOutbound(new Object())); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderByteBufTest.java b/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderByteBufTest.java index b1d5c4404a..209dd0e8d7 100644 --- a/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderByteBufTest.java +++ b/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderByteBufTest.java @@ -19,9 +19,10 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; import io.netty.util.Signal; -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.assertTrue; public class ReplayingDecoderByteBufTest { diff --git a/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java index e1b5fb8f6c..d9f975cb71 100644 --- a/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java @@ -21,14 +21,17 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.socket.ChannelInputShutdownEvent; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; -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.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ReplayingDecoderTest { @@ -119,7 +122,7 @@ public class ReplayingDecoderTest { buf.release(); buf2.release(); - assertNull("Must be null as it must only decode one frame", ch.readInbound()); + assertNull(ch.readInbound(), "Must be null as it must only decode one frame"); ch.read(); ch.finish(); @@ -176,7 +179,7 @@ public class ReplayingDecoderTest { channel.writeInbound(buf.copy()); ByteBuf b = channel.readInbound(); - assertEquals("Expect to have still all bytes in the buffer", b, buf); + assertEquals(b, buf, "Expect to have still all bytes in the buffer"); b.release(); buf.release(); } @@ -309,7 +312,7 @@ public class ReplayingDecoderTest { } private static void assertCumulationReleased(ByteBuf byteBuf) { - assertTrue("unexpected value: " + byteBuf, - byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0); + assertTrue(byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0, + "unexpected value: " + byteBuf); } } diff --git a/codec/src/test/java/io/netty/handler/codec/base64/Base64Test.java b/codec/src/test/java/io/netty/handler/codec/base64/Base64Test.java index 47e7f01ecb..90bf62e6e7 100644 --- a/codec/src/test/java/io/netty/handler/codec/base64/Base64Test.java +++ b/codec/src/test/java/io/netty/handler/codec/base64/Base64Test.java @@ -20,7 +20,7 @@ import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; import io.netty.util.internal.StringUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.nio.ByteOrder; @@ -29,7 +29,9 @@ import java.security.cert.X509Certificate; import java.util.concurrent.ThreadLocalRandom; import static io.netty.buffer.Unpooled.copiedBuffer; -import static org.junit.Assert.*; +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 class Base64Test { @@ -149,8 +151,9 @@ public class Base64Test { ByteBuf decoded = Base64.decode(encoded); ByteBuf expectedBuf = Unpooled.wrappedBuffer(bytes); try { - assertEquals(StringUtil.NEWLINE + "expected: " + ByteBufUtil.hexDump(expectedBuf) + - StringUtil.NEWLINE + "actual--: " + ByteBufUtil.hexDump(decoded), expectedBuf, decoded); + assertEquals(expectedBuf, decoded, + StringUtil.NEWLINE + "expected: " + ByteBufUtil.hexDump(expectedBuf) + + StringUtil.NEWLINE + "actual--: " + ByteBufUtil.hexDump(decoded)); } finally { src.release(); encoded.release(); diff --git a/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayDecoderTest.java index d71a41f2e9..f7beef9409 100644 --- a/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayDecoderTest.java @@ -17,8 +17,8 @@ package io.netty.handler.codec.bytes; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.internal.EmptyArrays; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.Random; @@ -30,7 +30,7 @@ public class ByteArrayDecoderTest { private EmbeddedChannel ch; - @Before + @BeforeEach public void setUp() { ch = new EmbeddedChannel(new ByteArrayDecoder()); } diff --git a/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayEncoderTest.java index ad04fe9f57..9e1df98387 100644 --- a/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/bytes/ByteArrayEncoderTest.java @@ -18,9 +18,9 @@ package io.netty.handler.codec.bytes; import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.internal.EmptyArrays; -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.Random; @@ -32,12 +32,12 @@ public class ByteArrayEncoderTest { private EmbeddedChannel ch; - @Before + @BeforeEach public void setUp() { ch = new EmbeddedChannel(new ByteArrayEncoder()); } - @After + @AfterEach public void tearDown() { assertThat(ch.finish(), is(false)); } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java index 44bffa298e..099db803ac 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java @@ -19,20 +19,16 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.experimental.theories.DataPoints; -import org.junit.experimental.theories.FromDataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(Theories.class) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public abstract class AbstractDecoderTest extends AbstractCompressionTest { protected static final ByteBuf WRAPPED_BYTES_SMALL; @@ -43,13 +39,10 @@ public abstract class AbstractDecoderTest extends AbstractCompressionTest { WRAPPED_BYTES_LARGE = Unpooled.wrappedBuffer(BYTES_LARGE); } - @Rule - public final ExpectedException expected = ExpectedException.none(); - protected EmbeddedChannel channel; - protected static byte[] compressedBytesSmall; - protected static byte[] compressedBytesLarge; + protected byte[] compressedBytesSmall; + protected byte[] compressedBytesLarge; protected AbstractDecoderTest() throws Exception { compressedBytesSmall = compress(BYTES_SMALL); @@ -61,10 +54,14 @@ public abstract class AbstractDecoderTest extends AbstractCompressionTest { */ protected abstract byte[] compress(byte[] data) throws Exception; - @Before - public abstract void initChannel(); + @BeforeEach + public final void initChannel() { + channel = createChannel(); + } - @After + protected abstract EmbeddedChannel createChannel(); + + @AfterEach public void destroyChannel() { if (channel != null) { channel.finishAndReleaseAll(); @@ -72,34 +69,35 @@ public abstract class AbstractDecoderTest extends AbstractCompressionTest { } } - @DataPoints("smallData") - public static ByteBuf[] smallData() { + public ByteBuf[] smallData() { ByteBuf heap = Unpooled.wrappedBuffer(compressedBytesSmall); ByteBuf direct = Unpooled.directBuffer(compressedBytesSmall.length); direct.writeBytes(compressedBytesSmall); return new ByteBuf[] {heap, direct}; } - @DataPoints("largeData") - public static ByteBuf[] largeData() { + public ByteBuf[] largeData() { ByteBuf heap = Unpooled.wrappedBuffer(compressedBytesLarge); ByteBuf direct = Unpooled.directBuffer(compressedBytesLarge.length); direct.writeBytes(compressedBytesLarge); return new ByteBuf[] {heap, direct}; } - @Theory - public void testDecompressionOfSmallChunkOfData(@FromDataPoints("smallData") ByteBuf data) throws Exception { + @ParameterizedTest + @MethodSource("smallData") + public void testDecompressionOfSmallChunkOfData(ByteBuf data) throws Exception { testDecompression(WRAPPED_BYTES_SMALL, data); } - @Theory - public void testDecompressionOfLargeChunkOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { + @ParameterizedTest + @MethodSource("largeData") + public void testDecompressionOfLargeChunkOfData(ByteBuf data) throws Exception { testDecompression(WRAPPED_BYTES_LARGE, data); } - @Theory - public void testDecompressionOfBatchedFlowOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { + @ParameterizedTest + @MethodSource("largeData") + public void testDecompressionOfBatchedFlowOfData(ByteBuf data) throws Exception { testDecompressionOfBatchedFlow(WRAPPED_BYTES_LARGE, data); } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java index bb4bc9e7fd..193849723a 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java @@ -19,18 +19,14 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.After; -import org.junit.Before; -import org.junit.experimental.theories.DataPoints; -import org.junit.experimental.theories.FromDataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(Theories.class) public abstract class AbstractEncoderTest extends AbstractCompressionTest { protected EmbeddedChannel channel; @@ -40,10 +36,14 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest { */ protected abstract ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception; - @Before - public abstract void initChannel(); + @BeforeEach + public final void initChannel() { + channel = createChannel(); + } - @After + protected abstract EmbeddedChannel createChannel(); + + @AfterEach public void destroyChannel() { if (channel != null) { channel.finishAndReleaseAll(); @@ -51,7 +51,6 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest { } } - @DataPoints("smallData") public static ByteBuf[] smallData() { ByteBuf heap = Unpooled.wrappedBuffer(BYTES_SMALL); ByteBuf direct = Unpooled.directBuffer(BYTES_SMALL.length); @@ -59,7 +58,6 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest { return new ByteBuf[] {heap, direct}; } - @DataPoints("largeData") public static ByteBuf[] largeData() { ByteBuf heap = Unpooled.wrappedBuffer(BYTES_LARGE); ByteBuf direct = Unpooled.directBuffer(BYTES_LARGE.length); @@ -67,18 +65,21 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest { return new ByteBuf[] {heap, direct}; } - @Theory - public void testCompressionOfSmallChunkOfData(@FromDataPoints("smallData") ByteBuf data) throws Exception { + @ParameterizedTest + @MethodSource("smallData") + public void testCompressionOfSmallChunkOfData(ByteBuf data) throws Exception { testCompression(data); } - @Theory - public void testCompressionOfLargeChunkOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { + @ParameterizedTest + @MethodSource("largeData") + public void testCompressionOfLargeChunkOfData(ByteBuf data) throws Exception { testCompression(data); } - @Theory - public void testCompressionOfBatchedFlowOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { + @ParameterizedTest + @MethodSource("largeData") + public void testCompressionOfBatchedFlowOfData(ByteBuf data) throws Exception { testCompressionOfBatchedFlow(data); } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java b/codec/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java index 0b79b9b345..c1edb483bd 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java @@ -22,16 +22,18 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; import io.netty.util.ReferenceCountUtil; import io.netty.util.internal.EmptyArrays; -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.Arrays; import java.util.Random; 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; public abstract class AbstractIntegrationTest { @@ -43,13 +45,13 @@ public abstract class AbstractIntegrationTest { protected abstract EmbeddedChannel createEncoder(); protected abstract EmbeddedChannel createDecoder(); - @Before + @BeforeEach public void initChannels() throws Exception { encoder = createEncoder(); decoder = createDecoder(); } - @After + @AfterEach public void closeChannels() throws Exception { encoder.close(); for (;;) { diff --git a/codec/src/test/java/io/netty/handler/codec/compression/BrotliDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/BrotliDecoderTest.java index 19377841b4..81299f554e 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/BrotliDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/BrotliDecoderTest.java @@ -21,22 +21,18 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.After; -import org.junit.Before; -import org.junit.experimental.theories.DataPoints; -import org.junit.experimental.theories.FromDataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Random; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(Theories.class) public class BrotliDecoderTest { private static final Random RANDOM; @@ -78,12 +74,12 @@ public class BrotliDecoderTest { private EmbeddedChannel channel; - @Before + @BeforeEach public void initChannel() { channel = new EmbeddedChannel(new BrotliDecoder()); } - @After + @AfterEach public void destroyChannel() { if (channel != null) { channel.finishAndReleaseAll(); @@ -91,7 +87,6 @@ public class BrotliDecoderTest { } } - @DataPoints("smallData") public static ByteBuf[] smallData() { ByteBuf heap = Unpooled.wrappedBuffer(COMPRESSED_BYTES_SMALL); ByteBuf direct = Unpooled.directBuffer(COMPRESSED_BYTES_SMALL.length); @@ -99,7 +94,6 @@ public class BrotliDecoderTest { return new ByteBuf[]{heap, direct}; } - @DataPoints("largeData") public static ByteBuf[] largeData() { ByteBuf heap = Unpooled.wrappedBuffer(COMPRESSED_BYTES_LARGE); ByteBuf direct = Unpooled.directBuffer(COMPRESSED_BYTES_LARGE.length); @@ -107,18 +101,21 @@ public class BrotliDecoderTest { return new ByteBuf[]{heap, direct}; } - @Theory - public void testDecompressionOfSmallChunkOfData(@FromDataPoints("smallData") ByteBuf data) { + @ParameterizedTest + @MethodSource("smallData") + public void testDecompressionOfSmallChunkOfData(ByteBuf data) { testDecompression(WRAPPED_BYTES_SMALL, data); } - @Theory - public void testDecompressionOfLargeChunkOfData(@FromDataPoints("largeData") ByteBuf data) { + @ParameterizedTest + @MethodSource("largeData") + public void testDecompressionOfLargeChunkOfData(ByteBuf data) { testDecompression(WRAPPED_BYTES_LARGE, data); } - @Theory - public void testDecompressionOfBatchedFlowOfData(@FromDataPoints("largeData") ByteBuf data) { + @ParameterizedTest + @MethodSource("largeData") + public void testDecompressionOfBatchedFlowOfData(ByteBuf data) { testDecompressionOfBatchedFlow(WRAPPED_BYTES_LARGE, data); } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/ByteBufChecksumTest.java b/codec/src/test/java/io/netty/handler/codec/compression/ByteBufChecksumTest.java index b6fe6c4cf1..bf3af6142b 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/ByteBufChecksumTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/ByteBufChecksumTest.java @@ -18,8 +18,8 @@ package io.netty.handler.codec.compression; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import net.jpountz.xxhash.XXHashFactory; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import java.util.Random; import java.util.zip.Adler32; @@ -27,13 +27,13 @@ import java.util.zip.CRC32; import java.util.zip.Checksum; import static io.netty.handler.codec.compression.Lz4Constants.DEFAULT_SEED; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class ByteBufChecksumTest { private static final byte[] BYTE_ARRAY = new byte[1024]; - @BeforeClass + @BeforeAll public static void setUp() { new Random().nextBytes(BYTE_ARRAY); } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java index 023b84d4bb..651d5d6938 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java @@ -19,13 +19,14 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; import java.util.Arrays; import static io.netty.handler.codec.compression.Bzip2Constants.*; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; public class Bzip2DecoderTest extends AbstractDecoderTest { @@ -40,8 +41,8 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { } @Override - public void initChannel() { - channel = new EmbeddedChannel(new Bzip2Decoder()); + protected EmbeddedChannel createChannel() { + return new EmbeddedChannel(new Bzip2Decoder()); } private void writeInboundDestroyAndExpectDecompressionException(ByteBuf in) { @@ -58,32 +59,24 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { } @Test - public void testUnexpectedStreamIdentifier() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("Unexpected stream identifier contents"); - + public void testUnexpectedStreamIdentifier() { ByteBuf in = Unpooled.buffer(); in.writeLong(1823080128301928729L); //random value - writeInboundDestroyAndExpectDecompressionException(in); + assertThrows(DecompressionException.class, + () -> writeInboundDestroyAndExpectDecompressionException(in), "Unexpected stream identifier contents"); } @Test - public void testInvalidBlockSize() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("block size is invalid"); - + public void testInvalidBlockSize() { ByteBuf in = Unpooled.buffer(); in.writeMedium(MAGIC_NUMBER); in.writeByte('0'); //incorrect block size - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "block size is invalid"); } @Test - public void testBadBlockHeader() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("bad block header"); - + public void testBadBlockHeader() { ByteBuf in = Unpooled.buffer(); in.writeMedium(MAGIC_NUMBER); in.writeByte('1'); //block size @@ -91,14 +84,11 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { in.writeMedium(11); //incorrect block header in.writeInt(11111); //block CRC - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "bad block header"); } @Test - public void testStreamCrcErrorOfEmptyBlock() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("stream CRC error"); - + public void testStreamCrcErrorOfEmptyBlock() { ByteBuf in = Unpooled.buffer(); in.writeMedium(MAGIC_NUMBER); in.writeByte('1'); //block size @@ -106,66 +96,54 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { in.writeMedium(END_OF_STREAM_MAGIC_2); in.writeInt(1); //wrong storedCombinedCRC - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "stream CRC error"); } @Test - public void testStreamCrcError() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("stream CRC error"); - + public void testStreamCrcError() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[41] = (byte) 0xDD; - tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)); + assertThrows(DecompressionException.class, + () -> tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)), "stream CRC error"); } @Test - public void testIncorrectHuffmanGroupsNumber() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("incorrect huffman groups number"); - + public void testIncorrectHuffmanGroupsNumber() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[25] = 0x70; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "incorrect huffman groups number"); } @Test - public void testIncorrectSelectorsNumber() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("incorrect selectors number"); - + public void testIncorrectSelectorsNumber() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[25] = 0x2F; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "incorrect selectors number"); } @Test - public void testBlockCrcError() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("block CRC error"); - + public void testBlockCrcError() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[11] = 0x77; ByteBuf in = Unpooled.wrappedBuffer(data); - writeInboundDestroyAndExpectDecompressionException(in); + assertThrows(DecompressionException.class, + () -> writeInboundDestroyAndExpectDecompressionException(in), "block CRC error"); } @Test - public void testStartPointerInvalid() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("start pointer invalid"); - + public void testStartPointerInvalid() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[14] = (byte) 0xFF; ByteBuf in = Unpooled.wrappedBuffer(data); - writeInboundDestroyAndExpectDecompressionException(in); + assertThrows(DecompressionException.class, + () -> writeInboundDestroyAndExpectDecompressionException(in), "start pointer invalid"); } @Override diff --git a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2EncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2EncoderTest.java index be058aefb0..8957ef6e94 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2EncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2EncoderTest.java @@ -24,13 +24,13 @@ import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import java.io.InputStream; import static io.netty.handler.codec.compression.Bzip2Constants.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class Bzip2EncoderTest extends AbstractEncoderTest { @Override - public void initChannel() { - channel = new EmbeddedChannel(new Bzip2Encoder(MIN_BLOCK_SIZE)); + protected EmbeddedChannel createChannel() { + return new EmbeddedChannel(new Bzip2Encoder(MIN_BLOCK_SIZE)); } @Override diff --git a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2IntegrationTest.java b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2IntegrationTest.java index 64b9108b05..d8f4c0ad0a 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2IntegrationTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2IntegrationTest.java @@ -16,7 +16,7 @@ package io.netty.handler.codec.compression; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class Bzip2IntegrationTest extends AbstractIntegrationTest { diff --git a/codec/src/test/java/io/netty/handler/codec/compression/FastLzIntegrationTest.java b/codec/src/test/java/io/netty/handler/codec/compression/FastLzIntegrationTest.java index e0ecf57917..3708685882 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/FastLzIntegrationTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/FastLzIntegrationTest.java @@ -22,7 +22,8 @@ import io.netty.channel.embedded.EmbeddedChannel; 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; public class FastLzIntegrationTest extends AbstractIntegrationTest { diff --git a/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameDecoderTest.java index 05dbf3c74e..e86638d371 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameDecoderTest.java @@ -19,12 +19,13 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import net.jpountz.lz4.LZ4BlockOutputStream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; import java.util.Arrays; import static io.netty.handler.codec.compression.Lz4Constants.*; +import static org.junit.jupiter.api.Assertions.assertThrows; public class Lz4FrameDecoderTest extends AbstractDecoderTest { @@ -42,91 +43,71 @@ public class Lz4FrameDecoderTest extends AbstractDecoderTest { } @Override - public void initChannel() { - channel = new EmbeddedChannel(new Lz4FrameDecoder(true)); + protected EmbeddedChannel createChannel() { + return new EmbeddedChannel(new Lz4FrameDecoder(true)); } @Test - public void testUnexpectedBlockIdentifier() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("unexpected block identifier"); - + public void testUnexpectedBlockIdentifier() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[1] = 0x00; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unexpected block identifier"); } @Test - public void testInvalidCompressedLength() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("invalid compressedLength"); - + public void testInvalidCompressedLength() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[12] = (byte) 0xFF; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "invalid compressedLength"); } @Test - public void testInvalidDecompressedLength() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("invalid decompressedLength"); - + public void testInvalidDecompressedLength() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[16] = (byte) 0xFF; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "invalid decompressedLength"); } @Test - public void testDecompressedAndCompressedLengthMismatch() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("mismatch"); - + public void testDecompressedAndCompressedLengthMismatch() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[13] = 0x01; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "mismatch"); } @Test - public void testUnexpectedBlockType() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("unexpected blockType"); - + public void testUnexpectedBlockType() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[8] = 0x36; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unexpected blockType"); } @Test - public void testMismatchingChecksum() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("mismatching checksum"); - + public void testMismatchingChecksum() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[17] = 0x01; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "mismatching checksum"); } @Test - public void testChecksumErrorOfLastBlock() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("checksum error"); - + public void testChecksumErrorOfLastBlock() { final byte[] data = Arrays.copyOf(DATA, DATA.length); data[44] = 0x01; - tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)); + assertThrows(DecompressionException.class, + () -> tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)), "checksum error"); } @Override diff --git a/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java index 9162e31b90..5b0c5188fb 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java @@ -33,12 +33,13 @@ import io.netty.channel.nio.NioHandler; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.EncoderException; +import java.util.concurrent.TimeUnit; import net.jpountz.lz4.LZ4BlockInputStream; import net.jpountz.lz4.LZ4Factory; import net.jpountz.xxhash.XXHashFactory; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -53,9 +54,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.not; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; +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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; public class Lz4FrameEncoderTest extends AbstractEncoderTest { @@ -75,15 +78,15 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { @Mock private ByteBuf buffer; - @Before + @BeforeEach public void setup() { MockitoAnnotations.initMocks(this); when(ctx.alloc()).thenReturn(ByteBufAllocator.DEFAULT); } @Override - public void initChannel() { - channel = new EmbeddedChannel(new Lz4FrameEncoder()); + protected EmbeddedChannel createChannel() { + return new EmbeddedChannel(new Lz4FrameEncoder()); } @Override @@ -139,11 +142,11 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { try { Lz4FrameEncoder encoder = newEncoder(blockSize, Lz4FrameEncoder.DEFAULT_MAX_ENCODE_SIZE); out = encoder.allocateBuffer(ctx, in, preferDirect); - Assert.assertNotNull(out); + assertNotNull(out); if (NONALLOCATABLE_SIZE == bufSize) { - Assert.assertFalse(out.isWritable()); + assertFalse(out.isWritable()); } else { - Assert.assertTrue(out.writableBytes() > 0); + assertTrue(out.writableBytes() > 0); if (!preferDirect) { // Only check if preferDirect is not true as if a direct buffer is returned or not depends on // if sun.misc.Unsafe is present. @@ -158,7 +161,7 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { } } - @Test (expected = EncoderException.class) + @Test public void testAllocateDirectBufferExceedMaxEncodeSize() { final int maxEncodeSize = 1024; Lz4FrameEncoder encoder = newEncoder(Lz4Constants.DEFAULT_BLOCK_SIZE, maxEncodeSize); @@ -166,7 +169,7 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(inputBufferSize, inputBufferSize); try { buf.writerIndex(inputBufferSize); - encoder.allocateBuffer(ctx, buf, false); + assertThrows(EncoderException.class, () -> encoder.allocateBuffer(ctx, buf, false)); } finally { buf.release(); } @@ -187,13 +190,13 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { * {@link Lz4FrameEncoder#allocateBuffer(ChannelHandlerContext, ByteBuf, boolean)}, but this is safest way * of testing the overflow conditions as allocating the huge buffers fails in many CI environments. */ - @Test (expected = EncoderException.class) + @Test public void testAllocateOnHeapBufferOverflowsOutputSize() { final int maxEncodeSize = Integer.MAX_VALUE; Lz4FrameEncoder encoder = newEncoder(Lz4Constants.DEFAULT_BLOCK_SIZE, maxEncodeSize); when(buffer.readableBytes()).thenReturn(maxEncodeSize); buffer.writerIndex(maxEncodeSize); - encoder.allocateBuffer(ctx, buffer, false); + assertThrows(EncoderException.class, () -> encoder.allocateBuffer(ctx, buffer, false)); } @Test @@ -203,14 +206,14 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { int size = 27; ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size); buf.writerIndex(size); - Assert.assertEquals(0, encoder.getBackingBuffer().readableBytes()); + assertEquals(0, encoder.getBackingBuffer().readableBytes()); channel.write(buf); - Assert.assertTrue(channel.outboundMessages().isEmpty()); - Assert.assertEquals(size, encoder.getBackingBuffer().readableBytes()); + assertTrue(channel.outboundMessages().isEmpty()); + assertEquals(size, encoder.getBackingBuffer().readableBytes()); channel.flush(); - Assert.assertTrue(channel.finish()); - Assert.assertTrue(channel.releaseOutbound()); - Assert.assertFalse(channel.releaseInbound()); + assertTrue(channel.finish()); + assertTrue(channel.releaseOutbound()); + assertFalse(channel.releaseInbound()); } @Test @@ -222,24 +225,25 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { int size = blockSize - 1; ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size); buf.writerIndex(size); - Assert.assertEquals(0, encoder.getBackingBuffer().readableBytes()); + assertEquals(0, encoder.getBackingBuffer().readableBytes()); channel.write(buf); - Assert.assertEquals(size, encoder.getBackingBuffer().readableBytes()); + assertEquals(size, encoder.getBackingBuffer().readableBytes()); int nextSize = size - 1; buf = ByteBufAllocator.DEFAULT.buffer(nextSize, nextSize); buf.writerIndex(nextSize); channel.write(buf); - Assert.assertEquals(size + nextSize - blockSize, encoder.getBackingBuffer().readableBytes()); + assertEquals(size + nextSize - blockSize, encoder.getBackingBuffer().readableBytes()); channel.flush(); - Assert.assertEquals(0, encoder.getBackingBuffer().readableBytes()); - Assert.assertTrue(channel.finish()); - Assert.assertTrue(channel.releaseOutbound()); - Assert.assertFalse(channel.releaseInbound()); + assertEquals(0, encoder.getBackingBuffer().readableBytes()); + assertTrue(channel.finish()); + assertTrue(channel.releaseOutbound()); + assertFalse(channel.releaseInbound()); } - @Test(timeout = 3000) + @Test + @Timeout(value = 3000, unit = TimeUnit.MILLISECONDS) public void writingAfterClosedChannelDoesNotNPE() throws InterruptedException { EventLoopGroup group = new MultithreadEventLoopGroup(2, NioHandler.newFactory()); Channel serverChannel = null; diff --git a/codec/src/test/java/io/netty/handler/codec/compression/LzfDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/LzfDecoderTest.java index e3d456dd3c..c5a772f3ae 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/LzfDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/LzfDecoderTest.java @@ -19,9 +19,10 @@ import com.ning.compress.lzf.LZFEncoder; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static com.ning.compress.lzf.LZFChunk.*; +import static org.junit.jupiter.api.Assertions.assertThrows; public class LzfDecoderTest extends AbstractDecoderTest { @@ -29,35 +30,29 @@ public class LzfDecoderTest extends AbstractDecoderTest { } @Override - public void initChannel() { - channel = new EmbeddedChannel(new LzfDecoder()); + protected EmbeddedChannel createChannel() { + return new EmbeddedChannel(new LzfDecoder()); } @Test - public void testUnexpectedBlockIdentifier() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("unexpected block identifier"); - + public void testUnexpectedBlockIdentifier() { ByteBuf in = Unpooled.buffer(); in.writeShort(0x1234); //random value in.writeByte(BLOCK_TYPE_NON_COMPRESSED); in.writeShort(0); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unexpected block identifier"); } @Test - public void testUnknownTypeOfChunk() throws Exception { - expected.expect(DecompressionException.class); - expected.expectMessage("unknown type of chunk"); - + public void testUnknownTypeOfChunk() { ByteBuf in = Unpooled.buffer(); in.writeByte(BYTE_Z); in.writeByte(BYTE_V); in.writeByte(0xFF); //random value in.writeInt(0); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unknown type of chunk"); } @Override diff --git a/codec/src/test/java/io/netty/handler/codec/compression/LzfEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/LzfEncoderTest.java index 463a421066..e006a95bc9 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/LzfEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/LzfEncoderTest.java @@ -23,8 +23,8 @@ import io.netty.channel.embedded.EmbeddedChannel; public class LzfEncoderTest extends AbstractEncoderTest { @Override - public void initChannel() { - channel = new EmbeddedChannel(new LzfEncoder()); + protected EmbeddedChannel createChannel() { + return new EmbeddedChannel(new LzfEncoder()); } @Override diff --git a/codec/src/test/java/io/netty/handler/codec/compression/LzmaFrameEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/LzmaFrameEncoderTest.java index 148f071996..380aa358e7 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/LzmaFrameEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/LzmaFrameEncoderTest.java @@ -22,26 +22,27 @@ import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import lzma.sdk.lzma.Decoder; import lzma.streams.LzmaInputStream; -import org.junit.experimental.theories.FromDataPoints; -import org.junit.experimental.theories.Theory; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.io.InputStream; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class LzmaFrameEncoderTest extends AbstractEncoderTest { @Override - public void initChannel() { - channel = new EmbeddedChannel(new LzmaFrameEncoder()); + protected EmbeddedChannel createChannel() { + return new EmbeddedChannel(new LzmaFrameEncoder()); } - @Theory + @ParameterizedTest + @MethodSource("smallData") @Override - public void testCompressionOfBatchedFlowOfData(@FromDataPoints("smallData") ByteBuf data) throws Exception { + public void testCompressionOfBatchedFlowOfData(ByteBuf data) throws Exception { testCompressionOfBatchedFlow(data); } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameDecoderTest.java index cf1819d834..cdb207d3b4 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameDecoderTest.java @@ -18,81 +18,85 @@ package io.netty.handler.codec.compression; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -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 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.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class SnappyFrameDecoderTest { private EmbeddedChannel channel; - @Before + @BeforeEach public void initChannel() { channel = new EmbeddedChannel(new SnappyFrameDecoder()); } - @After + @AfterEach public void tearDown() { assertFalse(channel.finishAndReleaseAll()); } - @Test(expected = DecompressionException.class) - public void testReservedUnskippableChunkTypeCausesError() throws Exception { + @Test + public void testReservedUnskippableChunkTypeCausesError() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 0x03, 0x01, 0x00, 0x00, 0x00 }); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in)); } - @Test(expected = DecompressionException.class) - public void testInvalidStreamIdentifierLength() throws Exception { + @Test + public void testInvalidStreamIdentifierLength() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { -0x80, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' }); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in)); } - @Test(expected = DecompressionException.class) - public void testInvalidStreamIdentifierValue() throws Exception { + @Test + public void testInvalidStreamIdentifierValue() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { (byte) 0xff, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y' }); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in)); } - @Test(expected = DecompressionException.class) - public void testReservedSkippableBeforeStreamIdentifier() throws Exception { + @Test + public void testReservedSkippableBeforeStreamIdentifier() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { -0x7f, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y' }); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in)); } - @Test(expected = DecompressionException.class) - public void testUncompressedDataBeforeStreamIdentifier() throws Exception { + @Test + public void testUncompressedDataBeforeStreamIdentifier() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 0x01, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' }); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in)); } - @Test(expected = DecompressionException.class) - public void testCompressedDataBeforeStreamIdentifier() throws Exception { + @Test + public void testCompressedDataBeforeStreamIdentifier() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 0x00, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' }); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in)); } @Test - public void testReservedSkippableSkipsInput() throws Exception { + public void testReservedSkippableSkipsInput() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, -0x7f, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' @@ -105,7 +109,7 @@ public class SnappyFrameDecoderTest { } @Test - public void testUncompressedDataAppendsToOut() throws Exception { + public void testUncompressedDataAppendsToOut() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y' @@ -122,7 +126,7 @@ public class SnappyFrameDecoderTest { } @Test - public void testCompressedDataDecodesAndAppendsToOut() throws Exception { + public void testCompressedDataDecodesAndAppendsToOut() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -145,8 +149,8 @@ public class SnappyFrameDecoderTest { // The following two tests differ in only the checksum provided for the literal // uncompressed string "netty" - @Test(expected = DecompressionException.class) - public void testInvalidChecksumThrowsException() throws Exception { + @Test + public void testInvalidChecksumThrowsException() { EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true)); try { // checksum here is presented as 0 @@ -155,14 +159,14 @@ public class SnappyFrameDecoderTest { 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y' }); - channel.writeInbound(in); + assertThrows(DecompressionException.class, () -> channel.writeInbound(in)); } finally { channel.finishAndReleaseAll(); } } @Test - public void testInvalidChecksumDoesNotThrowException() throws Exception { + public void testInvalidChecksumDoesNotThrowException() { EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true)); try { // checksum here is presented as a282986f (little endian) diff --git a/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameEncoderTest.java index 5f64788c3e..8dc3642e4a 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/SnappyFrameEncoderTest.java @@ -19,15 +19,15 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class SnappyFrameEncoderTest { private EmbeddedChannel channel; - @Before + @BeforeEach public void setUp() { channel = new EmbeddedChannel(new SnappyFrameEncoder()); } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/SnappyIntegrationTest.java b/codec/src/test/java/io/netty/handler/codec/compression/SnappyIntegrationTest.java index 4b192e2cde..2f4671074d 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/SnappyIntegrationTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/SnappyIntegrationTest.java @@ -16,7 +16,7 @@ package io.netty.handler.codec.compression; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Random; diff --git a/codec/src/test/java/io/netty/handler/codec/compression/SnappyTest.java b/codec/src/test/java/io/netty/handler/codec/compression/SnappyTest.java index c20091dc10..f3ba22e112 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/SnappyTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/SnappyTest.java @@ -18,18 +18,19 @@ package io.netty.handler.codec.compression; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import static io.netty.handler.codec.compression.Snappy.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.nio.CharBuffer; public class SnappyTest { private final Snappy snappy = new Snappy(); - @After + @AfterEach public void resetSnappy() { snappy.reset(); } @@ -48,7 +49,7 @@ public class SnappyTest { ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 0x6e, 0x65, 0x74, 0x74, 0x79 }); - assertEquals("Literal was not decoded correctly", expected, out); + assertEquals(expected, out, "Literal was not decoded correctly"); in.release(); out.release(); @@ -71,15 +72,15 @@ public class SnappyTest { ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 0x6e, 0x65, 0x74, 0x74, 0x79, 0x6e, 0x65, 0x74, 0x74, 0x79 }); - assertEquals("Copy was not decoded correctly", expected, out); + assertEquals(expected, out, "Copy was not decoded correctly"); in.release(); out.release(); expected.release(); } - @Test(expected = DecompressionException.class) - public void testDecodeCopyWithTinyOffset() throws Exception { + @Test + public void testDecodeCopyWithTinyOffset() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 0x0b, // preamble length 0x04 << 2, // literal tag + length @@ -89,15 +90,15 @@ public class SnappyTest { }); ByteBuf out = Unpooled.buffer(10); try { - snappy.decode(in, out); + assertThrows(DecompressionException.class, () -> snappy.decode(in, out)); } finally { in.release(); out.release(); } } - @Test(expected = DecompressionException.class) - public void testDecodeCopyWithOffsetBeforeChunk() throws Exception { + @Test + public void testDecodeCopyWithOffsetBeforeChunk() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 0x0a, // preamble length 0x04 << 2, // literal tag + length @@ -107,15 +108,15 @@ public class SnappyTest { }); ByteBuf out = Unpooled.buffer(10); try { - snappy.decode(in, out); + assertThrows(DecompressionException.class, () -> snappy.decode(in, out)); } finally { in.release(); out.release(); } } - @Test(expected = DecompressionException.class) - public void testDecodeWithOverlyLongPreamble() throws Exception { + @Test + public void testDecodeWithOverlyLongPreamble() { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { -0x80, -0x80, -0x80, -0x80, 0x7f, // preamble length 0x04 << 2, // literal tag + length @@ -123,7 +124,7 @@ public class SnappyTest { }); ByteBuf out = Unpooled.buffer(10); try { - snappy.decode(in, out); + assertThrows(DecompressionException.class, () -> snappy.decode(in, out)); } finally { in.release(); out.release(); @@ -143,7 +144,7 @@ public class SnappyTest { 0x04 << 2, // literal tag + length 0x6e, 0x65, 0x74, 0x74, 0x79 // "netty" }); - assertEquals("Encoded literal was invalid", expected, out); + assertEquals(expected, out, "Encoded literal was invalid"); in.release(); out.release(); @@ -216,7 +217,7 @@ public class SnappyTest { 0x11, 0x4c, }); - assertEquals("Encoded result was incorrect", expected, out); + assertEquals(expected, out, "Encoded result was incorrect"); // Decode ByteBuf outDecoded = Unpooled.buffer(); @@ -260,13 +261,13 @@ public class SnappyTest { input.release(); } - @Test(expected = DecompressionException.class) + @Test public void testValidateChecksumFails() { ByteBuf input = Unpooled.wrappedBuffer(new byte[] { 'y', 't', 't', 'e', 'n' }); try { - validateChecksum(maskChecksum(0xd6cb8b55), input); + assertThrows(DecompressionException.class, () -> validateChecksum(maskChecksum(0xd6cb8b55), input)); } finally { input.release(); } @@ -290,7 +291,7 @@ public class SnappyTest { encodeLiteral(in, encoded, len); byte tag = encoded.readByte(); decodeLiteral(tag, encoded, decoded); - assertEquals("Encoded or decoded literal was incorrect", expected, decoded); + assertEquals(expected, decoded, "Encoded or decoded literal was incorrect"); } finally { in.release(); encoded.release(); diff --git a/codec/src/test/java/io/netty/handler/codec/frame/DelimiterBasedFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/frame/DelimiterBasedFrameDecoderTest.java index ac719ed462..6fa63eb29c 100644 --- a/codec/src/test/java/io/netty/handler/codec/frame/DelimiterBasedFrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/frame/DelimiterBasedFrameDecoderTest.java @@ -23,9 +23,11 @@ import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.TooLongFrameException; 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; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class DelimiterBasedFrameDecoderTest { diff --git a/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldBasedFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldBasedFrameDecoderTest.java index 766416982d..ecdd2122ae 100644 --- a/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldBasedFrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldBasedFrameDecoderTest.java @@ -22,9 +22,12 @@ import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.codec.TooLongFrameException; 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; +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 LengthFieldBasedFrameDecoderTest { @Test diff --git a/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldPrependerTest.java b/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldPrependerTest.java index 975fec3e8f..325f3a9e98 100644 --- a/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldPrependerTest.java +++ b/codec/src/test/java/io/netty/handler/codec/frame/LengthFieldPrependerTest.java @@ -20,19 +20,22 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.EncoderException; import io.netty.handler.codec.LengthFieldPrepender; import io.netty.util.CharsetUtil; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static io.netty.buffer.Unpooled.*; import java.nio.ByteOrder; -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.fail; public class LengthFieldPrependerTest { private ByteBuf msg; - @Before + @BeforeEach public void setUp() throws Exception { msg = copiedBuffer("A", CharsetUtil.ISO_8859_1); } @@ -107,7 +110,7 @@ public class LengthFieldPrependerTest { buf = ch.readOutbound(); assertSame(buf, msg); buf.release(); - assertFalse("The channel must have been completely read", ch.finish()); + assertFalse(ch.finish(), "The channel must have been completely read"); } } diff --git a/codec/src/test/java/io/netty/handler/codec/json/JsonObjectDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/json/JsonObjectDecoderTest.java index a67dfc22b1..96017268bc 100644 --- a/codec/src/test/java/io/netty/handler/codec/json/JsonObjectDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/json/JsonObjectDecoderTest.java @@ -22,9 +22,13 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.TooLongFrameException; 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; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class JsonObjectDecoderTest { @Test @@ -248,19 +252,18 @@ public class JsonObjectDecoderTest { assertFalse(ch.finish()); } - @Test(expected = CorruptedFrameException.class) + @Test public void testNonJsonContent1() { EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder()); try { - ch.writeInbound(Unpooled.copiedBuffer(" b [1,2,3]", CharsetUtil.UTF_8)); + assertThrows(CorruptedFrameException.class, + () -> ch.writeInbound(Unpooled.copiedBuffer(" b [1,2,3]", CharsetUtil.UTF_8))); } finally { assertFalse(ch.finish()); } - - fail(); } - @Test(expected = CorruptedFrameException.class) + @Test public void testNonJsonContent2() { EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder()); ch.writeInbound(Unpooled.copiedBuffer(" [1,2,3] ", CharsetUtil.UTF_8)); @@ -270,24 +273,22 @@ public class JsonObjectDecoderTest { res.release(); try { - ch.writeInbound(Unpooled.copiedBuffer(" a {\"key\" : 10}", CharsetUtil.UTF_8)); + assertThrows(CorruptedFrameException.class, + () -> ch.writeInbound(Unpooled.copiedBuffer(" a {\"key\" : 10}", CharsetUtil.UTF_8))); } finally { assertFalse(ch.finish()); } - - fail(); } - @Test (expected = TooLongFrameException.class) + @Test public void testMaxObjectLength() { EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder(6)); try { - ch.writeInbound(Unpooled.copiedBuffer("[2,4,5]", CharsetUtil.UTF_8)); + assertThrows(TooLongFrameException.class, + () -> ch.writeInbound(Unpooled.copiedBuffer("[2,4,5]", CharsetUtil.UTF_8))); } finally { assertFalse(ch.finish()); } - - fail(); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java index cc9921fc0c..d019bdd076 100644 --- a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java @@ -23,15 +23,15 @@ import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.MarshallingConfiguration; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -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.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class AbstractCompatibleMarshallingDecoderTest extends AbstractMarshallingTest { @SuppressWarnings("RedundantStringConstructorCall") diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java index 44989bf31e..e2686b8cb6 100644 --- a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java @@ -22,9 +22,11 @@ import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.MarshallingConfiguration; import org.jboss.marshalling.Unmarshaller; -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.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class AbstractCompatibleMarshallingEncoderTest extends AbstractMarshallingTest { diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractMarshallingTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractMarshallingTest.java index 916e0f99a1..5be0d78cab 100644 --- a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractMarshallingTest.java +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractMarshallingTest.java @@ -17,15 +17,16 @@ package io.netty.handler.codec.marshalling; import io.netty.util.internal.PlatformDependent; import org.jboss.marshalling.Marshalling; -import org.junit.Assume; -import org.junit.BeforeClass; +import org.junit.jupiter.api.BeforeAll; + +import static org.junit.jupiter.api.Assumptions.assumeTrue; public abstract class AbstractMarshallingTest { static final String SERIAL_FACTORY = "serial"; static final String RIVER_FACTORY = "river"; - @BeforeClass + @BeforeAll public static void checkSupported() throws Throwable { Throwable error = null; try { @@ -37,6 +38,6 @@ public abstract class AbstractMarshallingTest { } error = cause; } - Assume.assumeNoException(error); + assumeTrue(error == null, error + " was not null"); } } diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java index d74823ea89..3b488f73c5 100644 --- a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java @@ -22,7 +22,8 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.CodecException; import io.netty.handler.codec.TooLongFrameException; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; public class RiverMarshallingDecoderTest extends RiverCompatibleMarshallingDecoderTest { diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java index 6fb330afe9..5da6ecb74a 100644 --- a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java @@ -22,7 +22,8 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.CodecException; import io.netty.handler.codec.TooLongFrameException; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; public class SerialMarshallingDecoderTest extends SerialCompatibleMarshallingDecoderTest { diff --git a/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoderTest.java index 6c32a859b0..7b6a3c4346 100644 --- a/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoderTest.java @@ -17,20 +17,21 @@ package io.netty.handler.codec.protobuf; import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static io.netty.buffer.Unpooled.*; import static org.hamcrest.core.Is.*; import static org.hamcrest.core.IsNull.*; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ProtobufVarint32FrameDecoderTest { private EmbeddedChannel ch; - @Before + @BeforeEach public void setUp() { ch = new EmbeddedChannel(new ProtobufVarint32FrameDecoder()); } diff --git a/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32LengthFieldPrependerTest.java b/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32LengthFieldPrependerTest.java index 74e30df651..46fea14929 100644 --- a/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32LengthFieldPrependerTest.java +++ b/codec/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32LengthFieldPrependerTest.java @@ -17,19 +17,20 @@ package io.netty.handler.codec.protobuf; import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static io.netty.buffer.Unpooled.*; import static org.hamcrest.core.Is.*; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ProtobufVarint32LengthFieldPrependerTest { private EmbeddedChannel ch; - @Before + @BeforeEach public void setUp() { ch = new EmbeddedChannel(new ProtobufVarint32LengthFieldPrepender()); } diff --git a/codec/src/test/java/io/netty/handler/codec/serialization/CompactObjectSerializationTest.java b/codec/src/test/java/io/netty/handler/codec/serialization/CompactObjectSerializationTest.java index be871f569e..f39b7ea8e6 100644 --- a/codec/src/test/java/io/netty/handler/codec/serialization/CompactObjectSerializationTest.java +++ b/codec/src/test/java/io/netty/handler/codec/serialization/CompactObjectSerializationTest.java @@ -19,8 +19,8 @@ import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.util.List; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class CompactObjectSerializationTest { @@ -31,6 +31,6 @@ public class CompactObjectSerializationTest { CompactObjectOutputStream out = new CompactObjectOutputStream(pipeOut); CompactObjectInputStream in = new CompactObjectInputStream(pipeIn, ClassResolvers.cacheDisabled(null)); out.writeObject(List.class); - Assert.assertSame(List.class, in.readObject()); + Assertions.assertSame(List.class, in.readObject()); } } diff --git a/codec/src/test/java/io/netty/handler/codec/serialization/CompatibleObjectEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/serialization/CompatibleObjectEncoderTest.java index 6eb071e9da..3b48836799 100644 --- a/codec/src/test/java/io/netty/handler/codec/serialization/CompatibleObjectEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/serialization/CompatibleObjectEncoderTest.java @@ -18,14 +18,14 @@ package io.netty.handler.codec.serialization; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; public class CompatibleObjectEncoderTest { @Test diff --git a/codec/src/test/java/io/netty/handler/codec/string/LineEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/string/LineEncoderTest.java index 71ee1496a6..a8f2e3f2da 100644 --- a/codec/src/test/java/io/netty/handler/codec/string/LineEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/string/LineEncoderTest.java @@ -18,11 +18,12 @@ package io.netty.handler.codec.string; import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class LineEncoderTest { @@ -41,8 +42,8 @@ public class LineEncoderTest { byte[] data = new byte[buf.readableBytes()]; buf.readBytes(data); byte[] expected = (msg + lineSeparator.value()).getBytes(CharsetUtil.UTF_8); - Assert.assertArrayEquals(expected, data); - Assert.assertNull(channel.readOutbound()); + assertArrayEquals(expected, data); + assertNull(channel.readOutbound()); } finally { buf.release(); assertFalse(channel.finish()); diff --git a/codec/src/test/java/io/netty/handler/codec/string/StringEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/string/StringEncoderTest.java index d625b9293e..ba9b023cce 100644 --- a/codec/src/test/java/io/netty/handler/codec/string/StringEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/string/StringEncoderTest.java @@ -18,8 +18,8 @@ package io.netty.handler.codec.string; import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class StringEncoderTest { @@ -27,13 +27,13 @@ public class StringEncoderTest { public void testEncode() { String msg = "Test"; EmbeddedChannel channel = new EmbeddedChannel(new StringEncoder()); - Assert.assertTrue(channel.writeOutbound(msg)); - Assert.assertTrue(channel.finish()); + Assertions.assertTrue(channel.writeOutbound(msg)); + Assertions.assertTrue(channel.finish()); ByteBuf buf = channel.readOutbound(); byte[] data = new byte[buf.readableBytes()]; buf.readBytes(data); - Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data); - Assert.assertNull(channel.readOutbound()); + Assertions.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data); + Assertions.assertNull(channel.readOutbound()); buf.release(); } } diff --git a/codec/src/test/java/io/netty/handler/codec/xml/XmlFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/xml/XmlFrameDecoderTest.java index 0d83a64efa..8bf8a947b3 100644 --- a/codec/src/test/java/io/netty/handler/codec/xml/XmlFrameDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/xml/XmlFrameDecoderTest.java @@ -22,7 +22,7 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.TooLongFrameException; import io.netty.util.CharsetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.net.URISyntaxException; @@ -38,6 +38,7 @@ import java.util.List; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; public class XmlFrameDecoderTest { @@ -50,35 +51,38 @@ public class XmlFrameDecoderTest { ); } - @Test(expected = IllegalArgumentException.class) + @Test public void testConstructorWithIllegalArgs01() { - new XmlFrameDecoder(0); + assertThrows(IllegalArgumentException.class, () -> new XmlFrameDecoder(0)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testConstructorWithIllegalArgs02() { - new XmlFrameDecoder(-23); + assertThrows(IllegalArgumentException.class, () -> new XmlFrameDecoder(-23)); } - @Test(expected = TooLongFrameException.class) + @Test public void testDecodeWithFrameExceedingMaxLength() { XmlFrameDecoder decoder = new XmlFrameDecoder(3); EmbeddedChannel ch = new EmbeddedChannel(decoder); - ch.writeInbound(Unpooled.copiedBuffer("", CharsetUtil.UTF_8)); + assertThrows(TooLongFrameException.class, + () -> ch.writeInbound(Unpooled.copiedBuffer("", CharsetUtil.UTF_8))); } - @Test(expected = CorruptedFrameException.class) + @Test public void testDecodeWithInvalidInput() { XmlFrameDecoder decoder = new XmlFrameDecoder(1048576); EmbeddedChannel ch = new EmbeddedChannel(decoder); - ch.writeInbound(Unpooled.copiedBuffer("invalid XML", CharsetUtil.UTF_8)); + assertThrows(CorruptedFrameException.class, + () -> ch.writeInbound(Unpooled.copiedBuffer("invalid XML", CharsetUtil.UTF_8))); } - @Test(expected = CorruptedFrameException.class) + @Test public void testDecodeWithInvalidContentBeforeXml() { XmlFrameDecoder decoder = new XmlFrameDecoder(1048576); EmbeddedChannel ch = new EmbeddedChannel(decoder); - ch.writeInbound(Unpooled.copiedBuffer("invalid XML", CharsetUtil.UTF_8)); + assertThrows(CorruptedFrameException.class, + () -> ch.writeInbound(Unpooled.copiedBuffer("invalid XML", CharsetUtil.UTF_8))); } @Test