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