test/jdk/java/net/URL/Equals.java
author chegar
Tue, 19 Jun 2018 09:13:58 +0100
branchhttp-client-branch
changeset 56771 73a6534bce94
parent 47216 71c04702a3d5
permissions -rw-r--r--
http-client-branch: prepare tests for TLS1.3 Contributed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9277
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2011, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
9277
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    26
 * @bug 4052976 7030649
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    27
 * @summary Test URL.equals with anchors, and jar URLs
2
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 Equals {
9277
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    33
    public static void main(String[] args) throws Exception {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    34
        anchors();
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    35
        jarURLs();
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    36
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
9277
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    38
    static void anchors() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        URL url1, url2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        url1 = new URL(null, "http://JavaSoft/Test#bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        url2 = new URL(null, "http://JavaSoft/Test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        if (url1.equals(url2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            throw new RuntimeException("URL.equals fails with anchors");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (url2.equals(url1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            throw new RuntimeException("URL.equals fails with anchors");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (url1.equals(null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            throw new RuntimeException("URL.equals fails given null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
9277
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    51
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    52
    static final String HTTP_URL1A = "http://localhost/xyz";
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    53
    static final String HTTP_URL1B = "http://LOCALHOST/xyz";
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    54
    static final String FILE_URL1A = "file:///c:/foo/xyz";
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    55
    static final String FILE_URL1B = "file:/c:/foo/xyz";
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    56
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    57
    static void jarURLs() throws Exception {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    58
        int failed = 0;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    59
        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1A, "!/abc", "!/abc", true);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    60
        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/abc", true);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    61
        failed = compareJarURLS(HTTP_URL1B, HTTP_URL1A, "!/", "!/", true);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    62
        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/", false);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    63
        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/xy", false);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    64
        failed = compareJarURLS(FILE_URL1A, FILE_URL1A, "!/abc", "!/abc", true);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    65
        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/abc", true);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    66
        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/", "!/", true);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    67
        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/", false);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    68
        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/xy", false);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    69
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    70
        failed = (new URL("jar:file://xzy!/abc")).equals(
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    71
                      new URL("file://xzy!/abc")) ? 1 : 0;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    72
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    73
        if (failed > 0)
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    74
            throw new RuntimeException("Some jar URL tests failed. Check output");
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    75
    }
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    76
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    77
    static int compareJarURLS(String urlStr1, String urlStr2,
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    78
                                  String entry1,  String entry2,
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    79
                                  boolean expectEqual) throws Exception {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    80
        int failed = 0;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    81
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    82
        URL url1 = new URL(urlStr1);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    83
        URL url2 = new URL(urlStr2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    84
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    85
        if (!url1.equals(url2)) {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    86
            System.out.println("Urls are not equal, so the test cannot run.");
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    87
            System.out.println("url1: " + url1 + ", url2:" + url2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    88
            return 1;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    89
        }
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    90
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    91
        URL jarUrl1 = new URL("jar:" + urlStr1 + entry1);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    92
        URL jarUrl2 = new URL("jar:" + urlStr2 + entry2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    93
        jarUrl2.openConnection();
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    94
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    95
        boolean equal = jarUrl1.equals(jarUrl2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    96
        if (expectEqual && !equal) {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    97
            System.out.println("URLs should be equal, but are not. " +
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    98
                                jarUrl1 + ", " + jarUrl2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
    99
            failed++;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   100
        } else if (!expectEqual && equal) {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   101
            System.out.println("URLs should NOT be equal, but are. " +
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   102
                                jarUrl1 + ", " + jarUrl2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   103
            failed++;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   104
        }
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   105
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   106
        if (expectEqual) {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   107
            // hashCode MUST produce the same integer result for equal urls
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   108
            int hash1 = jarUrl1.hashCode();
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   109
            int hash2 = jarUrl2.hashCode();
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   110
            if (hash1 != hash2) {
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   111
                System.out.println("jarUrl1.hashCode = " + hash1);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   112
                System.out.println("jarUrl2.hashCode = " + hash2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   113
                System.out.println("Equal urls should have same hashCode. " +
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   114
                                    jarUrl1 + ", " + jarUrl2);
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   115
                failed++;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   116
            }
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   117
        }
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   118
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   119
        return failed;
92ecd6edb959 7030649: URL.equals() fails to compare jar urls
chegar
parents: 5506
diff changeset
   120
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}