Make sure the TestUtils class will also compile on operation systems that not support SCTP by using reflection

This commit is contained in:
Norman Maurer 2012-10-13 17:04:16 +02:00
parent d504d78cb1
commit 303fb80d34

View File

@ -15,12 +15,12 @@
*/ */
package io.netty.testsuite.util; package io.netty.testsuite.util;
import com.sun.nio.sctp.SctpChannel;
import io.netty.util.NetworkConstants; import io.netty.util.NetworkConstants;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.nio.channels.Channel;
import java.util.*; import java.util.*;
public class TestUtils { public class TestUtils {
@ -84,17 +84,25 @@ public class TestUtils {
String os = System.getProperty("os.name").toLowerCase(Locale.UK); String os = System.getProperty("os.name").toLowerCase(Locale.UK);
if (os.equals("unix") || os.equals("linux") || os.equals("sun") || os.equals("solaris")) { if (os.equals("unix") || os.equals("linux") || os.equals("sun") || os.equals("solaris")) {
try { try {
SctpChannel.open(); // Try to open a SCTP Channel, by using reflection to make it compile also on
} catch (IOException e) { // operation systems that not support SCTP like OSX and Windows
// ignore Class<?> sctpChannelClass = Class.forName("com.sun.nio.sctp.SctpChannel");
Channel channel = (Channel) sctpChannelClass.getMethod("open", null).invoke(null, null);
try {
channel.close();
} catch (IOException e) {
// ignore
}
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
// This exception may get thrown if the OS does not have // This exception may get thrown if the OS does not have
// the shared libs installed. // the shared libs installed.
System.out.print("Not supported: " + e.getMessage()); System.out.print("Not supported: " + e.getMessage());
return false; return false;
} catch (Throwable t) {
if (!(t instanceof IOException)) {
return false;
}
} }
return true; return true;
} }
return false; return false;