jdk/test/javax/management/remote/mandatory/URLTest.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 14319 0b9fd764d40d
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14319
diff changeset
     2
 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5057532
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests that host names are parsed correctly in URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean URLTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build URLTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main URLTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class URLTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final String[] good = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        "",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        "a",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        "a.b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        "a.b.c.d.e.f.g",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        "aaa.bbb",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        "a-a.b-b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        "a-a",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        "a--b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        "1.2.3.4",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        "1.2.3.x",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        "111.222.222.111",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "23skiddoo",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        "23skiddoo.sfbay",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "a1.b2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "1234.sfbay",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "[::]",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "[ffff::ffff]",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final String[] bad = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "-a",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        "a-",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "-",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        "_",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "a_b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "a_b.sfbay",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ".",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        "..",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ".a",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        "a.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        "a..",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        "a..b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ".a.b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        "a.b.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        "a.b..",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "1.2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        "111.222.333.444",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        "a.23skiddoo",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "[:::]",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        "[:]",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.println("Testing that JMXServiceURL accepts the same " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                           "hosts as java.net.URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        System.out.println("(Except that it allows empty host names and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                           "forbids a trailing \".\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (int pass = 1; pass <= 2; pass++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            final boolean accept = (pass == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.out.println("  Hosts that should " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                               (accept ? "" : "not ") + "work");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            String[] hosts = accept ? good : bad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            for (int i = 0; i < hosts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                final String host = hosts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.out.print("    " + host + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                boolean jmxAccept = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    new JMXServiceURL("rmi", hosts[i], 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    jmxAccept = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                boolean uriAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    final URI uri = new URI("http://" + host + "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    uriAccept = (uri.getHost() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    uriAccept = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                final int len = host.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if (accept != uriAccept && len != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    !(len > 1 && host.charAt(len - 1) == '.'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                      && host.charAt(len - 2) != '.')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    // JMXServiceURL allows empty host name; also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    // java.net.URI allows trailing dot in hostname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    // following RFC 2396, but JMXServiceURL doesn't,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    // following RFC 2609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    System.out.println("TEST BUG: URI accept=" + uriAccept);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    if (jmxAccept == accept)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        System.out.println("FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (failures == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println("TEST FAILURES: " + failures);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}