Path: blob/master/test/jdk/java/net/URLConnection/SetDefaultUseCaches.java
41149 views
/*1* Copyright (c) 2016, 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/* @test24* @bug 8163449 817526125* @summary Allow per protocol setting for URLConnection defaultUseCaches26* @run testng/othervm SetDefaultUseCaches27*/2829import java.io.IOException;30import java.io.UncheckedIOException;31import java.net.URL;32import java.net.URLConnection;33import org.testng.annotations.Test;34import static org.testng.Assert.*;3536public class SetDefaultUseCaches {3738final URL fileURL = uncheckURL("file:///a/b.txt");39final URL httpURL = uncheckURL("http://www.foo.com/");40final URL jarFileURL = uncheckURL("jar:file:///a/b.jar!/anEntry");41final URL jarHttpURL = uncheckURL("jar:http://www.foo.com/a/b.jar!/anEntry");4243@Test44public void test() throws Exception {45// check JAR both before and after other protocol tests as JAR URLs46// effectively wrap/embed other URLs. The syntax is jar:<url>!/{entry}47checkJAR(true);48checkJAR(false);49checkJAR(true);5051checkHTTP();52checkFile();5354// ensure that JAR URLs still respect their per-protocol value55checkJAR(false);56checkJAR(true);57checkJAR(false);58}5960void checkHTTP() throws IOException {61// check default default is true62URLConnection httpURLConn = httpURL.openConnection();63assertTrue(httpURLConn.getDefaultUseCaches());6465// set default for http to false and check66URLConnection.setDefaultUseCaches("HTTP", false);6768httpURLConn = httpURL.openConnection();69assertTrue(httpURLConn.getDefaultUseCaches());70assertFalse(httpURLConn.getUseCaches());71assertFalse(URLConnection.getDefaultUseCaches("http"));72}7374void checkFile() throws IOException {75URLConnection fileURLConn = fileURL.openConnection();76assertTrue(fileURLConn.getDefaultUseCaches());7778// set default default to false and check other values the same79fileURLConn.setDefaultUseCaches(false);80fileURLConn.setDefaultUseCaches("fiLe", true);81assertFalse(fileURLConn.getDefaultUseCaches());82assertTrue(URLConnection.getDefaultUseCaches("fiLE"));83}8485void checkJAR(boolean defaultValue) throws IOException {86URLConnection.setDefaultUseCaches("JAR", defaultValue);87assertEquals(URLConnection.getDefaultUseCaches("JAr"), defaultValue);8889URLConnection jarFileURLConn = jarFileURL.openConnection();90URLConnection jarHttpURLConn = jarHttpURL.openConnection();91assertEquals(jarFileURLConn.getUseCaches(), defaultValue);92assertEquals(jarHttpURLConn.getUseCaches(), defaultValue);93jarFileURLConn.setUseCaches(!defaultValue);94jarHttpURLConn.setUseCaches(!defaultValue);95assertEquals(jarFileURLConn.getUseCaches(), !defaultValue);96assertEquals(jarHttpURLConn.getUseCaches(), !defaultValue);9798URLConnection.setDefaultUseCaches("JaR", !defaultValue); // case-insensitive99assertEquals(URLConnection.getDefaultUseCaches("jAR"), !defaultValue);100101jarFileURLConn = jarFileURL.openConnection();102jarHttpURLConn = jarHttpURL.openConnection();103assertEquals(jarFileURLConn.getUseCaches(), !defaultValue);104assertEquals(jarHttpURLConn.getUseCaches(), !defaultValue);105jarFileURLConn.setUseCaches(defaultValue);106jarHttpURLConn.setUseCaches(defaultValue);107assertEquals(jarFileURLConn.getUseCaches(), defaultValue);108assertEquals(jarHttpURLConn.getUseCaches(), defaultValue);109}110111static URL uncheckURL(String url) {112try { return new URL(url); }113catch (IOException e) { throw new UncheckedIOException(e); }114}115}116117118