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
This commit is contained in:
Riley Park 2021-05-26 04:46:15 -07:00 committed by GitHub
parent c44a8d7e1d
commit bd8b2519b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
53 changed files with 524 additions and 513 deletions

View File

@ -20,20 +20,23 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel; 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 { public class ByteToMessageCodecTest {
@Test(expected = IllegalStateException.class) @Test
public void testSharable() { public void testSharable() {
new InvalidByteToMessageCodec(); assertThrows(IllegalStateException.class, InvalidByteToMessageCodec::new);
} }
@Test(expected = IllegalStateException.class) @Test
public void testSharable2() { public void testSharable2() {
new InvalidByteToMessageCodec2(); assertThrows(IllegalStateException.class, InvalidByteToMessageCodec2::new);
} }
@Test @Test

View File

@ -27,7 +27,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.socket.ChannelInputShutdownEvent; 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.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
@ -35,12 +35,12 @@ import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static io.netty.buffer.Unpooled.wrappedBuffer; import static io.netty.buffer.Unpooled.wrappedBuffer;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.fail;
public class ByteToMessageDecoderTest { public class ByteToMessageDecoderTest {
@ -163,8 +163,8 @@ public class ByteToMessageDecoderTest {
} }
private static void assertCumulationReleased(ByteBuf byteBuf) { private static void assertCumulationReleased(ByteBuf byteBuf) {
assertTrue("unexpected value: " + byteBuf, assertTrue(byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0,
byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0); "unexpected value: " + byteBuf);
} }
@Test @Test

View File

@ -15,11 +15,12 @@
package io.netty.handler.codec; package io.netty.handler.codec;
import io.netty.util.AsciiString; 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.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CharSequenceValueConverterTest { public class CharSequenceValueConverterTest {
@ -36,9 +37,9 @@ public class CharSequenceValueConverterTest {
assertEquals(127, converter.convertToByte(AsciiString.of("127"))); assertEquals(127, converter.convertToByte(AsciiString.of("127")));
} }
@Test(expected = NumberFormatException.class) @Test
public void testByteFromEmptyAsciiString() { public void testByteFromEmptyAsciiString() {
converter.convertToByte(AsciiString.EMPTY_STRING); assertThrows(NumberFormatException.class, () ->converter.convertToByte(AsciiString.EMPTY_STRING));
} }
@Test @Test

View File

@ -15,10 +15,11 @@
*/ */
package io.netty.handler.codec; package io.netty.handler.codec;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CodecOutputListTest { public class CodecOutputListTest {

View File

@ -23,29 +23,29 @@ import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.internal.SocketUtils; import io.netty.util.internal.SocketUtils;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class DatagramPacketDecoderTest { public class DatagramPacketDecoderTest {
private EmbeddedChannel channel; private EmbeddedChannel channel;
@Before @BeforeEach
public void setUp() { public void setUp() {
channel = new EmbeddedChannel( channel = new EmbeddedChannel(
new DatagramPacketDecoder( new DatagramPacketDecoder(
new StringDecoder(CharsetUtil.UTF_8))); new StringDecoder(CharsetUtil.UTF_8)));
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
assertFalse(channel.finish()); assertFalse(channel.finish());
} }

View File

@ -24,27 +24,27 @@ import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.internal.SocketUtils; import io.netty.util.internal.SocketUtils;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.List; import java.util.List;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.*;
public class DatagramPacketEncoderTest { public class DatagramPacketEncoderTest {
private EmbeddedChannel channel; private EmbeddedChannel channel;
@Before @BeforeEach
public void setUp() { public void setUp() {
channel = new EmbeddedChannel( channel = new EmbeddedChannel(
new DatagramPacketEncoder<String>( new DatagramPacketEncoder<String>(
new StringEncoder(CharsetUtil.UTF_8))); new StringEncoder(CharsetUtil.UTF_8)));
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
assertFalse(channel.finish()); assertFalse(channel.finish());
} }

View File

@ -15,13 +15,14 @@
*/ */
package io.netty.handler.codec; package io.netty.handler.codec;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import static org.junit.Assert.*;
import static io.netty.handler.codec.DateFormatter.*; 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 { public class DateFormatterTest {
/** /**

View File

@ -16,7 +16,7 @@ package io.netty.handler.codec;
import io.netty.util.AsciiString; import io.netty.util.AsciiString;
import io.netty.util.HashingStrategy; import io.netty.util.HashingStrategy;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -30,13 +30,14 @@ import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.Assert.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Tests for {@link DefaultHeaders}. * Tests for {@link DefaultHeaders}.
@ -165,13 +166,13 @@ public class DefaultHeadersTest {
assertFalse(itr.hasNext()); assertFalse(itr.hasNext());
} }
@Test(expected = IllegalStateException.class) @Test
public void valuesItrRemoveThrowsWhenEmpty() { public void valuesItrRemoveThrowsWhenEmpty() {
TestDefaultHeaders headers = newInstance(); TestDefaultHeaders headers = newInstance();
assertEquals(0, headers.size()); assertEquals(0, headers.size());
assertTrue(headers.isEmpty()); assertTrue(headers.isEmpty());
Iterator<CharSequence> itr = headers.valueIterator(of("name")); Iterator<CharSequence> itr = headers.valueIterator(of("name"));
itr.remove(); assertThrows(IllegalStateException.class, itr::remove);
} }
@Test @Test
@ -462,11 +463,11 @@ public class DefaultHeadersTest {
assertEquals(h2, h2); assertEquals(h2, h2);
} }
@Test(expected = NoSuchElementException.class) @Test
public void iterateEmptyHeadersShouldThrow() { public void iterateEmptyHeadersShouldThrow() {
Iterator<Map.Entry<CharSequence, CharSequence>> iterator = newInstance().iterator(); Iterator<Map.Entry<CharSequence, CharSequence>> iterator = newInstance().iterator();
assertFalse(iterator.hasNext()); assertFalse(iterator.hasNext());
iterator.next(); assertThrows(NoSuchElementException.class, iterator::next);
} }
@Test @Test
@ -569,10 +570,10 @@ public class DefaultHeadersTest {
assertEquals(headers1, expected); assertEquals(headers1, expected);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testAddSelf() { public void testAddSelf() {
TestDefaultHeaders headers = newInstance(); TestDefaultHeaders headers = newInstance();
headers.add(headers); assertThrows(IllegalArgumentException.class, () -> headers.add(headers));
} }
@Test @Test

View File

@ -20,11 +20,12 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.nio.charset.Charset; 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 { public class DelimiterBasedFrameDecoderTest {

View File

@ -14,149 +14,150 @@
*/ */
package io.netty.handler.codec; package io.netty.handler.codec;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class EmptyHeadersTest { public class EmptyHeadersTest {
private static final TestEmptyHeaders HEADERS = new TestEmptyHeaders(); private static final TestEmptyHeaders HEADERS = new TestEmptyHeaders();
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddStringValue() { public void testAddStringValue() {
HEADERS.add("name", "value"); assertThrows(UnsupportedOperationException.class, () -> HEADERS.add("name", "value"));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddStringValues() { public void testAddStringValues() {
HEADERS.add("name", "value1", "value2"); assertThrows(UnsupportedOperationException.class, () -> HEADERS.add("name", "value1", "value2"));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddStringValuesIterable() { 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() { public void testAddBoolean() {
HEADERS.addBoolean("name", true); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addBoolean("name", true));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddByte() { public void testAddByte() {
HEADERS.addByte("name", (byte) 1); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addByte("name", (byte) 1));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddChar() { public void testAddChar() {
HEADERS.addChar("name", 'a'); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addChar("name", 'a'));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddDouble() { public void testAddDouble() {
HEADERS.addDouble("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addDouble("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddFloat() { public void testAddFloat() {
HEADERS.addFloat("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addFloat("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddInt() { public void testAddInt() {
HEADERS.addInt("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addInt("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddLong() { public void testAddLong() {
HEADERS.addLong("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addLong("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddShort() { public void testAddShort() {
HEADERS.addShort("name", (short) 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addShort("name", (short) 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testAddTimeMillis() { public void testAddTimeMillis() {
HEADERS.addTimeMillis("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.addTimeMillis("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetStringValue() { public void testSetStringValue() {
HEADERS.set("name", "value"); assertThrows(UnsupportedOperationException.class, () -> HEADERS.set("name", "value"));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetStringValues() { public void testSetStringValues() {
HEADERS.set("name", "value1", "value2"); assertThrows(UnsupportedOperationException.class, () -> HEADERS.set("name", "value1", "value2"));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetStringValuesIterable() { 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() { public void testSetBoolean() {
HEADERS.setBoolean("name", true); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setBoolean("name", true));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetByte() { public void testSetByte() {
HEADERS.setByte("name", (byte) 1); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setByte("name", (byte) 1));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetChar() { public void testSetChar() {
HEADERS.setChar("name", 'a'); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setChar("name", 'a'));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetDouble() { public void testSetDouble() {
HEADERS.setDouble("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setDouble("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetFloat() { public void testSetFloat() {
HEADERS.setFloat("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setFloat("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetInt() { public void testSetInt() {
HEADERS.setInt("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setInt("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetLong() { public void testSetLong() {
HEADERS.setLong("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setLong("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetShort() { public void testSetShort() {
HEADERS.setShort("name", (short) 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setShort("name", (short) 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetTimeMillis() { public void testSetTimeMillis() {
HEADERS.setTimeMillis("name", 0); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setTimeMillis("name", 0));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSetAll() { public void testSetAll() {
HEADERS.setAll(new TestEmptyHeaders()); assertThrows(UnsupportedOperationException.class, () -> HEADERS.setAll(new TestEmptyHeaders()));
} }
@Test(expected = UnsupportedOperationException.class) @Test
public void testSet() { public void testSet() {
HEADERS.set(new TestEmptyHeaders()); assertThrows(UnsupportedOperationException.class, () -> HEADERS.set(new TestEmptyHeaders()));
} }
@Test @Test

View File

@ -18,9 +18,12 @@ package io.netty.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.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 { public class LengthFieldBasedFrameDecoderTest {
@ -36,19 +39,19 @@ public class LengthFieldBasedFrameDecoderTest {
EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4)); EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4));
try { try {
channel.writeInbound(buf); channel.writeInbound(buf);
Assert.fail(); fail();
} catch (TooLongFrameException e) { } catch (TooLongFrameException e) {
// expected // expected
} }
Assert.assertTrue(channel.finish()); assertTrue(channel.finish());
ByteBuf b = channel.readInbound(); ByteBuf b = channel.readInbound();
Assert.assertEquals(5, b.readableBytes()); assertEquals(5, b.readableBytes());
Assert.assertEquals(1, b.readInt()); assertEquals(1, b.readInt());
Assert.assertEquals('a', b.readByte()); assertEquals('a', b.readByte());
b.release(); b.release();
Assert.assertNull(channel.readInbound()); assertNull(channel.readInbound());
channel.finish(); channel.finish();
} }
@ -64,21 +67,21 @@ public class LengthFieldBasedFrameDecoderTest {
EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4)); EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4));
try { try {
channel.writeInbound(buf.readRetainedSlice(14)); channel.writeInbound(buf.readRetainedSlice(14));
Assert.fail(); fail();
} catch (TooLongFrameException e) { } catch (TooLongFrameException e) {
// expected // 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(); ByteBuf b = channel.readInbound();
Assert.assertEquals(5, b.readableBytes()); assertEquals(5, b.readableBytes());
Assert.assertEquals(1, b.readInt()); assertEquals(1, b.readInt());
Assert.assertEquals('a', b.readByte()); assertEquals('a', b.readByte());
b.release(); b.release();
Assert.assertNull(channel.readInbound()); assertNull(channel.readInbound());
channel.finish(); channel.finish();
buf.release(); buf.release();

View File

@ -19,12 +19,16 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*; 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 { public class LineBasedFrameDecoderTest {
@Test @Test

View File

@ -17,9 +17,11 @@
package io.netty.handler.codec; package io.netty.handler.codec;
import io.netty.channel.ChannelHandler; 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 static org.mockito.Mockito.*;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;

View File

@ -20,18 +20,20 @@ import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.netty.channel.embedded.EmbeddedChannel; 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.List; import java.util.List;
public class MessageToMessageEncoderTest { public class MessageToMessageEncoderTest {
/** /**
* Test-case for https://github.com/netty/netty/issues/1656 * Test-case for https://github.com/netty/netty/issues/1656
*/ */
@Test(expected = EncoderException.class) @Test
public void testException() { public void testException() {
EmbeddedChannel channel = new EmbeddedChannel(new MessageToMessageEncoder<Object>() { EmbeddedChannel channel = new EmbeddedChannel(new MessageToMessageEncoder<Object>() {
@Override @Override
@ -39,7 +41,7 @@ public class MessageToMessageEncoderTest {
throw new Exception(); throw new Exception();
} }
}); });
channel.writeOutbound(new Object()); assertThrows(EncoderException.class, () -> channel.writeOutbound(new Object()));
} }
@Test @Test

View File

@ -19,9 +19,10 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.Signal; 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 { public class ReplayingDecoderByteBufTest {

View File

@ -21,14 +21,17 @@ import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.socket.ChannelInputShutdownEvent; 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.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicReference; 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 { public class ReplayingDecoderTest {
@ -119,7 +122,7 @@ public class ReplayingDecoderTest {
buf.release(); buf.release();
buf2.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.read();
ch.finish(); ch.finish();
@ -176,7 +179,7 @@ public class ReplayingDecoderTest {
channel.writeInbound(buf.copy()); channel.writeInbound(buf.copy());
ByteBuf b = channel.readInbound(); 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(); b.release();
buf.release(); buf.release();
} }
@ -309,7 +312,7 @@ public class ReplayingDecoderTest {
} }
private static void assertCumulationReleased(ByteBuf byteBuf) { private static void assertCumulationReleased(ByteBuf byteBuf) {
assertTrue("unexpected value: " + byteBuf, assertTrue(byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0,
byteBuf == null || byteBuf == Unpooled.EMPTY_BUFFER || byteBuf.refCnt() == 0); "unexpected value: " + byteBuf);
} }
} }

View File

@ -20,7 +20,7 @@ import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.ByteOrder; import java.nio.ByteOrder;
@ -29,7 +29,9 @@ import java.security.cert.X509Certificate;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import static io.netty.buffer.Unpooled.copiedBuffer; 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 { public class Base64Test {
@ -149,8 +151,9 @@ public class Base64Test {
ByteBuf decoded = Base64.decode(encoded); ByteBuf decoded = Base64.decode(encoded);
ByteBuf expectedBuf = Unpooled.wrappedBuffer(bytes); ByteBuf expectedBuf = Unpooled.wrappedBuffer(bytes);
try { try {
assertEquals(StringUtil.NEWLINE + "expected: " + ByteBufUtil.hexDump(expectedBuf) + assertEquals(expectedBuf, decoded,
StringUtil.NEWLINE + "actual--: " + ByteBufUtil.hexDump(decoded), expectedBuf, decoded); StringUtil.NEWLINE + "expected: " + ByteBufUtil.hexDump(expectedBuf) +
StringUtil.NEWLINE + "actual--: " + ByteBufUtil.hexDump(decoded));
} finally { } finally {
src.release(); src.release();
encoded.release(); encoded.release();

View File

@ -17,8 +17,8 @@ package io.netty.handler.codec.bytes;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.EmptyArrays; import io.netty.util.internal.EmptyArrays;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Random; import java.util.Random;
@ -30,7 +30,7 @@ public class ByteArrayDecoderTest {
private EmbeddedChannel ch; private EmbeddedChannel ch;
@Before @BeforeEach
public void setUp() { public void setUp() {
ch = new EmbeddedChannel(new ByteArrayDecoder()); ch = new EmbeddedChannel(new ByteArrayDecoder());
} }

View File

@ -18,9 +18,9 @@ package io.netty.handler.codec.bytes;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.EmptyArrays; import io.netty.util.internal.EmptyArrays;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Random; import java.util.Random;
@ -32,12 +32,12 @@ public class ByteArrayEncoderTest {
private EmbeddedChannel ch; private EmbeddedChannel ch;
@Before @BeforeEach
public void setUp() { public void setUp() {
ch = new EmbeddedChannel(new ByteArrayEncoder()); ch = new EmbeddedChannel(new ByteArrayEncoder());
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
assertThat(ch.finish(), is(false)); assertThat(ch.finish(), is(false));
} }

View File

@ -19,20 +19,16 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Rule; import org.junit.jupiter.api.TestInstance;
import org.junit.experimental.theories.DataPoints; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.experimental.theories.FromDataPoints; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(Theories.class) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class AbstractDecoderTest extends AbstractCompressionTest { public abstract class AbstractDecoderTest extends AbstractCompressionTest {
protected static final ByteBuf WRAPPED_BYTES_SMALL; protected static final ByteBuf WRAPPED_BYTES_SMALL;
@ -43,13 +39,10 @@ public abstract class AbstractDecoderTest extends AbstractCompressionTest {
WRAPPED_BYTES_LARGE = Unpooled.wrappedBuffer(BYTES_LARGE); WRAPPED_BYTES_LARGE = Unpooled.wrappedBuffer(BYTES_LARGE);
} }
@Rule
public final ExpectedException expected = ExpectedException.none();
protected EmbeddedChannel channel; protected EmbeddedChannel channel;
protected static byte[] compressedBytesSmall; protected byte[] compressedBytesSmall;
protected static byte[] compressedBytesLarge; protected byte[] compressedBytesLarge;
protected AbstractDecoderTest() throws Exception { protected AbstractDecoderTest() throws Exception {
compressedBytesSmall = compress(BYTES_SMALL); compressedBytesSmall = compress(BYTES_SMALL);
@ -61,10 +54,14 @@ public abstract class AbstractDecoderTest extends AbstractCompressionTest {
*/ */
protected abstract byte[] compress(byte[] data) throws Exception; protected abstract byte[] compress(byte[] data) throws Exception;
@Before @BeforeEach
public abstract void initChannel(); public final void initChannel() {
channel = createChannel();
}
@After protected abstract EmbeddedChannel createChannel();
@AfterEach
public void destroyChannel() { public void destroyChannel() {
if (channel != null) { if (channel != null) {
channel.finishAndReleaseAll(); channel.finishAndReleaseAll();
@ -72,34 +69,35 @@ public abstract class AbstractDecoderTest extends AbstractCompressionTest {
} }
} }
@DataPoints("smallData") public ByteBuf[] smallData() {
public static ByteBuf[] smallData() {
ByteBuf heap = Unpooled.wrappedBuffer(compressedBytesSmall); ByteBuf heap = Unpooled.wrappedBuffer(compressedBytesSmall);
ByteBuf direct = Unpooled.directBuffer(compressedBytesSmall.length); ByteBuf direct = Unpooled.directBuffer(compressedBytesSmall.length);
direct.writeBytes(compressedBytesSmall); direct.writeBytes(compressedBytesSmall);
return new ByteBuf[] {heap, direct}; return new ByteBuf[] {heap, direct};
} }
@DataPoints("largeData") public ByteBuf[] largeData() {
public static ByteBuf[] largeData() {
ByteBuf heap = Unpooled.wrappedBuffer(compressedBytesLarge); ByteBuf heap = Unpooled.wrappedBuffer(compressedBytesLarge);
ByteBuf direct = Unpooled.directBuffer(compressedBytesLarge.length); ByteBuf direct = Unpooled.directBuffer(compressedBytesLarge.length);
direct.writeBytes(compressedBytesLarge); direct.writeBytes(compressedBytesLarge);
return new ByteBuf[] {heap, direct}; return new ByteBuf[] {heap, direct};
} }
@Theory @ParameterizedTest
public void testDecompressionOfSmallChunkOfData(@FromDataPoints("smallData") ByteBuf data) throws Exception { @MethodSource("smallData")
public void testDecompressionOfSmallChunkOfData(ByteBuf data) throws Exception {
testDecompression(WRAPPED_BYTES_SMALL, data); testDecompression(WRAPPED_BYTES_SMALL, data);
} }
@Theory @ParameterizedTest
public void testDecompressionOfLargeChunkOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { @MethodSource("largeData")
public void testDecompressionOfLargeChunkOfData(ByteBuf data) throws Exception {
testDecompression(WRAPPED_BYTES_LARGE, data); testDecompression(WRAPPED_BYTES_LARGE, data);
} }
@Theory @ParameterizedTest
public void testDecompressionOfBatchedFlowOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { @MethodSource("largeData")
public void testDecompressionOfBatchedFlowOfData(ByteBuf data) throws Exception {
testDecompressionOfBatchedFlow(WRAPPED_BYTES_LARGE, data); testDecompressionOfBatchedFlow(WRAPPED_BYTES_LARGE, data);
} }

View File

@ -19,18 +19,14 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.experimental.theories.DataPoints; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.experimental.theories.FromDataPoints; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(Theories.class)
public abstract class AbstractEncoderTest extends AbstractCompressionTest { public abstract class AbstractEncoderTest extends AbstractCompressionTest {
protected EmbeddedChannel channel; protected EmbeddedChannel channel;
@ -40,10 +36,14 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest {
*/ */
protected abstract ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception; protected abstract ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception;
@Before @BeforeEach
public abstract void initChannel(); public final void initChannel() {
channel = createChannel();
}
@After protected abstract EmbeddedChannel createChannel();
@AfterEach
public void destroyChannel() { public void destroyChannel() {
if (channel != null) { if (channel != null) {
channel.finishAndReleaseAll(); channel.finishAndReleaseAll();
@ -51,7 +51,6 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest {
} }
} }
@DataPoints("smallData")
public static ByteBuf[] smallData() { public static ByteBuf[] smallData() {
ByteBuf heap = Unpooled.wrappedBuffer(BYTES_SMALL); ByteBuf heap = Unpooled.wrappedBuffer(BYTES_SMALL);
ByteBuf direct = Unpooled.directBuffer(BYTES_SMALL.length); ByteBuf direct = Unpooled.directBuffer(BYTES_SMALL.length);
@ -59,7 +58,6 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest {
return new ByteBuf[] {heap, direct}; return new ByteBuf[] {heap, direct};
} }
@DataPoints("largeData")
public static ByteBuf[] largeData() { public static ByteBuf[] largeData() {
ByteBuf heap = Unpooled.wrappedBuffer(BYTES_LARGE); ByteBuf heap = Unpooled.wrappedBuffer(BYTES_LARGE);
ByteBuf direct = Unpooled.directBuffer(BYTES_LARGE.length); ByteBuf direct = Unpooled.directBuffer(BYTES_LARGE.length);
@ -67,18 +65,21 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest {
return new ByteBuf[] {heap, direct}; return new ByteBuf[] {heap, direct};
} }
@Theory @ParameterizedTest
public void testCompressionOfSmallChunkOfData(@FromDataPoints("smallData") ByteBuf data) throws Exception { @MethodSource("smallData")
public void testCompressionOfSmallChunkOfData(ByteBuf data) throws Exception {
testCompression(data); testCompression(data);
} }
@Theory @ParameterizedTest
public void testCompressionOfLargeChunkOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { @MethodSource("largeData")
public void testCompressionOfLargeChunkOfData(ByteBuf data) throws Exception {
testCompression(data); testCompression(data);
} }
@Theory @ParameterizedTest
public void testCompressionOfBatchedFlowOfData(@FromDataPoints("largeData") ByteBuf data) throws Exception { @MethodSource("largeData")
public void testCompressionOfBatchedFlowOfData(ByteBuf data) throws Exception {
testCompressionOfBatchedFlow(data); testCompressionOfBatchedFlow(data);
} }

View File

@ -22,16 +22,18 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.EmptyArrays; import io.netty.util.internal.EmptyArrays;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; 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 { public abstract class AbstractIntegrationTest {
@ -43,13 +45,13 @@ public abstract class AbstractIntegrationTest {
protected abstract EmbeddedChannel createEncoder(); protected abstract EmbeddedChannel createEncoder();
protected abstract EmbeddedChannel createDecoder(); protected abstract EmbeddedChannel createDecoder();
@Before @BeforeEach
public void initChannels() throws Exception { public void initChannels() throws Exception {
encoder = createEncoder(); encoder = createEncoder();
decoder = createDecoder(); decoder = createDecoder();
} }
@After @AfterEach
public void closeChannels() throws Exception { public void closeChannels() throws Exception {
encoder.close(); encoder.close();
for (;;) { for (;;) {

View File

@ -21,22 +21,18 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.experimental.theories.DataPoints; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.experimental.theories.FromDataPoints; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Random; import java.util.Random;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(Theories.class)
public class BrotliDecoderTest { public class BrotliDecoderTest {
private static final Random RANDOM; private static final Random RANDOM;
@ -78,12 +74,12 @@ public class BrotliDecoderTest {
private EmbeddedChannel channel; private EmbeddedChannel channel;
@Before @BeforeEach
public void initChannel() { public void initChannel() {
channel = new EmbeddedChannel(new BrotliDecoder()); channel = new EmbeddedChannel(new BrotliDecoder());
} }
@After @AfterEach
public void destroyChannel() { public void destroyChannel() {
if (channel != null) { if (channel != null) {
channel.finishAndReleaseAll(); channel.finishAndReleaseAll();
@ -91,7 +87,6 @@ public class BrotliDecoderTest {
} }
} }
@DataPoints("smallData")
public static ByteBuf[] smallData() { public static ByteBuf[] smallData() {
ByteBuf heap = Unpooled.wrappedBuffer(COMPRESSED_BYTES_SMALL); ByteBuf heap = Unpooled.wrappedBuffer(COMPRESSED_BYTES_SMALL);
ByteBuf direct = Unpooled.directBuffer(COMPRESSED_BYTES_SMALL.length); ByteBuf direct = Unpooled.directBuffer(COMPRESSED_BYTES_SMALL.length);
@ -99,7 +94,6 @@ public class BrotliDecoderTest {
return new ByteBuf[]{heap, direct}; return new ByteBuf[]{heap, direct};
} }
@DataPoints("largeData")
public static ByteBuf[] largeData() { public static ByteBuf[] largeData() {
ByteBuf heap = Unpooled.wrappedBuffer(COMPRESSED_BYTES_LARGE); ByteBuf heap = Unpooled.wrappedBuffer(COMPRESSED_BYTES_LARGE);
ByteBuf direct = Unpooled.directBuffer(COMPRESSED_BYTES_LARGE.length); ByteBuf direct = Unpooled.directBuffer(COMPRESSED_BYTES_LARGE.length);
@ -107,18 +101,21 @@ public class BrotliDecoderTest {
return new ByteBuf[]{heap, direct}; return new ByteBuf[]{heap, direct};
} }
@Theory @ParameterizedTest
public void testDecompressionOfSmallChunkOfData(@FromDataPoints("smallData") ByteBuf data) { @MethodSource("smallData")
public void testDecompressionOfSmallChunkOfData(ByteBuf data) {
testDecompression(WRAPPED_BYTES_SMALL, data); testDecompression(WRAPPED_BYTES_SMALL, data);
} }
@Theory @ParameterizedTest
public void testDecompressionOfLargeChunkOfData(@FromDataPoints("largeData") ByteBuf data) { @MethodSource("largeData")
public void testDecompressionOfLargeChunkOfData(ByteBuf data) {
testDecompression(WRAPPED_BYTES_LARGE, data); testDecompression(WRAPPED_BYTES_LARGE, data);
} }
@Theory @ParameterizedTest
public void testDecompressionOfBatchedFlowOfData(@FromDataPoints("largeData") ByteBuf data) { @MethodSource("largeData")
public void testDecompressionOfBatchedFlowOfData(ByteBuf data) {
testDecompressionOfBatchedFlow(WRAPPED_BYTES_LARGE, data); testDecompressionOfBatchedFlow(WRAPPED_BYTES_LARGE, data);
} }

View File

@ -18,8 +18,8 @@ package io.netty.handler.codec.compression;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import net.jpountz.xxhash.XXHashFactory; import net.jpountz.xxhash.XXHashFactory;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Random; import java.util.Random;
import java.util.zip.Adler32; import java.util.zip.Adler32;
@ -27,13 +27,13 @@ import java.util.zip.CRC32;
import java.util.zip.Checksum; import java.util.zip.Checksum;
import static io.netty.handler.codec.compression.Lz4Constants.DEFAULT_SEED; 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 { public class ByteBufChecksumTest {
private static final byte[] BYTE_ARRAY = new byte[1024]; private static final byte[] BYTE_ARRAY = new byte[1024];
@BeforeClass @BeforeAll
public static void setUp() { public static void setUp() {
new Random().nextBytes(BYTE_ARRAY); new Random().nextBytes(BYTE_ARRAY);
} }

View File

@ -19,13 +19,14 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Arrays; import java.util.Arrays;
import static io.netty.handler.codec.compression.Bzip2Constants.*; 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 { public class Bzip2DecoderTest extends AbstractDecoderTest {
@ -40,8 +41,8 @@ public class Bzip2DecoderTest extends AbstractDecoderTest {
} }
@Override @Override
public void initChannel() { protected EmbeddedChannel createChannel() {
channel = new EmbeddedChannel(new Bzip2Decoder()); return new EmbeddedChannel(new Bzip2Decoder());
} }
private void writeInboundDestroyAndExpectDecompressionException(ByteBuf in) { private void writeInboundDestroyAndExpectDecompressionException(ByteBuf in) {
@ -58,32 +59,24 @@ public class Bzip2DecoderTest extends AbstractDecoderTest {
} }
@Test @Test
public void testUnexpectedStreamIdentifier() throws Exception { public void testUnexpectedStreamIdentifier() {
expected.expect(DecompressionException.class);
expected.expectMessage("Unexpected stream identifier contents");
ByteBuf in = Unpooled.buffer(); ByteBuf in = Unpooled.buffer();
in.writeLong(1823080128301928729L); //random value in.writeLong(1823080128301928729L); //random value
writeInboundDestroyAndExpectDecompressionException(in); assertThrows(DecompressionException.class,
() -> writeInboundDestroyAndExpectDecompressionException(in), "Unexpected stream identifier contents");
} }
@Test @Test
public void testInvalidBlockSize() throws Exception { public void testInvalidBlockSize() {
expected.expect(DecompressionException.class);
expected.expectMessage("block size is invalid");
ByteBuf in = Unpooled.buffer(); ByteBuf in = Unpooled.buffer();
in.writeMedium(MAGIC_NUMBER); in.writeMedium(MAGIC_NUMBER);
in.writeByte('0'); //incorrect block size in.writeByte('0'); //incorrect block size
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "block size is invalid");
} }
@Test @Test
public void testBadBlockHeader() throws Exception { public void testBadBlockHeader() {
expected.expect(DecompressionException.class);
expected.expectMessage("bad block header");
ByteBuf in = Unpooled.buffer(); ByteBuf in = Unpooled.buffer();
in.writeMedium(MAGIC_NUMBER); in.writeMedium(MAGIC_NUMBER);
in.writeByte('1'); //block size in.writeByte('1'); //block size
@ -91,14 +84,11 @@ public class Bzip2DecoderTest extends AbstractDecoderTest {
in.writeMedium(11); //incorrect block header in.writeMedium(11); //incorrect block header
in.writeInt(11111); //block CRC in.writeInt(11111); //block CRC
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "bad block header");
} }
@Test @Test
public void testStreamCrcErrorOfEmptyBlock() throws Exception { public void testStreamCrcErrorOfEmptyBlock() {
expected.expect(DecompressionException.class);
expected.expectMessage("stream CRC error");
ByteBuf in = Unpooled.buffer(); ByteBuf in = Unpooled.buffer();
in.writeMedium(MAGIC_NUMBER); in.writeMedium(MAGIC_NUMBER);
in.writeByte('1'); //block size in.writeByte('1'); //block size
@ -106,66 +96,54 @@ public class Bzip2DecoderTest extends AbstractDecoderTest {
in.writeMedium(END_OF_STREAM_MAGIC_2); in.writeMedium(END_OF_STREAM_MAGIC_2);
in.writeInt(1); //wrong storedCombinedCRC in.writeInt(1); //wrong storedCombinedCRC
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "stream CRC error");
} }
@Test @Test
public void testStreamCrcError() throws Exception { public void testStreamCrcError() {
expected.expect(DecompressionException.class);
expected.expectMessage("stream CRC error");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[41] = (byte) 0xDD; data[41] = (byte) 0xDD;
tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)); assertThrows(DecompressionException.class,
() -> tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)), "stream CRC error");
} }
@Test @Test
public void testIncorrectHuffmanGroupsNumber() throws Exception { public void testIncorrectHuffmanGroupsNumber() {
expected.expect(DecompressionException.class);
expected.expectMessage("incorrect huffman groups number");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[25] = 0x70; data[25] = 0x70;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "incorrect huffman groups number");
} }
@Test @Test
public void testIncorrectSelectorsNumber() throws Exception { public void testIncorrectSelectorsNumber() {
expected.expect(DecompressionException.class);
expected.expectMessage("incorrect selectors number");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[25] = 0x2F; data[25] = 0x2F;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "incorrect selectors number");
} }
@Test @Test
public void testBlockCrcError() throws Exception { public void testBlockCrcError() {
expected.expect(DecompressionException.class);
expected.expectMessage("block CRC error");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[11] = 0x77; data[11] = 0x77;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
writeInboundDestroyAndExpectDecompressionException(in); assertThrows(DecompressionException.class,
() -> writeInboundDestroyAndExpectDecompressionException(in), "block CRC error");
} }
@Test @Test
public void testStartPointerInvalid() throws Exception { public void testStartPointerInvalid() {
expected.expect(DecompressionException.class);
expected.expectMessage("start pointer invalid");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[14] = (byte) 0xFF; data[14] = (byte) 0xFF;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
writeInboundDestroyAndExpectDecompressionException(in); assertThrows(DecompressionException.class,
() -> writeInboundDestroyAndExpectDecompressionException(in), "start pointer invalid");
} }
@Override @Override

View File

@ -24,13 +24,13 @@ import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import java.io.InputStream; import java.io.InputStream;
import static io.netty.handler.codec.compression.Bzip2Constants.*; 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 { public class Bzip2EncoderTest extends AbstractEncoderTest {
@Override @Override
public void initChannel() { protected EmbeddedChannel createChannel() {
channel = new EmbeddedChannel(new Bzip2Encoder(MIN_BLOCK_SIZE)); return new EmbeddedChannel(new Bzip2Encoder(MIN_BLOCK_SIZE));
} }
@Override @Override

View File

@ -16,7 +16,7 @@
package io.netty.handler.codec.compression; package io.netty.handler.codec.compression;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test; import org.junit.jupiter.api.Test;
public class Bzip2IntegrationTest extends AbstractIntegrationTest { public class Bzip2IntegrationTest extends AbstractIntegrationTest {

View File

@ -22,7 +22,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; 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 { public class FastLzIntegrationTest extends AbstractIntegrationTest {

View File

@ -19,12 +19,13 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import net.jpountz.lz4.LZ4BlockOutputStream; import net.jpountz.lz4.LZ4BlockOutputStream;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Arrays; import java.util.Arrays;
import static io.netty.handler.codec.compression.Lz4Constants.*; import static io.netty.handler.codec.compression.Lz4Constants.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class Lz4FrameDecoderTest extends AbstractDecoderTest { public class Lz4FrameDecoderTest extends AbstractDecoderTest {
@ -42,91 +43,71 @@ public class Lz4FrameDecoderTest extends AbstractDecoderTest {
} }
@Override @Override
public void initChannel() { protected EmbeddedChannel createChannel() {
channel = new EmbeddedChannel(new Lz4FrameDecoder(true)); return new EmbeddedChannel(new Lz4FrameDecoder(true));
} }
@Test @Test
public void testUnexpectedBlockIdentifier() throws Exception { public void testUnexpectedBlockIdentifier() {
expected.expect(DecompressionException.class);
expected.expectMessage("unexpected block identifier");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[1] = 0x00; data[1] = 0x00;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unexpected block identifier");
} }
@Test @Test
public void testInvalidCompressedLength() throws Exception { public void testInvalidCompressedLength() {
expected.expect(DecompressionException.class);
expected.expectMessage("invalid compressedLength");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[12] = (byte) 0xFF; data[12] = (byte) 0xFF;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "invalid compressedLength");
} }
@Test @Test
public void testInvalidDecompressedLength() throws Exception { public void testInvalidDecompressedLength() {
expected.expect(DecompressionException.class);
expected.expectMessage("invalid decompressedLength");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[16] = (byte) 0xFF; data[16] = (byte) 0xFF;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "invalid decompressedLength");
} }
@Test @Test
public void testDecompressedAndCompressedLengthMismatch() throws Exception { public void testDecompressedAndCompressedLengthMismatch() {
expected.expect(DecompressionException.class);
expected.expectMessage("mismatch");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[13] = 0x01; data[13] = 0x01;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "mismatch");
} }
@Test @Test
public void testUnexpectedBlockType() throws Exception { public void testUnexpectedBlockType() {
expected.expect(DecompressionException.class);
expected.expectMessage("unexpected blockType");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[8] = 0x36; data[8] = 0x36;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unexpected blockType");
} }
@Test @Test
public void testMismatchingChecksum() throws Exception { public void testMismatchingChecksum() {
expected.expect(DecompressionException.class);
expected.expectMessage("mismatching checksum");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[17] = 0x01; data[17] = 0x01;
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "mismatching checksum");
} }
@Test @Test
public void testChecksumErrorOfLastBlock() throws Exception { public void testChecksumErrorOfLastBlock() {
expected.expect(DecompressionException.class);
expected.expectMessage("checksum error");
final byte[] data = Arrays.copyOf(DATA, DATA.length); final byte[] data = Arrays.copyOf(DATA, DATA.length);
data[44] = 0x01; data[44] = 0x01;
tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)); assertThrows(DecompressionException.class,
() -> tryDecodeAndCatchBufLeaks(channel, Unpooled.wrappedBuffer(data)), "checksum error");
} }
@Override @Override

View File

@ -33,12 +33,13 @@ import io.netty.channel.nio.NioHandler;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.EncoderException; import io.netty.handler.codec.EncoderException;
import java.util.concurrent.TimeUnit;
import net.jpountz.lz4.LZ4BlockInputStream; import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4Factory; import net.jpountz.lz4.LZ4Factory;
import net.jpountz.xxhash.XXHashFactory; import net.jpountz.xxhash.XXHashFactory;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.Timeout;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; 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.instanceOf;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotNull; 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; import static org.mockito.Mockito.when;
public class Lz4FrameEncoderTest extends AbstractEncoderTest { public class Lz4FrameEncoderTest extends AbstractEncoderTest {
@ -75,15 +78,15 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
@Mock @Mock
private ByteBuf buffer; private ByteBuf buffer;
@Before @BeforeEach
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(ctx.alloc()).thenReturn(ByteBufAllocator.DEFAULT); when(ctx.alloc()).thenReturn(ByteBufAllocator.DEFAULT);
} }
@Override @Override
public void initChannel() { protected EmbeddedChannel createChannel() {
channel = new EmbeddedChannel(new Lz4FrameEncoder()); return new EmbeddedChannel(new Lz4FrameEncoder());
} }
@Override @Override
@ -139,11 +142,11 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
try { try {
Lz4FrameEncoder encoder = newEncoder(blockSize, Lz4FrameEncoder.DEFAULT_MAX_ENCODE_SIZE); Lz4FrameEncoder encoder = newEncoder(blockSize, Lz4FrameEncoder.DEFAULT_MAX_ENCODE_SIZE);
out = encoder.allocateBuffer(ctx, in, preferDirect); out = encoder.allocateBuffer(ctx, in, preferDirect);
Assert.assertNotNull(out); assertNotNull(out);
if (NONALLOCATABLE_SIZE == bufSize) { if (NONALLOCATABLE_SIZE == bufSize) {
Assert.assertFalse(out.isWritable()); assertFalse(out.isWritable());
} else { } else {
Assert.assertTrue(out.writableBytes() > 0); assertTrue(out.writableBytes() > 0);
if (!preferDirect) { if (!preferDirect) {
// Only check if preferDirect is not true as if a direct buffer is returned or not depends on // Only check if preferDirect is not true as if a direct buffer is returned or not depends on
// if sun.misc.Unsafe is present. // if sun.misc.Unsafe is present.
@ -158,7 +161,7 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
} }
} }
@Test (expected = EncoderException.class) @Test
public void testAllocateDirectBufferExceedMaxEncodeSize() { public void testAllocateDirectBufferExceedMaxEncodeSize() {
final int maxEncodeSize = 1024; final int maxEncodeSize = 1024;
Lz4FrameEncoder encoder = newEncoder(Lz4Constants.DEFAULT_BLOCK_SIZE, maxEncodeSize); Lz4FrameEncoder encoder = newEncoder(Lz4Constants.DEFAULT_BLOCK_SIZE, maxEncodeSize);
@ -166,7 +169,7 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(inputBufferSize, inputBufferSize); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(inputBufferSize, inputBufferSize);
try { try {
buf.writerIndex(inputBufferSize); buf.writerIndex(inputBufferSize);
encoder.allocateBuffer(ctx, buf, false); assertThrows(EncoderException.class, () -> encoder.allocateBuffer(ctx, buf, false));
} finally { } finally {
buf.release(); buf.release();
} }
@ -187,13 +190,13 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
* {@link Lz4FrameEncoder#allocateBuffer(ChannelHandlerContext, ByteBuf, boolean)}, but this is safest way * {@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. * of testing the overflow conditions as allocating the huge buffers fails in many CI environments.
*/ */
@Test (expected = EncoderException.class) @Test
public void testAllocateOnHeapBufferOverflowsOutputSize() { public void testAllocateOnHeapBufferOverflowsOutputSize() {
final int maxEncodeSize = Integer.MAX_VALUE; final int maxEncodeSize = Integer.MAX_VALUE;
Lz4FrameEncoder encoder = newEncoder(Lz4Constants.DEFAULT_BLOCK_SIZE, maxEncodeSize); Lz4FrameEncoder encoder = newEncoder(Lz4Constants.DEFAULT_BLOCK_SIZE, maxEncodeSize);
when(buffer.readableBytes()).thenReturn(maxEncodeSize); when(buffer.readableBytes()).thenReturn(maxEncodeSize);
buffer.writerIndex(maxEncodeSize); buffer.writerIndex(maxEncodeSize);
encoder.allocateBuffer(ctx, buffer, false); assertThrows(EncoderException.class, () -> encoder.allocateBuffer(ctx, buffer, false));
} }
@Test @Test
@ -203,14 +206,14 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
int size = 27; int size = 27;
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size);
buf.writerIndex(size); buf.writerIndex(size);
Assert.assertEquals(0, encoder.getBackingBuffer().readableBytes()); assertEquals(0, encoder.getBackingBuffer().readableBytes());
channel.write(buf); channel.write(buf);
Assert.assertTrue(channel.outboundMessages().isEmpty()); assertTrue(channel.outboundMessages().isEmpty());
Assert.assertEquals(size, encoder.getBackingBuffer().readableBytes()); assertEquals(size, encoder.getBackingBuffer().readableBytes());
channel.flush(); channel.flush();
Assert.assertTrue(channel.finish()); assertTrue(channel.finish());
Assert.assertTrue(channel.releaseOutbound()); assertTrue(channel.releaseOutbound());
Assert.assertFalse(channel.releaseInbound()); assertFalse(channel.releaseInbound());
} }
@Test @Test
@ -222,24 +225,25 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
int size = blockSize - 1; int size = blockSize - 1;
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size);
buf.writerIndex(size); buf.writerIndex(size);
Assert.assertEquals(0, encoder.getBackingBuffer().readableBytes()); assertEquals(0, encoder.getBackingBuffer().readableBytes());
channel.write(buf); channel.write(buf);
Assert.assertEquals(size, encoder.getBackingBuffer().readableBytes()); assertEquals(size, encoder.getBackingBuffer().readableBytes());
int nextSize = size - 1; int nextSize = size - 1;
buf = ByteBufAllocator.DEFAULT.buffer(nextSize, nextSize); buf = ByteBufAllocator.DEFAULT.buffer(nextSize, nextSize);
buf.writerIndex(nextSize); buf.writerIndex(nextSize);
channel.write(buf); channel.write(buf);
Assert.assertEquals(size + nextSize - blockSize, encoder.getBackingBuffer().readableBytes()); assertEquals(size + nextSize - blockSize, encoder.getBackingBuffer().readableBytes());
channel.flush(); channel.flush();
Assert.assertEquals(0, encoder.getBackingBuffer().readableBytes()); assertEquals(0, encoder.getBackingBuffer().readableBytes());
Assert.assertTrue(channel.finish()); assertTrue(channel.finish());
Assert.assertTrue(channel.releaseOutbound()); assertTrue(channel.releaseOutbound());
Assert.assertFalse(channel.releaseInbound()); assertFalse(channel.releaseInbound());
} }
@Test(timeout = 3000) @Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void writingAfterClosedChannelDoesNotNPE() throws InterruptedException { public void writingAfterClosedChannelDoesNotNPE() throws InterruptedException {
EventLoopGroup group = new MultithreadEventLoopGroup(2, NioHandler.newFactory()); EventLoopGroup group = new MultithreadEventLoopGroup(2, NioHandler.newFactory());
Channel serverChannel = null; Channel serverChannel = null;

View File

@ -19,9 +19,10 @@ import com.ning.compress.lzf.LZFEncoder;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; 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 com.ning.compress.lzf.LZFChunk.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class LzfDecoderTest extends AbstractDecoderTest { public class LzfDecoderTest extends AbstractDecoderTest {
@ -29,35 +30,29 @@ public class LzfDecoderTest extends AbstractDecoderTest {
} }
@Override @Override
public void initChannel() { protected EmbeddedChannel createChannel() {
channel = new EmbeddedChannel(new LzfDecoder()); return new EmbeddedChannel(new LzfDecoder());
} }
@Test @Test
public void testUnexpectedBlockIdentifier() throws Exception { public void testUnexpectedBlockIdentifier() {
expected.expect(DecompressionException.class);
expected.expectMessage("unexpected block identifier");
ByteBuf in = Unpooled.buffer(); ByteBuf in = Unpooled.buffer();
in.writeShort(0x1234); //random value in.writeShort(0x1234); //random value
in.writeByte(BLOCK_TYPE_NON_COMPRESSED); in.writeByte(BLOCK_TYPE_NON_COMPRESSED);
in.writeShort(0); in.writeShort(0);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unexpected block identifier");
} }
@Test @Test
public void testUnknownTypeOfChunk() throws Exception { public void testUnknownTypeOfChunk() {
expected.expect(DecompressionException.class);
expected.expectMessage("unknown type of chunk");
ByteBuf in = Unpooled.buffer(); ByteBuf in = Unpooled.buffer();
in.writeByte(BYTE_Z); in.writeByte(BYTE_Z);
in.writeByte(BYTE_V); in.writeByte(BYTE_V);
in.writeByte(0xFF); //random value in.writeByte(0xFF); //random value
in.writeInt(0); in.writeInt(0);
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in), "unknown type of chunk");
} }
@Override @Override

View File

@ -23,8 +23,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
public class LzfEncoderTest extends AbstractEncoderTest { public class LzfEncoderTest extends AbstractEncoderTest {
@Override @Override
public void initChannel() { protected EmbeddedChannel createChannel() {
channel = new EmbeddedChannel(new LzfEncoder()); return new EmbeddedChannel(new LzfEncoder());
} }
@Override @Override

View File

@ -22,26 +22,27 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import lzma.sdk.lzma.Decoder; import lzma.sdk.lzma.Decoder;
import lzma.streams.LzmaInputStream; import lzma.streams.LzmaInputStream;
import org.junit.experimental.theories.FromDataPoints; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.experimental.theories.Theory; import org.junit.jupiter.params.provider.MethodSource;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class LzmaFrameEncoderTest extends AbstractEncoderTest { public class LzmaFrameEncoderTest extends AbstractEncoderTest {
@Override @Override
public void initChannel() { protected EmbeddedChannel createChannel() {
channel = new EmbeddedChannel(new LzmaFrameEncoder()); return new EmbeddedChannel(new LzmaFrameEncoder());
} }
@Theory @ParameterizedTest
@MethodSource("smallData")
@Override @Override
public void testCompressionOfBatchedFlowOfData(@FromDataPoints("smallData") ByteBuf data) throws Exception { public void testCompressionOfBatchedFlowOfData(ByteBuf data) throws Exception {
testCompressionOfBatchedFlow(data); testCompressionOfBatchedFlow(data);
} }

View File

@ -18,81 +18,85 @@ package io.netty.handler.codec.compression;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
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 SnappyFrameDecoderTest { public class SnappyFrameDecoderTest {
private EmbeddedChannel channel; private EmbeddedChannel channel;
@Before @BeforeEach
public void initChannel() { public void initChannel() {
channel = new EmbeddedChannel(new SnappyFrameDecoder()); channel = new EmbeddedChannel(new SnappyFrameDecoder());
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
assertFalse(channel.finishAndReleaseAll()); assertFalse(channel.finishAndReleaseAll());
} }
@Test(expected = DecompressionException.class) @Test
public void testReservedUnskippableChunkTypeCausesError() throws Exception { public void testReservedUnskippableChunkTypeCausesError() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x03, 0x01, 0x00, 0x00, 0x00 0x03, 0x01, 0x00, 0x00, 0x00
}); });
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in));
} }
@Test(expected = DecompressionException.class) @Test
public void testInvalidStreamIdentifierLength() throws Exception { public void testInvalidStreamIdentifierLength() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x80, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' -0x80, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in));
} }
@Test(expected = DecompressionException.class) @Test
public void testInvalidStreamIdentifierValue() throws Exception { public void testInvalidStreamIdentifierValue() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y' (byte) 0xff, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y'
}); });
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in));
} }
@Test(expected = DecompressionException.class) @Test
public void testReservedSkippableBeforeStreamIdentifier() throws Exception { public void testReservedSkippableBeforeStreamIdentifier() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x7f, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y' -0x7f, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y'
}); });
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in));
} }
@Test(expected = DecompressionException.class) @Test
public void testUncompressedDataBeforeStreamIdentifier() throws Exception { public void testUncompressedDataBeforeStreamIdentifier() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x01, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' 0x01, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in));
} }
@Test(expected = DecompressionException.class) @Test
public void testCompressedDataBeforeStreamIdentifier() throws Exception { public void testCompressedDataBeforeStreamIdentifier() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x00, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' 0x00, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in));
} }
@Test @Test
public void testReservedSkippableSkipsInput() throws Exception { public void testReservedSkippableSkipsInput() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
-0x7f, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' -0x7f, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
@ -105,7 +109,7 @@ public class SnappyFrameDecoderTest {
} }
@Test @Test
public void testUncompressedDataAppendsToOut() throws Exception { public void testUncompressedDataAppendsToOut() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y' 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
@ -122,7 +126,7 @@ public class SnappyFrameDecoderTest {
} }
@Test @Test
public void testCompressedDataDecodesAndAppendsToOut() throws Exception { public void testCompressedDataDecodesAndAppendsToOut() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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 // The following two tests differ in only the checksum provided for the literal
// uncompressed string "netty" // uncompressed string "netty"
@Test(expected = DecompressionException.class) @Test
public void testInvalidChecksumThrowsException() throws Exception { public void testInvalidChecksumThrowsException() {
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true)); EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));
try { try {
// checksum here is presented as 0 // 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' 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
channel.writeInbound(in); assertThrows(DecompressionException.class, () -> channel.writeInbound(in));
} finally { } finally {
channel.finishAndReleaseAll(); channel.finishAndReleaseAll();
} }
} }
@Test @Test
public void testInvalidChecksumDoesNotThrowException() throws Exception { public void testInvalidChecksumDoesNotThrowException() {
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true)); EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));
try { try {
// checksum here is presented as a282986f (little endian) // checksum here is presented as a282986f (little endian)

View File

@ -19,15 +19,15 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.*;
public class SnappyFrameEncoderTest { public class SnappyFrameEncoderTest {
private EmbeddedChannel channel; private EmbeddedChannel channel;
@Before @BeforeEach
public void setUp() { public void setUp() {
channel = new EmbeddedChannel(new SnappyFrameEncoder()); channel = new EmbeddedChannel(new SnappyFrameEncoder());
} }

View File

@ -16,7 +16,7 @@
package io.netty.handler.codec.compression; package io.netty.handler.codec.compression;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Random; import java.util.Random;

View File

@ -18,18 +18,19 @@ package io.netty.handler.codec.compression;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static io.netty.handler.codec.compression.Snappy.*; 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; import java.nio.CharBuffer;
public class SnappyTest { public class SnappyTest {
private final Snappy snappy = new Snappy(); private final Snappy snappy = new Snappy();
@After @AfterEach
public void resetSnappy() { public void resetSnappy() {
snappy.reset(); snappy.reset();
} }
@ -48,7 +49,7 @@ public class SnappyTest {
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { ByteBuf expected = Unpooled.wrappedBuffer(new byte[] {
0x6e, 0x65, 0x74, 0x74, 0x79 0x6e, 0x65, 0x74, 0x74, 0x79
}); });
assertEquals("Literal was not decoded correctly", expected, out); assertEquals(expected, out, "Literal was not decoded correctly");
in.release(); in.release();
out.release(); out.release();
@ -71,15 +72,15 @@ public class SnappyTest {
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { ByteBuf expected = Unpooled.wrappedBuffer(new byte[] {
0x6e, 0x65, 0x74, 0x74, 0x79, 0x6e, 0x65, 0x74, 0x74, 0x79 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(); in.release();
out.release(); out.release();
expected.release(); expected.release();
} }
@Test(expected = DecompressionException.class) @Test
public void testDecodeCopyWithTinyOffset() throws Exception { public void testDecodeCopyWithTinyOffset() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x0b, // preamble length 0x0b, // preamble length
0x04 << 2, // literal tag + length 0x04 << 2, // literal tag + length
@ -89,15 +90,15 @@ public class SnappyTest {
}); });
ByteBuf out = Unpooled.buffer(10); ByteBuf out = Unpooled.buffer(10);
try { try {
snappy.decode(in, out); assertThrows(DecompressionException.class, () -> snappy.decode(in, out));
} finally { } finally {
in.release(); in.release();
out.release(); out.release();
} }
} }
@Test(expected = DecompressionException.class) @Test
public void testDecodeCopyWithOffsetBeforeChunk() throws Exception { public void testDecodeCopyWithOffsetBeforeChunk() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x0a, // preamble length 0x0a, // preamble length
0x04 << 2, // literal tag + length 0x04 << 2, // literal tag + length
@ -107,15 +108,15 @@ public class SnappyTest {
}); });
ByteBuf out = Unpooled.buffer(10); ByteBuf out = Unpooled.buffer(10);
try { try {
snappy.decode(in, out); assertThrows(DecompressionException.class, () -> snappy.decode(in, out));
} finally { } finally {
in.release(); in.release();
out.release(); out.release();
} }
} }
@Test(expected = DecompressionException.class) @Test
public void testDecodeWithOverlyLongPreamble() throws Exception { public void testDecodeWithOverlyLongPreamble() {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x80, -0x80, -0x80, -0x80, 0x7f, // preamble length -0x80, -0x80, -0x80, -0x80, 0x7f, // preamble length
0x04 << 2, // literal tag + length 0x04 << 2, // literal tag + length
@ -123,7 +124,7 @@ public class SnappyTest {
}); });
ByteBuf out = Unpooled.buffer(10); ByteBuf out = Unpooled.buffer(10);
try { try {
snappy.decode(in, out); assertThrows(DecompressionException.class, () -> snappy.decode(in, out));
} finally { } finally {
in.release(); in.release();
out.release(); out.release();
@ -143,7 +144,7 @@ public class SnappyTest {
0x04 << 2, // literal tag + length 0x04 << 2, // literal tag + length
0x6e, 0x65, 0x74, 0x74, 0x79 // "netty" 0x6e, 0x65, 0x74, 0x74, 0x79 // "netty"
}); });
assertEquals("Encoded literal was invalid", expected, out); assertEquals(expected, out, "Encoded literal was invalid");
in.release(); in.release();
out.release(); out.release();
@ -216,7 +217,7 @@ public class SnappyTest {
0x11, 0x4c, 0x11, 0x4c,
}); });
assertEquals("Encoded result was incorrect", expected, out); assertEquals(expected, out, "Encoded result was incorrect");
// Decode // Decode
ByteBuf outDecoded = Unpooled.buffer(); ByteBuf outDecoded = Unpooled.buffer();
@ -260,13 +261,13 @@ public class SnappyTest {
input.release(); input.release();
} }
@Test(expected = DecompressionException.class) @Test
public void testValidateChecksumFails() { public void testValidateChecksumFails() {
ByteBuf input = Unpooled.wrappedBuffer(new byte[] { ByteBuf input = Unpooled.wrappedBuffer(new byte[] {
'y', 't', 't', 'e', 'n' 'y', 't', 't', 'e', 'n'
}); });
try { try {
validateChecksum(maskChecksum(0xd6cb8b55), input); assertThrows(DecompressionException.class, () -> validateChecksum(maskChecksum(0xd6cb8b55), input));
} finally { } finally {
input.release(); input.release();
} }
@ -290,7 +291,7 @@ public class SnappyTest {
encodeLiteral(in, encoded, len); encodeLiteral(in, encoded, len);
byte tag = encoded.readByte(); byte tag = encoded.readByte();
decodeLiteral(tag, encoded, decoded); decodeLiteral(tag, encoded, decoded);
assertEquals("Encoded or decoded literal was incorrect", expected, decoded); assertEquals(expected, decoded, "Encoded or decoded literal was incorrect");
} finally { } finally {
in.release(); in.release();
encoded.release(); encoded.release();

View File

@ -23,9 +23,11 @@ import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil; 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 { public class DelimiterBasedFrameDecoderTest {

View File

@ -22,9 +22,12 @@ import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil; 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 { public class LengthFieldBasedFrameDecoderTest {
@Test @Test

View File

@ -20,19 +20,22 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.EncoderException; import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.LengthFieldPrepender; import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import java.nio.ByteOrder; 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 { public class LengthFieldPrependerTest {
private ByteBuf msg; private ByteBuf msg;
@Before @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
msg = copiedBuffer("A", CharsetUtil.ISO_8859_1); msg = copiedBuffer("A", CharsetUtil.ISO_8859_1);
} }
@ -107,7 +110,7 @@ public class LengthFieldPrependerTest {
buf = ch.readOutbound(); buf = ch.readOutbound();
assertSame(buf, msg); assertSame(buf, msg);
buf.release(); buf.release();
assertFalse("The channel must have been completely read", ch.finish()); assertFalse(ch.finish(), "The channel must have been completely read");
} }
} }

View File

@ -22,9 +22,13 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil; 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 { public class JsonObjectDecoderTest {
@Test @Test
@ -248,19 +252,18 @@ public class JsonObjectDecoderTest {
assertFalse(ch.finish()); assertFalse(ch.finish());
} }
@Test(expected = CorruptedFrameException.class) @Test
public void testNonJsonContent1() { public void testNonJsonContent1() {
EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder()); EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder());
try { 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 { } finally {
assertFalse(ch.finish()); assertFalse(ch.finish());
} }
fail();
} }
@Test(expected = CorruptedFrameException.class) @Test
public void testNonJsonContent2() { public void testNonJsonContent2() {
EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder()); EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder());
ch.writeInbound(Unpooled.copiedBuffer(" [1,2,3] ", CharsetUtil.UTF_8)); ch.writeInbound(Unpooled.copiedBuffer(" [1,2,3] ", CharsetUtil.UTF_8));
@ -270,24 +273,22 @@ public class JsonObjectDecoderTest {
res.release(); res.release();
try { 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 { } finally {
assertFalse(ch.finish()); assertFalse(ch.finish());
} }
fail();
} }
@Test (expected = TooLongFrameException.class) @Test
public void testMaxObjectLength() { public void testMaxObjectLength() {
EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder(6)); EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder(6));
try { 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 { } finally {
assertFalse(ch.finish()); assertFalse(ch.finish());
} }
fail();
} }
@Test @Test

View File

@ -23,15 +23,15 @@ import org.jboss.marshalling.Marshaller;
import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration; import org.jboss.marshalling.MarshallingConfiguration;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public abstract class AbstractCompatibleMarshallingDecoderTest extends AbstractMarshallingTest { public abstract class AbstractCompatibleMarshallingDecoderTest extends AbstractMarshallingTest {
@SuppressWarnings("RedundantStringConstructorCall") @SuppressWarnings("RedundantStringConstructorCall")

View File

@ -22,9 +22,11 @@ import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration; import org.jboss.marshalling.MarshallingConfiguration;
import org.jboss.marshalling.Unmarshaller; 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 { public abstract class AbstractCompatibleMarshallingEncoderTest extends AbstractMarshallingTest {

View File

@ -17,15 +17,16 @@ package io.netty.handler.codec.marshalling;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.Marshalling;
import org.junit.Assume; import org.junit.jupiter.api.BeforeAll;
import org.junit.BeforeClass;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
public abstract class AbstractMarshallingTest { public abstract class AbstractMarshallingTest {
static final String SERIAL_FACTORY = "serial"; static final String SERIAL_FACTORY = "serial";
static final String RIVER_FACTORY = "river"; static final String RIVER_FACTORY = "river";
@BeforeClass @BeforeAll
public static void checkSupported() throws Throwable { public static void checkSupported() throws Throwable {
Throwable error = null; Throwable error = null;
try { try {
@ -37,6 +38,6 @@ public abstract class AbstractMarshallingTest {
} }
error = cause; error = cause;
} }
Assume.assumeNoException(error); assumeTrue(error == null, error + " was not null");
} }
} }

View File

@ -22,7 +22,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.CodecException; import io.netty.handler.codec.CodecException;
import io.netty.handler.codec.TooLongFrameException; 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 { public class RiverMarshallingDecoderTest extends RiverCompatibleMarshallingDecoderTest {

View File

@ -22,7 +22,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.CodecException; import io.netty.handler.codec.CodecException;
import io.netty.handler.codec.TooLongFrameException; 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 { public class SerialMarshallingDecoderTest extends SerialCompatibleMarshallingDecoderTest {

View File

@ -17,20 +17,21 @@ package io.netty.handler.codec.protobuf;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import static org.hamcrest.core.Is.*; import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*; import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.MatcherAssert.assertThat; 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 { public class ProtobufVarint32FrameDecoderTest {
private EmbeddedChannel ch; private EmbeddedChannel ch;
@Before @BeforeEach
public void setUp() { public void setUp() {
ch = new EmbeddedChannel(new ProtobufVarint32FrameDecoder()); ch = new EmbeddedChannel(new ProtobufVarint32FrameDecoder());
} }

View File

@ -17,19 +17,20 @@ package io.netty.handler.codec.protobuf;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import static org.hamcrest.core.Is.*; import static org.hamcrest.core.Is.*;
import static org.hamcrest.MatcherAssert.assertThat; 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 { public class ProtobufVarint32LengthFieldPrependerTest {
private EmbeddedChannel ch; private EmbeddedChannel ch;
@Before @BeforeEach
public void setUp() { public void setUp() {
ch = new EmbeddedChannel(new ProtobufVarint32LengthFieldPrepender()); ch = new EmbeddedChannel(new ProtobufVarint32LengthFieldPrepender());
} }

View File

@ -19,8 +19,8 @@ import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.util.List; import java.util.List;
import org.junit.Assert; import org.junit.jupiter.api.Assertions;
import org.junit.Test; import org.junit.jupiter.api.Test;
public class CompactObjectSerializationTest { public class CompactObjectSerializationTest {
@ -31,6 +31,6 @@ public class CompactObjectSerializationTest {
CompactObjectOutputStream out = new CompactObjectOutputStream(pipeOut); CompactObjectOutputStream out = new CompactObjectOutputStream(pipeOut);
CompactObjectInputStream in = new CompactObjectInputStream(pipeIn, ClassResolvers.cacheDisabled(null)); CompactObjectInputStream in = new CompactObjectInputStream(pipeIn, ClassResolvers.cacheDisabled(null));
out.writeObject(List.class); out.writeObject(List.class);
Assert.assertSame(List.class, in.readObject()); Assertions.assertSame(List.class, in.readObject());
} }
} }

View File

@ -18,14 +18,14 @@ package io.netty.handler.codec.serialization;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.Serializable; import java.io.Serializable;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
public class CompatibleObjectEncoderTest { public class CompatibleObjectEncoderTest {
@Test @Test

View File

@ -18,11 +18,12 @@ package io.netty.handler.codec.string;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.Assert.assertTrue; 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 { public class LineEncoderTest {
@ -41,8 +42,8 @@ public class LineEncoderTest {
byte[] data = new byte[buf.readableBytes()]; byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data); buf.readBytes(data);
byte[] expected = (msg + lineSeparator.value()).getBytes(CharsetUtil.UTF_8); byte[] expected = (msg + lineSeparator.value()).getBytes(CharsetUtil.UTF_8);
Assert.assertArrayEquals(expected, data); assertArrayEquals(expected, data);
Assert.assertNull(channel.readOutbound()); assertNull(channel.readOutbound());
} finally { } finally {
buf.release(); buf.release();
assertFalse(channel.finish()); assertFalse(channel.finish());

View File

@ -18,8 +18,8 @@ package io.netty.handler.codec.string;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Assert; import org.junit.jupiter.api.Assertions;
import org.junit.Test; import org.junit.jupiter.api.Test;
public class StringEncoderTest { public class StringEncoderTest {
@ -27,13 +27,13 @@ public class StringEncoderTest {
public void testEncode() { public void testEncode() {
String msg = "Test"; String msg = "Test";
EmbeddedChannel channel = new EmbeddedChannel(new StringEncoder()); EmbeddedChannel channel = new EmbeddedChannel(new StringEncoder());
Assert.assertTrue(channel.writeOutbound(msg)); Assertions.assertTrue(channel.writeOutbound(msg));
Assert.assertTrue(channel.finish()); Assertions.assertTrue(channel.finish());
ByteBuf buf = channel.readOutbound(); ByteBuf buf = channel.readOutbound();
byte[] data = new byte[buf.readableBytes()]; byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data); buf.readBytes(data);
Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data); Assertions.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data);
Assert.assertNull(channel.readOutbound()); Assertions.assertNull(channel.readOutbound());
buf.release(); buf.release();
} }
} }

View File

@ -22,7 +22,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -38,6 +38,7 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class XmlFrameDecoderTest { public class XmlFrameDecoderTest {
@ -50,35 +51,38 @@ public class XmlFrameDecoderTest {
); );
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testConstructorWithIllegalArgs01() { public void testConstructorWithIllegalArgs01() {
new XmlFrameDecoder(0); assertThrows(IllegalArgumentException.class, () -> new XmlFrameDecoder(0));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testConstructorWithIllegalArgs02() { public void testConstructorWithIllegalArgs02() {
new XmlFrameDecoder(-23); assertThrows(IllegalArgumentException.class, () -> new XmlFrameDecoder(-23));
} }
@Test(expected = TooLongFrameException.class) @Test
public void testDecodeWithFrameExceedingMaxLength() { public void testDecodeWithFrameExceedingMaxLength() {
XmlFrameDecoder decoder = new XmlFrameDecoder(3); XmlFrameDecoder decoder = new XmlFrameDecoder(3);
EmbeddedChannel ch = new EmbeddedChannel(decoder); EmbeddedChannel ch = new EmbeddedChannel(decoder);
ch.writeInbound(Unpooled.copiedBuffer("<v/>", CharsetUtil.UTF_8)); assertThrows(TooLongFrameException.class,
() -> ch.writeInbound(Unpooled.copiedBuffer("<v/>", CharsetUtil.UTF_8)));
} }
@Test(expected = CorruptedFrameException.class) @Test
public void testDecodeWithInvalidInput() { public void testDecodeWithInvalidInput() {
XmlFrameDecoder decoder = new XmlFrameDecoder(1048576); XmlFrameDecoder decoder = new XmlFrameDecoder(1048576);
EmbeddedChannel ch = new EmbeddedChannel(decoder); 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() { public void testDecodeWithInvalidContentBeforeXml() {
XmlFrameDecoder decoder = new XmlFrameDecoder(1048576); XmlFrameDecoder decoder = new XmlFrameDecoder(1048576);
EmbeddedChannel ch = new EmbeddedChannel(decoder); EmbeddedChannel ch = new EmbeddedChannel(decoder);
ch.writeInbound(Unpooled.copiedBuffer("invalid XML<foo/>", CharsetUtil.UTF_8)); assertThrows(CorruptedFrameException.class,
() -> ch.writeInbound(Unpooled.copiedBuffer("invalid XML<foo/>", CharsetUtil.UTF_8)));
} }
@Test @Test