1 /* |
1 /* |
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
38 import java.util.concurrent.atomic.AtomicBoolean; |
38 import java.util.concurrent.atomic.AtomicBoolean; |
39 import java.util.concurrent.atomic.AtomicLong; |
39 import java.util.concurrent.atomic.AtomicLong; |
40 |
40 |
41 import org.testng.annotations.Test; |
41 import org.testng.annotations.Test; |
42 |
42 |
|
43 import static org.testng.Assert.*; |
|
44 |
43 /* |
45 /* |
44 * @test |
46 * @test |
|
47 * @bug 8197564 8228970 |
45 * @summary Simple smoke test for BodySubscriber.asInputStream(); |
48 * @summary Simple smoke test for BodySubscriber.asInputStream(); |
46 * @run testng/othervm HttpResponseInputStreamTest |
49 * @run testng/othervm HttpResponseInputStreamTest |
47 * @author daniel fuchs |
50 * @author daniel fuchs |
48 */ |
51 */ |
49 public class HttpResponseInputStreamTest { |
52 public class HttpResponseInputStreamTest { |
|
53 static final Class<NullPointerException> NPE = NullPointerException.class; |
|
54 static final Class<IndexOutOfBoundsException> OOB = IndexOutOfBoundsException.class; |
50 |
55 |
51 static class TestException extends IOException {} |
56 static class TestException extends IOException {} |
52 |
57 |
53 public static void main(String[] args) throws InterruptedException, ExecutionException { |
58 public static void main(String[] args) throws InterruptedException, ExecutionException { |
54 testOnError(); |
59 testOnError(); |
182 throw new CompletionException(io); |
187 throw new CompletionException(io); |
183 } |
188 } |
184 } |
189 } |
185 |
190 |
186 @Test |
191 @Test |
|
192 public static void testReadParameters() throws InterruptedException, ExecutionException, IOException { |
|
193 BodySubscriber<InputStream> isb = BodySubscribers.ofInputStream(); |
|
194 InputStream is = isb.getBody().toCompletableFuture().get(); |
|
195 |
|
196 Throwable ex; |
|
197 |
|
198 // len == 0 |
|
199 assertEquals(is.read(new byte[16], 0, 0), 0); |
|
200 assertEquals(is.read(new byte[16], 16, 0), 0); |
|
201 |
|
202 // index == -1 |
|
203 ex = expectThrows(OOB, () -> is.read(new byte[16], -1, 10)); |
|
204 System.out.println("OutOfBoundsException thrown as expected: " + ex); |
|
205 |
|
206 // large offset |
|
207 ex = expectThrows(OOB, () -> is.read(new byte[16], 17, 10)); |
|
208 System.out.println("OutOfBoundsException thrown as expected: " + ex); |
|
209 |
|
210 ex = expectThrows(OOB, () -> is.read(new byte[16], 10, 10)); |
|
211 System.out.println("OutOfBoundsException thrown as expected: " + ex); |
|
212 |
|
213 // null value |
|
214 ex = expectThrows(NPE, () -> is.read(null, 0, 10)); |
|
215 System.out.println("NullPointerException thrown as expected: " + ex); |
|
216 } |
|
217 |
|
218 @Test |
187 public static void testSubscribeAndClose() |
219 public static void testSubscribeAndClose() |
188 throws InterruptedException, ExecutionException |
220 throws InterruptedException, ExecutionException |
189 { |
221 { |
190 BodySubscriber<InputStream> isb = BodySubscribers.ofInputStream(); |
222 BodySubscriber<InputStream> isb = BodySubscribers.ofInputStream(); |
191 TestCancelOnCloseSubscription s = new TestCancelOnCloseSubscription(); |
223 TestCancelOnCloseSubscription s = new TestCancelOnCloseSubscription(); |