test/jdk/java/net/httpclient/RelayingPublishers.java
changeset 55426 4efe251009b4
equal deleted inserted replaced
55309:8081b181bba8 55426:4efe251009b4
       
     1 /*
       
     2  * Copyright (c) 2019, 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.test.lib.util.FileUtils;
       
    25 import org.testng.annotations.Test;
       
    26 
       
    27 import java.io.FileNotFoundException;
       
    28 import java.io.IOException;
       
    29 import java.net.http.HttpRequest.BodyPublisher;
       
    30 import java.net.http.HttpRequest.BodyPublishers;
       
    31 import java.nio.ByteBuffer;
       
    32 import java.nio.file.Files;
       
    33 import java.nio.file.Path;
       
    34 import java.util.ArrayList;
       
    35 import java.util.List;
       
    36 import java.util.concurrent.CompletableFuture;
       
    37 import java.util.concurrent.Flow;
       
    38 
       
    39 import static org.testng.Assert.assertEquals;
       
    40 
       
    41 /*
       
    42  * @test
       
    43  * @summary Verifies that some of the standard BodyPublishers relay exception
       
    44  *          rather than throw it
       
    45  * @bug 8226303
       
    46  * @library /test/lib
       
    47  * @run testng/othervm RelayingPublishers
       
    48  */
       
    49 public class RelayingPublishers {
       
    50 
       
    51     @Test
       
    52     public void ofFile0() throws IOException {
       
    53         Path directory = Files.createDirectory(Path.of("d"));
       
    54         // Even though the path exists, the publisher should not be able
       
    55         // to read from it, as that path denotes a directory, not a file
       
    56         BodyPublisher pub = BodyPublishers.ofFile(directory);
       
    57         CompletableSubscriber<ByteBuffer> s = new CompletableSubscriber<>();
       
    58         pub.subscribe(s);
       
    59         s.future().join();
       
    60         // Interestingly enough, it's FileNotFoundException if a file
       
    61         // is a directory
       
    62         assertEquals(s.future().join().getClass(), FileNotFoundException.class);
       
    63     }
       
    64 
       
    65     @Test
       
    66     public void ofFile1() throws IOException {
       
    67         Path file = Files.createFile(Path.of("f"));
       
    68         BodyPublisher pub = BodyPublishers.ofFile(file);
       
    69         FileUtils.deleteFileWithRetry(file);
       
    70         CompletableSubscriber<ByteBuffer> s = new CompletableSubscriber<>();
       
    71         pub.subscribe(s);
       
    72         assertEquals(s.future().join().getClass(), FileNotFoundException.class);
       
    73     }
       
    74 
       
    75     @Test
       
    76     public void ofByteArrays() {
       
    77         List<byte[]> bytes = new ArrayList<>();
       
    78         bytes.add(null);
       
    79         BodyPublisher pub = BodyPublishers.ofByteArrays(bytes);
       
    80         CompletableSubscriber<ByteBuffer> s = new CompletableSubscriber<>();
       
    81         pub.subscribe(s);
       
    82         assertEquals(s.future().join().getClass(), NullPointerException.class);
       
    83     }
       
    84 
       
    85     static class CompletableSubscriber<T> implements Flow.Subscriber<T> {
       
    86 
       
    87         final CompletableFuture<Throwable> f = new CompletableFuture<>();
       
    88 
       
    89         @Override
       
    90         public void onSubscribe(Flow.Subscription subscription) {
       
    91             subscription.request(1);
       
    92         }
       
    93 
       
    94         @Override
       
    95         public void onNext(T item) {
       
    96             f.completeExceptionally(new RuntimeException("Unexpected onNext"));
       
    97         }
       
    98 
       
    99         @Override
       
   100         public void onError(Throwable throwable) {
       
   101             f.complete(throwable);
       
   102         }
       
   103 
       
   104         @Override
       
   105         public void onComplete() {
       
   106             f.completeExceptionally(new RuntimeException("Unexpected onNext"));
       
   107         }
       
   108 
       
   109         CompletableFuture<Throwable> future() {
       
   110             return f.copy();
       
   111         }
       
   112     }
       
   113 }