Path: blob/master/test/jdk/com/sun/jndi/dns/EnvTests/ProviderUrlGen.java
41155 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 820827926* @summary If DNS name resolution is configured normally for the local27* machine, construct a context using the configured servers and verify28* that a PROVIDER_URL property is generated for the context.29* If DNS is not configured, or if JDK version is earlier than 1.4.1,30* then this test is a no-op (it passes without doing anything).31* @library ../lib/32* @modules jdk.naming.dns/com.sun.jndi.dns33* java.base/sun.security.util34* @run main ProviderUrlGen35*/3637import javax.naming.Context;38import javax.naming.NamingException;39import javax.naming.directory.InitialDirContext;40import java.lang.reflect.InvocationTargetException;4142public class ProviderUrlGen extends DNSTestBase {4344public ProviderUrlGen() {45setLocalServer(false);46}4748public static void main(String[] args) throws Exception {49if (!isPlatformServersAvailable()) {50DNSTestUtils.debug("DNS not configured. There's nothing to test.");51return;52}5354new ProviderUrlGen().run(args);55}5657@Override public void runTest() throws Exception {58initContext();5960String providerUrl = (String) context().getEnvironment()61.get(Context.PROVIDER_URL);6263if (providerUrl == null) {64throw new RuntimeException("Failed: PROVIDER_URL not set");65}6667DNSTestUtils.debug("PROVIDER_URL = \"" + providerUrl + "\"");6869// We don't know exactly what providerUrl's value should be.70// Check that it is a space-separated list of one or more URLs71// of the form "dns://xxxxxx".7273String[] urls = providerUrl.split(" ");74if (urls.length < 1) {75throw new RuntimeException("Failed: PROVIDER_URL is empty");76}7778for (String url : urls) {79DNSTestUtils.debug(url);80if (!checkUrl(url)) {81throw new RuntimeException(82"Failed: Unexpected DNS URL: " + url);83}84}85}8687private void initContext() throws NamingException {88env().remove(Context.PROVIDER_URL);89setContext(new InitialDirContext(env()));90}9192private static boolean checkUrl(String url) {93return url.startsWith("dns://") && url.length() >= 7;94}9596private static boolean isPlatformServersAvailable() {97try {98java.lang.reflect.Method psa = com.sun.jndi.dns.DnsContextFactory.class99.getMethod("platformServersAvailable", null);100return (Boolean) psa.invoke(null, null);101} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {102e.printStackTrace();103return false;104}105}106}107108109