26 * @bug 4485208 |
26 * @bug 4485208 |
27 * @summary file: and ftp: URL handlers need to throw NPE in setRequestProperty |
27 * @summary file: and ftp: URL handlers need to throw NPE in setRequestProperty |
28 */ |
28 */ |
29 |
29 |
30 import java.net.*; |
30 import java.net.*; |
|
31 import java.util.ArrayList; |
|
32 import java.util.List; |
31 |
33 |
32 public class RequestProperties { |
34 public class RequestProperties { |
|
35 static int failed; |
|
36 |
33 public static void main (String args[]) throws Exception { |
37 public static void main (String args[]) throws Exception { |
34 URL url0 = new URL ("http://foo.com/bar/"); |
38 List<String> urls = new ArrayList<>(); |
35 URL url1 = new URL ("file:/etc/passwd"); |
39 urls.add("http://foo.com/bar/"); |
36 URL url2 = new URL ("ftp://foo:bar@foobar.com/etc/passwd"); |
40 urls.add("jar:http://foo.com/bar.html!/foo/bar"); |
37 URL url3 = new URL ("jar:http://foo.com/bar.html!/foo/bar"); |
41 urls.add("file:/etc/passwd"); |
38 URLConnection urlc0 = url0.openConnection (); |
42 if (hasFtp()) |
39 URLConnection urlc1 = url1.openConnection (); |
43 urls.add("ftp://foo:bar@foobar.com/etc/passwd"); |
40 URLConnection urlc2 = url2.openConnection (); |
44 |
41 URLConnection urlc3 = url3.openConnection (); |
45 for (String urlStr : urls) |
42 int count = 0; |
46 test(new URL(urlStr)); |
43 String s = null; |
47 |
|
48 if (failed != 0) |
|
49 throw new RuntimeException(failed + " errors") ; |
|
50 } |
|
51 |
|
52 static void test(URL url) throws Exception { |
|
53 URLConnection urlc = url.openConnection(); |
44 try { |
54 try { |
45 urlc0.setRequestProperty (null, null); |
55 urlc.setRequestProperty(null, null); |
46 System.out.println ("http: setRequestProperty (null,) did not throw NPE"); |
56 System.out.println(url.getProtocol() |
47 } catch (NullPointerException e) { |
57 + ": setRequestProperty(null,) did not throw NPE"); |
48 count ++; |
58 failed++; |
|
59 } catch (NullPointerException e) { /* Expected */ } |
|
60 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++; |
49 } |
71 } |
|
72 } |
|
73 |
|
74 private static boolean hasFtp() { |
50 try { |
75 try { |
51 urlc0.addRequestProperty (null, null); |
76 return new java.net.URL("ftp://") != null; |
52 System.out.println ("http: addRequestProperty (null,) did not throw NPE"); |
77 } catch (java.net.MalformedURLException x) { |
53 } catch (NullPointerException e) { |
78 System.out.println("FTP not supported by this runtime."); |
54 count ++; |
79 return false; |
55 } |
|
56 try { |
|
57 urlc1.setRequestProperty (null, null); |
|
58 System.out.println ("file: setRequestProperty (null,) did not throw NPE"); |
|
59 } catch (NullPointerException e) { |
|
60 count ++; |
|
61 } |
|
62 try { |
|
63 urlc1.addRequestProperty (null, null); |
|
64 System.out.println ("file: addRequestProperty (null,) did not throw NPE"); |
|
65 } catch (NullPointerException e) { |
|
66 count ++; |
|
67 } |
|
68 try { |
|
69 urlc2.setRequestProperty (null, null); |
|
70 System.out.println ("ftp: setRequestProperty (null,) did not throw NPE"); |
|
71 } catch (NullPointerException e) { |
|
72 count ++; |
|
73 } |
|
74 try { |
|
75 urlc2.addRequestProperty (null, null); |
|
76 System.out.println ("ftp: addRequestProperty (null,) did not throw NPE"); |
|
77 } catch (NullPointerException e) { |
|
78 count ++; |
|
79 } |
|
80 try { |
|
81 urlc3.setRequestProperty (null, null); |
|
82 System.out.println ("jar: setRequestProperty (null,) did not throw NPE"); |
|
83 } catch (NullPointerException e) { |
|
84 count ++; |
|
85 } |
|
86 try { |
|
87 urlc3.addRequestProperty (null, null); |
|
88 System.out.println ("jar: addRequestProperty (null,) did not throw NPE"); |
|
89 } catch (NullPointerException e) { |
|
90 count ++; |
|
91 } |
|
92 if (urlc0.getRequestProperty (null) != null) { |
|
93 System.out.println ("http: getRequestProperty (null,) did not return null"); |
|
94 } else { |
|
95 count ++; |
|
96 } |
|
97 if (urlc1.getRequestProperty (null) != null) { |
|
98 System.out.println ("file: getRequestProperty (null,) did not return null"); |
|
99 } else { |
|
100 count ++; |
|
101 } |
|
102 if (urlc2.getRequestProperty (null) != null) { |
|
103 System.out.println ("ftp: getRequestProperty (null,) did not return null"); |
|
104 } else { |
|
105 count ++; |
|
106 } |
|
107 if (urlc2.getRequestProperty (null) != null) { |
|
108 System.out.println ("jar: getRequestProperty (null,) did not return null"); |
|
109 } else { |
|
110 count ++; |
|
111 } |
|
112 |
|
113 if (count != 12) { |
|
114 throw new RuntimeException ((12 -count) + " errors") ; |
|
115 } |
80 } |
116 } |
81 } |
117 } |
82 } |