|
1 | 1 | /* |
2 | | - * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * @test |
26 | | - * @bug 4485208 |
27 | | - * @summary file: and ftp: URL handlers need to throw NPE in setRequestProperty |
| 26 | + * @bug 4485208 8252767 |
| 27 | + * @summary Validate various request property methods on java.net.URLConnection |
| 28 | + * throw NullPointerException and IllegalStateException when expected |
| 29 | + * @run testng RequestProperties |
28 | 30 | */ |
29 | 31 |
|
30 | | -import java.net.*; |
| 32 | +import org.testng.Assert; |
| 33 | +import org.testng.annotations.DataProvider; |
| 34 | +import org.testng.annotations.Test; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.net.URL; |
| 38 | +import java.net.URLConnection; |
| 39 | +import java.nio.file.Path; |
31 | 40 | import java.util.ArrayList; |
32 | 41 | import java.util.List; |
33 | 42 |
|
34 | 43 | public class RequestProperties { |
35 | | - static int failed; |
36 | 44 |
|
37 | | - public static void main (String args[]) throws Exception { |
38 | | - List<String> urls = new ArrayList<>(); |
| 45 | + private static final Class NPE = NullPointerException.class; |
| 46 | + private static final Class ISE = IllegalStateException.class; |
| 47 | + |
| 48 | + @DataProvider(name = "urls") |
| 49 | + private Object[][] urls() { |
| 50 | + final List<String> urls = new ArrayList<>(); |
39 | 51 | urls.add("http://foo.com/bar/"); |
40 | 52 | urls.add("jar:http://foo.com/bar.html!/foo/bar"); |
41 | 53 | urls.add("file:/etc/passwd"); |
42 | | - if (hasFtp()) |
| 54 | + if (hasFtp()) { |
43 | 55 | urls.add("ftp://foo:bar@foobar.com/etc/passwd"); |
| 56 | + } |
| 57 | + final Object[][] data = new Object[urls.size()][1]; |
| 58 | + for (int i = 0; i < urls.size(); i++) { |
| 59 | + data[i][0] = urls.get(i); |
| 60 | + } |
| 61 | + return data; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * Test that {@link java.net.URLConnection#setRequestProperty(String, String)} throws |
| 67 | + * a {@link NullPointerException} when passed null key |
| 68 | + */ |
| 69 | + @Test(dataProvider = "urls") |
| 70 | + public void testSetRequestPropertyNullPointerException(final String url) throws Exception { |
| 71 | + final URLConnection conn = new URL(url).openConnection(); |
| 72 | + Assert.assertThrows(NPE, () -> conn.setRequestProperty(null, "bar")); |
| 73 | + // expected to pass |
| 74 | + conn.setRequestProperty("key", null); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test that {@link java.net.URLConnection#addRequestProperty(String, String)} throws |
| 79 | + * a {@link NullPointerException} when passed null key |
| 80 | + */ |
| 81 | + @Test(dataProvider = "urls") |
| 82 | + public void testAddRequestPropertyNullPointerException(final String url) throws Exception { |
| 83 | + final URLConnection conn = new URL(url).openConnection(); |
| 84 | + Assert.assertThrows(NPE, () -> conn.addRequestProperty(null, "hello")); |
| 85 | + // expected to pass |
| 86 | + conn.addRequestProperty("key", null); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test that {@link java.net.URLConnection#getRequestProperty(String)} returns |
| 91 | + * null when the passed key is null |
| 92 | + */ |
| 93 | + @Test(dataProvider = "urls") |
| 94 | + public void testGetRequestPropertyReturnsNull(final String url) throws Exception { |
| 95 | + final URLConnection conn = new URL(url).openConnection(); |
| 96 | + Assert.assertNull(conn.getRequestProperty(null), |
| 97 | + "getRequestProperty was expected to return null for null key"); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test that {@link java.net.URLConnection#setRequestProperty(String, String)} throws |
| 102 | + * an {@link IllegalStateException} when already connected |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void testSetRequestPropertyIllegalStateException() throws Exception { |
| 106 | + final URLConnection conn = createAndConnectURLConnection(); |
| 107 | + try { |
| 108 | + Assert.assertThrows(ISE, () -> conn.setRequestProperty("foo", "bar")); |
| 109 | + } finally { |
| 110 | + safeClose(conn); |
| 111 | + } |
| 112 | + } |
44 | 113 |
|
45 | | - for (String urlStr : urls) |
46 | | - test(new URL(urlStr)); |
| 114 | + /** |
| 115 | + * Test that {@link java.net.URLConnection#addRequestProperty(String, String)} throws |
| 116 | + * an {@link IllegalStateException} when already connected |
| 117 | + */ |
| 118 | + @Test |
| 119 | + public void testAddRequestPropertyIllegalStateException() throws Exception { |
| 120 | + final URLConnection conn = createAndConnectURLConnection(); |
| 121 | + try { |
| 122 | + Assert.assertThrows(ISE, () -> conn.addRequestProperty("foo", "bar")); |
| 123 | + } finally { |
| 124 | + safeClose(conn); |
| 125 | + } |
| 126 | + } |
47 | 127 |
|
48 | | - if (failed != 0) |
49 | | - throw new RuntimeException(failed + " errors") ; |
| 128 | + /** |
| 129 | + * Test that {@link java.net.URLConnection#getRequestProperty(String)} throws |
| 130 | + * an {@link IllegalStateException} when already connected |
| 131 | + */ |
| 132 | + @Test |
| 133 | + public void testGetRequestPropertyIllegalStateException() throws Exception { |
| 134 | + final URLConnection conn = createAndConnectURLConnection(); |
| 135 | + try { |
| 136 | + Assert.assertThrows(ISE, () -> conn.getRequestProperty("hello")); |
| 137 | + } finally { |
| 138 | + safeClose(conn); |
| 139 | + } |
50 | 140 | } |
51 | 141 |
|
52 | | - static void test(URL url) throws Exception { |
53 | | - URLConnection urlc = url.openConnection(); |
| 142 | + /** |
| 143 | + * Test that {@link URLConnection#getRequestProperties()} throws |
| 144 | + * an {@link IllegalStateException} when already connected |
| 145 | + */ |
| 146 | + @Test |
| 147 | + public void testGetRequestPropertiesIllegalStateException() throws Exception { |
| 148 | + final URLConnection conn = createAndConnectURLConnection(); |
54 | 149 | try { |
55 | | - urlc.setRequestProperty(null, null); |
56 | | - System.out.println(url.getProtocol() |
57 | | - + ": setRequestProperty(null,) did not throw NPE"); |
58 | | - failed++; |
59 | | - } catch (NullPointerException e) { /* Expected */ } |
| 150 | + Assert.assertThrows(ISE, () -> conn.getRequestProperties()); |
| 151 | + } finally { |
| 152 | + safeClose(conn); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static URLConnection createAndConnectURLConnection() throws IOException { |
| 157 | + final URL url = Path.of(System.getProperty("java.io.tmpdir")).toUri().toURL(); |
| 158 | + final URLConnection conn = url.openConnection(); |
| 159 | + conn.connect(); |
| 160 | + return conn; |
| 161 | + } |
| 162 | + |
| 163 | + private static void safeClose(final URLConnection conn) { |
60 | 164 | try { |
61 | | - urlc.addRequestProperty(null, null); |
62 | | - System.out.println(url.getProtocol() |
63 | | - + ": addRequestProperty(null,) did not throw NPE"); |
64 | | - failed++; |
65 | | - } catch (NullPointerException e) { /* Expected */ } |
66 | | - |
67 | | - if (urlc.getRequestProperty(null) != null) { |
68 | | - System.out.println(url.getProtocol() |
69 | | - + ": getRequestProperty(null,) did not return null"); |
70 | | - failed++; |
| 165 | + conn.getInputStream().close(); |
| 166 | + } catch (Exception e) { |
| 167 | + // ignore |
71 | 168 | } |
72 | 169 | } |
73 | 170 |
|
74 | 171 | private static boolean hasFtp() { |
75 | 172 | try { |
76 | | - return new java.net.URL("ftp://") != null; |
| 173 | + new java.net.URL("ftp://"); |
| 174 | + return true; |
77 | 175 | } catch (java.net.MalformedURLException x) { |
78 | 176 | System.out.println("FTP not supported by this runtime."); |
79 | 177 | return false; |
|
0 commit comments