test/jdk/com/sun/jndi/dns/EnvTests/ProviderUrlGen.java
author xyin
Wed, 08 Aug 2018 10:21:23 +0800
changeset 51331 7939b3c4e408
permissions -rw-r--r--
8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/ Reviewed-by: vtewari, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51331
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     1
/*
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     2
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     4
 *
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     8
 *
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    13
 * accompanied this code).
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    14
 *
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    18
 *
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    21
 * questions.
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    22
 */
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    23
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    24
/*
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    25
 * @test
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    26
 * @bug 8208279
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    27
 * @summary If DNS name resolution is configured normally for the local
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    28
 *          machine, construct a context using the configured servers and verify
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    29
 *          that a PROVIDER_URL property is generated for the context.
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    30
 *          If DNS is not configured, or if JDK version is earlier than 1.4.1,
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    31
 *          then this test is a no-op (it passes without doing anything).
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    32
 * @library ../lib/
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    33
 * @modules jdk.naming.dns/com.sun.jndi.dns
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    34
 *          java.base/sun.security.util
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    35
 * @run main ProviderUrlGen
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    36
 */
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    37
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    38
import javax.naming.Context;
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    39
import javax.naming.NamingException;
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    40
import javax.naming.directory.InitialDirContext;
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    41
import java.lang.reflect.InvocationTargetException;
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    42
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    43
public class ProviderUrlGen extends DNSTestBase {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    44
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    45
    public ProviderUrlGen() {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    46
        setLocalServer(false);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    47
    }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    48
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    50
        if (!isPlatformServersAvailable()) {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    51
            DNSTestUtils.debug("DNS not configured. There's nothing to test.");
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    52
            return;
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    53
        }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    54
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    55
        new ProviderUrlGen().run(args);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    56
    }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    57
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    58
    @Override public void runTest() throws Exception {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    59
        initContext();
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    60
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    61
        String providerUrl = (String) context().getEnvironment()
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    62
                .get(Context.PROVIDER_URL);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    63
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    64
        if (providerUrl == null) {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    65
            throw new RuntimeException("Failed: PROVIDER_URL not set");
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    66
        }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    67
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    68
        DNSTestUtils.debug("PROVIDER_URL = \"" + providerUrl + "\"");
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    69
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    70
        // We don't know exactly what providerUrl's value should be.
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    71
        // Check that it is a space-separated list of one or more URLs
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    72
        // of the form "dns://xxxxxx".
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    73
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    74
        String[] urls = providerUrl.split(" ");
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    75
        if (urls.length < 1) {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    76
            throw new RuntimeException("Failed:  PROVIDER_URL is empty");
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    77
        }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    78
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    79
        for (String url : urls) {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    80
            DNSTestUtils.debug(url);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    81
            if (!checkUrl(url)) {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    82
                throw new RuntimeException(
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    83
                        "Failed:  Unexpected DNS URL: " + url);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    84
            }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    85
        }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    86
    }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    87
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    88
    private void initContext() throws NamingException {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    89
        env().remove(Context.PROVIDER_URL);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    90
        setContext(new InitialDirContext(env()));
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    91
    }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    92
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    93
    private static boolean checkUrl(String url) {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    94
        return url.startsWith("dns://") && url.length() >= 7;
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    95
    }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    96
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    97
    private static boolean isPlatformServersAvailable() {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    98
        try {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
    99
            java.lang.reflect.Method psa = com.sun.jndi.dns.DnsContextFactory.class
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   100
                    .getMethod("platformServersAvailable", null);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   101
            return (Boolean) psa.invoke(null, null);
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   102
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   103
            e.printStackTrace();
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   104
            return false;
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   105
        }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   106
    }
7939b3c4e408 8208279: Add 8 JNDI tests to com/sun/jndi/dns/EnvTests/
xyin
parents:
diff changeset
   107
}