Migrate codec-socks tests to JUnit 5 (#11314)

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  handler-proxy  tests

Result:

Related to https://github.com/netty/netty/issues/10757
This commit is contained in:
Riley Park 2021-05-26 01:39:13 -07:00 committed by GitHub
parent 7a5d9db0ec
commit aedccda965
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 94 additions and 60 deletions

View File

@ -18,9 +18,11 @@ package io.netty.handler.codec.socks;
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 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;
public class SocksAuthRequestDecoderTest { public class SocksAuthRequestDecoderTest {

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socks; package io.netty.handler.codec.socks;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksAuthRequestTest { public class SocksAuthRequestTest {
@Test @Test

View File

@ -18,9 +18,10 @@ package io.netty.handler.codec.socks;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
public class SocksAuthResponseDecoderTest { public class SocksAuthResponseDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksAuthResponseDecoderTest.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksAuthResponseDecoderTest.class);

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socks; package io.netty.handler.codec.socks;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksAuthResponseTest { public class SocksAuthResponseTest {
@Test @Test

View File

@ -19,11 +19,14 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.SocketUtils; import io.netty.util.internal.SocketUtils;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.net.UnknownHostException; import java.net.UnknownHostException;
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.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksCmdRequestDecoderTest { public class SocksCmdRequestDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksCmdRequestDecoderTest.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksCmdRequestDecoderTest.class);

View File

@ -18,12 +18,14 @@ package io.netty.handler.codec.socks;
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.Test; import org.junit.jupiter.api.Test;
import java.net.IDN; import java.net.IDN;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksCmdRequestTest { public class SocksCmdRequestTest {
@Test @Test

View File

@ -18,9 +18,12 @@ package io.netty.handler.codec.socks;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
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 SocksCmdResponseDecoderTest { public class SocksCmdResponseDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksCmdResponseDecoderTest.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksCmdResponseDecoderTest.class);
@ -60,9 +63,11 @@ public class SocksCmdResponseDecoderTest {
/** /**
* Verifies that invalid bound host will fail with IllegalArgumentException during encoding. * Verifies that invalid bound host will fail with IllegalArgumentException during encoding.
*/ */
@Test(expected = IllegalArgumentException.class) @Test
public void testInvalidAddress() { public void testInvalidAddress() {
testSocksCmdResponseDecoderWithDifferentParams(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "1", 80); assertThrows(IllegalArgumentException.class,
() -> testSocksCmdResponseDecoderWithDifferentParams(
SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "1", 80));
} }
/** /**

View File

@ -18,12 +18,17 @@ package io.netty.handler.codec.socks;
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.Test; import org.junit.jupiter.api.Test;
import java.net.IDN; import java.net.IDN;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksCmdResponseTest { public class SocksCmdResponseTest {
@Test @Test
@ -161,16 +166,17 @@ public class SocksCmdResponseTest {
/** /**
* Verifies that Response cannot be constructed with invalid IP. * Verifies that Response cannot be constructed with invalid IP.
*/ */
@Test(expected = IllegalArgumentException.class) @Test
public void testInvalidBoundAddress() { public void testInvalidBoundAddress() {
new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "127.0.0", 1000); assertThrows(IllegalArgumentException.class,
() -> new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "127.0.0", 1000));
} }
private static void assertByteBufEquals(byte[] expected, ByteBuf actual) { private static void assertByteBufEquals(byte[] expected, ByteBuf actual) {
byte[] actualBytes = new byte[actual.readableBytes()]; byte[] actualBytes = new byte[actual.readableBytes()];
actual.readBytes(actualBytes); actual.readBytes(actualBytes);
assertEquals("Generated response has incorrect length", expected.length, actualBytes.length); assertEquals(expected.length, actualBytes.length, "Generated response has incorrect length");
assertArrayEquals("Generated response differs from expected", expected, actualBytes); assertArrayEquals(expected, actualBytes, "Generated response differs from expected");
} }
@Test @Test

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socks; package io.netty.handler.codec.socks;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksInitRequestTest { public class SocksInitRequestTest {
@Test @Test

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socks; package io.netty.handler.codec.socks;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksInitResponseTest { public class SocksInitResponseTest {
@Test @Test

View File

@ -16,11 +16,12 @@
package io.netty.handler.codec.socksx.v4; package io.netty.handler.codec.socksx.v4;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class Socks4ClientDecoderTest { public class Socks4ClientDecoderTest {
private static final Logger logger = LoggerFactory.getLogger(Socks4ClientDecoderTest.class); private static final Logger logger = LoggerFactory.getLogger(Socks4ClientDecoderTest.class);

View File

@ -18,11 +18,13 @@ package io.netty.handler.codec.socksx.v4;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
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.assertSame;
public class Socks4ServerDecoderTest { public class Socks4ServerDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Socks4ServerDecoderTest.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(Socks4ServerDecoderTest.class);

View File

@ -15,9 +15,9 @@
*/ */
package io.netty.handler.codec.socksx.v5; package io.netty.handler.codec.socksx.v5;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.*;
public class DefaultSocks5CommandRequestTest { public class DefaultSocks5CommandRequestTest {
@Test @Test

View File

@ -16,9 +16,13 @@
package io.netty.handler.codec.socksx.v5; package io.netty.handler.codec.socksx.v5;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultSocks5CommandResponseTest { public class DefaultSocks5CommandResponseTest {
@Test @Test
@ -113,17 +117,17 @@ public class DefaultSocks5CommandResponseTest {
/** /**
* Verifies that Response cannot be constructed with invalid IP. * Verifies that Response cannot be constructed with invalid IP.
*/ */
@Test(expected = IllegalArgumentException.class) @Test
public void testInvalidBoundAddress() { public void testInvalidBoundAddress() {
new DefaultSocks5CommandResponse( assertThrows(IllegalArgumentException.class, () -> new DefaultSocks5CommandResponse(
Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4, "127.0.0", 1000); Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4, "127.0.0", 1000));
} }
private static void assertByteBufEquals(byte[] expected, ByteBuf actual) { private static void assertByteBufEquals(byte[] expected, ByteBuf actual) {
byte[] actualBytes = new byte[actual.readableBytes()]; byte[] actualBytes = new byte[actual.readableBytes()];
actual.readBytes(actualBytes); actual.readBytes(actualBytes);
assertEquals("Generated response has incorrect length", expected.length, actualBytes.length); assertEquals(expected.length, actualBytes.length, "Generated response has incorrect length");
assertArrayEquals("Generated response differs from expected", expected, actualBytes); assertArrayEquals(expected, actualBytes, "Generated response differs from expected");
} }
@Test @Test

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socksx.v5; package io.netty.handler.codec.socksx.v5;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultSocks5InitialRequestTest { public class DefaultSocks5InitialRequestTest {
@Test @Test

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socksx.v5; package io.netty.handler.codec.socksx.v5;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultSocks5InitialResponseTest { public class DefaultSocks5InitialResponseTest {
@Test @Test

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socksx.v5; package io.netty.handler.codec.socksx.v5;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultSocks5PasswordAuthRequestTest { public class DefaultSocks5PasswordAuthRequestTest {
@Test @Test

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.handler.codec.socksx.v5; package io.netty.handler.codec.socksx.v5;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultSocks5PasswordAuthResponseTest { public class DefaultSocks5PasswordAuthResponseTest {
@Test @Test

View File

@ -20,13 +20,15 @@ import io.netty.util.NetUtil;
import io.netty.util.internal.SocketUtils; import io.netty.util.internal.SocketUtils;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.net.IDN; import java.net.IDN;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Arrays; import java.util.Arrays;
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.assertSame;
public class Socks5CommandRequestDecoderTest { public class Socks5CommandRequestDecoderTest {
private static final InternalLogger logger = private static final InternalLogger logger =

View File

@ -18,12 +18,13 @@ package io.netty.handler.codec.socksx.v5;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class Socks5CommandResponseDecoderTest { public class Socks5CommandResponseDecoderTest {
@ -75,9 +76,10 @@ public class Socks5CommandResponseDecoderTest {
/** /**
* Verifies that invalid bound host will fail with IllegalArgumentException during encoding. * Verifies that invalid bound host will fail with IllegalArgumentException during encoding.
*/ */
@Test(expected = IllegalArgumentException.class) @Test
public void testInvalidAddress() { public void testInvalidAddress() {
test(Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4, "1", 80); assertThrows(IllegalArgumentException.class,
() -> test(Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4, "1", 80));
} }
/** /**

View File

@ -18,9 +18,11 @@ package io.netty.handler.codec.socksx.v5;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.DecoderResult;
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.assertTrue;
public class Socks5InitialRequestDecoderTest { public class Socks5InitialRequestDecoderTest {
@Test @Test

View File

@ -16,9 +16,10 @@
package io.netty.handler.codec.socksx.v5; package io.netty.handler.codec.socksx.v5;
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;
public class Socks5PasswordAuthRequestDecoderTest { public class Socks5PasswordAuthRequestDecoderTest {

View File

@ -18,9 +18,10 @@ package io.netty.handler.codec.socksx.v5;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
public class Socks5PasswordAuthResponseDecoderTest { public class Socks5PasswordAuthResponseDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance( private static final InternalLogger logger = InternalLoggerFactory.getInstance(