Path: blob/master/test/jdk/java/net/URL/HandlersPkgPrefix/HandlersPkgPrefix.java
41154 views
/*1* Copyright (c) 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*/2223import java.net.MalformedURLException;24import java.net.URL;25import java.util.function.Consumer;2627/*28* @test29* @bug 807513930* @summary Basic test for java.protocol.handler.pkgs31* @compile handlers/foo/Handler.java handlers/bar/Handler.java HandlersPkgPrefix.java32* @run main/othervm HandlersPkgPrefix33*/3435public class HandlersPkgPrefix {36static final Consumer<Result> KNOWN = r -> {37if (r.exception != null)38throw new RuntimeException("Unexpected exception " + r.exception);39String p = r.url.getProtocol();40if (!r.protocol.equals(p))41throw new RuntimeException("Expected:" + r.protocol + ", got:" + p);42};43static final Consumer<Result> UNKNOWN = r -> {44if (r.url != null)45throw new RuntimeException("Unexpected url:" + r.url);46if (!(r.exception instanceof MalformedURLException))47throw new RuntimeException("Expected MalformedURLException, got:"48+ r.exception);49};5051public static void main(String[] args) {52withPrefix("unknown", "", UNKNOWN);53withPrefix("unknown", "handlers", UNKNOWN);5455withPrefix("foo", "", UNKNOWN);56withPrefix("foo", "xxx|yyy|zzz", UNKNOWN);57withPrefix("foo", "||||", UNKNOWN);58withPrefix("foo", "|a|b|c|handlers", KNOWN);5960withPrefix("bar", "", UNKNOWN);61withPrefix("bar", "x.y.z|y.y.y|z.z.z", UNKNOWN);62withPrefix("bar", " x.y.z | y.y.y | z.z.z| | ", UNKNOWN);63withPrefix("bar", "| a | b | c | handlers | d | e", KNOWN);64}6566static void withPrefix(String protocol, String pkgPrefix,67Consumer<Result> resultChecker) {68System.out.println("Testing, " + protocol + ", " + pkgPrefix);6970// The long standing implementation behavior is that the71// property is read multiple times, not cached.72System.setProperty("java.protocol.handler.pkgs", pkgPrefix);73URL url = null;74Exception exception = null;75try {76url = new URL(protocol + "://");77} catch (MalformedURLException x) {78exception = x;79}80resultChecker.accept(new Result(protocol, url, exception));81}8283static class Result {84final String protocol;85final URL url;86final Exception exception;87Result(String protocol, URL url, Exception exception) {88this.protocol = protocol;89this.url = url;90this.exception = exception;91}92}93}94959697