< prev index next >

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

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.naming.Context;
  25 import javax.naming.NamingException;
  26 import javax.naming.directory.Attributes;

  27 import java.io.PrintStream;
  28 import java.net.DatagramSocket;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Hashtable;
  33 
  34 public class DNSTestUtils {
  35     public static final String TEST_DNS_SERVER_THREAD = "test.dns.server.thread";
  36     public static final String TEST_DNS_ROOT_URL = "test.dns.root.url";
  37     public static final int HOSTS_LOOKUP_MAX_DEPTH = 3;
  38 
  39     protected static boolean debug = true;
  40 
  41     /*
  42      * Check that attrs contains the mandatory attributes and the right
  43      * objectclass attribute
  44      */
  45     public static boolean checkSchema(Attributes attrs, String[] mandatory,
  46             String[] optional) {


 132             env.put(TEST_DNS_ROOT_URL, url);
 133             env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
 134         }
 135 
 136         return env;
 137     }
 138 
 139     /*
 140      * Clean-up the directory context.
 141      */
 142     public static void cleanup(Context ctx) {
 143         if (ctx != null) {
 144             try {
 145                 ctx.close();
 146             } catch (NamingException e) {
 147                 // ignore
 148             }
 149         }
 150     }
 151 













 152     private static void extractProperty(String propString,
 153             Hashtable<Object, Object> env) {
 154         int index;
 155 
 156         if ((index = propString.indexOf('=')) > 0) {
 157             env.put(propString.substring(0, index),
 158                     propString.substring(index + 1));
 159         } else {
 160             throw new RuntimeException(
 161                     "Failed to extract test args property from " + propString);
 162         }
 163     }
 164 
 165     public static DNSTracer createDNSTracer(DatagramSocket socket,
 166             String testname, Hashtable<Object, Object> env) {
 167         if (socket == null) {
 168             throw new RuntimeException("Error: failed to create DNSTracer "
 169                     + "since DatagramSocket is null");
 170         }
 171 


 226             path = path.getParent();
 227             if (path == null) {
 228                 break;
 229             }
 230         }
 231     }
 232 
 233     public static void debug(Object object) {
 234         if (debug) {
 235             System.out.println(object);
 236         }
 237     }
 238 
 239     public static void verifySchema(Attributes attrs, String[] mandatory,
 240             String[] optional) {
 241         debug(attrs);
 242         if (!checkSchema(attrs, mandatory, optional)) {
 243             throw new RuntimeException("Check schema failed.");
 244         }
 245     }

















 246 }


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.naming.Context;
  25 import javax.naming.NamingException;
  26 import javax.naming.directory.Attributes;
  27 import java.io.Closeable;
  28 import java.io.PrintStream;
  29 import java.net.DatagramSocket;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.util.Hashtable;
  34 
  35 public class DNSTestUtils {
  36     public static final String TEST_DNS_SERVER_THREAD = "test.dns.server.thread";
  37     public static final String TEST_DNS_ROOT_URL = "test.dns.root.url";
  38     public static final int HOSTS_LOOKUP_MAX_DEPTH = 3;
  39 
  40     protected static boolean debug = true;
  41 
  42     /*
  43      * Check that attrs contains the mandatory attributes and the right
  44      * objectclass attribute
  45      */
  46     public static boolean checkSchema(Attributes attrs, String[] mandatory,
  47             String[] optional) {


 133             env.put(TEST_DNS_ROOT_URL, url);
 134             env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
 135         }
 136 
 137         return env;
 138     }
 139 
 140     /*
 141      * Clean-up the directory context.
 142      */
 143     public static void cleanup(Context ctx) {
 144         if (ctx != null) {
 145             try {
 146                 ctx.close();
 147             } catch (NamingException e) {
 148                 // ignore
 149             }
 150         }
 151     }
 152 
 153     /*
 154      * Clean up given closable resource
 155      */
 156     public static void cleanupClosableRes(Closeable res) {
 157         if (res != null) {
 158             try {
 159                 res.close();
 160             } catch (Exception e) {
 161                 // ignore
 162             }
 163         }
 164     }
 165 
 166     private static void extractProperty(String propString,
 167             Hashtable<Object, Object> env) {
 168         int index;
 169 
 170         if ((index = propString.indexOf('=')) > 0) {
 171             env.put(propString.substring(0, index),
 172                     propString.substring(index + 1));
 173         } else {
 174             throw new RuntimeException(
 175                     "Failed to extract test args property from " + propString);
 176         }
 177     }
 178 
 179     public static DNSTracer createDNSTracer(DatagramSocket socket,
 180             String testname, Hashtable<Object, Object> env) {
 181         if (socket == null) {
 182             throw new RuntimeException("Error: failed to create DNSTracer "
 183                     + "since DatagramSocket is null");
 184         }
 185 


 240             path = path.getParent();
 241             if (path == null) {
 242                 break;
 243             }
 244         }
 245     }
 246 
 247     public static void debug(Object object) {
 248         if (debug) {
 249             System.out.println(object);
 250         }
 251     }
 252 
 253     public static void verifySchema(Attributes attrs, String[] mandatory,
 254             String[] optional) {
 255         debug(attrs);
 256         if (!checkSchema(attrs, mandatory, optional)) {
 257             throw new RuntimeException("Check schema failed.");
 258         }
 259     }
 260 
 261     public static String getRootUrl(Hashtable<Object, Object> env) {
 262         return (String) env.get(TEST_DNS_ROOT_URL);
 263     }
 264 
 265     /*
 266      * Assemble a fully-qualified domain name from the base component and the
 267      * domain name.
 268      */
 269     public static String buildFqdn(String base, Hashtable<Object, Object> env,
 270             boolean primary) {
 271         String domain = (String) (primary ?
 272                 env.get("DNS_DOMAIN") :
 273                 env.get("FOREIGN_DOMAIN"));
 274 
 275         return base + "." + domain;
 276     }
 277 }
< prev index next >