diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultAuthoritativeDnsServerCacheTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultAuthoritativeDnsServerCacheTest.java index dcba7906f467..3bc95342d1a9 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultAuthoritativeDnsServerCacheTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultAuthoritativeDnsServerCacheTest.java @@ -20,7 +20,7 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.NetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -28,7 +28,8 @@ import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; public class DefaultAuthoritativeDnsServerCacheTest { diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java index 90fa58bf6784..ea00af461693 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java @@ -21,7 +21,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.NetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.InetAddress; import java.util.List; @@ -29,10 +29,10 @@ import java.util.concurrent.TimeUnit; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; public class DefaultDnsCacheTest { diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCnameCacheTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCnameCacheTest.java index 3df4a0ba6e6a..fe495bf0e2f0 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCnameCacheTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCnameCacheTest.java @@ -18,12 +18,15 @@ import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; -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 class DefaultDnsCnameCacheTest { diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsAddressResolverGroupTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsAddressResolverGroupTest.java index 560acb668a8a..758bfcc7f77b 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsAddressResolverGroupTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsAddressResolverGroupTest.java @@ -23,14 +23,14 @@ import io.netty.util.concurrent.Future; import io.netty.util.concurrent.FutureListener; import io.netty.util.concurrent.Promise; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.SocketAddress; import java.nio.channels.UnsupportedAddressTypeException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; public class DnsAddressResolverGroupTest { @Test diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverClientSubnetTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverClientSubnetTest.java index 21fcbf623fe1..f707e6fcbb7d 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverClientSubnetTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverClientSubnetTest.java @@ -22,8 +22,8 @@ import io.netty.handler.codec.dns.DnsRecord; import io.netty.util.internal.SocketUtils; import io.netty.util.concurrent.Future; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import java.net.InetAddress; import java.util.Collections; @@ -34,7 +34,7 @@ public class DnsNameResolverClientSubnetTest { // See https://www.gsic.uva.es/~jnisigl/dig-edns-client-subnet.html // Ignore as this needs to query real DNS servers. - @Ignore + @Disabled @Test public void testSubnetQuery() throws Exception { EventLoopGroup group = new NioEventLoopGroup(1); diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java index 6c926d7103d0..b21130801667 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java @@ -65,11 +65,9 @@ import org.apache.directory.server.dns.store.RecordStore; import org.apache.mina.core.buffer.IoBuffer; import org.hamcrest.Matchers; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.io.InputStream; @@ -105,6 +103,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.function.Executable; import static io.netty.handler.codec.dns.DnsRecordType.A; import static io.netty.handler.codec.dns.DnsRecordType.AAAA; @@ -120,7 +120,16 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; -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.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class DnsNameResolverTest { @@ -337,9 +346,6 @@ public class DnsNameResolverTest { private static final TestDnsServer dnsServer = new TestDnsServer(DOMAINS_ALL); private static final EventLoopGroup group = new NioEventLoopGroup(1); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private static DnsNameResolverBuilder newResolver(boolean decodeToUnicode) { return newResolver(decodeToUnicode, null); } @@ -379,12 +385,12 @@ private static DnsNameResolverBuilder newNonCachedResolver(ResolvedAddressTypes .resolvedAddressTypes(resolvedAddressTypes); } - @BeforeClass + @BeforeAll public static void init() throws Exception { dnsServer.start(); } - @AfterClass + @AfterAll public static void destroy() { dnsServer.stop(); group.shutdownGracefully(); @@ -459,11 +465,11 @@ public DnsServerAddressStream nameServerAddressStream(String hostname) { continue; } if (overriddenHostnames.contains(resolvedEntry.getKey())) { - assertEquals("failed to resolve " + resolvedEntry.getKey(), - overriddenIP, resolvedEntry.getValue().getHostAddress()); + assertEquals(overriddenIP, resolvedEntry.getValue().getHostAddress(), + "failed to resolve " + resolvedEntry.getKey()); } else { - assertNotEquals("failed to resolve " + resolvedEntry.getKey(), - overriddenIP, resolvedEntry.getValue().getHostAddress()); + assertNotEquals(overriddenIP, resolvedEntry.getValue().getHostAddress(), + "failed to resolve " + resolvedEntry.getKey()); } } } finally { @@ -522,12 +528,14 @@ public void testNonCachedResolve() throws Exception { } } - @Test(timeout = DEFAULT_TEST_TIMEOUT_MS) + @Test + @Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS) public void testNonCachedResolveEmptyHostName() throws Exception { testNonCachedResolveEmptyHostName(""); } - @Test(timeout = DEFAULT_TEST_TIMEOUT_MS) + @Test + @Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS) public void testNonCachedResolveNullHostName() throws Exception { testNonCachedResolveEmptyHostName(null); } @@ -542,12 +550,14 @@ private static void testNonCachedResolveEmptyHostName(String inetHost) throws Ex } } - @Test(timeout = DEFAULT_TEST_TIMEOUT_MS) + @Test + @Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS) public void testNonCachedResolveAllEmptyHostName() throws Exception { testNonCachedResolveAllEmptyHostName(""); } - @Test(timeout = DEFAULT_TEST_TIMEOUT_MS) + @Test + @Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS) public void testNonCachedResolveAllNullHostName() throws Exception { testNonCachedResolveAllEmptyHostName(null); } @@ -717,8 +727,8 @@ private static UnknownHostException resolveNonExistentDomain(DnsNameResolver res if (observer != null) { Object o = observer.events.poll(); if (o instanceof QueryCancelledEvent) { - assertTrue("unexpected type: " + observer.question, - observer.question.type() == CNAME || observer.question.type() == AAAA); + assertTrue(observer.question.type() == CNAME || observer.question.type() == AAAA, + "unexpected type: " + observer.question); } else if (o instanceof QueryWrittenEvent) { QueryFailedEvent failedEvent = (QueryFailedEvent) observer.events.poll(); } else if (!(o instanceof QueryFailedEvent)) { @@ -1149,12 +1159,14 @@ private static void testResolveUnicode(boolean decode) { } } - @Test(timeout = DEFAULT_TEST_TIMEOUT_MS) + @Test + @Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS) public void secondDnsServerShouldBeUsedBeforeCNAMEFirstServerNotStarted() throws IOException { secondDnsServerShouldBeUsedBeforeCNAME(false); } - @Test(timeout = DEFAULT_TEST_TIMEOUT_MS) + @Test + @Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS) public void secondDnsServerShouldBeUsedBeforeCNAMEFirstServerFailResolve() throws IOException { secondDnsServerShouldBeUsedBeforeCNAME(true); } @@ -1213,7 +1225,8 @@ private static void secondDnsServerShouldBeUsedBeforeCNAME(boolean startDnsServe } } - @Test(timeout = DEFAULT_TEST_TIMEOUT_MS) + @Test + @Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS) public void aAndAAAAQueryShouldTryFirstDnsServerBeforeSecond() throws IOException { final String knownHostName = "netty.io"; final TestDnsServer dnsServer1 = new TestDnsServer(Collections.singleton("notnetty.com")); @@ -2163,7 +2176,8 @@ protected DnsMessage filterMessage(DnsMessage message) { } } - @Test(timeout = 3000) + @Test + @Timeout(value = 3000, unit = TimeUnit.MILLISECONDS) public void testTimeoutNotCached() { DnsCache cache = new DnsCache() { @Override @@ -2277,7 +2291,6 @@ public Set getRecords(QuestionRecord question) { @Test public void testFollowCNAMELoop() throws IOException { - expectedException.expect(UnknownHostException.class); TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() { @Override @@ -2312,7 +2325,13 @@ public Set getRecords(QuestionRecord question) { .nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress())); resolver = builder.build(); - resolver.resolveAll("somehost.netty.io").syncUninterruptibly().getNow(); + final DnsNameResolver finalResolver = resolver; + assertThrows(UnknownHostException.class, new Executable() { + @Override + public void execute() throws Throwable { + finalResolver.resolveAll("somehost.netty.io").syncUninterruptibly().getNow(); + } + }); } finally { dnsServer2.stop(); if (resolver != null) { @@ -2322,8 +2341,7 @@ public Set getRecords(QuestionRecord question) { } @Test - public void testCNAMELoopInCache() { - expectedException.expect(UnknownHostException.class); + public void testCNAMELoopInCache() throws Throwable { DnsNameResolver resolver = null; try { DnsNameResolverBuilder builder = newResolver() @@ -2334,12 +2352,18 @@ public void testCNAMELoopInCache() { resolver = builder.build(); // Add a CNAME loop into the cache - String name = "somehost.netty.io."; + final String name = "somehost.netty.io."; String name2 = "cname.netty.io."; resolver.cnameCache().cache(name, name2, Long.MAX_VALUE, resolver.executor()); resolver.cnameCache().cache(name2, name, Long.MAX_VALUE, resolver.executor()); - resolver.resolve(name).syncUninterruptibly().getNow(); + final DnsNameResolver finalResolver = resolver; + assertThrows(UnknownHostException.class, new Executable() { + @Override + public void execute() throws Throwable { + finalResolver.resolve(name).syncUninterruptibly().getNow(); + } + }); } finally { if (resolver != null) { resolver.close(); @@ -2349,14 +2373,22 @@ public void testCNAMELoopInCache() { @Test public void testSearchDomainQueryFailureForSingleAddressTypeCompletes() { - expectedException.expect(UnknownHostException.class); - testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_ONLY); + assertThrows(UnknownHostException.class, new Executable() { + @Override + public void execute() { + testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_ONLY); + } + }); } @Test public void testSearchDomainQueryFailureForMultipleAddressTypeCompletes() { - expectedException.expect(UnknownHostException.class); - testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_PREFERRED); + assertThrows(UnknownHostException.class, new Executable() { + @Override + public void execute() throws Throwable { + testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_PREFERRED); + } + }); } private void testSearchDomainQueryFailureCompletes(ResolvedAddressTypes types) { @@ -2371,7 +2403,8 @@ private void testSearchDomainQueryFailureCompletes(ResolvedAddressTypes types) { } } - @Test(timeout = 2000L) + @Test + @Timeout(value = 2000, unit = TimeUnit.MILLISECONDS) public void testCachesClearedOnClose() throws Exception { final CountDownLatch resolveLatch = new CountDownLatch(1); final CountDownLatch authoritativeLatch = new CountDownLatch(1); @@ -2550,13 +2583,13 @@ public Set getRecords(QuestionRecord question) { .cnameCache(new DnsCnameCache() { @Override public String get(String hostname) { - assertTrue(hostname, hostname.endsWith(".")); + assertTrue(hostname.endsWith("."), hostname); return cache.get(hostname); } @Override public void cache(String hostname, String cname, long originalTtl, EventLoop loop) { - assertTrue(hostname, hostname.endsWith(".")); + assertTrue(hostname.endsWith("."), hostname); cache.put(hostname, cname); } @@ -2824,7 +2857,8 @@ public void testDropAAAA() throws IOException { } } - @Test(timeout = 2000) + @Test + @Timeout(value = 2000, unit = TimeUnit.MILLISECONDS) public void testDropAAAAResolveFast() throws IOException { String host = "somehost.netty.io"; TestDnsServer dnsServer2 = new TestDnsServer(Collections.singleton(host)); @@ -2849,7 +2883,8 @@ public void testDropAAAAResolveFast() throws IOException { } } - @Test(timeout = 2000) + @Test + @Timeout(value = 2000, unit = TimeUnit.MILLISECONDS) public void testDropAAAAResolveAllFast() throws IOException { final String host = "somehost.netty.io"; TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() { @@ -2895,17 +2930,20 @@ public Set getRecords(QuestionRecord question) throws DnsExcepti } } - @Test(timeout = 5000) + @Test + @Timeout(value = 5000, unit = TimeUnit.MILLISECONDS) public void testTruncatedWithoutTcpFallback() throws IOException { testTruncated0(false, false); } - @Test(timeout = 5000) + @Test + @Timeout(value = 5000, unit = TimeUnit.MILLISECONDS) public void testTruncatedWithTcpFallback() throws IOException { testTruncated0(true, false); } - @Test(timeout = 5000) + @Test + @Timeout(value = 5000, unit = TimeUnit.MILLISECONDS) public void testTruncatedWithTcpFallbackBecauseOfMtu() throws IOException { testTruncated0(true, true); } @@ -3248,7 +3286,8 @@ public DnsServerAddressStream nameServerAddressStream(String hostname) { } } - @Test(timeout = 2000) + @Test + @Timeout(value = 2000, unit = TimeUnit.MILLISECONDS) public void testSrvWithCnameNotCached() throws Exception { final AtomicBoolean alias = new AtomicBoolean(); TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() { diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsResolveContextTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsResolveContextTest.java index 32d7522a8f4f..f6b19f9ed766 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsResolveContextTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsResolveContextTest.java @@ -16,11 +16,11 @@ package io.netty.resolver.dns; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.UnknownHostException; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; public class DnsResolveContextTest { diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressStreamProvidersTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressStreamProvidersTest.java index 678139f7e224..f2bfcc104f03 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressStreamProvidersTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressStreamProvidersTest.java @@ -12,16 +12,18 @@ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. - */package io.netty.resolver.dns; + */ +package io.netty.resolver.dns; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertSame; public class DnsServerAddressStreamProvidersTest { @Test public void testUseCorrectProvider() { - Assert.assertSame(DnsServerAddressStreamProviders.unixDefault(), + assertSame(DnsServerAddressStreamProviders.unixDefault(), DnsServerAddressStreamProviders.platformDefault()); } } diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressesTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressesTest.java index 03171e3fb4f3..8b19f578fca4 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressesTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsServerAddressesTest.java @@ -17,7 +17,7 @@ package io.netty.resolver.dns; import io.netty.util.NetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.InetSocketAddress; import java.util.Collections; diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/NameServerComparatorTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/NameServerComparatorTest.java index bd37ee0b8f92..fdb10beb6630 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/NameServerComparatorTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/NameServerComparatorTest.java @@ -15,8 +15,8 @@ */ package io.netty.resolver.dns; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import java.net.Inet4Address; import java.net.Inet6Address; @@ -28,8 +28,7 @@ import java.util.Collections; import java.util.List; -import static org.junit.Assert.*; - +import static org.junit.jupiter.api.Assertions.assertEquals; public class NameServerComparatorTest { @@ -44,7 +43,7 @@ public class NameServerComparatorTest { private static InetSocketAddress UNRESOLVED2; private static InetSocketAddress UNRESOLVED3; - @BeforeClass + @BeforeAll public static void before() throws UnknownHostException { IPV4ADDRESS1 = new InetSocketAddress(InetAddress.getByAddress("ns1", new byte[] { 10, 0, 0, 1 }), 53); IPV4ADDRESS2 = new InetSocketAddress(InetAddress.getByAddress("ns2", new byte[] { 10, 0, 0, 2 }), 53); diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/PreferredAddressTypeComparatorTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/PreferredAddressTypeComparatorTest.java index 6ceff7aed88e..427855306d85 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/PreferredAddressTypeComparatorTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/PreferredAddressTypeComparatorTest.java @@ -16,8 +16,7 @@ package io.netty.resolver.dns; import io.netty.channel.socket.InternetProtocolFamily; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.InetAddress; import java.net.UnknownHostException; @@ -26,6 +25,8 @@ import java.util.Collections; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class PreferredAddressTypeComparatorTest { @Test @@ -44,7 +45,7 @@ public void testIpv4() throws UnknownHostException { ipv6Address2, ipv4Address3, ipv6Address3); Collections.sort(addressList, ipv4); - Assert.assertEquals(Arrays.asList(ipv4Address1, ipv4Address2, ipv4Address3, ipv6Address1, + assertEquals(Arrays.asList(ipv4Address1, ipv4Address2, ipv4Address3, ipv6Address1, ipv6Address2, ipv6Address3), addressList); } @@ -64,7 +65,7 @@ public void testIpv6() throws UnknownHostException { ipv6Address2, ipv4Address3, ipv6Address3); Collections.sort(addressList, ipv4); - Assert.assertEquals(Arrays.asList(ipv6Address1, + assertEquals(Arrays.asList(ipv6Address1, ipv6Address2, ipv6Address3, ipv4Address1, ipv4Address2, ipv4Address3), addressList); } } diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java index 36e6400dbda6..98b0a689ec65 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java @@ -19,9 +19,9 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.util.concurrent.Future; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.net.InetAddress; import java.net.UnknownHostException; @@ -37,9 +37,9 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.not; import static org.hamcrest.core.StringContains.containsString; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +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 SearchDomainTest { @@ -56,12 +56,12 @@ private DnsNameResolverBuilder newResolver() { private EventLoopGroup group; private DnsNameResolver resolver; - @Before + @BeforeEach public void before() { group = new NioEventLoopGroup(1); } - @After + @AfterEach public void destroy() { if (dnsServer != null) { dnsServer.stop(); diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java index 10bee74c5a25..41411baf751b 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/UnixResolverDnsServerAddressStreamProviderTest.java @@ -16,29 +16,26 @@ package io.netty.resolver.dns; import io.netty.util.CharsetUtil; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.net.InetSocketAddress; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.UUID; import static io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.parseEtcResolverOptions; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class UnixResolverDnsServerAddressStreamProviderTest { - @Rule - public final TemporaryFolder folder = new TemporaryFolder(); - @Test - public void defaultLookupShouldReturnResultsIfOnlySingleFileSpecified() throws Exception { - File f = buildFile("domain linecorp.local\n" + + public void defaultLookupShouldReturnResultsIfOnlySingleFileSpecified(@TempDir Path tempDir) throws Exception { + File f = buildFile(tempDir, "domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n"); UnixResolverDnsServerAddressStreamProvider p = @@ -50,8 +47,9 @@ public void defaultLookupShouldReturnResultsIfOnlySingleFileSpecified() throws E } @Test - public void nameServerAddressStreamShouldBeRotationalWhenRotationOptionsIsPresent() throws Exception { - File f = buildFile("options rotate\n" + + public void nameServerAddressStreamShouldBeRotationalWhenRotationOptionsIsPresent( + @TempDir Path tempDir) throws Exception { + File f = buildFile(tempDir, "options rotate\n" + "domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n" + @@ -81,8 +79,9 @@ public void nameServerAddressStreamShouldBeRotationalWhenRotationOptionsIsPresen } @Test - public void nameServerAddressStreamShouldAlwaysStartFromTheTopWhenRotationOptionsIsAbsent() throws Exception { - File f = buildFile("domain linecorp.local\n" + + public void nameServerAddressStreamShouldAlwaysStartFromTheTopWhenRotationOptionsIsAbsent( + @TempDir Path tempDir) throws Exception { + File f = buildFile(tempDir, "domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n" + "nameserver 127.0.0.4\n"); @@ -106,11 +105,11 @@ public void nameServerAddressStreamShouldAlwaysStartFromTheTopWhenRotationOption } @Test - public void defaultReturnedWhenNoBetterMatch() throws Exception { - File f = buildFile("domain linecorp.local\n" + + public void defaultReturnedWhenNoBetterMatch(@TempDir Path tempDir) throws Exception { + File f = buildFile(tempDir, "domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n"); - File f2 = buildFile("domain squarecorp.local\n" + + File f2 = buildFile(tempDir, "domain squarecorp.local\n" + "nameserver 127.0.0.4\n" + "nameserver 127.0.0.5\n"); UnixResolverDnsServerAddressStreamProvider p = @@ -122,11 +121,11 @@ public void defaultReturnedWhenNoBetterMatch() throws Exception { } @Test - public void moreRefinedSelectionReturnedWhenMatch() throws Exception { - File f = buildFile("domain linecorp.local\n" + + public void moreRefinedSelectionReturnedWhenMatch(@TempDir Path tempDir) throws Exception { + File f = buildFile(tempDir, "domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n"); - File f2 = buildFile("domain dc1.linecorp.local\n" + + File f2 = buildFile(tempDir, "domain dc1.linecorp.local\n" + "nameserver 127.0.0.4\n" + "nameserver 127.0.0.5\n"); UnixResolverDnsServerAddressStreamProvider p = @@ -138,96 +137,96 @@ public void moreRefinedSelectionReturnedWhenMatch() throws Exception { } @Test - public void ndotsOptionIsParsedIfPresent() throws IOException { - File f = buildFile("search localdomain\n" + + public void ndotsOptionIsParsedIfPresent(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n" + "options ndots:0\n"); assertEquals(0, parseEtcResolverOptions(f).ndots()); - f = buildFile("search localdomain\n" + + f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n" + "options ndots:123 foo:goo\n"); assertEquals(123, parseEtcResolverOptions(f).ndots()); } @Test - public void defaultValueReturnedIfNdotsOptionsNotPresent() throws IOException { - File f = buildFile("search localdomain\n" + + public void defaultValueReturnedIfNdotsOptionsNotPresent(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n"); assertEquals(1, parseEtcResolverOptions(f).ndots()); } @Test - public void timeoutOptionIsParsedIfPresent() throws IOException { - File f = buildFile("search localdomain\n" + + public void timeoutOptionIsParsedIfPresent(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n" + "options timeout:0\n"); assertEquals(0, parseEtcResolverOptions(f).timeout()); - f = buildFile("search localdomain\n" + + f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n" + "options foo:bar timeout:124\n"); assertEquals(124, parseEtcResolverOptions(f).timeout()); } @Test - public void defaultValueReturnedIfTimeoutOptionsIsNotPresent() throws IOException { - File f = buildFile("search localdomain\n" + + public void defaultValueReturnedIfTimeoutOptionsIsNotPresent(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n"); assertEquals(5, parseEtcResolverOptions(f).timeout()); } @Test - public void attemptsOptionIsParsedIfPresent() throws IOException { - File f = buildFile("search localdomain\n" + + public void attemptsOptionIsParsedIfPresent(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n" + "options attempts:0\n"); assertEquals(0, parseEtcResolverOptions(f).attempts()); - f = buildFile("search localdomain\n" + + f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n" + "options foo:bar attempts:12\n"); assertEquals(12, parseEtcResolverOptions(f).attempts()); } @Test - public void defaultValueReturnedIfAttemptsOptionsIsNotPresent() throws IOException { - File f = buildFile("search localdomain\n" + + public void defaultValueReturnedIfAttemptsOptionsIsNotPresent(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search localdomain\n" + "nameserver 127.0.0.11\n"); assertEquals(16, parseEtcResolverOptions(f).attempts()); } @Test - public void emptyEtcResolverDirectoryDoesNotThrow() throws IOException { - File f = buildFile("domain linecorp.local\n" + + public void emptyEtcResolverDirectoryDoesNotThrow(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n"); UnixResolverDnsServerAddressStreamProvider p = - new UnixResolverDnsServerAddressStreamProvider(f, folder.newFolder().listFiles()); + new UnixResolverDnsServerAddressStreamProvider(f, tempDir.resolve("netty-empty").toFile().listFiles()); DnsServerAddressStream stream = p.nameServerAddressStream("somehost"); assertHostNameEquals("127.0.0.2", stream.next()); } @Test - public void searchDomainsWithOnlyDomain() throws IOException { - File f = buildFile("domain linecorp.local\n" + + public void searchDomainsWithOnlyDomain(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "domain linecorp.local\n" + "nameserver 127.0.0.2\n"); List domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f); assertEquals(Collections.singletonList("linecorp.local"), domains); } @Test - public void searchDomainsWithOnlySearch() throws IOException { - File f = buildFile("search linecorp.local\n" + + public void searchDomainsWithOnlySearch(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search linecorp.local\n" + "nameserver 127.0.0.2\n"); List domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f); assertEquals(Collections.singletonList("linecorp.local"), domains); } @Test - public void searchDomainsWithMultipleSearch() throws IOException { - File f = buildFile("search linecorp.local\n" + + public void searchDomainsWithMultipleSearch(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search linecorp.local\n" + "search squarecorp.local\n" + "nameserver 127.0.0.2\n"); List domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f); @@ -235,24 +234,24 @@ public void searchDomainsWithMultipleSearch() throws IOException { } @Test - public void searchDomainsWithMultipleSearchSeperatedByWhitespace() throws IOException { - File f = buildFile("search linecorp.local squarecorp.local\n" + + public void searchDomainsWithMultipleSearchSeperatedByWhitespace(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search linecorp.local squarecorp.local\n" + "nameserver 127.0.0.2\n"); List domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f); assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains); } @Test - public void searchDomainsWithMultipleSearchSeperatedByTab() throws IOException { - File f = buildFile("search linecorp.local\tsquarecorp.local\n" + + public void searchDomainsWithMultipleSearchSeperatedByTab(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "search linecorp.local\tsquarecorp.local\n" + "nameserver 127.0.0.2\n"); List domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f); assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains); } @Test - public void searchDomainsPrecedence() throws IOException { - File f = buildFile("domain linecorp.local\n" + + public void searchDomainsPrecedence(@TempDir Path tempDir) throws IOException { + File f = buildFile(tempDir, "domain linecorp.local\n" + "search squarecorp.local\n" + "nameserver 127.0.0.2\n"); List domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f); @@ -260,8 +259,8 @@ public void searchDomainsPrecedence() throws IOException { } @Test - public void ignoreInvalidEntries() throws Exception { - File f = buildFile("domain netty.local\n" + + public void ignoreInvalidEntries(@TempDir Path tempDir) throws Exception { + File f = buildFile(tempDir, "domain netty.local\n" + "nameserver nil\n" + "nameserver 127.0.0.3\n"); UnixResolverDnsServerAddressStreamProvider p = @@ -272,20 +271,15 @@ public void ignoreInvalidEntries() throws Exception { assertHostNameEquals("127.0.0.3", stream.next()); } - private File buildFile(String contents) throws IOException { - File f = folder.newFile(); - OutputStream out = new FileOutputStream(f); - try { - out.write(contents.getBytes(CharsetUtil.UTF_8)); - } finally { - out.close(); - } - return f; + private File buildFile(Path tempDir, String contents) throws IOException { + Path path = tempDir.resolve("netty-dns-" + UUID.randomUUID().toString().substring(24) + ".txt"); + Files.write(path, contents.getBytes(CharsetUtil.UTF_8)); + return path.toFile(); } @Test - public void ignoreComments() throws Exception { - File f = buildFile("domain linecorp.local\n" + + public void ignoreComments(@TempDir Path tempDir) throws Exception { + File f = buildFile(tempDir, "domain linecorp.local\n" + "nameserver 127.0.0.2 #somecomment\n"); UnixResolverDnsServerAddressStreamProvider p = new UnixResolverDnsServerAddressStreamProvider(f, null); @@ -295,6 +289,6 @@ public void ignoreComments() throws Exception { } private static void assertHostNameEquals(String expectedHostname, InetSocketAddress next) { - assertEquals("unexpected hostname: " + next, expectedHostname, next.getHostString()); + assertEquals(expectedHostname, next.getHostString(), "unexpected hostname: " + next); } }