--- old/test/jdk/com/sun/jndi/dns/lib/DNSTestUtils.java 2018-07-13 13:53:20.000000000 +0800 +++ new/test/jdk/com/sun/jndi/dns/lib/DNSTestUtils.java 2018-07-13 13:53:20.000000000 +0800 @@ -24,6 +24,7 @@ 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; @@ -73,7 +74,7 @@ /* * Process command line arguments and init env */ - public static Hashtable initEnv(DatagramSocket socket, + public static Hashtable initEnv(boolean localServer, String testname, String[] args) { Hashtable env = new Hashtable<>(); @@ -111,23 +112,23 @@ 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")); @@ -149,6 +150,19 @@ } } + /* + * 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 env) { int index; @@ -162,17 +176,11 @@ } } - public static DNSTracer createDNSTracer(DatagramSocket socket, - String testname, Hashtable env) { - if (socket == null) { - throw new RuntimeException("Error: failed to create DNSTracer " - + "since DatagramSocket is null"); - } - + public static DNSTracer createDNSTracer(String testname, + Hashtable 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( @@ -180,16 +188,16 @@ } } - 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 " @@ -197,12 +205,6 @@ } } - 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(); @@ -243,4 +245,21 @@ throw new RuntimeException("Check schema failed."); } } + + public static String getRootUrl(Hashtable 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 env, + boolean primary) { + String domain = (String) (primary ? + env.get("DNS_DOMAIN") : + env.get("FOREIGN_DOMAIN")); + + return base + "." + domain; + } }