jdk/test/java/net/URL/Test.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
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14215
diff changeset
     2
 * Copyright (c) 2001, 2012, 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
 * @summary Unit test for java.net.URL (Based on the URI tests that is authored by Mark Reinhold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4496251
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static PrintStream out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static int testCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // Properties that we check
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int PARSEFAIL   = 1 << 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final int PROTOCOL    = 1 << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final int USERINFO    = 1 << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final int HOST        = 1 << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final int PORT        = 1 << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final int PATH        = 1 << 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final int QUERY       = 1 << 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final int REF         = 1 << 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    String input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    URL originalURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    URL base = null;                    // Base for resolution/relativization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    String op = null;                   // Op performed if url != originalURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    int checked = 0;                    // Mask for checked properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int failed = 0;                     // Mask for failed properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    Exception exc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Test(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        input = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            url = new URL(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } catch (MalformedURLException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            exc = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        originalURL = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static Test test(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return new Test(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Test(String s, boolean xxx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            url = new URL(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            exc = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (url != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            input = url.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        originalURL = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static Test test(URL base, String spec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return new Test(base, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private Test(URL base, String spec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            url = new URL(base, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            exc = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (url != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            input = url.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        originalURL = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   static Test test(String protocol, String host, int port, String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return new Test(protocol, host, port, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private Test(String protocol, String host, int port, String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            url = new URL(protocol, host, port, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            exc = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (url != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            input = url.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        originalURL = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    boolean parsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return url != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    boolean resolved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return base != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    URL url() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // Operations on Test instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // These are short so as to make test cases compact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    //    s      Scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    //    u      User info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    //    h      Host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    //    n      port Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    //    p      Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    //    q      Query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    //    f      Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    //    rslv   Resolve against given base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    //    rtvz   Relativize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    //    psa    Parse server Authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    //    norm   Normalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    //    x      Check that parse failed as expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    //    z      End -- ensure that unchecked components are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private boolean check1(int prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        checked |= prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (!parsed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            failed |= prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private void check2(String s, String ans, int prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (s == null && ans == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if ((s == null) || !s.equals(ans))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            failed |= prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    Test s(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (check1(PROTOCOL)) check2(url.getProtocol(), s, PROTOCOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    Test u(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (check1(USERINFO)) check2(url.getUserInfo(), s, USERINFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    Test h(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (check1(HOST)) check2(url.getHost(), s, HOST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    Test n(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        checked |= PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!parsed() || (url.getPort() != n))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            failed |= PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    Test p(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (check1(PATH)) check2(url.getPath(), s, PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    Test q(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (check1(QUERY)) check2(url.getQuery(), s, QUERY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    Test f(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (check1(REF)) check2(url.getRef(), s, REF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    Test x() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        checked |= PARSEFAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (parsed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            failed |= PARSEFAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private void checkEmpty(String s, int prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (((checked & prop) == 0) && (s != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            failed |= prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // Check that unchecked component properties are not defined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // and report any failures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    Test z() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (!parsed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            report();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        checkEmpty(url.getProtocol(), PROTOCOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        checkEmpty(url.getUserInfo(), USERINFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        checkEmpty(url.getHost(), HOST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (((checked & PORT) == 0) && (url.getPort() != -1)) failed |= PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        checkEmpty(url.getPath(), PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        checkEmpty(url.getQuery(), QUERY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        checkEmpty(url.getRef(), REF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        report();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // Summarization and reporting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    static void header(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        out.println("-- " + s + " --");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    static void show(String prefix, MalformedURLException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        out.println(prefix + ": " + x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private void summarize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (input.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            sb.append("\"\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            sb.append(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (base != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            sb.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            sb.append(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (!parsed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            String s = (((checked & PARSEFAIL) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        ? "Correct exception" : "UNEXPECTED EXCEPTION");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (exc instanceof MalformedURLException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                show(s, (MalformedURLException)exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                out.println(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                out.print(s + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                exc.printStackTrace(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (url != originalURL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                sb.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                sb.append(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                sb.append(" --> ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                sb.append(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            out.println(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    static void show(String n, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        out.println("  " + n + "          = ".substring(n.length()) + v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public static void show(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        show("scheme", u.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        show("authority", u.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        show("userInfo", u.getUserInfo());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        show("host", u.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        show("port", "" + u.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        show("path", u.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        show("query", u.getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        show("ref", u.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private void report() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        summarize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (failed == 0) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        sb.append("FAIL:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if ((failed & PARSEFAIL) != 0) sb.append(" parsefail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ((failed & PROTOCOL) != 0) sb.append(" scheme");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if ((failed & USERINFO) != 0) sb.append(" userinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if ((failed & HOST) != 0) sb.append(" host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if ((failed & PORT) != 0) sb.append(" port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if ((failed & PATH) != 0) sb.append(" path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if ((failed & QUERY) != 0) sb.append(" query");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if ((failed & REF) != 0) sb.append(" fragment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        out.println(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (url != null) show(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        throw new RuntimeException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   313
    private static boolean hasFtp() {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   314
        try {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   315
            return new java.net.URL("ftp://") != null;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   316
        } catch (java.net.MalformedURLException x) {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   317
            System.out.println("FTP not supported by this runtime.");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   318
            return false;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   319
        }
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   320
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // -- Tests --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    static void rfc2396() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        header("RFC2396: Basic examples");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   329
        if (hasFtp())
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   330
            test("ftp://ftp.is.co.za/rfc/rfc1808.txt")
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   331
                .s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        test("http://www.math.uio.no/faq/compression-faq/part1.html")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            .s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        test("http://www.w3.org/Addressing/")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            .s("http").h("www.w3.org").p("/Addressing/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   339
        if (hasFtp())
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   340
            test("ftp://ds.internic.net/rfc/")
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 14342
diff changeset
   341
                .s("ftp").h("ds.internic.net").p("/rfc/").z();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        test("http://www.ics.uci.edu/pub/ietf/url/historical.html#WARNING")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            .s("http").h("www.ics.uci.edu").p("/pub/ietf/url/historical.html")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            .f("WARNING").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        test("http://www.ics.uci.edu/pub/ietf/url/#Related")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            .s("http").h("www.ics.uci.edu").p("/pub/ietf/url/")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            .f("Related").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        test("file:/home/someone/dir1/dir2/file").s("file").h("").p("/home/someone/dir1/dir2/file").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        header("RFC2396: Normal relative-URL examples (appendix C)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        URL base = (test("http://a/b/c/d;p?q")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    .s("http").h("a").p("/b/c/d;p").q("q").z().url());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // g:h       g:h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // test(base, "http:h").s("g").p("h").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // g         http://a/b/c/g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        test(base, "g").s("http").h("a").p("/b/c/g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // ./g       http://a/b/c/g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        test(base, "./g").s("http").h("a").p("/b/c/g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // g/        http://a/b/c/g/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        test(base, "g/").s("http").h("a").p("/b/c/g/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // /g        http://a/g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        test(base, "/g").s("http").h("a").p("/g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // //g       http://g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        test(base,"//g").s("http").h("g").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // ?y        http://a/b/c/?y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        test(base, "?y").s("http").h("a").p("/b/c/").q("y").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // g?y       http://a/b/c/g?y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        test(base, "g?y").s("http").h("a").p("/b/c/g").q("y").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // #s        (current document)#s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // DEVIATION: Lone fragment parses as relative URL with empty path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // and resolves without removing the last segment of the base path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        // test(base,"#s").s("http").h("a").p("/b/c/d;p").f("s").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        test(base,"#s").s("http").h("a").p("/b/c/d;p").q("q").f("s").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // g#s       http://a/b/c/g#s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        test(base, "g#s").s("http").h("a").p("/b/c/g").f("s").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // g?y#s     http://a/b/c/g?y#s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        test(base,"g?y#s").s("http").h("a").p("/b/c/g").q("y").f("s").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // ;x        http://a/b/c/;x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        test(base,";x").s("http").h("a").p("/b/c/;x").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // g;x       http://a/b/c/g;x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        test(base,"g;x").s("http").h("a").p("/b/c/g;x").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // g;x?y#s   http://a/b/c/g;x?y#s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        test(base,"g;x?y#s").s("http").h("a").p("/b/c/g;x").q("y").f("s").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // .         http://a/b/c/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        test(base,".").s("http").h("a").p("/b/c/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // ./        http://a/b/c/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        test(base,"./").s("http").h("a").p("/b/c/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // ..        http://a/b/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        test(base,"..").s("http").h("a").p("/b/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // ../       http://a/b/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        test(base,"../").s("http").h("a").p("/b/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // ../g      http://a/b/g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        test(base,"../g").s("http").h("a").p("/b/g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // ../..     http://a/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        test(base,"../..").s("http").h("a").p("/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        // ../../    http://a/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        test(base,"../../").s("http").h("a").p("/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // ../../g   http://a/g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        test(base,"../../g").s("http").h("a").p("/g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // http://u@s1/p1 http://s2/p2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        test(test("http://u:p@s1/p1").url(),"http://s2/p2")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
             .s("http").h("s2").u(null).p("/p2").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        header("RFC2396: Abnormal relative-URL examples (appendix C)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        // ../../../g    =  http://a/../g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        test(base,"../../../g").s("http").h("a").p("/../g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // ../../../../g =  http://a/../../g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        test(base, "../../../../g").s("http").h("a").p("/../../g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // /./g          =  http://a/./g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        test(base,"/./g").s("http").h("a").p("/./g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // /../g         =  http://a/../g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        test(base,"/../g").s("http").h("a").p("/../g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // g.            =  http://a/b/c/g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        test(base,"g.").s("http").h("a").p("/b/c/g.").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // .g            =  http://a/b/c/.g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        test(base,".g").s("http").h("a").p("/b/c/.g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // g..           =  http://a/b/c/g..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        test(base,"g..").s("http").h("a").p("/b/c/g..").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // ..g           =  http://a/b/c/..g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        test(base,"..g").s("http").h("a").p("/b/c/..g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // ./../g        =  http://a/b/g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        test(base,"./../g").s("http").h("a").p("/b/g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // ./g/.         =  http://a/b/c/g/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        test(base,"./g/.").s("http").h("a").p("/b/c/g/").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // g/./h         =  http://a/b/c/g/h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        test(base,"g/./h").s("http").h("a").p("/b/c/g/h").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        // g/../h        =  http://a/b/c/h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        test(base,"g/../h").s("http").h("a").p("/b/c/h").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // g;x=1/./y     =  http://a/b/c/g;x=1/y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        test(base,"g;x=1/./y").s("http").h("a").p("/b/c/g;x=1/y").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // g;x=1/../y    =  http://a/b/c/y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        test(base,"g;x=1/../y").s("http").h("a").p("/b/c/y").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // g?y/./x       =  http://a/b/c/g?y/./x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        test(base,"g?y/./x").s("http").h("a").p("/b/c/g").q("y/./x").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // g?y/../x      =  http://a/b/c/g?y/../x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        test(base,"g?y/../x").s("http").h("a").p("/b/c/g").q("y/../x").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // g#s/./x       =  http://a/b/c/g#s/./x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        test(base,"g#s/./x").s("http").h("a").p("/b/c/g").f("s/./x").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        // g#s/../x      =  http://a/b/c/g#s/../x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        test(base,"g#s/../x").s("http").h("a").p("/b/c/g").f("s/../x").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // http:g        =  http:g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // test(base,"http:g").s("http").p("g").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    static void ip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        header("IP addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        test("http://1.2.3.4:5")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            .s("http").h("1.2.3.4").n(5).p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // From RFC2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            .s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            .n(80).p("/index.html").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210%12]:80/index.html")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            .s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210%12]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            .n(80).p("/index.html").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        test("http://[1080:0:0:0:8:800:200C:417A]/index.html")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            .s("http").h("[1080:0:0:0:8:800:200C:417A]").p("/index.html").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        test("http://[1080:0:0:0:8:800:200C:417A%1]/index.html")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            .s("http").h("[1080:0:0:0:8:800:200C:417A%1]").p("/index.html").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        test("http://[3ffe:2a00:100:7031::1]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            .s("http").h("[3ffe:2a00:100:7031::1]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        test("http://[1080::8:800:200C:417A]/foo")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            .s("http").h("[1080::8:800:200C:417A]").p("/foo").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        test("http://[::192.9.5.5]/ipng")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            .s("http").h("[::192.9.5.5]").p("/ipng").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        test("http://[::192.9.5.5%interface]/ipng")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            .s("http").h("[::192.9.5.5%interface]").p("/ipng").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        test("http://[::FFFF:129.144.52.38]:80/index.html")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            .s("http").h("[::FFFF:129.144.52.38]").n(80).p("/index.html").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        test("http://[2010:836B:4179::836B:4179]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            .s("http").h("[2010:836B:4179::836B:4179]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // From RFC2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        test("http://[FF01::101]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            .s("http").h("[FF01::101]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        test("http://[::1]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            .s("http").h("[::1]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        test("http://[::]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            .s("http").h("[::]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        test("http://[::%hme0]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            .s("http").h("[::%hme0]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        test("http://[0:0:0:0:0:0:13.1.68.3]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            .s("http").h("[0:0:0:0:0:0:13.1.68.3]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        test("http://[0:0:0:0:0:FFFF:129.144.52.38]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            .s("http").h("[0:0:0:0:0:FFFF:129.144.52.38]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        test("http://[0:0:0:0:0:FFFF:129.144.52.38%33]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            .s("http").h("[0:0:0:0:0:FFFF:129.144.52.38%33]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        test("http://[::13.1.68.3]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            .s("http").h("[::13.1.68.3]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        test("http://[::13.1.68.3]:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            .s("http").h("[::13.1.68.3]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // Error cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        test("http://[ff01:234/foo").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        test("http://[ff01:234:zzz]/foo").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        test("http://[foo]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        test("http://[]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        test("http://[129.33.44.55]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        test("http://[ff:ee:dd::cc:bb::aa:9:8]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        test("http://[1:2:3:4:5:6:7:8%]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        test("http://[1:2:3:4:5:6:7:8%!/]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        test("http://[1:2:3:4:5:6:7:8:9]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        test("http://[::1.2.3.300]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        test("http://[1.2.3.4:5]").x().z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // Optional IPv6 brackets in constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        test("http", "1:2:3:4:5:6:7:8", -1, "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            .s("http").h("[1:2:3:4:5:6:7:8]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        test("http", "1:2:3:4:5:6:7:8%hme0", -1, "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            .s("http").h("[1:2:3:4:5:6:7:8%hme0]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        test("http", "[1:2:3:4:5:6:7:8]", -1, "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            .s("http").h("[1:2:3:4:5:6:7:8]").p("").z();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    static void serial() throws IOException, MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        header("Serialization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        ObjectOutputStream oo = new ObjectOutputStream(bo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        URL u = new URL("http://java.sun.com/jdk/1.4?release#beta");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        oo.writeObject(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        oo.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        ObjectInputStream oi = new ObjectInputStream(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            Object o = oi.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            u.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            throw new RuntimeException(x.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    static void tests() throws IOException, MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        rfc2396();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        ip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        serial();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    // -- Command-line invocation --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        out.println("Usage:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        out.println("  java Test               --  Runs all tests in this file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        out.println("  java Test <url>         --  Parses url, shows components");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        out.println("  java Test <base> <url>  --  Parses url and base, then resolves");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        out.println("                              url against base");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        switch (args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            tests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            out.println("Test cases: " + testCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            if (args[0].equals("-help")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            // clargs(null, args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            // clargs(args[0], args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
}