jdk/test/java/net/URLConnection/RequestProperties.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2001 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 4485208
       
    27  * @summary  file: and ftp: URL handlers need to throw NPE in setRequestProperty
       
    28  */
       
    29 
       
    30 import java.net.*;
       
    31 
       
    32 public class RequestProperties {
       
    33     public static void main (String args[]) throws Exception {
       
    34         URL url0 = new URL ("http://foo.com/bar/");
       
    35         URL url1 = new URL ("file:/etc/passwd");
       
    36         URL url2 = new URL ("ftp://foo:bar@foobar.com/etc/passwd");
       
    37         URL url3 = new URL ("jar:http://foo.com/bar.html!/foo/bar");
       
    38         URLConnection urlc0 = url0.openConnection ();
       
    39         URLConnection urlc1 = url1.openConnection ();
       
    40         URLConnection urlc2 = url2.openConnection ();
       
    41         URLConnection urlc3 = url3.openConnection ();
       
    42         int count = 0;
       
    43         String s = null;
       
    44         try {
       
    45             urlc0.setRequestProperty (null, null);
       
    46             System.out.println ("http: setRequestProperty (null,) did not throw NPE");
       
    47         } catch (NullPointerException e) {
       
    48             count ++;
       
    49         }
       
    50         try {
       
    51             urlc0.addRequestProperty (null, null);
       
    52             System.out.println ("http: addRequestProperty (null,) did not throw NPE");
       
    53         } catch (NullPointerException e) {
       
    54             count ++;
       
    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         }
       
   116     }
       
   117 }