0N/A/*
4019N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
4019N/A * @bug 4052976 7030649
4019N/A * @summary Test URL.equals with anchors, and jar URLs
0N/A */
0N/A
0N/Aimport java.net.*;
0N/A
0N/Apublic class Equals {
4019N/A public static void main(String[] args) throws Exception {
4019N/A anchors();
4019N/A jarURLs();
4019N/A }
0N/A
4019N/A static void anchors() throws Exception {
0N/A URL url1, url2;
0N/A
0N/A url1 = new URL(null, "http://JavaSoft/Test#bar");
0N/A url2 = new URL(null, "http://JavaSoft/Test");
0N/A
0N/A if (url1.equals(url2))
0N/A throw new RuntimeException("URL.equals fails with anchors");
0N/A if (url2.equals(url1))
0N/A throw new RuntimeException("URL.equals fails with anchors");
0N/A if (url1.equals(null))
0N/A throw new RuntimeException("URL.equals fails given null");
0N/A }
4019N/A
4019N/A static final String HTTP_URL1A = "http://localhost/xyz";
4019N/A static final String HTTP_URL1B = "http://LOCALHOST/xyz";
4019N/A static final String FILE_URL1A = "file:///c:/foo/xyz";
4019N/A static final String FILE_URL1B = "file:/c:/foo/xyz";
4019N/A
4019N/A static void jarURLs() throws Exception {
4019N/A int failed = 0;
4019N/A failed = compareJarURLS(HTTP_URL1A, HTTP_URL1A, "!/abc", "!/abc", true);
4019N/A failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/abc", true);
4019N/A failed = compareJarURLS(HTTP_URL1B, HTTP_URL1A, "!/", "!/", true);
4019N/A failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/", false);
4019N/A failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/xy", false);
4019N/A failed = compareJarURLS(FILE_URL1A, FILE_URL1A, "!/abc", "!/abc", true);
4019N/A failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/abc", true);
4019N/A failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/", "!/", true);
4019N/A failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/", false);
4019N/A failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/xy", false);
4019N/A
4019N/A failed = (new URL("jar:file://xzy!/abc")).equals(
4019N/A new URL("file://xzy!/abc")) ? 1 : 0;
4019N/A
4019N/A if (failed > 0)
4019N/A throw new RuntimeException("Some jar URL tests failed. Check output");
4019N/A }
4019N/A
4019N/A static int compareJarURLS(String urlStr1, String urlStr2,
4019N/A String entry1, String entry2,
4019N/A boolean expectEqual) throws Exception {
4019N/A int failed = 0;
4019N/A
4019N/A URL url1 = new URL(urlStr1);
4019N/A URL url2 = new URL(urlStr2);
4019N/A
4019N/A if (!url1.equals(url2)) {
4019N/A System.out.println("Urls are not equal, so the test cannot run.");
4019N/A System.out.println("url1: " + url1 + ", url2:" + url2);
4019N/A return 1;
4019N/A }
4019N/A
4019N/A URL jarUrl1 = new URL("jar:" + urlStr1 + entry1);
4019N/A URL jarUrl2 = new URL("jar:" + urlStr2 + entry2);
4019N/A jarUrl2.openConnection();
4019N/A
4019N/A boolean equal = jarUrl1.equals(jarUrl2);
4019N/A if (expectEqual && !equal) {
4019N/A System.out.println("URLs should be equal, but are not. " +
4019N/A jarUrl1 + ", " + jarUrl2);
4019N/A failed++;
4019N/A } else if (!expectEqual && equal) {
4019N/A System.out.println("URLs should NOT be equal, but are. " +
4019N/A jarUrl1 + ", " + jarUrl2);
4019N/A failed++;
4019N/A }
4019N/A
4019N/A if (expectEqual) {
4019N/A // hashCode MUST produce the same integer result for equal urls
4019N/A int hash1 = jarUrl1.hashCode();
4019N/A int hash2 = jarUrl2.hashCode();
4019N/A if (hash1 != hash2) {
4019N/A System.out.println("jarUrl1.hashCode = " + hash1);
4019N/A System.out.println("jarUrl2.hashCode = " + hash2);
4019N/A System.out.println("Equal urls should have same hashCode. " +
4019N/A jarUrl1 + ", " + jarUrl2);
4019N/A failed++;
4019N/A }
4019N/A }
4019N/A
4019N/A return failed;
4019N/A }
0N/A}