blob: 6874063ca1db504fa65b66f47a4f5bb9d29999c3 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohair2283b9d2010-05-25 15:58:33 -07002 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/*
25 * @test
26 * @bug 4644775 6230836
27 * @summary Test URLConnection Request Proterties
28 */
29
chegar7ac25fd2013-02-02 17:15:13 +000030import java.net.MalformedURLException;
duke6e45e102007-12-01 00:00:00 +000031import java.net.URL;
32import java.net.URLConnection;
chegar7ac25fd2013-02-02 17:15:13 +000033import java.util.ArrayList;
34import java.util.List;
duke6e45e102007-12-01 00:00:00 +000035
36/**
37 * Part1:
38 * bug 4644775: Unexpected NPE in setRequestProperty(key, null) call
39 * Part2:
40 * bug 6230836: A few methods of class URLConnection implemented incorrectly
41 */
42
43public class RequestPropertyValues {
44
45 public static void main(String[] args) throws Exception {
46 part1();
47 part2();
48 }
49
50 public static void part1() throws Exception {
chegar7ac25fd2013-02-02 17:15:13 +000051 List<URL> urls = new ArrayList<>();
52 urls.add(new URL("http://localhost:8088"));
53 urls.add(new URL("file:/etc/passwd"));
54 urls.add(new URL("jar:http://foo.com/bar.html!/foo/bar"));
55 if (hasFtp())
56 urls.add(new URL("ftp://foo:bar@foobar.com/etc/passwd"));
duke6e45e102007-12-01 00:00:00 +000057
58 boolean failed = false;
59
chegar7ac25fd2013-02-02 17:15:13 +000060 for (URL url : urls) {
61 URLConnection uc = url.openConnection();
duke6e45e102007-12-01 00:00:00 +000062 try {
63 uc.setRequestProperty("TestHeader", null);
64 } catch (NullPointerException npe) {
65 System.out.println("setRequestProperty is throwing NPE" +
chegar7ac25fd2013-02-02 17:15:13 +000066 " for url: " + url);
duke6e45e102007-12-01 00:00:00 +000067 failed = true;
68 }
69 try {
70 uc.addRequestProperty("TestHeader", null);
71 } catch (NullPointerException npe) {
72 System.out.println("addRequestProperty is throwing NPE" +
chegar7ac25fd2013-02-02 17:15:13 +000073 " for url: " + url);
duke6e45e102007-12-01 00:00:00 +000074 failed = true;
75 }
76 }
77 if (failed) {
78 throw new Exception("RequestProperty setting/adding is" +
79 " throwing NPE for null values");
80 }
81 }
82
83 public static void part2() throws Exception {
84 URL url = null;
85 String[] goodKeys = {"", "$", "key", "Key", " ", " "};
86 String[] goodValues = {"", "$", "value", "Value", " ", " "};
87
88 URLConnection conn = getConnection(url);
89
90 for (int i = 0; i < goodKeys.length; ++i) {
91 for (int j = 0; j < goodValues.length; ++j) {
92 // If a property with the key already exists, overwrite its value with the new value
93 conn.setRequestProperty(goodKeys[i], goodValues[j]);
94 String value = conn.getRequestProperty(goodKeys[i]);
95
96 if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))
97 throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");
98 }
99 }
100 }
101
102 static URLConnection getConnection(URL url) {
103 return new DummyURLConnection(url);
104 }
105
106 static class DummyURLConnection extends URLConnection {
107
108 DummyURLConnection(URL url) {
109 super(url);
110 }
111
112 public void connect() {
113 connected = true;
114 }
115 }
116
chegar7ac25fd2013-02-02 17:15:13 +0000117 private static boolean hasFtp() {
118 try {
119 return new java.net.URL("ftp://") != null;
120 } catch (java.net.MalformedURLException x) {
121 System.out.println("FTP not supported by this runtime.");
122 return false;
123 }
124 }
duke6e45e102007-12-01 00:00:00 +0000125}