jdk/test/java/net/URL/URIToURLTest.java
author juh
Tue, 25 Jun 2013 14:41:46 -0700
changeset 18552 005e115dc6ee
parent 15528 18f0bac88177
child 23010 6dadb192ad81
permissions -rw-r--r--
8017326: Cleanup of the javadoc <code> tag in java.security.spec Summary: Convert javadoc <code> and <tt> tags to {@code ...} Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4482455
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary URI.toURL() implementation needs to be improved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    31
import java.util.ArrayList;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    32
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class URIToURLTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main(String args[]) throws Exception {
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    36
        List<String> uris = new ArrayList<>();
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    37
        uris.add("http://jag:cafebabe@java.sun.com:94/b/c/d?q#g");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    38
        uris.add("http://[1080:0:0:0:8:800:200C:417A]/index.html");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    39
        uris.add("http://a/b/c/d;p?q");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    40
        uris.add("mailto:mduerst@ifi.unizh.ch");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    41
        uris.add("http:comp.infosystems.www.servers.unix");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    42
        if (hasFtp())
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    43
            uris.add("ftp://ftp.is.co.za/rfc/rfc1808.txt");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    45
        for (String uriStr : uris) {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    46
            URI uri = new URI(uriStr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            URL url = uri.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            String scheme = uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            boolean schemeCheck = scheme == null? url.getProtocol() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                scheme.equals(url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            if (!schemeCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                throw new RuntimeException("uri.scheme is " + scheme +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                           " url.protocol is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                           url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            if (uri.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                String ssp = uri.getSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                boolean sspCheck = ssp == null? uri.getPath() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    ssp.equals(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                if (!sspCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    throw new RuntimeException("uri.ssp is " + ssp +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                           " url.path is " + url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                String authority = uri.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                boolean authorityCheck = authority == null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    url.getAuthority() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    authority.equals(url.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (!authorityCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    throw new RuntimeException("uri.authority is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                               authority + " url's is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                               url.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                String host = uri.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                boolean hostCheck = host == null ? url.getHost() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    host.equals(url.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                if (!hostCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    throw new RuntimeException("uri.host is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                               host + " url's is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                               url.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    String userInfo = uri.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    boolean userInfoCheck = userInfo == null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        url.getUserInfo() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        userInfo.equals(url.getUserInfo());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    if (uri.getPort() != url.getPort())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        throw new RuntimeException("uri.port is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                               uri.getPort() + " url's is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                               url.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                String path = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                boolean pathCheck = path == null? url.getPath() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    path.equals(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (!pathCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    throw new RuntimeException("uri.path is " + path +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                               " url.path is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                               url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                String query = uri.getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                boolean queryCheck = query == null? url.getQuery() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    query.equals(url.getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (!queryCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new RuntimeException("uri.query is " + query +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                               " url.query is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                               url.getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            String frag = uri.getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            boolean fragCheck = frag == null? url.getRef() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            frag.equals(url.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (!fragCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    throw new RuntimeException("uri.frag is " + frag +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                               " url.ref is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                               url.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   116
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   117
    private static boolean hasFtp() {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   118
        try {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   119
            return new java.net.URL("ftp://") != null;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   120
        } catch (java.net.MalformedURLException x) {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   121
            System.out.println("FTP not supported by this runtime.");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   122
            return false;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   123
        }
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   124
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}