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 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 Norman Maurer
parent a63a4d99e0
commit 22faf32c25
23 changed files with 115 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.Unpooled;
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 {

View File

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

View File

@ -18,9 +18,10 @@ package io.netty.handler.codec.socks;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger;
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 {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksAuthResponseDecoderTest.class);

View File

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

View File

@ -19,11 +19,14 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.SocketUtils;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test;
import org.junit.jupiter.api.Test;
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 {
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.Unpooled;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.IDN;
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 {
@Test

View File

@ -18,9 +18,13 @@ package io.netty.handler.codec.socks;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SocksCmdResponseDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksCmdResponseDecoderTest.class);
@ -60,9 +64,15 @@ public class SocksCmdResponseDecoderTest {
/**
* Verifies that invalid bound host will fail with IllegalArgumentException during encoding.
*/
@Test(expected = IllegalArgumentException.class)
@Test
public void testInvalidAddress() {
testSocksCmdResponseDecoderWithDifferentParams(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "1", 80);
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
testSocksCmdResponseDecoderWithDifferentParams(
SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "1", 80);
}
});
}
/**

View File

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

View File

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

View File

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

View File

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

View File

@ -15,9 +15,9 @@
*/
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 {
@Test

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,9 +18,10 @@ package io.netty.handler.codec.socksx.v5;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger;
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 {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(