test/jdk/java/net/httpclient/RelayingPublishers.java
author dfuchs
Tue, 01 Oct 2019 12:10:33 +0100
changeset 58423 54de0c861d32
parent 55426 4efe251009b4
permissions -rw-r--r--
8231506: Fix some instabilities in a few networking tests Reviewed-by: alanb, chegar, msheppar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55426
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     1
/*
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     4
 *
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     8
 *
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    13
 * accompanied this code).
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    14
 *
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    18
 *
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    21
 * questions.
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    22
 */
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    23
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    24
import jdk.test.lib.util.FileUtils;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    25
import org.testng.annotations.Test;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    26
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    27
import java.io.FileNotFoundException;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    28
import java.io.IOException;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    29
import java.net.http.HttpRequest.BodyPublisher;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    30
import java.net.http.HttpRequest.BodyPublishers;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    31
import java.nio.ByteBuffer;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    32
import java.nio.file.Files;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    33
import java.nio.file.Path;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    34
import java.util.ArrayList;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    35
import java.util.List;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    36
import java.util.concurrent.CompletableFuture;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    37
import java.util.concurrent.Flow;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    38
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    39
import static org.testng.Assert.assertEquals;
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    40
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    41
/*
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    42
 * @test
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    43
 * @summary Verifies that some of the standard BodyPublishers relay exception
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    44
 *          rather than throw it
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    45
 * @bug 8226303
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    46
 * @library /test/lib
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    47
 * @run testng/othervm RelayingPublishers
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    48
 */
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    49
public class RelayingPublishers {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    50
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    51
    @Test
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    52
    public void ofFile0() throws IOException {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    53
        Path directory = Files.createDirectory(Path.of("d"));
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    54
        // Even though the path exists, the publisher should not be able
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    55
        // to read from it, as that path denotes a directory, not a file
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    56
        BodyPublisher pub = BodyPublishers.ofFile(directory);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    57
        CompletableSubscriber<ByteBuffer> s = new CompletableSubscriber<>();
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    58
        pub.subscribe(s);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    59
        s.future().join();
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    60
        // Interestingly enough, it's FileNotFoundException if a file
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    61
        // is a directory
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    62
        assertEquals(s.future().join().getClass(), FileNotFoundException.class);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    63
    }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    64
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    65
    @Test
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    66
    public void ofFile1() throws IOException {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    67
        Path file = Files.createFile(Path.of("f"));
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    68
        BodyPublisher pub = BodyPublishers.ofFile(file);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    69
        FileUtils.deleteFileWithRetry(file);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    70
        CompletableSubscriber<ByteBuffer> s = new CompletableSubscriber<>();
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    71
        pub.subscribe(s);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    72
        assertEquals(s.future().join().getClass(), FileNotFoundException.class);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    73
    }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    74
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    75
    @Test
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    76
    public void ofByteArrays() {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    77
        List<byte[]> bytes = new ArrayList<>();
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    78
        bytes.add(null);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    79
        BodyPublisher pub = BodyPublishers.ofByteArrays(bytes);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    80
        CompletableSubscriber<ByteBuffer> s = new CompletableSubscriber<>();
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    81
        pub.subscribe(s);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    82
        assertEquals(s.future().join().getClass(), NullPointerException.class);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    83
    }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    84
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    85
    static class CompletableSubscriber<T> implements Flow.Subscriber<T> {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    86
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    87
        final CompletableFuture<Throwable> f = new CompletableFuture<>();
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    88
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    89
        @Override
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    90
        public void onSubscribe(Flow.Subscription subscription) {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    91
            subscription.request(1);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    92
        }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    93
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    94
        @Override
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    95
        public void onNext(T item) {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    96
            f.completeExceptionally(new RuntimeException("Unexpected onNext"));
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    97
        }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    98
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
    99
        @Override
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   100
        public void onError(Throwable throwable) {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   101
            f.complete(throwable);
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   102
        }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   103
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   104
        @Override
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   105
        public void onComplete() {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   106
            f.completeExceptionally(new RuntimeException("Unexpected onNext"));
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   107
        }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   108
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   109
        CompletableFuture<Throwable> future() {
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   110
            return f.copy();
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   111
        }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   112
    }
4efe251009b4 8226303: Examine the HttpRequest.BodyPublishers for exception handling
prappo
parents:
diff changeset
   113
}