1/* 2 * Copyright (c) 2002, 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 4644775 6230836 27 * @summary Test URLConnection Request Proterties 28 */ 29 30import java.net.MalformedURLException; 31import java.net.URL; 32import java.net.URLConnection; 33import java.util.ArrayList; 34import java.util.List; 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 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 { 51 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")); 57 58 boolean failed = false; 59 60 for (URL url : urls) { 61 URLConnection uc = url.openConnection(); 62 try { 63 uc.setRequestProperty("TestHeader", null); 64 } catch (NullPointerException npe) { 65 System.out.println("setRequestProperty is throwing NPE" + 66 " for url: " + url); 67 failed = true; 68 } 69 try { 70 uc.addRequestProperty("TestHeader", null); 71 } catch (NullPointerException npe) { 72 System.out.println("addRequestProperty is throwing NPE" + 73 " for url: " + url); 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 117 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 } 125} 126