Path: blob/master/test/jdk/java/net/URL/URIToURLTest.java
41152 views
/*1* Copyright (c) 2001, 2013, 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/* @test24* @bug 448245525* @summary URI.toURL() implementation needs to be improved26*27*/2829import java.net.*;30import java.util.ArrayList;31import java.util.List;3233public class URIToURLTest {34public static void main(String args[]) throws Exception {35List<String> uris = new ArrayList<>();36uris.add("http://jag:[email protected]:94/b/c/d?q#g");37uris.add("http://[1080:0:0:0:8:800:200C:417A]/index.html");38uris.add("http://a/b/c/d;p?q");39uris.add("mailto:[email protected]");40uris.add("http:comp.infosystems.www.servers.unix");41if (hasFtp())42uris.add("ftp://ftp.is.co.za/rfc/rfc1808.txt");4344for (String uriStr : uris) {45URI uri = new URI(uriStr);46URL url = uri.toURL();47String scheme = uri.getScheme();48boolean schemeCheck = scheme == null? url.getProtocol() == null :49scheme.equals(url.getProtocol());50if (!schemeCheck)51throw new RuntimeException("uri.scheme is " + scheme +52" url.protocol is " +53url.getProtocol());5455if (uri.isOpaque()) {56String ssp = uri.getSchemeSpecificPart();57boolean sspCheck = ssp == null? uri.getPath() == null :58ssp.equals(url.getPath());59if (!sspCheck) {60throw new RuntimeException("uri.ssp is " + ssp +61" url.path is " + url.getPath());62}63} else {64String authority = uri.getAuthority();65boolean authorityCheck = authority == null?66url.getAuthority() == null :67authority.equals(url.getAuthority());68if (!authorityCheck) {69throw new RuntimeException("uri.authority is " +70authority + " url's is " +71url.getAuthority());72}73String host = uri.getHost();74boolean hostCheck = host == null ? url.getHost() == null :75host.equals(url.getHost());76if (!hostCheck)77throw new RuntimeException("uri.host is " +78host + " url's is " +79url.getHost());80if (host != null) {81String userInfo = uri.getUserInfo();82boolean userInfoCheck = userInfo == null?83url.getUserInfo() == null :84userInfo.equals(url.getUserInfo());85if (uri.getPort() != url.getPort())86throw new RuntimeException("uri.port is " +87uri.getPort() + " url's is " +88url.getPort());89}9091String path = uri.getPath();92boolean pathCheck = path == null? url.getPath() == null :93path.equals(url.getPath());94if (!pathCheck)95throw new RuntimeException("uri.path is " + path +96" url.path is " +97url.getPath());98String query = uri.getQuery();99boolean queryCheck = query == null? url.getQuery() == null :100query.equals(url.getQuery());101if (!queryCheck)102throw new RuntimeException("uri.query is " + query +103" url.query is " +104url.getQuery());105}106String frag = uri.getFragment();107boolean fragCheck = frag == null? url.getRef() == null :108frag.equals(url.getRef());109if (!fragCheck)110throw new RuntimeException("uri.frag is " + frag +111" url.ref is " +112url.getRef());113}114}115116private static boolean hasFtp() {117try {118return new java.net.URL("ftp://") != null;119} catch (java.net.MalformedURLException x) {120System.out.println("FTP not supported by this runtime.");121return false;122}123}124}125126127