Path: blob/master/test/jdk/java/net/URLConnection/RequestProperties.java
41149 views
/*1* Copyright (c) 2001, 2020, 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 4485208 825276726* @summary Validate various request property methods on java.net.URLConnection27* throw NullPointerException and IllegalStateException when expected28* @run testng RequestProperties29*/3031import org.testng.Assert;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;3435import java.io.IOException;36import java.net.URL;37import java.net.URLConnection;38import java.nio.file.Path;39import java.util.ArrayList;40import java.util.List;4142public class RequestProperties {4344private static final Class NPE = NullPointerException.class;45private static final Class ISE = IllegalStateException.class;4647@DataProvider(name = "urls")48private Object[][] urls() {49final List<String> urls = new ArrayList<>();50urls.add("http://foo.com/bar/");51urls.add("jar:http://foo.com/bar.html!/foo/bar");52urls.add("file:/etc/passwd");53if (hasFtp()) {54urls.add("ftp://foo:[email protected]/etc/passwd");55}56final Object[][] data = new Object[urls.size()][1];57for (int i = 0; i < urls.size(); i++) {58data[i][0] = urls.get(i);59}60return data;61}626364/**65* Test that {@link java.net.URLConnection#setRequestProperty(String, String)} throws66* a {@link NullPointerException} when passed null key67*/68@Test(dataProvider = "urls")69public void testSetRequestPropertyNullPointerException(final String url) throws Exception {70final URLConnection conn = new URL(url).openConnection();71Assert.assertThrows(NPE, () -> conn.setRequestProperty(null, "bar"));72// expected to pass73conn.setRequestProperty("key", null);74}7576/**77* Test that {@link java.net.URLConnection#addRequestProperty(String, String)} throws78* a {@link NullPointerException} when passed null key79*/80@Test(dataProvider = "urls")81public void testAddRequestPropertyNullPointerException(final String url) throws Exception {82final URLConnection conn = new URL(url).openConnection();83Assert.assertThrows(NPE, () -> conn.addRequestProperty(null, "hello"));84// expected to pass85conn.addRequestProperty("key", null);86}8788/**89* Test that {@link java.net.URLConnection#getRequestProperty(String)} returns90* null when the passed key is null91*/92@Test(dataProvider = "urls")93public void testGetRequestPropertyReturnsNull(final String url) throws Exception {94final URLConnection conn = new URL(url).openConnection();95Assert.assertNull(conn.getRequestProperty(null),96"getRequestProperty was expected to return null for null key");97}9899/**100* Test that {@link java.net.URLConnection#setRequestProperty(String, String)} throws101* an {@link IllegalStateException} when already connected102*/103@Test104public void testSetRequestPropertyIllegalStateException() throws Exception {105final URLConnection conn = createAndConnectURLConnection();106try {107Assert.assertThrows(ISE, () -> conn.setRequestProperty("foo", "bar"));108} finally {109safeClose(conn);110}111}112113/**114* Test that {@link java.net.URLConnection#addRequestProperty(String, String)} throws115* an {@link IllegalStateException} when already connected116*/117@Test118public void testAddRequestPropertyIllegalStateException() throws Exception {119final URLConnection conn = createAndConnectURLConnection();120try {121Assert.assertThrows(ISE, () -> conn.addRequestProperty("foo", "bar"));122} finally {123safeClose(conn);124}125}126127/**128* Test that {@link java.net.URLConnection#getRequestProperty(String)} throws129* an {@link IllegalStateException} when already connected130*/131@Test132public void testGetRequestPropertyIllegalStateException() throws Exception {133final URLConnection conn = createAndConnectURLConnection();134try {135Assert.assertThrows(ISE, () -> conn.getRequestProperty("hello"));136} finally {137safeClose(conn);138}139}140141/**142* Test that {@link URLConnection#getRequestProperties()} throws143* an {@link IllegalStateException} when already connected144*/145@Test146public void testGetRequestPropertiesIllegalStateException() throws Exception {147final URLConnection conn = createAndConnectURLConnection();148try {149Assert.assertThrows(ISE, () -> conn.getRequestProperties());150} finally {151safeClose(conn);152}153}154155private static URLConnection createAndConnectURLConnection() throws IOException {156final URL url = Path.of(System.getProperty("java.io.tmpdir")).toUri().toURL();157final URLConnection conn = url.openConnection();158conn.connect();159return conn;160}161162private static void safeClose(final URLConnection conn) {163try {164conn.getInputStream().close();165} catch (Exception e) {166// ignore167}168}169170private static boolean hasFtp() {171try {172new java.net.URL("ftp://");173return true;174} catch (java.net.MalformedURLException x) {175System.out.println("FTP not supported by this runtime.");176return false;177}178}179}180181182