jdk/test/java/net/URLConnection/SetDefaultUseCaches.java
author michaelm
Thu, 12 Jan 2017 18:02:48 +0000
changeset 43070 2c327aa46246
child 43991 b2661845440c
permissions -rw-r--r--
8163449: Allow per protocol setting for URLConnection defaultUseCaches Reviewed-by: chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43070
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     1
/*
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     4
 *
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     8
 *
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    13
 * accompanied this code).
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    14
 *
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    18
 *
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    21
 * questions.
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    22
 */
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    23
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    24
/* @test
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    25
 * @bug 8163449
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    26
 * @summary Allow per protocol setting for URLConnection defaultUseCaches
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    27
 * @run main/othervm SetDefaultUseCaches
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    28
 */
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    29
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    30
import java.net.*;
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    31
import java.io.*;
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    32
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    33
public class SetDefaultUseCaches {
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    34
    static void testAssert(boolean value, boolean comparator) {
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    35
        if (value != comparator) {
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    36
            System.err.println("Expected " + comparator + " Got " + value);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    37
            throw new RuntimeException("Test failed:");
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    38
        } else
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    39
            System.err.println("OK");
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    40
    }
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    41
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    42
    public static void main(String s[]) throws Exception {
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    43
        URL url = new URL("http://www.foo.com/");
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    44
        URL url1 = new URL("file:///a/b.txt");
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    45
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    46
        // check default default is true
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    47
        URLConnection urlc = url.openConnection();
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    48
        testAssert(urlc.getDefaultUseCaches(), true);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    49
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    50
        // set default for http to false and check
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    51
        URLConnection.setDefaultUseCaches("HTTP", false);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    52
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    53
        urlc = url.openConnection();
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    54
        testAssert(urlc.getDefaultUseCaches(), true);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    55
        testAssert(urlc.getUseCaches(), false);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    56
        testAssert(URLConnection.getDefaultUseCaches("http"), false);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    57
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    58
        URLConnection urlc1 = url1.openConnection();
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    59
        testAssert(urlc1.getDefaultUseCaches(), true);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    60
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    61
        // set default default to false and check other values the same
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    62
        urlc.setDefaultUseCaches(false);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    63
        urlc1.setDefaultUseCaches("fiLe", true);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    64
        testAssert(urlc1.getDefaultUseCaches(), false);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    65
        testAssert(URLConnection.getDefaultUseCaches("fiLE"), true);
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    66
    }
2c327aa46246 8163449: Allow per protocol setting for URLConnection defaultUseCaches
michaelm
parents:
diff changeset
    67
}