1 /*
   2  * Copyright (c) 2016, 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 /* @test
  25  * @bug 8163449 8175261
  26  * @summary Allow per protocol setting for URLConnection defaultUseCaches
  27  * @run testng/othervm SetDefaultUseCaches
  28  */
  29 
  30 import java.io.IOException;
  31 import java.io.UncheckedIOException;
  32 import java.net.URL;
  33 import java.net.URLConnection;
  34 import org.testng.annotations.Test;
  35 import static org.testng.Assert.*;
  36 
  37 public class SetDefaultUseCaches {
  38 
  39     final URL fileURL = uncheckURL("file:///a/b.txt");
  40     final URL httpURL = uncheckURL("http://www.foo.com/");
  41     final URL jarFileURL = uncheckURL("jar:file:///a/b.jar!/anEntry");
  42     final URL jarHttpURL = uncheckURL("jar:http://www.foo.com/a/b.jar!/anEntry");
  43 
  44     @Test
  45     public void test() throws Exception {
  46         // check JAR both before and after other protocol tests as JAR URLs
  47         // effectively wrap/embed other URLs. The syntax is jar:<url>!/{entry}
  48         checkJAR(true);
  49         checkJAR(false);
  50         checkJAR(true);
  51 
  52         checkHTTP();
  53         checkFile();
  54 
  55         // ensure that JAR URLs still respect their per-protocol value
  56         checkJAR(false);
  57         checkJAR(true);
  58         checkJAR(false);
  59     }
  60 
  61     void checkHTTP() throws IOException {
  62         // check default default is true
  63         URLConnection httpURLConn = httpURL.openConnection();
  64         assertTrue(httpURLConn.getDefaultUseCaches());
  65 
  66         // set default for http to false and check
  67         URLConnection.setDefaultUseCaches("HTTP", false);
  68 
  69         httpURLConn = httpURL.openConnection();
  70         assertTrue(httpURLConn.getDefaultUseCaches());
  71         assertFalse(httpURLConn.getUseCaches());
  72         assertFalse(URLConnection.getDefaultUseCaches("http"));
  73     }
  74 
  75     void checkFile() throws IOException {
  76         URLConnection fileURLConn = fileURL.openConnection();
  77         assertTrue(fileURLConn.getDefaultUseCaches());
  78 
  79         // set default default to false and check other values the same
  80         fileURLConn.setDefaultUseCaches(false);
  81         fileURLConn.setDefaultUseCaches("fiLe", true);
  82         assertFalse(fileURLConn.getDefaultUseCaches());
  83         assertTrue(URLConnection.getDefaultUseCaches("fiLE"));
  84     }
  85 
  86     void checkJAR(boolean defaultValue) throws IOException {
  87         URLConnection.setDefaultUseCaches("JAR", defaultValue);
  88         assertEquals(URLConnection.getDefaultUseCaches("JAr"), defaultValue);
  89 
  90         URLConnection jarFileURLConn = jarFileURL.openConnection();
  91         URLConnection jarHttpURLConn = jarHttpURL.openConnection();
  92         assertEquals(jarFileURLConn.getUseCaches(), defaultValue);
  93         assertEquals(jarHttpURLConn.getUseCaches(), defaultValue);
  94         jarFileURLConn.setUseCaches(!defaultValue);
  95         jarHttpURLConn.setUseCaches(!defaultValue);
  96         assertEquals(jarFileURLConn.getUseCaches(), !defaultValue);
  97         assertEquals(jarHttpURLConn.getUseCaches(), !defaultValue);
  98 
  99         URLConnection.setDefaultUseCaches("JaR", !defaultValue); // case-insensitive
 100         assertEquals(URLConnection.getDefaultUseCaches("jAR"), !defaultValue);
 101 
 102         jarFileURLConn = jarFileURL.openConnection();
 103         jarHttpURLConn = jarHttpURL.openConnection();
 104         assertEquals(jarFileURLConn.getUseCaches(), !defaultValue);
 105         assertEquals(jarHttpURLConn.getUseCaches(), !defaultValue);
 106         jarFileURLConn.setUseCaches(defaultValue);
 107         jarHttpURLConn.setUseCaches(defaultValue);
 108         assertEquals(jarFileURLConn.getUseCaches(), defaultValue);
 109         assertEquals(jarHttpURLConn.getUseCaches(), defaultValue);
 110     }
 111 
 112     static URL uncheckURL(String url) {
 113         try { return new URL(url); }
 114         catch (IOException e) { throw new UncheckedIOException(e); }
 115     }
 116 }