From 5f28d015077e276696c759a12b623cde957048c9 Mon Sep 17 00:00:00 2001 From: norman Date: Tue, 22 May 2012 10:03:33 +0200 Subject: [PATCH] Make sure the test also work on ipv6 interfaces --- .../DefaultNioDatagramChannelConfigTest.java | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/jboss/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java b/src/test/java/org/jboss/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java index 3bbf7ab06c..040cba1db1 100644 --- a/src/test/java/org/jboss/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java +++ b/src/test/java/org/jboss/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java @@ -18,10 +18,12 @@ package org.jboss.netty.channel.socket.nio; import org.jboss.netty.util.internal.DetectionUtil; import java.io.IOException; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.StandardProtocolFamily; import java.nio.channels.DatagramChannel; +import java.util.Enumeration; import junit.framework.Assert; @@ -29,25 +31,47 @@ import org.junit.Test; public class DefaultNioDatagramChannelConfigTest { + @Test public void testMulticastOptions() throws IOException { if (DetectionUtil.javaVersion() < 7) { return; } - DefaultNioDatagramChannelConfig config = new DefaultNioDatagramChannelConfig(DatagramChannel.open(StandardProtocolFamily.INET)); - NetworkInterface inf = NetworkInterface.getNetworkInterfaces().nextElement(); + + StandardProtocolFamily family = null; + NetworkInterface inf = null; + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + inf = interfaces.nextElement(); + Enumeration addresses = inf.getInetAddresses(); + while(addresses.hasMoreElements()) { + InetAddress addr = addresses.nextElement(); + if (addr instanceof Inet4Address) { + family = StandardProtocolFamily.INET; + break; + } else { + family = StandardProtocolFamily.INET6; + } + } + } + if (inf == null) { + // No usable interface found so just skip the test + return; + } + + DefaultNioDatagramChannelConfig config = new DefaultNioDatagramChannelConfig(DatagramChannel.open(family)); + config.setNetworkInterface(inf); Assert.assertEquals(inf, config.getNetworkInterface()); - + InetAddress localhost = inf.getInetAddresses().nextElement(); config.setInterface(localhost); Assert.assertEquals(localhost, config.getInterface()); - + config.setTimeToLive(100); Assert.assertEquals(100, config.getTimeToLive()); - + config.setLoopbackModeDisabled(false); Assert.assertEquals(false, config.isLoopbackModeDisabled()); - } }