jdk/test/java/net/URLConnection/RequestProperties.java
author mikael
Fri, 04 Dec 2015 15:08:49 -0800
changeset 35090 1f5b6aa795d0
parent 16748 ed60b6527f64
permissions -rw-r--r--
8144748: Move assembler/macroAssembler inline function definitions to corresponding inline.hpp files Reviewed-by: kvn, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16748
ed60b6527f64 8012048: JDK8 b85 source with GPL header errors
katleman
parents: 15528
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4485208
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  file: and ftp: URL handlers need to throw NPE in setRequestProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    31
import java.util.ArrayList;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    32
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class RequestProperties {
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    35
    static int failed;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main (String args[]) throws Exception {
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    38
        List<String> urls = new ArrayList<>();
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    39
        urls.add("http://foo.com/bar/");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    40
        urls.add("jar:http://foo.com/bar.html!/foo/bar");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    41
        urls.add("file:/etc/passwd");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    42
        if (hasFtp())
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    43
            urls.add("ftp://foo:bar@foobar.com/etc/passwd");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    44
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    45
        for (String urlStr : urls)
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    46
            test(new URL(urlStr));
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    47
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    48
        if (failed != 0)
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    49
            throw new RuntimeException(failed + " errors") ;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    50
    }
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    51
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    52
    static void test(URL url) throws Exception {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    53
        URLConnection urlc = url.openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    55
            urlc.setRequestProperty(null, null);
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    56
            System.out.println(url.getProtocol()
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    57
                               + ": setRequestProperty(null,) did not throw NPE");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    58
            failed++;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    59
        } catch (NullPointerException e) { /* Expected */ }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    61
            urlc.addRequestProperty(null, null);
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    62
            System.out.println(url.getProtocol()
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    63
                               + ": addRequestProperty(null,) did not throw NPE");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    64
            failed++;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    65
        } catch (NullPointerException e)  { /* Expected */ }
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    66
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    67
        if (urlc.getRequestProperty(null) != null) {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    68
            System.out.println(url.getProtocol()
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    69
                               + ": getRequestProperty(null,) did not return null");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    70
            failed++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    72
    }
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    73
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    74
    private static boolean hasFtp() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    76
            return new java.net.URL("ftp://") != null;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    77
        } catch (java.net.MalformedURLException x) {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    78
            System.out.println("FTP not supported by this runtime.");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    79
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
}