< prev index next >

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

Print this page

        

*** 22,31 **** --- 22,32 ---- */ 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,81 **** } /* * Process command line arguments and init env */ ! public static Hashtable<Object, Object> initEnv(DatagramSocket socket, String testname, String[] args) { Hashtable<Object, Object> env = new Hashtable<>(); // set some default parameters if no additional specified --- 72,82 ---- } /* * Process command line arguments and init env */ ! 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,135 **** 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; if (traceEnable) { ! inst = createDNSTracer(socket, testname, env); } else { ! if (socket != null) { ! inst = createDNSServer(socket, testname, loopPlayback); } else { // for tests which run against remote server // or no server required ! debug("Skip local DNS Server creation " ! + "since DatagramSocket is null"); } } if (inst != null) { ! env.put(TEST_DNS_SERVER_THREAD, startServer(inst)); ! String url = "dns://localhost:" + socket.getLocalPort(); env.put(TEST_DNS_ROOT_URL, url); env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN")); } --- 110,136 ---- String url = "dns://" + env.get("DNS_SERVER") + portSuffix; env.put(Context.PROVIDER_URL, url); env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN")); } ! Thread inst = null; if (traceEnable) { ! inst = createDNSTracer(testname, env); } else { ! if (localServer) { ! inst = createDNSServer(testname, loopPlayback); } else { // for tests which run against remote server // or no server required ! debug("Skip local DNS Server creation "); } } if (inst != null) { ! 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,156 **** --- 148,170 ---- // 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,210 **** 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"); ! } ! try { PrintStream outStream = new PrintStream(getCaptureFile(testname)); ! return new DNSTracer(socket, 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"); ! } ! String path = getCaptureFile(testname); if (Files.exists(Paths.get(path))) { ! return new DNSServer(socket, path, loop); } 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(); } --- 174,212 ---- throw new RuntimeException( "Failed to extract test args property from " + propString); } } ! public static DNSTracer createDNSTracer(String testname, ! Hashtable<Object, Object> env) { try { PrintStream outStream = new PrintStream(getCaptureFile(testname)); ! 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(String testname, boolean loop) { String path = getCaptureFile(testname); if (Files.exists(Paths.get(path))) { ! 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 String getCaptureFile(String testname) { return Paths.get(System.getProperty("test.src")) .resolve(testname + ".dns").toString(); }
*** 241,246 **** --- 243,265 ---- 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 >