1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import jdk.incubator.http.HttpRequest;
  25 import java.net.URI;
  26 import jdk.incubator.http.HttpClient;
  27 import java.time.Duration;
  28 import java.util.function.BiFunction;
  29 import java.util.function.Function;
  30 import java.util.stream.Collectors;
  31 import java.util.stream.Stream;
  32 
  33 /**
  34  * @test
  35  * @bug 8170064
  36  * @summary  HttpRequest API documentation says:" Unless otherwise stated,
  37  * {@code null} parameter values will cause methods
  38  * of this class to throw {@code NullPointerException}".
  39  */
  40 public class HttpRequestBuilderTest {
  41 
  42     static final URI TEST_URI = URI.create("http://www.foo.com/");
  43 
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         HttpRequest.Builder builder = HttpRequest.newBuilder();
  48         builder = test1("uri", builder, builder::uri, (URI)null,
  49                         NullPointerException.class);
  50         builder = test2("header", builder, builder::header, (String) null, "bar",
  51                         NullPointerException.class);
  52         builder = test2("header", builder, builder::header, "foo", (String) null,
  53                         NullPointerException.class);
  54         builder = test2("header", builder, builder::header, (String)null,
  55                         (String) null, NullPointerException.class);
  56         builder = test1("headers", builder, builder::headers, (String[]) null,
  57                         NullPointerException.class);
  58         builder = test1("headers", builder, builder::headers,
  59                         (String[]) new String[] {null, "bar"},
  60                         NullPointerException.class);
  61         builder = test1("headers", builder, builder::headers,
  62                         (String[]) new String[] {"foo", null},
  63                         NullPointerException.class);
  64         builder = test1("headers", builder, builder::headers,
  65                         (String[]) new String[] {null, null},
  66                         NullPointerException.class);
  67         builder = test1("headers", builder, builder::headers,
  68                        (String[]) new String[] {"foo", "bar", null},
  69                        NullPointerException.class,
  70                        IllegalArgumentException.class);
  71         builder = test1("headers", builder, builder::headers,
  72                        (String[]) new String[] {"foo", "bar", null, null},
  73                        NullPointerException.class);
  74         builder = test1("headers", builder, builder::headers,
  75                        (String[]) new String[] {"foo", "bar", "baz", null},
  76                        NullPointerException.class);
  77         builder = test1("headers", builder, builder::headers,
  78                        (String[]) new String[] {"foo", "bar", null, "baz"},
  79                        NullPointerException.class);
  80         builder = test1("headers", builder, builder::headers,
  81                        (String[]) new String[] {"foo", "bar", "baz"},
  82                        IllegalArgumentException.class);
  83         builder = test1("headers", builder, builder::headers,
  84                        (String[]) new String[] {"foo"},
  85                        IllegalArgumentException.class);
  86         builder = test1("DELETE", builder, builder::DELETE,
  87                         (HttpRequest.BodyProcessor)null, null);
  88         builder = test1("POST", builder, builder::POST,
  89                         (HttpRequest.BodyProcessor)null, null);
  90         builder = test1("PUT", builder, builder::PUT,
  91                         (HttpRequest.BodyProcessor)null, null);
  92         builder = test2("method", builder, builder::method, "GET",
  93                         (HttpRequest.BodyProcessor) null, null);
  94         builder = test2("setHeader", builder, builder::setHeader,
  95                         (String) null, "bar",
  96                         NullPointerException.class);
  97         builder = test2("setHeader", builder, builder::setHeader,
  98                         "foo", (String) null,
  99                         NullPointerException.class);
 100         builder = test2("setHeader", builder, builder::setHeader,
 101                         (String)null, (String) null,
 102                         NullPointerException.class);
 103         builder = test1("timeout", builder, builder::timeout,
 104                         (Duration)null, NullPointerException.class);
 105         builder = test1("version", builder, builder::version,
 106                         (HttpClient.Version)null,
 107                         NullPointerException.class);
 108         builder = test2("method", builder, builder::method, null,
 109                        HttpRequest.BodyProcessor.fromString("foo"),
 110                        NullPointerException.class);
 111 // see JDK-8170093
 112 //
 113 //        builder = test2("method", builder, builder::method, "foo",
 114 //                       HttpRequest.BodyProcessor.fromString("foo"),
 115 //                       IllegalArgumentException.class);
 116 //
 117 //        builder.build();
 118 
 119     }
 120 
 121     private static boolean shouldFail(Class<? extends Exception> ...exceptions) {
 122         return exceptions != null && exceptions.length > 0;
 123     }
 124 
 125     private static String expectedNames(Class<? extends Exception> ...exceptions) {
 126         return Stream.of(exceptions).map(Class::getSimpleName)
 127                 .collect(Collectors.joining("|"));
 128     }
 129     private static boolean isExpected(Exception x,
 130                                      Class<? extends Exception> ...expected) {
 131         return expected != null && Stream.of(expected)
 132                 .filter(c -> c.isInstance(x))
 133                 .findAny().isPresent();
 134     }
 135 
 136     public static <R,P> R test1(String name, R receiver, Function<P, R> m, P arg,
 137                                Class<? extends Exception> ...ex) {
 138         try {
 139             R result =  m.apply(arg);
 140             if (!shouldFail(ex)) {
 141                 System.out.println("success: " + name + "(" + arg + ")");
 142                 return result;
 143             } else {
 144                 throw new AssertionError("Expected " + expectedNames(ex)
 145                     + " not raised for " + name + "(" + arg + ")");
 146             }
 147         } catch (Exception x) {
 148             if (!isExpected(x, ex)) {
 149                 throw x;
 150             } else {
 151                 System.out.println("success: " + name + "(" + arg + ")" +
 152                         " - Got expected exception: " + x);
 153                 return receiver;
 154             }
 155         }
 156     }
 157 
 158 
 159     public static <R,P1, P2> R test2(String name, R receiver, BiFunction<P1, P2, R> m,
 160                                P1 arg1, P2 arg2,
 161                                Class<? extends Exception> ...ex) {
 162         try {
 163             R result =  m.apply(arg1, arg2);
 164             if (!shouldFail(ex)) {
 165                 System.out.println("success: " + name + "(" + arg1 + ", "
 166                                    + arg2 + ")");
 167                 return result;
 168             } else {
 169                 throw new AssertionError("Expected " + expectedNames(ex)
 170                     + " not raised for "
 171                     + name + "(" + arg1 +", " + arg2 + ")");
 172             }
 173         } catch (Exception x) {
 174             if (!isExpected(x, ex)) {
 175                 throw x;
 176             } else {
 177                 System.out.println("success: " + name + "(" + arg1 + ", "
 178                         + arg2 + ") - Got expected exception: " + x);
 179                 return receiver;
 180             }
 181         }
 182     }
 183 }