test/jdk/com/sun/jndi/dns/EnvTests/ProviderUrlGen.java
changeset 51331 7939b3c4e408
equal deleted inserted replaced
51330:edd33b9946ca 51331:7939b3c4e408
       
     1 /*
       
     2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     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 /*
       
    25  * @test
       
    26  * @bug 8208279
       
    27  * @summary If DNS name resolution is configured normally for the local
       
    28  *          machine, construct a context using the configured servers and verify
       
    29  *          that a PROVIDER_URL property is generated for the context.
       
    30  *          If DNS is not configured, or if JDK version is earlier than 1.4.1,
       
    31  *          then this test is a no-op (it passes without doing anything).
       
    32  * @library ../lib/
       
    33  * @modules jdk.naming.dns/com.sun.jndi.dns
       
    34  *          java.base/sun.security.util
       
    35  * @run main ProviderUrlGen
       
    36  */
       
    37 
       
    38 import javax.naming.Context;
       
    39 import javax.naming.NamingException;
       
    40 import javax.naming.directory.InitialDirContext;
       
    41 import java.lang.reflect.InvocationTargetException;
       
    42 
       
    43 public class ProviderUrlGen extends DNSTestBase {
       
    44 
       
    45     public ProviderUrlGen() {
       
    46         setLocalServer(false);
       
    47     }
       
    48 
       
    49     public static void main(String[] args) throws Exception {
       
    50         if (!isPlatformServersAvailable()) {
       
    51             DNSTestUtils.debug("DNS not configured. There's nothing to test.");
       
    52             return;
       
    53         }
       
    54 
       
    55         new ProviderUrlGen().run(args);
       
    56     }
       
    57 
       
    58     @Override public void runTest() throws Exception {
       
    59         initContext();
       
    60 
       
    61         String providerUrl = (String) context().getEnvironment()
       
    62                 .get(Context.PROVIDER_URL);
       
    63 
       
    64         if (providerUrl == null) {
       
    65             throw new RuntimeException("Failed: PROVIDER_URL not set");
       
    66         }
       
    67 
       
    68         DNSTestUtils.debug("PROVIDER_URL = \"" + providerUrl + "\"");
       
    69 
       
    70         // We don't know exactly what providerUrl's value should be.
       
    71         // Check that it is a space-separated list of one or more URLs
       
    72         // of the form "dns://xxxxxx".
       
    73 
       
    74         String[] urls = providerUrl.split(" ");
       
    75         if (urls.length < 1) {
       
    76             throw new RuntimeException("Failed:  PROVIDER_URL is empty");
       
    77         }
       
    78 
       
    79         for (String url : urls) {
       
    80             DNSTestUtils.debug(url);
       
    81             if (!checkUrl(url)) {
       
    82                 throw new RuntimeException(
       
    83                         "Failed:  Unexpected DNS URL: " + url);
       
    84             }
       
    85         }
       
    86     }
       
    87 
       
    88     private void initContext() throws NamingException {
       
    89         env().remove(Context.PROVIDER_URL);
       
    90         setContext(new InitialDirContext(env()));
       
    91     }
       
    92 
       
    93     private static boolean checkUrl(String url) {
       
    94         return url.startsWith("dns://") && url.length() >= 7;
       
    95     }
       
    96 
       
    97     private static boolean isPlatformServersAvailable() {
       
    98         try {
       
    99             java.lang.reflect.Method psa = com.sun.jndi.dns.DnsContextFactory.class
       
   100                     .getMethod("platformServersAvailable", null);
       
   101             return (Boolean) psa.invoke(null, null);
       
   102         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
       
   103             e.printStackTrace();
       
   104             return false;
       
   105         }
       
   106     }
       
   107 }