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
4141import 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
5143import javax .net .ssl .SSLContext ;
5244import java .io .BufferedReader ;
10092import static java .net .http .HttpOption .Http3DiscoveryMode .HTTP_3_URI_ONLY ;
10193import static java .net .http .HttpOption .H3_DISCOVERY ;
10294import 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 )
106109public 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\t Aborting due to %s%n%n" ,
148+ now (), context .getDisplayName (), cause );
149+ System .err .printf ("%nTEST FAILED: %s%s%n\t Aborting 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\t response: %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 ;
0 commit comments