Skip to content

Commit 47a8faf

Browse files
rm-gh-8Paul Hohensee
authored andcommitted
8373869: Refactor java/net/httpclient/ThrowingPushPromises*.java tests to use JUnit5
Reviewed-by: phh Backport-of: e5ca778
1 parent 9c5338a commit 47a8faf

8 files changed

Lines changed: 111 additions & 99 deletions

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

Lines changed: 69 additions & 71 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;
@@ -95,18 +87,29 @@
9587
import static java.lang.String.format;
9688
import static java.net.http.HttpClient.Version.HTTP_2;
9789
import static java.nio.charset.StandardCharsets.UTF_8;
98-
import static org.testng.Assert.assertEquals;
99-
import static org.testng.Assert.assertTrue;
100-
90+
import org.junit.jupiter.api.AfterAll;
91+
import static org.junit.jupiter.api.Assertions.assertEquals;
92+
import static org.junit.jupiter.api.Assertions.assertTrue;
93+
94+
import org.junit.jupiter.api.Assumptions;
95+
import org.junit.jupiter.api.BeforeAll;
96+
import org.junit.jupiter.api.BeforeEach;
97+
import org.junit.jupiter.api.TestInstance;
98+
import org.junit.jupiter.api.extension.BeforeEachCallback;
99+
import org.junit.jupiter.api.extension.ExtensionContext;
100+
import org.junit.jupiter.api.extension.RegisterExtension;
101+
import org.junit.jupiter.api.extension.TestWatcher;
102+
103+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
101104
public abstract class AbstractThrowingPushPromises implements HttpServerAdapters {
102105

103-
SSLContext sslContext;
104-
HttpTestServer http2TestServer; // HTTP/2 ( h2c )
105-
HttpTestServer https2TestServer; // HTTP/2 ( h2 )
106-
String http2URI_fixed;
107-
String http2URI_chunk;
108-
String https2URI_fixed;
109-
String https2URI_chunk;
106+
static SSLContext sslContext;
107+
static HttpTestServer http2TestServer; // HTTP/2 ( h2c )
108+
static HttpTestServer https2TestServer; // HTTP/2 ( h2 )
109+
static String http2URI_fixed;
110+
static String http2URI_chunk;
111+
static String https2URI_fixed;
112+
static String https2URI_chunk;
110113

111114
static final int ITERATION_COUNT = 1;
112115
// a shared executor helps reduce the amount of threads created by the test
@@ -124,8 +127,34 @@ public static String now() {
124127
return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
125128
}
126129

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

130159
static class TestExecutor implements Executor {
131160
final AtomicLong tasks = new AtomicLong();
@@ -151,34 +180,13 @@ public void execute(Runnable command) {
151180
}
152181
}
153182

154-
protected boolean stopAfterFirstFailure() {
183+
protected static boolean stopAfterFirstFailure() {
155184
return Boolean.getBoolean("jdk.internal.httpclient.debug");
156185
}
157186

158-
final AtomicReference<SkipException> skiptests = new AtomicReference<>();
159-
void checkSkip() {
160-
var skip = skiptests.get();
161-
if (skip != null) throw skip;
162-
}
163-
static String name(ITestResult result) {
164-
var params = result.getParameters();
165-
return result.getName()
166-
+ (params == null ? "()" : Arrays.toString(result.getParameters()));
167-
}
168187

169-
@BeforeMethod
170-
void beforeMethod(ITestContext context) {
171-
if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {
172-
if (skiptests.get() == null) {
173-
SkipException skip = new SkipException("some tests failed");
174-
skip.setStackTrace(new StackTraceElement[0]);
175-
skiptests.compareAndSet(null, skip);
176-
}
177-
}
178-
}
179-
180-
@AfterClass
181-
static final void printFailedTests(ITestContext context) {
188+
@AfterAll
189+
static final void printFailedTests() {
182190
out.println("\n=========================");
183191
try {
184192
// Exceptions should already have been added to FAILURES
@@ -203,7 +211,7 @@ static final void printFailedTests(ITestContext context) {
203211
}
204212
}
205213

206-
private String[] uris() {
214+
private static String[] uris() {
207215
return new String[] {
208216
http2URI_fixed,
209217
http2URI_chunk,
@@ -212,8 +220,7 @@ private String[] uris() {
212220
};
213221
}
214222

215-
@DataProvider(name = "sanity")
216-
public Object[][] sanity() {
223+
public static Object[][] sanity() {
217224
String[] uris = uris();
218225
Object[][] result = new Object[uris.length * 2][];
219226

@@ -242,7 +249,7 @@ public void accept(Where where) {
242249
}
243250
}
244251

245-
private Object[][] variants(List<Thrower> throwers) {
252+
private static Object[][] variants(List<Thrower> throwers) {
246253
String[] uris = uris();
247254
// reduce traces by always using the same client if
248255
// stopAfterFirstFailure is requested.
@@ -262,25 +269,17 @@ private Object[][] variants(List<Thrower> throwers) {
262269
return result;
263270
}
264271

265-
@DataProvider(name = "ioVariants")
266-
public Object[][] ioVariants(ITestContext context) {
267-
if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {
268-
return new Object[0][];
269-
}
272+
public static Object[][] ioVariants() {
270273
return variants(List.of(
271274
new UncheckedIOExceptionThrower()));
272275
}
273276

274-
@DataProvider(name = "customVariants")
275-
public Object[][] customVariants(ITestContext context) {
276-
if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {
277-
return new Object[0][];
278-
}
277+
public static Object[][] customVariants() {
279278
return variants(List.of(
280279
new UncheckedCustomExceptionThrower()));
281280
}
282281

283-
private HttpClient makeNewClient() {
282+
private static HttpClient makeNewClient() {
284283
clientCount.incrementAndGet();
285284
return TRACKER.track(HttpClient.newBuilder()
286285
.proxy(HttpClient.Builder.NO_PROXY)
@@ -289,11 +288,11 @@ private HttpClient makeNewClient() {
289288
.build());
290289
}
291290

292-
HttpClient newHttpClient(boolean share) {
291+
static HttpClient newHttpClient(boolean share) {
293292
if (!share) return makeNewClient();
294293
HttpClient shared = sharedClient;
295294
if (shared != null) return shared;
296-
synchronized (this) {
295+
synchronized (AbstractThrowingPushPromises.class) {
297296
shared = sharedClient;
298297
if (shared == null) {
299298
shared = sharedClient = makeNewClient();
@@ -331,13 +330,13 @@ public void applyPushPromise(HttpRequest initiatingRequest,
331330
HttpResponse<Stream<String>> response =
332331
client.sendAsync(req, BodyHandlers.ofLines(), pushHandler).get();
333332
String body = response.body().collect(Collectors.joining("|"));
334-
assertEquals(URI.create(body).getPath(), URI.create(uri).getPath());
333+
assertEquals(URI.create(uri).getPath(), URI.create(body).getPath());
335334
for (HttpRequest promised : pushPromises.keySet()) {
336335
out.printf("%s Received promise: %s%n\tresponse: %s%n",
337336
now(), promised, pushPromises.get(promised).get());
338337
String promisedBody = pushPromises.get(promised).get().body()
339338
.collect(Collectors.joining("|"));
340-
assertEquals(promisedBody, promised.uri().toASCIIString());
339+
assertEquals(promised.uri().toASCIIString(), promisedBody);
341340
}
342341
assertEquals(3, pushPromises.size());
343342
if (!sameClient) {
@@ -402,7 +401,6 @@ private <T,U> void testThrowing(String name, String uri, boolean sameClient,
402401
Finisher finisher, Thrower thrower)
403402
throws Exception
404403
{
405-
checkSkip();
406404
out.printf("%n%s%s%n", now(), name);
407405
try {
408406
testThrowing(uri, sameClient, handlers, finisher, thrower);
@@ -577,9 +575,9 @@ private final <T> List<String> check(Where w, URI reqURI,
577575
default:
578576
expectedCount = 3;
579577
}
580-
assertEquals(promises.size(), expectedCount,
578+
assertEquals(expectedCount, promises.size(),
581579
"bad promise count for " + reqURI + " with " + w);
582-
assertEquals(result, List.of(reqURI.toASCIIString()));
580+
assertEquals(List.of(reqURI.toASCIIString()), result);
583581
return result;
584582
}
585583

@@ -732,8 +730,8 @@ public CompletionStage<T> getBody() {
732730
}
733731

734732

735-
@BeforeTest
736-
public void setup() throws Exception {
733+
@BeforeAll
734+
public static void setup() throws Exception {
737735
sslContext = new SimpleSSLContext().get();
738736
if (sslContext == null)
739737
throw new AssertionError("Unexpected null sslContext");
@@ -759,8 +757,8 @@ public void setup() throws Exception {
759757
https2TestServer.start();
760758
}
761759

762-
@AfterTest
763-
public void teardown() throws Exception {
760+
@AfterAll
761+
public static void teardown() throws Exception {
764762
String sharedClientName =
765763
sharedClient == null ? null : sharedClient.toString();
766764
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);

test/jdk/java/net/httpclient/ThrowingPushPromisesAsLinesIO.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 ThrowingPushPromisesAsLinesIO
3232
* jdk.httpclient.test.lib.common.HttpServerAdapters
33-
* @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsLinesIO
33+
* @run junit/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsLinesIO
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 ThrowingPushPromisesAsLinesIO extends AbstractThrowingPushPromises {
3940

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

test/jdk/java/net/httpclient/ThrowingPushPromisesAsStringCustom.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 ThrowingPushPromisesAsStringCustom
3232
* jdk.httpclient.test.lib.common.HttpServerAdapters
33-
* @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsStringCustom
33+
* @run junit/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromisesAsStringCustom
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 ThrowingPushPromisesAsStringCustom extends AbstractThrowingPushPromises {
3940

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

0 commit comments

Comments
 (0)