Skip to content

Commit e5ca778

Browse files
committed
8373869: Refactor java/net/httpclient/ThrowingPushPromises*.java tests to use JUnit5
Reviewed-by: jpai
1 parent 3f20eb9 commit e5ca778

8 files changed

+119
-107
lines changed

test/jdk/java/net/httpclient/AbstractThrowingPushPromises.java

Lines changed: 77 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,10 @@
3535
* ReferenceTracker AbstractThrowingPushPromises
3636
* jdk.httpclient.test.lib.common.HttpServerAdapters
3737
* <concrete-class-name>
38-
* @run testng/othervm -Djdk.internal.httpclient.debug=true <concrete-class-name>
38+
* @run junit/othervm -Djdk.internal.httpclient.debug=true <concrete-class-name>
3939
*/
4040

4141
import jdk.test.lib.net.SimpleSSLContext;
42-
import org.testng.ITestContext;
43-
import org.testng.ITestResult;
44-
import org.testng.SkipException;
45-
import org.testng.annotations.AfterTest;
46-
import org.testng.annotations.AfterClass;
47-
import org.testng.annotations.BeforeMethod;
48-
import org.testng.annotations.BeforeTest;
49-
import org.testng.annotations.DataProvider;
5042

5143
import javax.net.ssl.SSLContext;
5244
import java.io.BufferedReader;
@@ -100,21 +92,32 @@
10092
import static java.net.http.HttpOption.Http3DiscoveryMode.HTTP_3_URI_ONLY;
10193
import static java.net.http.HttpOption.H3_DISCOVERY;
10294
import static java.nio.charset.StandardCharsets.UTF_8;
103-
import static org.testng.Assert.assertEquals;
104-
import static org.testng.Assert.assertTrue;
105-
95+
import org.junit.jupiter.api.AfterAll;
96+
import static org.junit.jupiter.api.Assertions.assertEquals;
97+
import static org.junit.jupiter.api.Assertions.assertTrue;
98+
99+
import org.junit.jupiter.api.Assumptions;
100+
import org.junit.jupiter.api.BeforeAll;
101+
import org.junit.jupiter.api.BeforeEach;
102+
import org.junit.jupiter.api.TestInstance;
103+
import org.junit.jupiter.api.extension.BeforeEachCallback;
104+
import org.junit.jupiter.api.extension.ExtensionContext;
105+
import org.junit.jupiter.api.extension.RegisterExtension;
106+
import org.junit.jupiter.api.extension.TestWatcher;
107+
108+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
106109
public abstract class AbstractThrowingPushPromises implements HttpServerAdapters {
107110

108-
SSLContext sslContext;
109-
HttpTestServer http2TestServer; // HTTP/2 ( h2c )
110-
HttpTestServer https2TestServer; // HTTP/2 ( h2 )
111-
HttpTestServer http3TestServer; // HTTP/3 ( h3 )
112-
String http2URI_fixed;
113-
String http2URI_chunk;
114-
String https2URI_fixed;
115-
String https2URI_chunk;
116-
String http3URI_fixed;
117-
String http3URI_chunk;
111+
static SSLContext sslContext;
112+
static HttpTestServer http2TestServer; // HTTP/2 ( h2c )
113+
static HttpTestServer https2TestServer; // HTTP/2 ( h2 )
114+
static HttpTestServer http3TestServer; // HTTP/3 ( h3 )
115+
static String http2URI_fixed;
116+
static String http2URI_chunk;
117+
static String https2URI_fixed;
118+
static String https2URI_chunk;
119+
static String http3URI_fixed;
120+
static String http3URI_chunk;
118121

119122
static final int ITERATION_COUNT = 1;
120123
// a shared executor helps reduce the amount of threads created by the test
@@ -132,8 +135,34 @@ public static String now() {
132135
return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
133136
}
134137

135-
final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
136-
private volatile HttpClient sharedClient;
138+
static final class TestStopper implements TestWatcher, BeforeEachCallback {
139+
final AtomicReference<String> failed = new AtomicReference<>();
140+
TestStopper() { }
141+
@Override
142+
public void testFailed(ExtensionContext context, Throwable cause) {
143+
if (stopAfterFirstFailure()) {
144+
String msg = "Aborting due to: " + cause;
145+
failed.compareAndSet(null, msg);
146+
FAILURES.putIfAbsent(context.getDisplayName(), cause);
147+
System.out.printf("%nTEST FAILED: %s%s%n\tAborting due to %s%n%n",
148+
now(), context.getDisplayName(), cause);
149+
System.err.printf("%nTEST FAILED: %s%s%n\tAborting due to %s%n%n",
150+
now(), context.getDisplayName(), cause);
151+
}
152+
}
153+
154+
@Override
155+
public void beforeEach(ExtensionContext context) {
156+
String msg = failed.get();
157+
Assumptions.assumeTrue(msg == null, msg);
158+
}
159+
}
160+
161+
@RegisterExtension
162+
static final TestStopper stopper = new TestStopper();
163+
164+
static final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
165+
private static volatile HttpClient sharedClient;
137166

138167
static class TestExecutor implements Executor {
139168
final AtomicLong tasks = new AtomicLong();
@@ -159,34 +188,13 @@ public void execute(Runnable command) {
159188
}
160189
}
161190

162-
protected boolean stopAfterFirstFailure() {
191+
protected static boolean stopAfterFirstFailure() {
163192
return Boolean.getBoolean("jdk.internal.httpclient.debug");
164193
}
165194

166-
final AtomicReference<SkipException> skiptests = new AtomicReference<>();
167-
void checkSkip() {
168-
var skip = skiptests.get();
169-
if (skip != null) throw skip;
170-
}
171-
static String name(ITestResult result) {
172-
var params = result.getParameters();
173-
return result.getName()
174-
+ (params == null ? "()" : Arrays.toString(result.getParameters()));
175-
}
176195

177-
@BeforeMethod
178-
void beforeMethod(ITestContext context) {
179-
if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {
180-
if (skiptests.get() == null) {
181-
SkipException skip = new SkipException("some tests failed");
182-
skip.setStackTrace(new StackTraceElement[0]);
183-
skiptests.compareAndSet(null, skip);
184-
}
185-
}
186-
}
187-
188-
@AfterClass
189-
static final void printFailedTests(ITestContext context) {
196+
@AfterAll
197+
static final void printFailedTests() {
190198
out.println("\n=========================");
191199
try {
192200
// Exceptions should already have been added to FAILURES
@@ -211,7 +219,7 @@ static final void printFailedTests(ITestContext context) {
211219
}
212220
}
213221

214-
private String[] uris() {
222+
private static String[] uris() {
215223
return new String[] {
216224
http3URI_fixed,
217225
http3URI_chunk,
@@ -222,8 +230,7 @@ private String[] uris() {
222230
};
223231
}
224232

225-
@DataProvider(name = "sanity")
226-
public Object[][] sanity() {
233+
public static Object[][] sanity() {
227234
String[] uris = uris();
228235
Object[][] result = new Object[uris.length * 2][];
229236

@@ -252,7 +259,7 @@ public void accept(Where where) {
252259
}
253260
}
254261

255-
private Object[][] variants(List<Thrower> throwers) {
262+
private static Object[][] variants(List<Thrower> throwers) {
256263
String[] uris = uris();
257264
// reduce traces by always using the same client if
258265
// stopAfterFirstFailure is requested.
@@ -272,39 +279,31 @@ private Object[][] variants(List<Thrower> throwers) {
272279
return result;
273280
}
274281

275-
@DataProvider(name = "ioVariants")
276-
public Object[][] ioVariants(ITestContext context) {
277-
if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {
278-
return new Object[0][];
279-
}
282+
public static Object[][] ioVariants() {
280283
return variants(List.of(
281284
new UncheckedIOExceptionThrower()));
282285
}
283286

284-
@DataProvider(name = "customVariants")
285-
public Object[][] customVariants(ITestContext context) {
286-
if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {
287-
return new Object[0][];
288-
}
287+
public static Object[][] customVariants() {
289288
return variants(List.of(
290289
new UncheckedCustomExceptionThrower()));
291290
}
292291

293-
private HttpClient makeNewClient() {
292+
private static HttpClient makeNewClient() {
294293
clientCount.incrementAndGet();
295-
return TRACKER.track(newClientBuilderForH3()
294+
return TRACKER.track(HttpServerAdapters.createClientBuilderForH3()
296295
.version(HTTP_3)
297296
.proxy(HttpClient.Builder.NO_PROXY)
298297
.executor(executor)
299298
.sslContext(sslContext)
300299
.build());
301300
}
302301

303-
HttpClient newHttpClient(boolean share) {
302+
static HttpClient newHttpClient(boolean share) {
304303
if (!share) return makeNewClient();
305304
HttpClient shared = sharedClient;
306305
if (shared != null) return shared;
307-
synchronized (this) {
306+
synchronized (AbstractThrowingPushPromises.class) {
308307
shared = sharedClient;
309308
if (shared == null) {
310309
shared = sharedClient = makeNewClient();
@@ -313,15 +312,15 @@ HttpClient newHttpClient(boolean share) {
313312
}
314313
}
315314

316-
Http3DiscoveryMode config(String uri) {
315+
static Http3DiscoveryMode config(String uri) {
317316
return uri.contains("/http3/") ? HTTP_3_URI_ONLY : null;
318317
}
319318

320-
Version version(String uri) {
319+
static Version version(String uri) {
321320
return uri.contains("/http3/") ? HTTP_3 : HTTP_2;
322321
}
323322

324-
HttpRequest request(String uri) {
323+
static HttpRequest request(String uri) {
325324
var builder = HttpRequest.newBuilder(URI.create(uri))
326325
.version(version(uri));
327326
var config = config(uri);
@@ -358,15 +357,15 @@ public void applyPushPromise(HttpRequest initiatingRequest,
358357
HttpResponse<Stream<String>> response =
359358
client.sendAsync(req, BodyHandlers.ofLines(), pushHandler).get();
360359
String body = response.body().collect(Collectors.joining("|"));
361-
assertEquals(URI.create(body).getPath(), URI.create(uri).getPath());
360+
assertEquals(URI.create(uri).getPath(), URI.create(body).getPath());
362361
for (HttpRequest promised : pushPromises.keySet()) {
363362
out.printf("%s Received promise: %s%n\tresponse: %s%n",
364363
now(), promised, pushPromises.get(promised).get());
365364
String promisedBody = pushPromises.get(promised).get().body()
366365
.collect(Collectors.joining("|"));
367-
assertEquals(promisedBody, promised.uri().toASCIIString());
366+
assertEquals(promised.uri().toASCIIString(), promisedBody);
368367
}
369-
assertEquals(pushPromises.size(), 3);
368+
assertEquals(3, pushPromises.size());
370369
if (!sameClient) {
371370
// Wait for the client to be garbage collected.
372371
// we use the ReferenceTracker API rather than HttpClient::close here,
@@ -429,7 +428,6 @@ private <T,U> void testThrowing(String name, String uri, boolean sameClient,
429428
Finisher finisher, Thrower thrower)
430429
throws Exception
431430
{
432-
checkSkip();
433431
out.printf("%n%s%s%n", now(), name);
434432
try {
435433
testThrowing(uri, sameClient, handlers, finisher, thrower);
@@ -603,9 +601,9 @@ private final <T> List<String> check(Where w, URI reqURI,
603601
default:
604602
expectedCount = 3;
605603
}
606-
assertEquals(promises.size(), expectedCount,
604+
assertEquals(expectedCount, promises.size(),
607605
"bad promise count for " + reqURI + " with " + w);
608-
assertEquals(result, List.of(reqURI.toASCIIString()));
606+
assertEquals(List.of(reqURI.toASCIIString()), result);
609607
return result;
610608
}
611609

@@ -758,8 +756,8 @@ public CompletionStage<T> getBody() {
758756
}
759757

760758

761-
@BeforeTest
762-
public void setup() throws Exception {
759+
@BeforeAll
760+
public static void setup() throws Exception {
763761
sslContext = new SimpleSSLContext().get();
764762
if (sslContext == null)
765763
throw new AssertionError("Unexpected null sslContext");
@@ -792,8 +790,8 @@ public void setup() throws Exception {
792790
http3TestServer.start();
793791
}
794792

795-
@AfterTest
796-
public void teardown() throws Exception {
793+
@AfterAll
794+
public static void teardown() throws Exception {
797795
String sharedClientName =
798796
sharedClient == null ? null : sharedClient.toString();
799797
sharedClient = null;

test/jdk/java/net/httpclient/ThrowingPushPromisesAsInputStreamCustom.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,14 +30,16 @@
3030
* @build jdk.test.lib.net.SimpleSSLContext
3131
* ReferenceTracker AbstractThrowingPushPromises ThrowingPushPromisesAsInputStreamCustom
3232
* jdk.httpclient.test.lib.common.HttpServerAdapters
33-
* @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsInputStreamCustom
33+
* @run junit/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsInputStreamCustom
3434
*/
3535

36-
import org.testng.annotations.Test;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.MethodSource;
3738

3839
public class ThrowingPushPromisesAsInputStreamCustom extends AbstractThrowingPushPromises {
3940

40-
@Test(dataProvider = "customVariants")
41+
@ParameterizedTest
42+
@MethodSource("customVariants")
4143
public void testThrowingAsInputStream(String uri, boolean sameClient, Thrower thrower)
4244
throws Exception {
4345
super.testThrowingAsInputStreamImpl(uri, sameClient, thrower);

test/jdk/java/net/httpclient/ThrowingPushPromisesAsInputStreamIO.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,14 +30,16 @@
3030
* @build jdk.test.lib.net.SimpleSSLContext
3131
* ReferenceTracker AbstractThrowingPushPromises ThrowingPushPromisesAsInputStreamIO
3232
* jdk.httpclient.test.lib.common.HttpServerAdapters
33-
* @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsInputStreamIO
33+
* @run junit/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsInputStreamIO
3434
*/
3535

36-
import org.testng.annotations.Test;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.MethodSource;
3738

3839
public class ThrowingPushPromisesAsInputStreamIO extends AbstractThrowingPushPromises {
3940

40-
@Test(dataProvider = "ioVariants")
41+
@ParameterizedTest
42+
@MethodSource("ioVariants")
4143
public void testThrowingAsInputStream(String uri, boolean sameClient, Thrower thrower)
4244
throws Exception {
4345
super.testThrowingAsInputStreamImpl(uri, sameClient, thrower);

test/jdk/java/net/httpclient/ThrowingPushPromisesAsLinesCustom.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,14 +30,16 @@
3030
* @build jdk.test.lib.net.SimpleSSLContext
3131
* ReferenceTracker AbstractThrowingPushPromises ThrowingPushPromisesAsLinesCustom
3232
* jdk.httpclient.test.lib.common.HttpServerAdapters
33-
* @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsLinesCustom
33+
* @run junit/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsLinesCustom
3434
*/
3535

36-
import org.testng.annotations.Test;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.MethodSource;
3738

3839
public class ThrowingPushPromisesAsLinesCustom extends AbstractThrowingPushPromises {
3940

40-
@Test(dataProvider = "customVariants")
41+
@ParameterizedTest
42+
@MethodSource("customVariants")
4143
public void testThrowingAsLines(String uri, boolean sameClient, Thrower thrower)
4244
throws Exception {
4345
super.testThrowingAsLinesImpl(uri, sameClient, thrower);

0 commit comments

Comments
 (0)