From 288822a5c2bbaba7b6b897faab1a9cc076c906cc Mon Sep 17 00:00:00 2001 From: Volkan Yazici Date: Wed, 24 Sep 2025 08:07:58 +0000 Subject: [PATCH] 8367068: Remove redundant HttpRequest.BodyPublisher tests Reviewed-by: dfuchs --- .../net/httpclient/FilePublisherTest.java | 22 ---- .../httpclient/FlowAdapterPublisherTest.java | 17 --- .../OfByteArraysTest.java | 2 +- .../HttpRequestBodyPublishers/OfFileTest.java | 2 +- .../net/httpclient/RelayingPublishers.java | 113 ------------------ ...ions.java => SubscriberAPIExceptions.java} | 51 +------- 6 files changed, 8 insertions(+), 199 deletions(-) delete mode 100644 test/jdk/java/net/httpclient/RelayingPublishers.java rename test/jdk/java/net/httpclient/{SubscriberPublisherAPIExceptions.java => SubscriberAPIExceptions.java} (75%) diff --git a/test/jdk/java/net/httpclient/FilePublisherTest.java b/test/jdk/java/net/httpclient/FilePublisherTest.java index 174afaf3f65..5ab9e6f8e7a 100644 --- a/test/jdk/java/net/httpclient/FilePublisherTest.java +++ b/test/jdk/java/net/httpclient/FilePublisherTest.java @@ -39,7 +39,6 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import javax.net.ssl.SSLContext; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -60,7 +59,6 @@ import static java.net.http.HttpClient.Builder.NO_PROXY; import static java.net.http.HttpClient.Version.HTTP_1_1; import static java.net.http.HttpClient.Version.HTTP_2; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; public class FilePublisherTest implements HttpServerAdapters { SSLContext sslContext; @@ -156,26 +154,6 @@ public class FilePublisherTest implements HttpServerAdapters { send(uriString, path, expectedMsg, sameClient); } - @Test - public void testFileNotFound() throws Exception { - out.printf("\n\n--- testFileNotFound(): starting\n"); - try (FileSystem fs = newZipFs()) { - Path fileInZip = fs.getPath("non-existent.txt"); - BodyPublishers.ofFile(fileInZip); - fail(); - } catch (FileNotFoundException e) { - out.println("Caught expected: " + e); - } - var path = Path.of("fileNotFound.txt"); - try { - Files.deleteIfExists(path); - BodyPublishers.ofFile(path); - fail(); - } catch (FileNotFoundException e) { - out.println("Caught expected: " + e); - } - } - private static final int ITERATION_COUNT = 3; private void send(String uriString, diff --git a/test/jdk/java/net/httpclient/FlowAdapterPublisherTest.java b/test/jdk/java/net/httpclient/FlowAdapterPublisherTest.java index ddefd2a9aa7..de511d6bd75 100644 --- a/test/jdk/java/net/httpclient/FlowAdapterPublisherTest.java +++ b/test/jdk/java/net/httpclient/FlowAdapterPublisherTest.java @@ -33,7 +33,6 @@ import java.nio.MappedByteBuffer; import java.util.Arrays; import java.util.Optional; import java.util.concurrent.Flow; -import java.util.concurrent.Flow.Publisher; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; @@ -55,8 +54,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static java.net.http.HttpRequest.BodyPublishers.fromPublisher; import static java.net.http.HttpResponse.BodyHandlers.ofString; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertThrows; -import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; /* @@ -123,20 +120,6 @@ public class FlowAdapterPublisherTest implements HttpServerAdapters { return builder; } - @Test - public void testAPIExceptions() { - assertThrows(NPE, () -> fromPublisher(null)); - assertThrows(NPE, () -> fromPublisher(null, 1)); - assertThrows(IAE, () -> fromPublisher(new BBPublisher(), 0)); - assertThrows(IAE, () -> fromPublisher(new BBPublisher(), -1)); - assertThrows(IAE, () -> fromPublisher(new BBPublisher(), Long.MIN_VALUE)); - - Publisher publisher = fromPublisher(new BBPublisher()); - assertThrows(NPE, () -> publisher.subscribe(null)); - } - - // Flow.Publisher - @Test(dataProvider = "uris") void testByteBufferPublisherUnknownLength(String uri) { String[] body = new String[] { "You know ", "it's summer ", "in Ireland ", diff --git a/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfByteArraysTest.java b/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfByteArraysTest.java index 6b852407907..ab051d2020f 100644 --- a/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfByteArraysTest.java +++ b/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfByteArraysTest.java @@ -44,7 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; /* * @test - * @bug 8364733 + * @bug 8226303 8364733 * @summary Verify all specified `HttpRequest.BodyPublishers::ofByteArrays` behavior * @build ByteBufferUtils * RecordingSubscriber diff --git a/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfFileTest.java b/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfFileTest.java index 7705faef109..94b5a596fc6 100644 --- a/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfFileTest.java +++ b/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfFileTest.java @@ -49,7 +49,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /* * @test - * @bug 8364733 + * @bug 8226303 8235459 8358688 8364733 * @summary Verify all specified `HttpRequest.BodyPublishers::ofFile` behavior * @build ByteBufferUtils * RecordingSubscriber diff --git a/test/jdk/java/net/httpclient/RelayingPublishers.java b/test/jdk/java/net/httpclient/RelayingPublishers.java deleted file mode 100644 index 83d74cd13ae..00000000000 --- a/test/jdk/java/net/httpclient/RelayingPublishers.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import jdk.test.lib.util.FileUtils; -import org.testng.annotations.Test; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.net.http.HttpRequest.BodyPublisher; -import java.net.http.HttpRequest.BodyPublishers; -import java.nio.ByteBuffer; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Flow; - -import static org.testng.Assert.assertEquals; - -/* - * @test - * @summary Verifies that some of the standard BodyPublishers relay exception - * rather than throw it - * @bug 8226303 8358688 - * @library /test/lib - * @run testng/othervm RelayingPublishers - */ -public class RelayingPublishers { - - @Test - public void ofFile0() throws IOException { - Path directory = Files.createDirectory(Path.of("d")); - // Even though the path exists, the publisher should not be able - // to read from it, as that path denotes a directory, not a file - BodyPublisher pub = BodyPublishers.ofFile(directory); - CompletableSubscriber s = new CompletableSubscriber<>(); - pub.subscribe(s); - s.future().join(); - // Interestingly enough, it's FileNotFoundException if a file - // is a directory - assertEquals(s.future().join().getClass(), FileNotFoundException.class); - } - - @Test - public void ofFile1() throws IOException { - Path file = Files.createFile(Path.of("f")); - BodyPublisher pub = BodyPublishers.ofFile(file); - FileUtils.deleteFileWithRetry(file); - CompletableSubscriber s = new CompletableSubscriber<>(); - pub.subscribe(s); - assertEquals(s.future().join().getClass(), FileNotFoundException.class); - } - - @Test - public void ofByteArrays() { - List bytes = new ArrayList<>(); - bytes.add(null); - BodyPublisher pub = BodyPublishers.ofByteArrays(bytes); - CompletableSubscriber s = new CompletableSubscriber<>(); - pub.subscribe(s); - assertEquals(s.future().join().getClass(), NullPointerException.class); - } - - static class CompletableSubscriber implements Flow.Subscriber { - - final CompletableFuture f = new CompletableFuture<>(); - - @Override - public void onSubscribe(Flow.Subscription subscription) { - subscription.request(1); - } - - @Override - public void onNext(T item) { - f.completeExceptionally(new RuntimeException("Unexpected onNext")); - } - - @Override - public void onError(Throwable throwable) { - f.complete(throwable); - } - - @Override - public void onComplete() { - f.completeExceptionally(new RuntimeException("Unexpected onNext")); - } - - CompletableFuture future() { - return f.copy(); - } - } -} diff --git a/test/jdk/java/net/httpclient/SubscriberPublisherAPIExceptions.java b/test/jdk/java/net/httpclient/SubscriberAPIExceptions.java similarity index 75% rename from test/jdk/java/net/httpclient/SubscriberPublisherAPIExceptions.java rename to test/jdk/java/net/httpclient/SubscriberAPIExceptions.java index 62027095810..bf0f7c0bda7 100644 --- a/test/jdk/java/net/httpclient/SubscriberPublisherAPIExceptions.java +++ b/test/jdk/java/net/httpclient/SubscriberAPIExceptions.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,7 +21,6 @@ * questions. */ -import java.io.FileNotFoundException; import java.nio.ByteBuffer; import java.nio.file.Files; import java.nio.file.Path; @@ -30,17 +29,15 @@ import java.nio.file.OpenOption; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Flow; -import java.net.http.HttpHeaders; -import java.net.http.HttpRequest.BodyPublishers; import java.net.http.HttpResponse.BodyHandler; import java.net.http.HttpResponse.ResponseInfo; import java.net.http.HttpResponse.BodyHandlers; import java.net.http.HttpResponse.BodySubscriber; import java.net.http.HttpResponse.BodySubscribers; import java.util.function.Function; -import org.testng.annotations.DataProvider; + import org.testng.annotations.Test; -import static java.nio.charset.StandardCharsets.UTF_8; + import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE; import static java.nio.file.StandardOpenOption.WRITE; @@ -49,53 +46,17 @@ import static org.testng.Assert.assertThrows; /* * @test - * @summary Basic tests for API specified exceptions from Publisher, Handler, + * @summary Basic tests for API specified exceptions from Handler, * and Subscriber convenience static factory methods. - * @run testng SubscriberPublisherAPIExceptions + * @run testng SubscriberAPIExceptions */ -public class SubscriberPublisherAPIExceptions { +public class SubscriberAPIExceptions { static final Class NPE = NullPointerException.class; static final Class IAE = IllegalArgumentException.class; static final Class IOB = IndexOutOfBoundsException.class; - @Test - public void publisherAPIExceptions() { - assertThrows(NPE, () -> BodyPublishers.ofByteArray(null)); - assertThrows(NPE, () -> BodyPublishers.ofByteArray(null, 0, 1)); - assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100], 0, 101)); - assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100], 1, 100)); - assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100], -1, 10)); - assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[100], 99, 2)); - assertThrows(IOB, () -> BodyPublishers.ofByteArray(new byte[1], -100, 1)); - assertThrows(NPE, () -> BodyPublishers.ofByteArray(null)); - assertThrows(NPE, () -> BodyPublishers.ofFile(null)); - assertThrows(NPE, () -> BodyPublishers.ofInputStream(null)); - assertThrows(NPE, () -> BodyPublishers.ofString(null)); - assertThrows(NPE, () -> BodyPublishers.ofString("A", null)); - assertThrows(NPE, () -> BodyPublishers.ofString(null, UTF_8)); - assertThrows(NPE, () -> BodyPublishers.ofString(null, null)); - } - - @DataProvider(name = "nonExistentFiles") - public Object[][] nonExistentFiles() { - List paths = List.of(Paths.get("doesNotExist"), - Paths.get("tsixEtoNseod"), - Paths.get("doesNotExist2")); - paths.forEach(p -> { - if (Files.exists(p)) - throw new AssertionError("Unexpected " + p); - }); - - return paths.stream().map(p -> new Object[] { p }).toArray(Object[][]::new); - } - - @Test(dataProvider = "nonExistentFiles", expectedExceptions = FileNotFoundException.class) - public void fromFileCheck(Path path) throws Exception { - BodyPublishers.ofFile(path); - } - @Test public void handlerAPIExceptions() throws Exception { Path path = Paths.get(".").resolve("tt");