11/*
2- * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2020, 2021, 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
4747import java .net .http .HttpResponse ;
4848import java .net .http .HttpResponse .BodyHandlers ;
4949import java .nio .ByteBuffer ;
50+ import java .util .Arrays ;
5051import java .util .LinkedHashMap ;
5152import java .util .List ;
5253import java .util .Map ;
6364import java .util .concurrent .TimeUnit ;
6465import java .util .concurrent .TimeoutException ;
6566import java .util .concurrent .atomic .AtomicLong ;
67+ import java .util .concurrent .atomic .AtomicReference ;
6668import java .util .function .Consumer ;
6769import java .util .function .Supplier ;
6870import java .util .stream .Collectors ;
7678import jdk .test .lib .net .SimpleSSLContext ;
7779import org .testng .Assert ;
7880import org .testng .ITestContext ;
81+ import org .testng .ITestResult ;
82+ import org .testng .SkipException ;
7983import org .testng .annotations .AfterClass ;
8084import org .testng .annotations .AfterTest ;
8185import org .testng .annotations .BeforeMethod ;
@@ -151,17 +155,36 @@ protected boolean stopAfterFirstFailure() {
151155 return Boolean .getBoolean ("jdk.internal.httpclient.debug" );
152156 }
153157
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+ }
168+
154169 @ BeforeMethod
155170 void beforeMethod (ITestContext context ) {
156171 if (stopAfterFirstFailure () && context .getFailedTests ().size () > 0 ) {
157- throw new RuntimeException ("some tests failed" );
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+ }
158177 }
159178 }
160179
161180 @ AfterClass
162- static final void printFailedTests () {
181+ static final void printFailedTests (ITestContext context ) {
163182 out .println ("\n =========================" );
164183 try {
184+ var failed = context .getFailedTests ().getAllResults ().stream ()
185+ .collect (Collectors .toMap (r -> name (r ), ITestResult ::getThrowable ));
186+ FAILURES .putAll (failed );
187+
165188 out .printf ("%n%sCreated %d servers and %d clients%n" ,
166189 now (), serverCount .get (), clientCount .get ());
167190 if (FAILURES .isEmpty ()) return ;
@@ -446,13 +469,15 @@ static PublishWithError withNoError(List<String> content) {
446469
447470 @ Test (dataProvider = "sparseContent" ) // checks that NPE is thrown
448471 public void testNullPointerException (String description , String [] content ) {
472+ checkSkip ();
449473 BodyPublisher [] publishers = publishers (content );
450474 Assert .assertThrows (NullPointerException .class , () -> BodyPublishers .concat (publishers ));
451475 }
452476
453477 // Verifies that an empty array creates a "noBody" publisher
454478 @ Test
455479 public void testEmpty () {
480+ checkSkip ();
456481 BodyPublisher publisher = BodyPublishers .concat ();
457482 RequestSubscriber subscriber = new RequestSubscriber ();
458483 assertEquals (publisher .contentLength (), 0 );
@@ -466,6 +491,7 @@ public void testEmpty() {
466491 // verifies that error emitted by upstream publishers are propagated downstream.
467492 @ Test (dataProvider = "sparseContent" ) // nulls are replaced with error publisher
468493 public void testOnError (String description , String [] content ) {
494+ checkSkip ();
469495 final RequestSubscriber subscriber = new RequestSubscriber ();
470496 final PublishWithError errorPublisher ;
471497 final BodyPublisher [] publishers ;
@@ -521,6 +547,7 @@ public void testOnError(String description, String[] content) {
521547 // the length should be known.
522548 @ Test (dataProvider = "sparseContent" ) // nulls are replaced with unknown length
523549 public void testUnknownContentLength (String description , String [] content ) {
550+ checkSkip ();
524551 if (content == null ) {
525552 content = BODIES .toArray (String []::new );
526553 description = "BODIES (known length)" ;
@@ -561,6 +588,7 @@ private static final Throwable completionCause(CompletionException x) {
561588
562589 @ Test (dataProvider = "negativeRequests" )
563590 public void testNegativeRequest (long n ) {
591+ checkSkip ();
564592 assert n <= 0 : "test for negative request called with n > 0 : " + n ;
565593 BodyPublisher [] publishers = ContentLengthPublisher .of (List .of (1L , 2L , 3L ));
566594 BodyPublisher publisher = BodyPublishers .concat (publishers );
@@ -584,6 +612,7 @@ static BodyPublisher[] ofStrings(String... strings) {
584612
585613 @ Test
586614 public void testPositiveRequests () {
615+ checkSkip ();
587616 // A composite array of publishers
588617 BodyPublisher [] publishers = Stream .of (
589618 Stream .of (ofStrings ("Lorem" , " " , "ipsum" , " " )),
@@ -626,6 +655,7 @@ public void testPositiveRequests() {
626655
627656 @ Test (dataProvider = "contentLengths" )
628657 public void testContentLength (long expected , List <Long > lengths ) {
658+ checkSkip ();
629659 BodyPublisher [] publishers = ContentLengthPublisher .of (lengths );
630660 BodyPublisher aggregate = BodyPublishers .concat (publishers );
631661 assertEquals (aggregate .contentLength (), expected ,
@@ -636,6 +666,7 @@ public void testContentLength(long expected, List<Long> lengths) {
636666 // publishers are no longer subscribed etc...
637667 @ Test
638668 public void testCancel () {
669+ checkSkip ();
639670 BodyPublisher [] publishers = BODIES .stream ()
640671 .map (BodyPublishers ::ofString )
641672 .toArray (BodyPublisher []::new );
@@ -695,6 +726,7 @@ public void testCancel() {
695726 // Verifies that cancelling the subscription is propagated downstream
696727 @ Test
697728 public void testCancelSubscription () {
729+ checkSkip ();
698730 PublishWithError upstream = new PublishWithError (BODIES , BODIES .size (),
699731 () -> new AssertionError ("should not come here" ));
700732 BodyPublisher publisher = BodyPublishers .concat (upstream );
@@ -756,6 +788,7 @@ public void testCancelSubscription() {
756788
757789 @ Test (dataProvider = "variants" )
758790 public void test (String uri , boolean sameClient ) throws Exception {
791+ checkSkip ();
759792 System .out .println ("Request to " + uri );
760793
761794 HttpClient client = newHttpClient (sameClient );
0 commit comments