Path: blob/master/test/jdk/java/net/httpclient/BodySubscribersTest.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Basic test for the standard BodySubscribers default behavior26* @bug 822558327* @run testng BodySubscribersTest28*/2930import java.net.http.HttpResponse.BodySubscriber;31import java.nio.ByteBuffer;32import java.nio.file.Path;33import java.util.List;34import java.util.concurrent.Flow;35import java.util.function.Supplier;36import org.testng.annotations.DataProvider;37import org.testng.annotations.Test;38import static java.lang.System.out;39import static java.net.http.HttpResponse.BodySubscribers.*;40import static java.nio.charset.StandardCharsets.UTF_8;41import static java.nio.file.StandardOpenOption.CREATE;42import static org.testng.Assert.assertTrue;43import static org.testng.Assert.assertNotNull;44import static org.testng.Assert.expectThrows;45import static org.testng.Assert.fail;4647public class BodySubscribersTest {4849static final Class<NullPointerException> NPE = NullPointerException.class;5051// Supplier of BodySubscriber<?>, with a descriptive name52static class BSSupplier implements Supplier<BodySubscriber<?>> {53private final Supplier<BodySubscriber<?>> supplier;54private final String name;55private BSSupplier(Supplier<BodySubscriber<?>> supplier, String name) {56this.supplier = supplier;57this.name = name;58}59static BSSupplier create(String name, Supplier<BodySubscriber<?>> supplier) {60return new BSSupplier(supplier, name);61}62@Override public BodySubscriber<?> get() { return supplier.get(); }63@Override public String toString() { return name; }64}6566static class LineSubscriber implements Flow.Subscriber<String> {67@Override public void onSubscribe(Flow.Subscription subscription) { }68@Override public void onNext(String item) { fail(); }69@Override public void onError(Throwable throwable) { fail(); }70@Override public void onComplete() { fail(); }71}7273static class BBSubscriber implements Flow.Subscriber<List<ByteBuffer>> {74@Override public void onSubscribe(Flow.Subscription subscription) { }75@Override public void onNext(List<ByteBuffer> item) { fail(); }76@Override public void onError(Throwable throwable) { fail(); }77@Override public void onComplete() { fail(); }78}7980@DataProvider(name = "bodySubscriberSuppliers")81public Object[][] bodySubscriberSuppliers() { ;82List<Supplier<BodySubscriber<?>>> list = List.of(83BSSupplier.create("ofByteArray", () -> ofByteArray()),84BSSupplier.create("ofInputStream", () -> ofInputStream()),85BSSupplier.create("ofBAConsumer", () -> ofByteArrayConsumer(ba -> { })),86BSSupplier.create("ofLines", () -> ofLines(UTF_8)),87BSSupplier.create("ofPublisher", () -> ofPublisher()),88BSSupplier.create("ofFile", () -> ofFile(Path.of("f"))),89BSSupplier.create("ofFile-opts)", () -> ofFile(Path.of("f"), CREATE)),90BSSupplier.create("ofString", () -> ofString(UTF_8)),91BSSupplier.create("buffering", () -> buffering(ofByteArray(), 10)),92BSSupplier.create("discarding", () -> discarding()),93BSSupplier.create("mapping", () -> mapping(ofString(UTF_8), s -> s)),94BSSupplier.create("replacing", () -> replacing("hello")),95BSSupplier.create("fromSubscriber-1", () -> fromSubscriber(new BBSubscriber())),96BSSupplier.create("fromSubscriber-2", () -> fromSubscriber(new BBSubscriber(), s -> s)),97BSSupplier.create("fromLineSubscriber-1", () -> fromLineSubscriber(new LineSubscriber())),98BSSupplier.create("fromLineSubscriber-2", () -> fromLineSubscriber(new LineSubscriber(), s -> s, UTF_8, ","))99);100101return list.stream().map(x -> new Object[] { x }).toArray(Object[][]::new);102}103104@Test(dataProvider = "bodySubscriberSuppliers")105void nulls(Supplier<BodySubscriber<?>> bodySubscriberSupplier) {106BodySubscriber<?> bodySubscriber = bodySubscriberSupplier.get();107boolean subscribed = false;108109do {110assertNotNull(bodySubscriber.getBody());111assertNotNull(bodySubscriber.getBody());112assertNotNull(bodySubscriber.getBody());113expectThrows(NPE, () -> bodySubscriber.onSubscribe(null));114expectThrows(NPE, () -> bodySubscriber.onSubscribe(null));115expectThrows(NPE, () -> bodySubscriber.onSubscribe(null));116117expectThrows(NPE, () -> bodySubscriber.onNext(null));118expectThrows(NPE, () -> bodySubscriber.onNext(null));119expectThrows(NPE, () -> bodySubscriber.onNext(null));120expectThrows(NPE, () -> bodySubscriber.onNext(null));121122expectThrows(NPE, () -> bodySubscriber.onError(null));123expectThrows(NPE, () -> bodySubscriber.onError(null));124expectThrows(NPE, () -> bodySubscriber.onError(null));125126if (!subscribed) {127out.println("subscribing");128// subscribe the Subscriber and repeat129bodySubscriber.onSubscribe(new Flow.Subscription() {130@Override public void request(long n) { /* do nothing */ }131@Override public void cancel() { fail(); }132});133subscribed = true;134continue;135}136break;137} while (true);138}139140@Test(dataProvider = "bodySubscriberSuppliers")141void subscribeMoreThanOnce(Supplier<BodySubscriber<?>> bodySubscriberSupplier) {142BodySubscriber<?> bodySubscriber = bodySubscriberSupplier.get();143bodySubscriber.onSubscribe(new Flow.Subscription() {144@Override public void request(long n) { /* do nothing */ }145@Override public void cancel() { fail(); }146});147148for (int i = 0; i < 5; i++) {149var subscription = new Flow.Subscription() {150volatile boolean cancelled;151@Override public void request(long n) { fail(); }152@Override public void cancel() { cancelled = true; }153};154bodySubscriber.onSubscribe(subscription);155assertTrue(subscription.cancelled);156}157}158}159160161