author | lana |
Thu, 26 Dec 2013 12:04:16 -0800 | |
changeset 23010 | 6dadb192ad81 |
parent 15528 | 18f0bac88177 |
permissions | -rw-r--r-- |
2 | 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 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4644775 6230836 |
|
27 |
* @summary Test URLConnection Request Proterties |
|
28 |
*/ |
|
29 |
||
15528
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
30 |
import java.net.MalformedURLException; |
2 | 31 |
import java.net.URL; |
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 | 35 |
|
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 |
||
43 |
public 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 { |
|
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 | 57 |
|
58 |
boolean failed = false; |
|
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 | 62 |
try { |
63 |
uc.setRequestProperty("TestHeader", null); |
|
64 |
} catch (NullPointerException npe) { |
|
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 | 67 |
failed = true; |
68 |
} |
|
69 |
try { |
|
70 |
uc.addRequestProperty("TestHeader", null); |
|
71 |
} catch (NullPointerException npe) { |
|
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 | 74 |
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 |
||
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 | 125 |
} |