Migrate codec-dns tests to JUnit 5 (#11307)

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 codec-dns tests

Result:

Related to https://github.com/netty/netty/issues/10757
This commit is contained in:
Riley Park 2021-05-26 01:09:21 -07:00 committed by GitHub
parent f2f19c7fba
commit 6963638263
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 39 deletions

View File

@ -15,8 +15,10 @@
*/
package io.netty.handler.codec.dns;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class AbstractDnsRecordTest {
@ -24,39 +26,39 @@ public class AbstractDnsRecordTest {
public void testValidDomainName() {
String name = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
Assert.assertEquals(name + '.', record.name());
assertEquals(name + '.', record.name());
}
@Test
public void testValidDomainNameUmlaut() {
String name = "ä";
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
Assert.assertEquals("xn--4ca.", record.name());
assertEquals("xn--4ca.", record.name());
}
@Test
public void testValidDomainNameTrailingDot() {
String name = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
Assert.assertEquals(name, record.name());
assertEquals(name, record.name());
}
@Test
public void testValidDomainNameUmlautTrailingDot() {
String name = "ä.";
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
Assert.assertEquals("xn--4ca.", record.name());
assertEquals("xn--4ca.", record.name());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testValidDomainNameLength() {
String name = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
assertThrows(IllegalArgumentException.class, () -> new AbstractDnsRecord(name, DnsRecordType.A, 0) { });
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testValidDomainNameUmlautLength() {
String name = "äaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
assertThrows(IllegalArgumentException.class, () -> new AbstractDnsRecord(name, DnsRecordType.A, 0) { });
}
}

View File

@ -18,9 +18,10 @@ package io.netty.handler.codec.dns;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
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 DefaultDnsRecordDecoderTest {
@ -145,13 +146,13 @@ public class DefaultDnsRecordDecoderTest {
try {
cnameRecord = (DefaultDnsRawRecord) decoder.decodeRecord(
"netty.github.io", DnsRecordType.CNAME, DnsRecord.CLASS_IN, 60, buffer, 10, 2);
assertEquals("The rdata of CNAME-type record should be decompressed in advance",
0, ByteBufUtil.compare(buffer.duplicate().setIndex(0, 10), cnameRecord.content()));
assertEquals(0, ByteBufUtil.compare(buffer.duplicate().setIndex(0, 10), cnameRecord.content()),
"The rdata of CNAME-type record should be decompressed in advance");
assertEquals("netty.io.", DnsCodecUtil.decodeDomainName(cnameRecord.content()));
nsRecord = (DefaultDnsRawRecord) decoder.decodeRecord(
"netty.github.io", DnsRecordType.NS, DnsRecord.CLASS_IN, 60, buffer, 10, 2);
assertEquals("The rdata of NS-type record should be decompressed in advance",
0, ByteBufUtil.compare(buffer.duplicate().setIndex(0, 10), nsRecord.content()));
assertEquals(0, ByteBufUtil.compare(buffer.duplicate().setIndex(0, 10), nsRecord.content()),
"The rdata of NS-type record should be decompressed in advance");
assertEquals("netty.io.", DnsCodecUtil.decodeDomainName(nsRecord.content()));
} finally {
buffer.release();

View File

@ -20,12 +20,12 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.util.internal.SocketUtils;
import io.netty.util.internal.StringUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DefaultDnsRecordEncoderTest {

View File

@ -19,8 +19,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.internal.SocketUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress;
import java.util.ArrayList;
@ -28,6 +27,8 @@ import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DnsQueryTest {
@ -61,9 +62,9 @@ public class DnsQueryTest {
embedder.writeOutbound(query);
DatagramPacket packet = embedder.readOutbound();
Assert.assertTrue(packet.content().isReadable());
assertTrue(packet.content().isReadable());
packet.release();
Assert.assertNull(embedder.readOutbound());
assertNull(embedder.readOutbound());
}
}
}

View File

@ -15,7 +15,7 @@
*/
package io.netty.handler.codec.dns;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
@ -23,7 +23,10 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
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.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
public class DnsRecordTypeTest {
@ -40,8 +43,8 @@ public class DnsRecordTypeTest {
@Test
public void testSanity() throws Exception {
assertEquals("More than one type has the same int value",
allTypes().size(), new HashSet<>(allTypes()).size());
assertEquals(allTypes().size(), new HashSet<>(allTypes()).size(),
"More than one type has the same int value");
}
/**
@ -77,7 +80,7 @@ public class DnsRecordTypeTest {
DnsRecordType found = DnsRecordType.valueOf(t.intValue());
assertSame(t, found);
found = DnsRecordType.valueOf(t.name());
assertSame(t.name(), t, found);
assertSame(t, found, t.name());
}
}
}

View File

@ -21,16 +21,14 @@ import io.netty.channel.AddressedEnvelope;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.CorruptedFrameException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class DnsResponseTest {
@ -95,16 +93,13 @@ public class DnsResponseTest {
assertFalse(embedder.finish());
}
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void readMalformedResponseTest() throws Exception {
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
exception.expect(CorruptedFrameException.class);
try {
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
assertThrows(CorruptedFrameException.class,
() -> embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0))));
} finally {
assertFalse(embedder.finish());
}
@ -114,9 +109,9 @@ public class DnsResponseTest {
public void readIncompleteResponseTest() {
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
ByteBuf packet = embedder.alloc().buffer(512);
exception.expect(CorruptedFrameException.class);
try {
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
assertThrows(CorruptedFrameException.class,
() -> embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0))));
} finally {
assertFalse(embedder.finish());
}