Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/RequestProperties.java
38811 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 448520826* @summary file: and ftp: URL handlers need to throw NPE in setRequestProperty27*/2829import java.net.*;30import java.util.ArrayList;31import java.util.List;3233public class RequestProperties {34static int failed;3536public static void main (String args[]) throws Exception {37List<String> urls = new ArrayList<>();38urls.add("http://foo.com/bar/");39urls.add("jar:http://foo.com/bar.html!/foo/bar");40urls.add("file:/etc/passwd");41if (hasFtp())42urls.add("ftp://foo:[email protected]/etc/passwd");4344for (String urlStr : urls)45test(new URL(urlStr));4647if (failed != 0)48throw new RuntimeException(failed + " errors") ;49}5051static void test(URL url) throws Exception {52URLConnection urlc = url.openConnection();53try {54urlc.setRequestProperty(null, null);55System.out.println(url.getProtocol()56+ ": setRequestProperty(null,) did not throw NPE");57failed++;58} catch (NullPointerException e) { /* Expected */ }59try {60urlc.addRequestProperty(null, null);61System.out.println(url.getProtocol()62+ ": addRequestProperty(null,) did not throw NPE");63failed++;64} catch (NullPointerException e) { /* Expected */ }6566if (urlc.getRequestProperty(null) != null) {67System.out.println(url.getProtocol()68+ ": getRequestProperty(null,) did not return null");69failed++;70}71}7273private static boolean hasFtp() {74try {75return new java.net.URL("ftp://") != null;76} catch (java.net.MalformedURLException x) {77System.out.println("FTP not supported by this runtime.");78return false;79}80}81}828384