jdk/test/java/net/URL/HandlersPkgPrefix/HandlersPkgPrefix.java
author chegar
Tue, 21 Apr 2015 14:23:58 +0100
changeset 29977 ad35f9986f75
permissions -rw-r--r--
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration Reviewed-by: alanb, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29977
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     1
/*
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     4
 *
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     8
 *
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    13
 * accompanied this code).
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    14
 *
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    18
 *
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    21
 * questions.
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    22
 */
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    23
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    24
import java.net.MalformedURLException;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    25
import java.net.URL;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    26
import java.util.function.Consumer;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    27
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    28
/*
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    29
 * @test
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    30
 * @bug 8075139
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    31
 * @summary Basic test for java.protocol.handler.pkgs
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    32
 * @compile handlers/foo/Handler.java handlers/bar/Handler.java HandlersPkgPrefix.java
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    33
 * @run main/othervm HandlersPkgPrefix
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    34
 */
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    35
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    36
public class HandlersPkgPrefix {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    37
    static final Consumer<Result> KNOWN = r -> {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    38
        if (r.exception != null)
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    39
            throw new RuntimeException("Unexpected exception " + r.exception);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    40
        String p = r.url.getProtocol();
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    41
        if (!r.protocol.equals(p))
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    42
            throw new RuntimeException("Expected:" + r.protocol + ", got:" + p);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    43
    };
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    44
    static final Consumer<Result> UNKNOWN = r -> {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    45
        if (r.url != null)
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    46
            throw new RuntimeException("Unexpected url:" + r.url);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    47
        if (!(r.exception instanceof MalformedURLException))
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    48
            throw new RuntimeException("Expected MalformedURLException, got:"
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    49
                                       + r.exception);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    50
    };
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    51
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    52
    public static void main(String[] args) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    53
        withPrefix("unknown", "", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    54
        withPrefix("unknown", "handlers", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    55
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    56
        withPrefix("foo", "", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    57
        withPrefix("foo", "xxx|yyy|zzz", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    58
        withPrefix("foo", "||||", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    59
        withPrefix("foo", "|a|b|c|handlers", KNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    60
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    61
        withPrefix("bar", "", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    62
        withPrefix("bar", "x.y.z|y.y.y|z.z.z", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    63
        withPrefix("bar", " x.y.z | y.y.y | z.z.z| |  ", UNKNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    64
        withPrefix("bar", "| a | b | c | handlers | d | e", KNOWN);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    65
    }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    66
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    67
    static void withPrefix(String protocol, String pkgPrefix,
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    68
                           Consumer<Result> resultChecker) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    69
        System.out.println("Testing, " + protocol + ", " + pkgPrefix);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    70
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    71
        // The long standing implementation behavior is that the
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    72
        // property is read multiple times, not cached.
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    73
        System.setProperty("java.protocol.handler.pkgs", pkgPrefix);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    74
        URL url = null;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    75
        Exception exception = null;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    76
        try {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    77
            url = new URL(protocol + "://");
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    78
        } catch (MalformedURLException x) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    79
            exception = x;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    80
        }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    81
        resultChecker.accept(new Result(protocol, url, exception));
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    82
    }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    83
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    84
    static class Result {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    85
        final String protocol;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    86
        final URL url;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    87
        final Exception exception;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    88
        Result(String protocol, URL url, Exception exception) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    89
            this.protocol = protocol;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    90
            this.url = url;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    91
            this.exception = exception;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    92
        }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    93
    }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    94
}
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
diff changeset
    95