1/*
2 * Copyright (c) 2001, 2013, 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/**
25 * @test
26 * @bug 4485208
27 * @summary  file: and ftp: URL handlers need to throw NPE in setRequestProperty
28 */
29
30import java.net.*;
31import java.util.ArrayList;
32import java.util.List;
33
34public class RequestProperties {
35    static int failed;
36
37    public static void main (String args[]) throws Exception {
38        List<String> urls = new ArrayList<>();
39        urls.add("http://foo.com/bar/");
40        urls.add("jar:http://foo.com/bar.html!/foo/bar");
41        urls.add("file:/etc/passwd");
42        if (hasFtp())
43            urls.add("ftp://foo:bar@foobar.com/etc/passwd");
44
45        for (String urlStr : urls)
46            test(new URL(urlStr));
47
48        if (failed != 0)
49            throw new RuntimeException(failed + " errors") ;
50    }
51
52    static void test(URL url) throws Exception {
53        URLConnection urlc = url.openConnection();
54        try {
55            urlc.setRequestProperty(null, null);
56            System.out.println(url.getProtocol()
57                               + ": setRequestProperty(null,) did not throw NPE");
58            failed++;
59        } catch (NullPointerException e) { /* Expected */ }
60        try {
61            urlc.addRequestProperty(null, null);
62            System.out.println(url.getProtocol()
63                               + ": addRequestProperty(null,) did not throw NPE");
64            failed++;
65        } catch (NullPointerException e)  { /* Expected */ }
66
67        if (urlc.getRequestProperty(null) != null) {
68            System.out.println(url.getProtocol()
69                               + ": getRequestProperty(null,) did not return null");
70            failed++;
71        }
72    }
73
74    private static boolean hasFtp() {
75        try {
76            return new java.net.URL("ftp://") != null;
77        } catch (java.net.MalformedURLException x) {
78            System.out.println("FTP not supported by this runtime.");
79            return false;
80        }
81    }
82}
83