mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-11 14:11:36 +00:00
8367068: Remove redundant HttpRequest.BodyPublisher tests
Reviewed-by: dfuchs
This commit is contained in:
parent
f993f90c86
commit
288822a5c2
@ -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,
|
||||
|
||||
@ -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<ByteBuffer>
|
||||
|
||||
@Test(dataProvider = "uris")
|
||||
void testByteBufferPublisherUnknownLength(String uri) {
|
||||
String[] body = new String[] { "You know ", "it's summer ", "in Ireland ",
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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<ByteBuffer> 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<ByteBuffer> s = new CompletableSubscriber<>();
|
||||
pub.subscribe(s);
|
||||
assertEquals(s.future().join().getClass(), FileNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofByteArrays() {
|
||||
List<byte[]> bytes = new ArrayList<>();
|
||||
bytes.add(null);
|
||||
BodyPublisher pub = BodyPublishers.ofByteArrays(bytes);
|
||||
CompletableSubscriber<ByteBuffer> s = new CompletableSubscriber<>();
|
||||
pub.subscribe(s);
|
||||
assertEquals(s.future().join().getClass(), NullPointerException.class);
|
||||
}
|
||||
|
||||
static class CompletableSubscriber<T> implements Flow.Subscriber<T> {
|
||||
|
||||
final CompletableFuture<Throwable> 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<Throwable> future() {
|
||||
return f.copy();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<NullPointerException> NPE = NullPointerException.class;
|
||||
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
|
||||
static final Class<IndexOutOfBoundsException> 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<Path> 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");
|
||||
Loading…
x
Reference in New Issue
Block a user