1/*
2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4052976 7030649
27 * @summary Test URL.equals with anchors, and jar URLs
28 */
29
30import java.net.*;
31
32public class Equals {
33    public static void main(String[] args) throws Exception {
34        anchors();
35        jarURLs();
36    }
37
38    static void anchors() throws Exception {
39        URL url1, url2;
40
41        url1 = new URL(null, "http://JavaSoft/Test#bar");
42        url2 = new URL(null, "http://JavaSoft/Test");
43
44        if (url1.equals(url2))
45            throw new RuntimeException("URL.equals fails with anchors");
46        if (url2.equals(url1))
47            throw new RuntimeException("URL.equals fails with anchors");
48        if (url1.equals(null))
49            throw new RuntimeException("URL.equals fails given null");
50    }
51
52    static final String HTTP_URL1A = "http://localhost/xyz";
53    static final String HTTP_URL1B = "http://LOCALHOST/xyz";
54    static final String FILE_URL1A = "file:///c:/foo/xyz";
55    static final String FILE_URL1B = "file:/c:/foo/xyz";
56
57    static void jarURLs() throws Exception {
58        int failed = 0;
59        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1A, "!/abc", "!/abc", true);
60        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/abc", true);
61        failed = compareJarURLS(HTTP_URL1B, HTTP_URL1A, "!/", "!/", true);
62        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/", false);
63        failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/xy", false);
64        failed = compareJarURLS(FILE_URL1A, FILE_URL1A, "!/abc", "!/abc", true);
65        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/abc", true);
66        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/", "!/", true);
67        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/", false);
68        failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/xy", false);
69
70        failed = (new URL("jar:file://xzy!/abc")).equals(
71                      new URL("file://xzy!/abc")) ? 1 : 0;
72
73        if (failed > 0)
74            throw new RuntimeException("Some jar URL tests failed. Check output");
75    }
76
77    static int compareJarURLS(String urlStr1, String urlStr2,
78                                  String entry1,  String entry2,
79                                  boolean expectEqual) throws Exception {
80        int failed = 0;
81
82        URL url1 = new URL(urlStr1);
83        URL url2 = new URL(urlStr2);
84
85        if (!url1.equals(url2)) {
86            System.out.println("Urls are not equal, so the test cannot run.");
87            System.out.println("url1: " + url1 + ", url2:" + url2);
88            return 1;
89        }
90
91        URL jarUrl1 = new URL("jar:" + urlStr1 + entry1);
92        URL jarUrl2 = new URL("jar:" + urlStr2 + entry2);
93        jarUrl2.openConnection();
94
95        boolean equal = jarUrl1.equals(jarUrl2);
96        if (expectEqual && !equal) {
97            System.out.println("URLs should be equal, but are not. " +
98                                jarUrl1 + ", " + jarUrl2);
99            failed++;
100        } else if (!expectEqual && equal) {
101            System.out.println("URLs should NOT be equal, but are. " +
102                                jarUrl1 + ", " + jarUrl2);
103            failed++;
104        }
105
106        if (expectEqual) {
107            // hashCode MUST produce the same integer result for equal urls
108            int hash1 = jarUrl1.hashCode();
109            int hash2 = jarUrl2.hashCode();
110            if (hash1 != hash2) {
111                System.out.println("jarUrl1.hashCode = " + hash1);
112                System.out.println("jarUrl2.hashCode = " + hash2);
113                System.out.println("Equal urls should have same hashCode. " +
114                                    jarUrl1 + ", " + jarUrl2);
115                failed++;
116            }
117        }
118
119        return failed;
120    }
121}
122