Make sure the test also work on ipv6 interfaces

This commit is contained in:
norman 2012-05-22 10:03:33 +02:00
parent 21eada0a6a
commit 5f28d01507

View File

@ -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<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
inf = interfaces.nextElement();
Enumeration<InetAddress> 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());
}
}