|
| 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 | +/* |
| 25 | + * @test |
| 26 | + * @summary Basic test for the standard BodySubscribers default behavior |
| 27 | + * @bug 8225583 |
| 28 | + * @run testng BodySubscribersTest |
| 29 | + */ |
| 30 | + |
| 31 | +import java.net.http.HttpResponse.BodySubscriber; |
| 32 | +import java.nio.ByteBuffer; |
| 33 | +import java.nio.file.Path; |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.Flow; |
| 36 | +import java.util.function.Supplier; |
| 37 | +import org.testng.annotations.DataProvider; |
| 38 | +import org.testng.annotations.Test; |
| 39 | +import static java.lang.System.out; |
| 40 | +import static java.net.http.HttpResponse.BodySubscribers.*; |
| 41 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 42 | +import static java.nio.file.StandardOpenOption.CREATE; |
| 43 | +import static org.testng.Assert.assertTrue; |
| 44 | +import static org.testng.Assert.assertNotNull; |
| 45 | +import static org.testng.Assert.expectThrows; |
| 46 | +import static org.testng.Assert.fail; |
| 47 | + |
| 48 | +public class BodySubscribersTest { |
| 49 | + |
| 50 | + static final Class<NullPointerException> NPE = NullPointerException.class; |
| 51 | + |
| 52 | + // Supplier of BodySubscriber<?>, with a descriptive name |
| 53 | + static class BSSupplier implements Supplier<BodySubscriber<?>> { |
| 54 | + private final Supplier<BodySubscriber<?>> supplier; |
| 55 | + private final String name; |
| 56 | + private BSSupplier(Supplier<BodySubscriber<?>> supplier, String name) { |
| 57 | + this.supplier = supplier; |
| 58 | + this.name = name; |
| 59 | + } |
| 60 | + static BSSupplier create(String name, Supplier<BodySubscriber<?>> supplier) { |
| 61 | + return new BSSupplier(supplier, name); |
| 62 | + } |
| 63 | + @Override public BodySubscriber<?> get() { return supplier.get(); } |
| 64 | + @Override public String toString() { return name; } |
| 65 | + } |
| 66 | + |
| 67 | + static class LineSubscriber implements Flow.Subscriber<String> { |
| 68 | + @Override public void onSubscribe(Flow.Subscription subscription) { } |
| 69 | + @Override public void onNext(String item) { fail(); } |
| 70 | + @Override public void onError(Throwable throwable) { fail(); } |
| 71 | + @Override public void onComplete() { fail(); } |
| 72 | + } |
| 73 | + |
| 74 | + static class BBSubscriber implements Flow.Subscriber<List<ByteBuffer>> { |
| 75 | + @Override public void onSubscribe(Flow.Subscription subscription) { } |
| 76 | + @Override public void onNext(List<ByteBuffer> item) { fail(); } |
| 77 | + @Override public void onError(Throwable throwable) { fail(); } |
| 78 | + @Override public void onComplete() { fail(); } |
| 79 | + } |
| 80 | + |
| 81 | + @DataProvider(name = "bodySubscriberSuppliers") |
| 82 | + public Object[][] bodySubscriberSuppliers() { ; |
| 83 | + List<Supplier<BodySubscriber<?>>> list = List.of( |
| 84 | + BSSupplier.create("ofByteArray", () -> ofByteArray()), |
| 85 | + BSSupplier.create("ofInputStream", () -> ofInputStream()), |
| 86 | + BSSupplier.create("ofBAConsumer", () -> ofByteArrayConsumer(ba -> { })), |
| 87 | + BSSupplier.create("ofLines", () -> ofLines(UTF_8)), |
| 88 | + BSSupplier.create("ofPublisher", () -> ofPublisher()), |
| 89 | + BSSupplier.create("ofFile", () -> ofFile(Path.of("f"))), |
| 90 | + BSSupplier.create("ofFile-opts)", () -> ofFile(Path.of("f"), CREATE)), |
| 91 | + BSSupplier.create("ofString", () -> ofString(UTF_8)), |
| 92 | + BSSupplier.create("buffering", () -> buffering(ofByteArray(), 10)), |
| 93 | + BSSupplier.create("discarding", () -> discarding()), |
| 94 | + BSSupplier.create("mapping", () -> mapping(ofString(UTF_8), s -> s)), |
| 95 | + BSSupplier.create("replacing", () -> replacing("hello")), |
| 96 | + BSSupplier.create("fromSubscriber-1", () -> fromSubscriber(new BBSubscriber())), |
| 97 | + BSSupplier.create("fromSubscriber-2", () -> fromSubscriber(new BBSubscriber(), s -> s)), |
| 98 | + BSSupplier.create("fromLineSubscriber-1", () -> fromLineSubscriber(new LineSubscriber())), |
| 99 | + BSSupplier.create("fromLineSubscriber-2", () -> fromLineSubscriber(new LineSubscriber(), s -> s, UTF_8, ",")) |
| 100 | + ); |
| 101 | + |
| 102 | + return list.stream().map(x -> new Object[] { x }).toArray(Object[][]::new); |
| 103 | + } |
| 104 | + |
| 105 | + @Test(dataProvider = "bodySubscriberSuppliers") |
| 106 | + void nulls(Supplier<BodySubscriber<?>> bodySubscriberSupplier) { |
| 107 | + BodySubscriber<?> bodySubscriber = bodySubscriberSupplier.get(); |
| 108 | + boolean subscribed = false; |
| 109 | + |
| 110 | + do { |
| 111 | + assertNotNull(bodySubscriber.getBody()); |
| 112 | + assertNotNull(bodySubscriber.getBody()); |
| 113 | + assertNotNull(bodySubscriber.getBody()); |
| 114 | + expectThrows(NPE, () -> bodySubscriber.onSubscribe(null)); |
| 115 | + expectThrows(NPE, () -> bodySubscriber.onSubscribe(null)); |
| 116 | + expectThrows(NPE, () -> bodySubscriber.onSubscribe(null)); |
| 117 | + |
| 118 | + expectThrows(NPE, () -> bodySubscriber.onNext(null)); |
| 119 | + expectThrows(NPE, () -> bodySubscriber.onNext(null)); |
| 120 | + expectThrows(NPE, () -> bodySubscriber.onNext(null)); |
| 121 | + expectThrows(NPE, () -> bodySubscriber.onNext(null)); |
| 122 | + |
| 123 | + expectThrows(NPE, () -> bodySubscriber.onError(null)); |
| 124 | + expectThrows(NPE, () -> bodySubscriber.onError(null)); |
| 125 | + expectThrows(NPE, () -> bodySubscriber.onError(null)); |
| 126 | + |
| 127 | + if (!subscribed) { |
| 128 | + out.println("subscribing"); |
| 129 | + // subscribe the Subscriber and repeat |
| 130 | + bodySubscriber.onSubscribe(new Flow.Subscription() { |
| 131 | + @Override public void request(long n) { /* do nothing */ } |
| 132 | + @Override public void cancel() { fail(); } |
| 133 | + }); |
| 134 | + subscribed = true; |
| 135 | + continue; |
| 136 | + } |
| 137 | + break; |
| 138 | + } while (true); |
| 139 | + } |
| 140 | + |
| 141 | + @Test(dataProvider = "bodySubscriberSuppliers") |
| 142 | + void subscribeMoreThanOnce(Supplier<BodySubscriber<?>> bodySubscriberSupplier) { |
| 143 | + BodySubscriber<?> bodySubscriber = bodySubscriberSupplier.get(); |
| 144 | + bodySubscriber.onSubscribe(new Flow.Subscription() { |
| 145 | + @Override public void request(long n) { /* do nothing */ } |
| 146 | + @Override public void cancel() { fail(); } |
| 147 | + }); |
| 148 | + |
| 149 | + for (int i = 0; i < 5; i++) { |
| 150 | + var subscription = new Flow.Subscription() { |
| 151 | + volatile boolean cancelled; |
| 152 | + @Override public void request(long n) { fail(); } |
| 153 | + @Override public void cancel() { cancelled = true; } |
| 154 | + }; |
| 155 | + bodySubscriber.onSubscribe(subscription); |
| 156 | + assertTrue(subscription.cancelled); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments