Path: blob/master/test/jdk/javax/management/remote/mandatory/URLTest.java
41153 views
/*1* Copyright (c) 2004, 2015, 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 505753226* @summary Tests that host names are parsed correctly in URLs27* @author Eamonn McManus28*29* @run clean URLTest30* @run build URLTest31* @run main URLTest32*/3334import java.net.MalformedURLException;35import java.net.URISyntaxException;36import java.net.URI;37import javax.management.remote.JMXServiceURL;3839public class URLTest {40private static final String[] good = {41"",42"a",43"a.b",44"a.b.c.d.e.f.g",45"aaa.bbb",46"a-a.b-b",47"a-a",48"a--b",49"1.2.3.4",50"1.2.3.x",51"111.222.222.111",52"1",53"23skiddoo",54"23skiddoo.sfbay",55"a1.b2",56"1234.sfbay",57"[::]",58"[ffff::ffff]",59};60private static final String[] bad = {61"-a",62"a-",63"-",64"_",65"a_b",66"a_b.sfbay",67".",68"..",69".a",70"a.",71"a..",72"a..b",73".a.b",74"a.b.",75"a.b..",76"1.2",77"111.222.333.444",78"a.23skiddoo",79"[:::]",80"[:]",81};8283public static void main(String[] args) throws Exception {84System.out.println("Testing that JMXServiceURL accepts the same " +85"hosts as java.net.URI");86System.out.println("(Except that it allows empty host names and " +87"forbids a trailing \".\")");88System.out.println();8990int failures = 0;9192for (int pass = 1; pass <= 2; pass++) {93final boolean accept = (pass == 1);94System.out.println(" Hosts that should " +95(accept ? "" : "not ") + "work");96String[] hosts = accept ? good : bad;9798for (int i = 0; i < hosts.length; i++) {99final String host = hosts[i];100System.out.print(" " + host + ": ");101102boolean jmxAccept = true;103try {104new JMXServiceURL("rmi", hosts[i], 0);105} catch (MalformedURLException e) {106jmxAccept = false;107}108109boolean uriAccept;110try {111final URI uri = new URI("http://" + host + "/");112uriAccept = (uri.getHost() != null);113} catch (URISyntaxException e) {114uriAccept = false;115}116117final int len = host.length();118if (accept != uriAccept && len != 0 &&119!(len > 1 && host.charAt(len - 1) == '.'120&& host.charAt(len - 2) != '.')) {121// JMXServiceURL allows empty host name; also122// java.net.URI allows trailing dot in hostname,123// following RFC 2396, but JMXServiceURL doesn't,124// following RFC 2609125System.out.println("TEST BUG: URI accept=" + uriAccept);126failures++;127} else {128if (jmxAccept == accept)129System.out.println("OK");130else {131System.out.println("FAILED");132failures++;133}134}135}136137System.out.println();138}139140if (failures == 0)141System.out.println("Test passed");142else {143System.out.println("TEST FAILURES: " + failures);144System.exit(1);145}146}147}148149150