1 /*
2  * Copyright (c) 2017, 2018, 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 java.net.http.HttpHeaders;
34 import java.net.http.HttpRequest.BodyPublishers;
35 import java.net.http.HttpResponse.BodyHandler;
36 import java.net.http.HttpResponse.ResponseInfo;
37 import java.net.http.HttpResponse.BodyHandlers;
38 import java.net.http.HttpResponse.BodySubscriber;
39 import java.net.http.HttpResponse.BodySubscribers;
40 import java.util.function.Function;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import static java.nio.charset.StandardCharsets.UTF_8;
44 import static java.nio.file.StandardOpenOption.CREATE;
45 import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;
46 import static java.nio.file.StandardOpenOption.WRITE;
47 import static java.nio.file.StandardOpenOption.READ;
48 import static org.testng.Assert.assertThrows;
49 
50 /*
51  * @test
52  * @summary Basic tests for API specified exceptions from Publisher, Handler,
53  *          and Subscriber convenience static factory methods.
54  * @run testng SubscriberPublisherAPIExceptions
55  */
56 
57 public class SubscriberPublisherAPIExceptions {
58 
59     static final Class<NullPointerException> NPE = NullPointerException.class;
60     static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
61     static final Class<IndexOutOfBoundsException> IOB = IndexOutOfBoundsException.class;
62 
63     @Test
publisherAPIExceptions()64     public void publisherAPIExceptions() {
65         assertThrows(NPE, () -> BodyPublishers.ofByteArray(null));
66         assertThrows(NPE, () -> BodyPublishers.ofByteArray(null, 0, 1));
67         assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100],    0, 101));
68         assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100],    1, 100));
69         assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100],   -1,  10));
70         assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100],   99,   2));
71         assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[1],   -100,   1));
72         assertThrows(NPE, () -> BodyPublishers.ofByteArray(null));
73         assertThrows(NPE, () -> BodyPublishers.ofFile(null));
74         assertThrows(NPE, () -> BodyPublishers.ofInputStream(null));
75         assertThrows(NPE, () -> BodyPublishers.ofString(null));
76         assertThrows(NPE, () -> BodyPublishers.ofString("A", null));
77         assertThrows(NPE, () -> BodyPublishers.ofString(null, UTF_8));
78         assertThrows(NPE, () -> BodyPublishers.ofString(null, null));
79     }
80 
81     @DataProvider(name = "nonExistentFiles")
nonExistentFiles()82     public Object[][] nonExistentFiles() {
83         List<Path> paths = List.of(Paths.get("doesNotExist"),
84                                    Paths.get("tsixEtoNseod"),
85                                    Paths.get("doesNotExist2"));
86         paths.forEach(p -> {
87             if (Files.exists(p))
88                 throw new AssertionError("Unexpected " + p);
89         });
90 
91         return paths.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
92     }
93 
94     @Test(dataProvider = "nonExistentFiles", expectedExceptions = FileNotFoundException.class)
fromFileCheck(Path path)95     public void fromFileCheck(Path path) throws Exception {
96         BodyPublishers.ofFile(path);
97     }
98 
99     @Test
handlerAPIExceptions()100     public void handlerAPIExceptions() throws Exception {
101         Path path = Paths.get(".").resolve("tt");
102         Path file = Files.createFile(Paths.get(".").resolve("aFile"));
103         Path doesNotExist = Paths.get(".").resolve("doneNotExist");
104         if (Files.exists(doesNotExist))
105             throw new AssertionError("Unexpected " + doesNotExist);
106 
107         assertThrows(NPE, () -> BodyHandlers.ofByteArrayConsumer(null));
108         assertThrows(NPE, () -> BodyHandlers.ofFile(null));
109         assertThrows(NPE, () -> BodyHandlers.ofFile(null, CREATE, WRITE));
110         assertThrows(NPE, () -> BodyHandlers.ofFile(path, (OpenOption[])null));
111         assertThrows(NPE, () -> BodyHandlers.ofFile(path, new OpenOption[] {null}));
112         assertThrows(NPE, () -> BodyHandlers.ofFile(path, new OpenOption[] {CREATE, null}));
113         assertThrows(NPE, () -> BodyHandlers.ofFile(path, new OpenOption[] {null, CREATE}));
114         assertThrows(NPE, () -> BodyHandlers.ofFile(null, (OpenOption[])null));
115         assertThrows(NPE, () -> BodyHandlers.ofFileDownload(null, CREATE, WRITE));
116         assertThrows(NPE, () -> BodyHandlers.ofFileDownload(path, (OpenOption[])null));
117         assertThrows(NPE, () -> BodyHandlers.ofFileDownload(path, new OpenOption[] {null}));
118         assertThrows(NPE, () -> BodyHandlers.ofFileDownload(path, new OpenOption[] {CREATE, null}));
119         assertThrows(NPE, () -> BodyHandlers.ofFileDownload(path, new OpenOption[] {null, CREATE}));
120         assertThrows(NPE, () -> BodyHandlers.ofFileDownload(null, (OpenOption[])null));
121         assertThrows(IAE, () -> BodyHandlers.ofFileDownload(file, CREATE, WRITE));
122         assertThrows(IAE, () -> BodyHandlers.ofFileDownload(doesNotExist, CREATE, WRITE));
123         assertThrows(NPE, () -> BodyHandlers.ofString(null));
124         assertThrows(NPE, () -> BodyHandlers.buffering(null, 1));
125         assertThrows(IAE, () -> BodyHandlers.buffering(new NoOpHandler(), 0));
126         assertThrows(IAE, () -> BodyHandlers.buffering(new NoOpHandler(), -1));
127         assertThrows(IAE, () -> BodyHandlers.buffering(new NoOpHandler(), Integer.MIN_VALUE));
128 
129         // implementation specific exceptions
130         assertThrows(IAE, () -> BodyHandlers.ofFile(path, READ));
131         assertThrows(IAE, () -> BodyHandlers.ofFile(path, DELETE_ON_CLOSE));
132         assertThrows(IAE, () -> BodyHandlers.ofFile(path, READ, DELETE_ON_CLOSE));
133         assertThrows(IAE, () -> BodyHandlers.ofFileDownload(path, DELETE_ON_CLOSE));
134     }
135 
136     @Test
subscriberAPIExceptions()137     public void subscriberAPIExceptions() {
138         Path path = Paths.get(".").resolve("tt");
139         assertThrows(NPE, () -> BodySubscribers.ofByteArrayConsumer(null));
140         assertThrows(NPE, () -> BodySubscribers.ofFile(null));
141         assertThrows(NPE, () -> BodySubscribers.ofFile(null, CREATE, WRITE));
142         assertThrows(NPE, () -> BodySubscribers.ofFile(path, (OpenOption[])null));
143         assertThrows(NPE, () -> BodySubscribers.ofFile(path, new OpenOption[] {null}));
144         assertThrows(NPE, () -> BodySubscribers.ofFile(path, new OpenOption[] {CREATE, null}));
145         assertThrows(NPE, () -> BodySubscribers.ofFile(path, new OpenOption[] {null, CREATE}));
146         assertThrows(NPE, () -> BodySubscribers.ofFile(null, (OpenOption[])null));
147         assertThrows(NPE, () -> BodySubscribers.ofString(null));
148         assertThrows(NPE, () -> BodySubscribers.buffering(null, 1));
149         assertThrows(IAE, () -> BodySubscribers.buffering(new NoOpSubscriber(), 0));
150         assertThrows(IAE, () -> BodySubscribers.buffering(new NoOpSubscriber(), -1));
151         assertThrows(IAE, () -> BodySubscribers.buffering(new NoOpSubscriber(), Integer.MIN_VALUE));
152         assertThrows(NPE, () -> BodySubscribers.mapping(null, Function.identity()));
153         assertThrows(NPE, () -> BodySubscribers.mapping(BodySubscribers.ofByteArray(), null));
154         assertThrows(NPE, () -> BodySubscribers.mapping(null, null));
155 
156         // implementation specific exceptions
157         assertThrows(IAE, () -> BodySubscribers.ofFile(path, READ));
158         assertThrows(IAE, () -> BodySubscribers.ofFile(path, DELETE_ON_CLOSE));
159         assertThrows(IAE, () -> BodySubscribers.ofFile(path, READ, DELETE_ON_CLOSE));
160     }
161 
162     static class NoOpHandler implements BodyHandler<Void> {
apply(ResponseInfo rinfo)163         @Override public BodySubscriber<Void> apply(ResponseInfo rinfo) { return null; }
164     }
165 
166     static class NoOpSubscriber implements BodySubscriber<Void> {
onSubscribe(Flow.Subscription subscription)167         @Override public void onSubscribe(Flow.Subscription subscription) { }
onNext(List<ByteBuffer> item)168         @Override public void onNext(List<ByteBuffer> item) { }
onError(Throwable throwable)169         @Override public void onError(Throwable throwable) { }
onComplete()170         @Override public void onComplete() { }
getBody()171         @Override public CompletableFuture<Void> getBody() { return null; }
172     }
173 }
174