test/java/net/SocketOption/OptionsTest.java

Print this page
rev 14282 : 8044773: Refactor jdk.net API so that it can be moved out of the base module
Reviewed-by:

@@ -21,15 +21,17 @@
  * questions.
  */
 
 /*
  * @test
- * @bug 8036979 8072384
+ * @bug 8036979 8072384 8044773
  * @run main/othervm -Xcheck:jni OptionsTest
  * @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest
+ * @run main/othervm -Djdk.launcher.limitmods=java.base OptionsTest
  */
 
+import java.lang.reflect.Method;
 import java.net.*;
 import java.util.*;
 
 public class OptionsTest {
 

@@ -41,11 +43,11 @@
         static Test create (SocketOption<?> option, Object testValue) {
             return new Test(option, testValue);
         }
         Object option;
         Object testValue;
-    };
+    }
 
     // The tests set the option using the new API, read back the set value
     // which could be diferent, and then use the legacy get API to check
     // these values are the same
 

@@ -221,12 +223,11 @@
             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
                 return Boolean.valueOf(socket.getReuseAddress());
             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
-                return Integer.valueOf(jdk.net.Sockets.getOption(
-                    socket, StandardSocketOptions.IP_TOS));
+                return getServerSocketTrafficClass(socket);
             } else {
                 throw new RuntimeException("unexecpted socket option");
             }
         } else if  (type.equals(DatagramSocket.class)) {
             DatagramSocket socket = (DatagramSocket)s;

@@ -279,6 +280,22 @@
         doSocketTests();
         doServerSocketTests();
         doDgSocketTests();
         doMcSocketTests();
     }
+
+    // Reflectively access jdk.net.Sockets.getOption so that the test can run
+    // without the jdk.net module.
+    static Object getServerSocketTrafficClass(ServerSocket ss) throws Exception {
+        try {
+            Class<?> c = Class.forName("jdk.net.Sockets");
+            Method m = c.getDeclaredMethod("getOption", ServerSocket.class, SocketOption.class);
+            return m.invoke(null, ss, StandardSocketOptions.IP_TOS);
+        } catch (ClassNotFoundException e) {
+            // Ok, jdk.net module not present, just fall back
+            System.out.println("jdk.net module not present, falling back.");
+            return Integer.valueOf(ss.getOption(StandardSocketOptions.IP_TOS));
+        } catch (ReflectiveOperationException e) {
+            throw new AssertionError(e);
+        }
+    }
 }