Skip to content

Commit

Permalink
Migrate codec-socks tests to JUnit 5 (netty#11314)
Browse files Browse the repository at this point in the history
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 netty#10757
  • Loading branch information
kashike authored and normanmaurer committed May 26, 2021
1 parent a63a4d9 commit 22faf32
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
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);
Expand Down Expand Up @@ -60,9 +64,15 @@ public void testSocksCmdResponseDecoder() {
/**
* 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);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
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
Expand Down Expand Up @@ -161,16 +167,21 @@ public void testIDNEncodeToAsciiForDomain() {
/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,17 +118,22 @@ public void testEmptyBoundAddress() {
/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
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 {

Expand Down Expand Up @@ -75,9 +77,14 @@ public void testSocksCmdResponseDecoder() {
/**
* 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);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Loading

0 comments on commit 22faf32

Please sign in to comment.