Path: blob/master/test/jdk/java/net/URL/Equals.java
41149 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4052976 703064926* @summary Test URL.equals with anchors, and jar URLs27*/2829import java.net.*;3031public class Equals {32public static void main(String[] args) throws Exception {33anchors();34jarURLs();35}3637static void anchors() throws Exception {38URL url1, url2;3940url1 = new URL(null, "http://JavaSoft/Test#bar");41url2 = new URL(null, "http://JavaSoft/Test");4243if (url1.equals(url2))44throw new RuntimeException("URL.equals fails with anchors");45if (url2.equals(url1))46throw new RuntimeException("URL.equals fails with anchors");47if (url1.equals(null))48throw new RuntimeException("URL.equals fails given null");49}5051static final String HTTP_URL1A = "http://localhost/xyz";52static final String HTTP_URL1B = "http://LOCALHOST/xyz";53static final String FILE_URL1A = "file:///c:/foo/xyz";54static final String FILE_URL1B = "file:/c:/foo/xyz";5556static void jarURLs() throws Exception {57int failed = 0;58failed = compareJarURLS(HTTP_URL1A, HTTP_URL1A, "!/abc", "!/abc", true);59failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/abc", true);60failed = compareJarURLS(HTTP_URL1B, HTTP_URL1A, "!/", "!/", true);61failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/", false);62failed = compareJarURLS(HTTP_URL1A, HTTP_URL1B, "!/abc", "!/xy", false);63failed = compareJarURLS(FILE_URL1A, FILE_URL1A, "!/abc", "!/abc", true);64failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/abc", true);65failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/", "!/", true);66failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/", false);67failed = compareJarURLS(FILE_URL1A, FILE_URL1B, "!/abc", "!/xy", false);6869failed = (new URL("jar:file://xzy!/abc")).equals(70new URL("file://xzy!/abc")) ? 1 : 0;7172if (failed > 0)73throw new RuntimeException("Some jar URL tests failed. Check output");74}7576static int compareJarURLS(String urlStr1, String urlStr2,77String entry1, String entry2,78boolean expectEqual) throws Exception {79int failed = 0;8081URL url1 = new URL(urlStr1);82URL url2 = new URL(urlStr2);8384if (!url1.equals(url2)) {85System.out.println("Urls are not equal, so the test cannot run.");86System.out.println("url1: " + url1 + ", url2:" + url2);87return 1;88}8990URL jarUrl1 = new URL("jar:" + urlStr1 + entry1);91URL jarUrl2 = new URL("jar:" + urlStr2 + entry2);92jarUrl2.openConnection();9394boolean equal = jarUrl1.equals(jarUrl2);95if (expectEqual && !equal) {96System.out.println("URLs should be equal, but are not. " +97jarUrl1 + ", " + jarUrl2);98failed++;99} else if (!expectEqual && equal) {100System.out.println("URLs should NOT be equal, but are. " +101jarUrl1 + ", " + jarUrl2);102failed++;103}104105if (expectEqual) {106// hashCode MUST produce the same integer result for equal urls107int hash1 = jarUrl1.hashCode();108int hash2 = jarUrl2.hashCode();109if (hash1 != hash2) {110System.out.println("jarUrl1.hashCode = " + hash1);111System.out.println("jarUrl2.hashCode = " + hash2);112System.out.println("Equal urls should have same hashCode. " +113jarUrl1 + ", " + jarUrl2);114failed++;115}116}117118return failed;119}120}121122123