Skip to content

Commit 96e5c27

Browse files
8373893: Refactor networking http server tests to use JUnit
Reviewed-by: djelinski
1 parent 481ef1d commit 96e5c27

31 files changed

+940
-899
lines changed

test/jdk/com/sun/net/httpserver/BasicAuthenticatorRealm.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -28,7 +28,7 @@
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

3434
import com.sun.net.httpserver.BasicAuthenticator;
@@ -47,12 +47,20 @@
4747
import java.net.http.HttpResponse.BodyHandlers;
4848

4949
import jdk.test.lib.net.URIBuilder;
50-
import org.testng.annotations.Test;
5150

5251
import static java.net.http.HttpClient.Builder.NO_PROXY;
5352
import 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)
5664
public class BasicAuthenticatorRealm {
5765

5866
static final String REALM = "U\u00ffU@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
}

test/jdk/com/sun/net/httpserver/CreateHttpServerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 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
@@ -25,17 +25,17 @@
2525
* @test
2626
* @bug 8251496
2727
* @summary summary
28-
* @run testng/othervm CreateHttpServerTest
28+
* @run junit/othervm CreateHttpServerTest
2929
*/
3030

3131
import com.sun.net.httpserver.HttpServer;
32-
import org.testng.annotations.Test;
3332

3433
import java.io.IOException;
3534
import java.net.InetAddress;
3635
import java.net.InetSocketAddress;
3736

38-
import static org.testng.Assert.assertTrue;
37+
import static org.junit.jupiter.api.Assertions.assertTrue;
38+
import org.junit.jupiter.api.Test;
3939

4040
public class CreateHttpServerTest {
4141
@Test

test/jdk/com/sun/net/httpserver/DateFormatterTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 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
@@ -39,12 +39,13 @@
3939
import java.util.regex.Pattern;
4040

4141
import jdk.test.lib.net.URIBuilder;
42-
import org.testng.annotations.AfterTest;
43-
import org.testng.annotations.BeforeTest;
44-
import org.testng.annotations.Test;
4542
import static java.net.http.HttpResponse.BodyHandlers.ofString;
46-
import static org.testng.Assert.assertTrue;
47-
import static org.testng.Assert.fail;
43+
44+
import org.junit.jupiter.api.AfterAll;
45+
import static org.junit.jupiter.api.Assertions.assertTrue;
46+
import static org.junit.jupiter.api.Assertions.fail;
47+
import org.junit.jupiter.api.BeforeAll;
48+
import org.junit.jupiter.api.Test;
4849

4950
/**
5051
* @test
@@ -53,19 +54,19 @@
5354
* @modules java.net.http
5455
* @library /test/lib
5556
* @build DateFormatterTest
56-
* @run testng/othervm DateFormatterTest
57+
* @run junit/othervm DateFormatterTest
5758
*/
5859
public class DateFormatterTest {
5960

60-
private HttpServer server;
61+
private static HttpServer server;
6162

6263
static URI httpURI;
6364
static final Integer ITERATIONS = 10;
6465
static String format;
6566
static Pattern pattern;
6667

67-
@BeforeTest
68-
public void setUp() throws IOException, URISyntaxException {
68+
@BeforeAll
69+
public static void setUp() throws IOException, URISyntaxException {
6970
String days = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(,)";
7071
String dayNo = "(\\s\\d\\d\\s)";
7172
String month = "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
@@ -85,8 +86,8 @@ public void setUp() throws IOException, URISyntaxException {
8586
server.start();
8687
}
8788

88-
@AfterTest
89-
public void cleanUp() {
89+
@AfterAll
90+
public static void cleanUp() {
9091
server.stop(0);
9192
}
9293

0 commit comments

Comments
 (0)