11/*
2- * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2021, 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
2828 * with HttpURLConnection and HttpClient
2929 * @modules jdk.httpserver
3030 * @library /test/lib
31- * @run testng /othervm BasicAuthenticatorRealm
31+ * @run junit /othervm BasicAuthenticatorRealm
3232 */
3333
3434import com .sun .net .httpserver .BasicAuthenticator ;
4747import java .net .http .HttpResponse .BodyHandlers ;
4848
4949import jdk .test .lib .net .URIBuilder ;
50- import org .testng .annotations .Test ;
5150
5251import static java .net .http .HttpClient .Builder .NO_PROXY ;
5352import static java .nio .charset .StandardCharsets .UTF_8 ;
54- import static org .testng .Assert .assertEquals ;
5553
54+ import org .junit .jupiter .api .*;
55+ import static org .junit .jupiter .api .Assertions .assertEquals ;
56+ import org .junit .jupiter .api .Test ;
57+
58+ /**
59+ * The second test @Order(2) must run after the first test because it
60+ * sets a VM wide authenticator and the first test depends on no authenticator
61+ * being set.
62+ */
63+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
5664public class BasicAuthenticatorRealm {
5765
5866 static final String REALM = "U\u00ff U@realm" ; // non-ASCII char
@@ -61,7 +69,8 @@ public class BasicAuthenticatorRealm {
6169 static final InetAddress LOOPBACK_ADDR = InetAddress .getLoopbackAddress ();
6270
6371 @ Test
64- public static void testURLConnection () throws Exception {
72+ @ Order (1 )
73+ public void testURLConnection () throws Exception {
6574 var server = HttpServer .create (new InetSocketAddress (LOOPBACK_ADDR , 0 ), 0 );
6675 var handler = HttpHandlers .of (200 , Headers .of (), "" );
6776 var context = server .createContext ("/test" , handler );
@@ -73,15 +82,16 @@ public static void testURLConnection() throws Exception {
7382 server .start ();
7483 var url = uri (server ).toURL ();
7584 var connection = (HttpURLConnection )url .openConnection (Proxy .NO_PROXY );
76- assertEquals (connection .getResponseCode (), 401 );
77- assertEquals (connection .getHeaderField ("WWW-Authenticate" ), EXPECTED_AUTH_HEADER_VALUE );
85+ assertEquals (401 , connection .getResponseCode ());
86+ assertEquals (EXPECTED_AUTH_HEADER_VALUE , connection .getHeaderField ("WWW-Authenticate" ));
7887 } finally {
7988 server .stop (0 );
8089 }
8190 }
8291
8392 @ Test
84- public static void testURLConnectionAuthenticated () throws Exception {
93+ @ Order (2 )
94+ public void testURLConnectionAuthenticated () throws Exception {
8595 var server = HttpServer .create (new InetSocketAddress (LOOPBACK_ADDR , 0 ), 0 );
8696 var handler = HttpHandlers .of (200 , Headers .of (), "foo" );
8797 var context = server .createContext ("/test" , handler );
@@ -94,15 +104,16 @@ public static void testURLConnectionAuthenticated() throws Exception {
94104 server .start ();
95105 var url = uri (server ).toURL ();
96106 var connection = (HttpURLConnection )url .openConnection (Proxy .NO_PROXY );
97- assertEquals (connection .getResponseCode (), 200 );
98- assertEquals ( connection . getInputStream (). readAllBytes (), "foo" .getBytes (UTF_8 ));
107+ assertEquals (200 , connection .getResponseCode ());
108+ Assertions . assertArrayEquals ( "foo" .getBytes (UTF_8 ), connection . getInputStream (). readAllBytes ( ));
99109 } finally {
100110 server .stop (0 );
101111 }
102112 }
103113
104114 @ Test
105- public static void testHttpClient () throws Exception {
115+ @ Order (3 )
116+ public void testHttpClient () throws Exception {
106117 var server = HttpServer .create (new InetSocketAddress (LOOPBACK_ADDR , 0 ), 0 );
107118 var client = HttpClient .newBuilder ().proxy (NO_PROXY ).build ();
108119 var request = HttpRequest .newBuilder (uri (server )).build ();
@@ -115,15 +126,16 @@ public static void testHttpClient() throws Exception {
115126 try {
116127 server .start ();
117128 var response = client .send (request , BodyHandlers .ofString (UTF_8 ));
118- assertEquals (response .statusCode (), 401 );
119- assertEquals (response .headers ().firstValue ("WWW-Authenticate" ).orElseThrow (), EXPECTED_AUTH_HEADER_VALUE );
129+ assertEquals (401 , response .statusCode ());
130+ assertEquals (EXPECTED_AUTH_HEADER_VALUE , response .headers ().firstValue ("WWW-Authenticate" ).orElseThrow ());
120131 } finally {
121132 server .stop (0 );
122133 }
123134 }
124135
125136 @ Test
126- public static void testHttpClientAuthenticated () throws Exception {
137+ @ Order (4 )
138+ public void testHttpClientAuthenticated () throws Exception {
127139 var server = HttpServer .create (new InetSocketAddress (LOOPBACK_ADDR , 0 ), 0 );
128140 var request = HttpRequest .newBuilder (uri (server )).build ();
129141 var handler = HttpHandlers .of (200 , Headers .of (), "foo" );
@@ -139,8 +151,8 @@ public static void testHttpClientAuthenticated() throws Exception {
139151 try {
140152 server .start ();
141153 var response = client .send (request , BodyHandlers .ofString (UTF_8 ));
142- assertEquals (response .statusCode (), 200 );
143- assertEquals (response .body (), "foo" );
154+ assertEquals (200 , response .statusCode ());
155+ assertEquals ("foo" , response .body ());
144156 } finally {
145157 server .stop (0 );
146158 }
0 commit comments