J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4644775 6230836 |
| 27 | * @summary Test URLConnection Request Proterties |
| 28 | */ |
| 29 | |
| 30 | import java.net.URL; |
| 31 | import java.net.URLConnection; |
| 32 | |
| 33 | /** |
| 34 | * Part1: |
| 35 | * bug 4644775: Unexpected NPE in setRequestProperty(key, null) call |
| 36 | * Part2: |
| 37 | * bug 6230836: A few methods of class URLConnection implemented incorrectly |
| 38 | */ |
| 39 | |
| 40 | public class RequestPropertyValues { |
| 41 | |
| 42 | public static void main(String[] args) throws Exception { |
| 43 | part1(); |
| 44 | part2(); |
| 45 | } |
| 46 | |
| 47 | public static void part1() throws Exception { |
| 48 | URL[] urls = { new URL("http://localhost:8088"), |
| 49 | new URL("file:/etc/passwd"), |
| 50 | new URL("ftp://foo:bar@foobar.com/etc/passwd"), |
| 51 | new URL("jar:http://foo.com/bar.html!/foo/bar") |
| 52 | }; |
| 53 | |
| 54 | boolean failed = false; |
| 55 | |
| 56 | for (int proto = 0; proto < urls.length; proto++) { |
| 57 | URLConnection uc = (URLConnection) urls[proto].openConnection(); |
| 58 | try { |
| 59 | uc.setRequestProperty("TestHeader", null); |
| 60 | } catch (NullPointerException npe) { |
| 61 | System.out.println("setRequestProperty is throwing NPE" + |
| 62 | " for url: " + urls[proto]); |
| 63 | failed = true; |
| 64 | } |
| 65 | try { |
| 66 | uc.addRequestProperty("TestHeader", null); |
| 67 | } catch (NullPointerException npe) { |
| 68 | System.out.println("addRequestProperty is throwing NPE" + |
| 69 | " for url: " + urls[proto]); |
| 70 | failed = true; |
| 71 | } |
| 72 | } |
| 73 | if (failed) { |
| 74 | throw new Exception("RequestProperty setting/adding is" + |
| 75 | " throwing NPE for null values"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public static void part2() throws Exception { |
| 80 | URL url = null; |
| 81 | String[] goodKeys = {"", "$", "key", "Key", " ", " "}; |
| 82 | String[] goodValues = {"", "$", "value", "Value", " ", " "}; |
| 83 | |
| 84 | URLConnection conn = getConnection(url); |
| 85 | |
| 86 | for (int i = 0; i < goodKeys.length; ++i) { |
| 87 | for (int j = 0; j < goodValues.length; ++j) { |
| 88 | // If a property with the key already exists, overwrite its value with the new value |
| 89 | conn.setRequestProperty(goodKeys[i], goodValues[j]); |
| 90 | String value = conn.getRequestProperty(goodKeys[i]); |
| 91 | |
| 92 | if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j])))) |
| 93 | throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly"); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static URLConnection getConnection(URL url) { |
| 99 | return new DummyURLConnection(url); |
| 100 | } |
| 101 | |
| 102 | static class DummyURLConnection extends URLConnection { |
| 103 | |
| 104 | DummyURLConnection(URL url) { |
| 105 | super(url); |
| 106 | } |
| 107 | |
| 108 | public void connect() { |
| 109 | connected = true; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } |