jdk/test/java/net/URL/URIToURLTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class URIToURLTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
        String[] uris = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
           "http://jag:cafebabe@java.sun.com:94/b/c/d?q#g",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
           "http://[1080:0:0:0:8:800:200C:417A]/index.html",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
           "http://a/b/c/d;p?q",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
           "ftp://ftp.is.co.za/rfc/rfc1808.txt",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
           "mailto:mduerst@ifi.unizh.ch", // opaque url
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
           "http:comp.infosystems.www.servers.unix" //opaque url
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        for (int i = 0; i < uris.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            URI uri = new URI(uris[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            URL url = uri.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            String scheme = uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            boolean schemeCheck = scheme == null? url.getProtocol() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                scheme.equals(url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            if (!schemeCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                throw new RuntimeException("uri.scheme is " + scheme +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                           " url.protocol is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                           url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (uri.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                String ssp = uri.getSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                boolean sspCheck = ssp == null? uri.getPath() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    ssp.equals(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                if (!sspCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    throw new RuntimeException("uri.ssp is " + ssp +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                           " url.path is " + url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                String authority = uri.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                boolean authorityCheck = authority == null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    url.getAuthority() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    authority.equals(url.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                if (!authorityCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    throw new RuntimeException("uri.authority is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                               authority + " url's is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                               url.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                String host = uri.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                boolean hostCheck = host == null ? url.getHost() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    host.equals(url.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (!hostCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    throw new RuntimeException("uri.host is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                               host + " url's is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                               url.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    String userInfo = uri.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    boolean userInfoCheck = userInfo == null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        url.getUserInfo() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        userInfo.equals(url.getUserInfo());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    if (uri.getPort() != url.getPort())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        throw new RuntimeException("uri.port is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                               uri.getPort() + " url's is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                               url.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                String path = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                boolean pathCheck = path == null? url.getPath() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    path.equals(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (!pathCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    throw new RuntimeException("uri.path is " + path +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                               " url.path is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                               url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                String query = uri.getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                boolean queryCheck = query == null? url.getQuery() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    query.equals(url.getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if (!queryCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    throw new RuntimeException("uri.query is " + query +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                               " url.query is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                               url.getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            String frag = uri.getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            boolean fragCheck = frag == null? url.getRef() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            frag.equals(url.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (!fragCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    throw new RuntimeException("uri.frag is " + frag +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                               " url.ref is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                               url.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}