jdk/test/java/net/URLConnection/RequestPropertyValues.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 15528 18f0bac88177
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 15528
diff changeset
     2
 * Copyright (c) 2002, 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 4644775 6230836
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test URLConnection Request Proterties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    30
import java.net.MalformedURLException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URLConnection;
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    33
import java.util.ArrayList;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    34
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Part1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *   bug 4644775: Unexpected NPE in setRequestProperty(key, null) call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Part2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   bug 6230836: A few methods of class URLConnection implemented incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class RequestPropertyValues {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        part1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        part2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void part1() throws Exception {
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    51
        List<URL> urls = new ArrayList<>();
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    52
        urls.add(new URL("http://localhost:8088"));
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    53
        urls.add(new URL("file:/etc/passwd"));
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    54
        urls.add(new URL("jar:http://foo.com/bar.html!/foo/bar"));
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    55
        if (hasFtp())
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    56
            urls.add(new URL("ftp://foo:bar@foobar.com/etc/passwd"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    60
        for (URL url : urls) {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    61
            URLConnection uc = url.openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                uc.setRequestProperty("TestHeader", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                System.out.println("setRequestProperty is throwing NPE" +
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    66
                                " for url: " + url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                uc.addRequestProperty("TestHeader", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                System.out.println("addRequestProperty is throwing NPE" +
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
    73
                                " for url: " + url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new Exception("RequestProperty setting/adding is" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                " throwing NPE for null values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static void part2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String[] goodKeys = {"", "$", "key", "Key", " ", "    "};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String[] goodValues = {"", "$", "value", "Value", " ", "    "};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        URLConnection conn = getConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int i = 0; i < goodKeys.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            for (int j = 0; j < goodValues.length; ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                // If a property with the key already exists, overwrite its value with the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                conn.setRequestProperty(goodKeys[i], goodValues[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                String value = conn.getRequestProperty(goodKeys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static URLConnection getConnection(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return new DummyURLConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static class DummyURLConnection extends URLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        DummyURLConnection(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            super(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public void connect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
15528
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   117
    private static boolean hasFtp() {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   118
        try {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   119
            return new java.net.URL("ftp://") != null;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   120
        } catch (java.net.MalformedURLException x) {
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   121
            System.out.println("FTP not supported by this runtime.");
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   122
            return false;
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   123
        }
18f0bac88177 8007322: untangle ftp protocol from general networking URL tests
chegar
parents: 5506
diff changeset
   124
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}