< prev index next >

test/jdk/com/sun/jndi/dns/lib/DNSTestUtils.java

Print this page

        

@@ -22,10 +22,11 @@
  */
 
 import javax.naming.Context;
 import javax.naming.NamingException;
 import javax.naming.directory.Attributes;
+import java.io.Closeable;
 import java.io.PrintStream;
 import java.net.DatagramSocket;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;

@@ -71,11 +72,11 @@
     }
 
     /*
      * Process command line arguments and init env
      */
-    public static Hashtable<Object, Object> initEnv(DatagramSocket socket,
+    public static Hashtable<Object, Object> initEnv(boolean localServer,
             String testname, String[] args) {
 
         Hashtable<Object, Object> env = new Hashtable<>();
 
         // set some default parameters if no additional specified

@@ -109,27 +110,27 @@
             String url = "dns://" + env.get("DNS_SERVER") + portSuffix;
             env.put(Context.PROVIDER_URL, url);
             env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
         }
 
-        Runnable inst = null;
+        Thread inst = null;
         if (traceEnable) {
-            inst = createDNSTracer(socket, testname, env);
+            inst = createDNSTracer(testname, env);
         } else {
-            if (socket != null) {
-                inst = createDNSServer(socket, testname, loopPlayback);
+            if (localServer) {
+                inst = createDNSServer(testname, loopPlayback);
             } else {
                 // for tests which run against remote server
                 // or no server required
-                debug("Skip local DNS Server creation "
-                        + "since DatagramSocket is null");
+                debug("Skip local DNS Server creation ");
             }
         }
 
         if (inst != null) {
-            env.put(TEST_DNS_SERVER_THREAD, startServer(inst));
-            String url = "dns://localhost:" + socket.getLocalPort();
+            inst.start();
+            env.put(TEST_DNS_SERVER_THREAD, inst);
+            String url = "dns://localhost:" + ((Server) inst).getPort();
 
             env.put(TEST_DNS_ROOT_URL, url);
             env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
         }
 

@@ -147,10 +148,23 @@
                 // ignore
             }
         }
     }
 
+    /*
+     * Clean up given closable resource
+     */
+    public static void cleanupClosableRes(Closeable res) {
+        if (res != null) {
+            try {
+                res.close();
+            } catch (Exception e) {
+                // ignore
+            }
+        }
+    }
+
     private static void extractProperty(String propString,
             Hashtable<Object, Object> env) {
         int index;
 
         if ((index = propString.indexOf('=')) > 0) {

@@ -160,51 +174,39 @@
             throw new RuntimeException(
                     "Failed to extract test args property from " + propString);
         }
     }
 
-    public static DNSTracer createDNSTracer(DatagramSocket socket,
-            String testname, Hashtable<Object, Object> env) {
-        if (socket == null) {
-            throw new RuntimeException("Error: failed to create DNSTracer "
-                    + "since DatagramSocket is null");
-        }
-
+    public static DNSTracer createDNSTracer(String testname,
+            Hashtable<Object, Object> env) {
         try {
             PrintStream outStream = new PrintStream(getCaptureFile(testname));
-            return new DNSTracer(socket, outStream,
-                    (String) env.get("DNS_SERVER"),
+            return new DNSTracer(outStream, (String) env.get("DNS_SERVER"),
                     Integer.parseInt((String) env.get("DNS_PORT")));
         } catch (Exception e) {
             throw new RuntimeException(
                     "Error: failed to create DNSTracer : " + e.getMessage(), e);
         }
     }
 
-    public static DNSServer createDNSServer(DatagramSocket socket,
-            String testname, boolean loop) {
-        if (socket == null) {
-            throw new RuntimeException("Error: failed to create DNSServer "
-                    + "since DatagramSocket is null");
-        }
-
+    public static DNSServer createDNSServer(String testname, boolean loop) {
         String path = getCaptureFile(testname);
         if (Files.exists(Paths.get(path))) {
-            return new DNSServer(socket, path, loop);
+            try {
+                return new DNSServer(path, loop);
+            } catch (Exception e) {
+                throw new RuntimeException(
+                        "Error: failed to create DNSServer : " + e.getMessage(),
+                        e);
+            }
         } else {
             throw new RuntimeException(
                     "Error: failed to create DNSServer, not found dns "
                             + "cache file " + path);
         }
     }
 
-    public static Thread startServer(Runnable runnable) {
-        Thread thread = new Thread(runnable);
-        thread.start();
-        return thread;
-    }
-
     public static String getCaptureFile(String testname) {
         return Paths.get(System.getProperty("test.src"))
                 .resolve(testname + ".dns").toString();
     }
 

@@ -241,6 +243,23 @@
         debug(attrs);
         if (!checkSchema(attrs, mandatory, optional)) {
             throw new RuntimeException("Check schema failed.");
         }
     }
+
+    public static String getRootUrl(Hashtable<Object, Object> env) {
+        return (String) env.get(TEST_DNS_ROOT_URL);
+    }
+
+    /*
+     * Assemble a fully-qualified domain name from the base component and the
+     * domain name.
+     */
+    public static String buildFqdn(String base, Hashtable<Object, Object> env,
+            boolean primary) {
+        String domain = (String) (primary ?
+                env.get("DNS_DOMAIN") :
+                env.get("FOREIGN_DOMAIN"));
+
+        return base + "." + domain;
+    }
 }
< prev index next >