1 /* 2 * Copyright (c) 2017, 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 java.io.FileNotFoundException; 25 import java.nio.ByteBuffer; 26 import java.nio.file.Files; 27 import java.nio.file.Path; 28 import java.nio.file.Paths; 29 import java.nio.file.OpenOption; 30 import java.util.List; 31 import java.util.concurrent.CompletableFuture; 32 import java.util.concurrent.Flow; 33 import jdk.incubator.http.HttpHeaders; 34 import jdk.incubator.http.HttpRequest.BodyPublisher; 35 import jdk.incubator.http.HttpResponse.BodyHandler; 36 import jdk.incubator.http.HttpResponse.BodySubscriber; 37 import org.testng.annotations.DataProvider; 38 import org.testng.annotations.Test; 39 import static java.nio.charset.StandardCharsets.UTF_8; 40 import static java.nio.file.StandardOpenOption.CREATE; 41 import static java.nio.file.StandardOpenOption.WRITE; 42 import static org.testng.Assert.assertThrows; 43 44 /* 45 * @test 46 * @summary Basic tests for API specified exceptions from Publisher, Handler, 47 * and Subscriber convenience static factory methods. 48 * @run testng SubscriberPublisherAPIExceptions 49 */ 50 51 public class SubscriberPublisherAPIExceptions { 52 53 static final Class<NullPointerException> NPE = NullPointerException.class; 54 static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; 55 static final Class<IndexOutOfBoundsException> IOB = IndexOutOfBoundsException.class; 56 57 @Test 58 public void publisherAPIExceptions() { 59 assertThrows(NPE, () -> BodyPublisher.fromByteArray(null)); 60 assertThrows(NPE, () -> BodyPublisher.fromByteArray(null, 0, 1)); 61 assertThrows(IOB, () -> BodyPublisher.fromByteArray(new byte[100], 0, 101)); 62 assertThrows(IOB, () -> BodyPublisher.fromByteArray(new byte[100], 1, 100)); 63 assertThrows(IOB, () -> BodyPublisher.fromByteArray(new byte[100], -1, 10)); 64 assertThrows(IOB, () -> BodyPublisher.fromByteArray(new byte[100], 99, 2)); 65 assertThrows(IOB, () -> BodyPublisher.fromByteArray(new byte[1], -100, 1)); 66 assertThrows(NPE, () -> BodyPublisher.fromByteArrays(null)); 67 assertThrows(NPE, () -> BodyPublisher.fromFile(null)); 68 assertThrows(NPE, () -> BodyPublisher.fromInputStream(null)); 69 assertThrows(NPE, () -> BodyPublisher.fromString(null)); 70 assertThrows(NPE, () -> BodyPublisher.fromString("A", null)); 71 assertThrows(NPE, () -> BodyPublisher.fromString(null, UTF_8)); 72 assertThrows(NPE, () -> BodyPublisher.fromString(null, null)); 73 } 74 75 @DataProvider(name = "nonExistentFiles") 76 public Object[][] nonExistentFiles() { 77 List<Path> paths = List.of(Paths.get("doesNotExist"), 78 Paths.get("tsixEtoNseod"), 79 Paths.get("doesNotExist2")); 80 paths.forEach(p -> { 81 if (Files.exists(p)) 82 throw new AssertionError("Unexpected " + p); 83 }); 84 85 return paths.stream().map(p -> new Object[] { p }).toArray(Object[][]::new); 86 } 87 88 @Test(dataProvider = "nonExistentFiles", expectedExceptions = FileNotFoundException.class) 89 public void fromFileCheck(Path path) throws Exception { 90 BodyPublisher.fromFile(path); 91 } 92 93 @Test 94 public void handlerAPIExceptions() { 95 Path path = Paths.get(".").resolve("tt"); 96 assertThrows(NPE, () -> BodyHandler.asByteArrayConsumer(null)); 97 assertThrows(NPE, () -> BodyHandler.asFile(null)); 98 assertThrows(NPE, () -> BodyHandler.asFile(null, CREATE, WRITE)); 99 assertThrows(NPE, () -> BodyHandler.asFile(path, (OpenOption[])null)); 100 assertThrows(NPE, () -> BodyHandler.asFile(path, new OpenOption[] {null})); 101 assertThrows(NPE, () -> BodyHandler.asFile(path, new OpenOption[] {CREATE, null})); 102 assertThrows(NPE, () -> BodyHandler.asFile(path, new OpenOption[] {null, CREATE})); 103 assertThrows(NPE, () -> BodyHandler.asFile(null, (OpenOption[])null)); 104 assertThrows(NPE, () -> BodyHandler.asFileDownload(null, CREATE, WRITE)); 105 assertThrows(NPE, () -> BodyHandler.asFileDownload(path, (OpenOption[])null)); 106 assertThrows(NPE, () -> BodyHandler.asFileDownload(path, new OpenOption[] {null})); 107 assertThrows(NPE, () -> BodyHandler.asFileDownload(path, new OpenOption[] {CREATE, null})); 108 assertThrows(NPE, () -> BodyHandler.asFileDownload(path, new OpenOption[] {null, CREATE})); 109 assertThrows(NPE, () -> BodyHandler.asFileDownload(null, (OpenOption[])null)); 110 assertThrows(NPE, () -> BodyHandler.asString(null)); 111 assertThrows(NPE, () -> BodyHandler.buffering(null, 1)); 112 assertThrows(IAE, () -> BodyHandler.buffering(new NoOpHandler(), 0)); 113 assertThrows(IAE, () -> BodyHandler.buffering(new NoOpHandler(), -1)); 114 assertThrows(IAE, () -> BodyHandler.buffering(new NoOpHandler(), Integer.MIN_VALUE)); 115 } 116 117 @Test 118 public void subscriberAPIExceptions() { 119 Path path = Paths.get(".").resolve("tt"); 120 assertThrows(NPE, () -> BodySubscriber.asByteArrayConsumer(null)); 121 assertThrows(NPE, () -> BodySubscriber.asFile(null)); 122 assertThrows(NPE, () -> BodySubscriber.asFile(null, CREATE, WRITE)); 123 assertThrows(NPE, () -> BodySubscriber.asFile(path, (OpenOption[])null)); 124 assertThrows(NPE, () -> BodySubscriber.asFile(path, new OpenOption[] {null})); 125 assertThrows(NPE, () -> BodySubscriber.asFile(path, new OpenOption[] {CREATE, null})); 126 assertThrows(NPE, () -> BodySubscriber.asFile(path, new OpenOption[] {null, CREATE})); 127 assertThrows(NPE, () -> BodySubscriber.asFile(null, (OpenOption[])null)); 128 assertThrows(NPE, () -> BodySubscriber.asString(null)); 129 assertThrows(NPE, () -> BodySubscriber.buffering(null, 1)); 130 assertThrows(IAE, () -> BodySubscriber.buffering(new NoOpSubscriber(), 0)); 131 assertThrows(IAE, () -> BodySubscriber.buffering(new NoOpSubscriber(), -1)); 132 assertThrows(IAE, () -> BodySubscriber.buffering(new NoOpSubscriber(), Integer.MIN_VALUE)); 133 } 134 135 static class NoOpHandler implements BodyHandler<Void> { 136 @Override public BodySubscriber<Void> apply(int code, HttpHeaders hrds) { return null; } 137 } 138 139 static class NoOpSubscriber implements BodySubscriber<Void> { 140 @Override public void onSubscribe(Flow.Subscription subscription) { } 141 @Override public void onNext(List<ByteBuffer> item) { } 142 @Override public void onError(Throwable throwable) { } 143 @Override public void onComplete() { } 144 @Override public CompletableFuture<Void> getBody() { return null; } 145 } 146 }