Skip to content

Commit 52a54da

Browse files
committed
8381616: Refactor various java/net/*Socket*/ TestNG tests to use JUnit
Reviewed-by: alanb
1 parent 988ec86 commit 52a54da

12 files changed

Lines changed: 378 additions & 305 deletions

test/jdk/java/net/DatagramSocketImpl/TestCreate.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2026, 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,10 +25,9 @@
2525
* @test
2626
* @bug 8238231
2727
* @summary test that DatagramSocket calls java.net.DatagramSocketImpl::create
28-
* @run testng/othervm TestCreate
28+
* @run junit/othervm ${test.main.class}
2929
*/
3030

31-
import org.testng.annotations.Test;
3231

3332
import java.io.IOException;
3433
import java.net.DatagramPacket;
@@ -44,7 +43,9 @@
4443
import java.util.List;
4544
import java.util.Set;
4645
import java.util.concurrent.atomic.AtomicBoolean;
47-
import static org.testng.Assert.assertTrue;
46+
47+
import org.junit.jupiter.api.Test;
48+
import static org.junit.jupiter.api.Assertions.assertTrue;
4849

4950
public class TestCreate {
5051

test/jdk/java/net/DatagramSocketImpl/TestDefaultBehavior.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2026, 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,7 +25,7 @@
2525
* @test
2626
* @bug 8224477
2727
* @summary Basic test for java.net.DatagramSocketImpl default behavior
28-
* @run testng TestDefaultBehavior
28+
* @run junit ${test.main.class}
2929
*/
3030

3131
import java.io.IOException;
@@ -36,11 +36,13 @@
3636
import java.net.SocketAddress;
3737
import java.net.SocketOption;
3838
import java.util.Set;
39-
import org.testng.annotations.Test;
4039
import static java.lang.Boolean.*;
4140
import static java.net.StandardSocketOptions.*;
42-
import static org.testng.Assert.assertEquals;
43-
import static org.testng.Assert.expectThrows;
41+
42+
import org.junit.jupiter.api.Test;
43+
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertThrows;
45+
4446

4547
public class TestDefaultBehavior {
4648

@@ -51,21 +53,21 @@ public class TestDefaultBehavior {
5153
public void datagramSocketImpl() {
5254
CustomDatagramSocketImpl dsi = new CustomDatagramSocketImpl();
5355

54-
assertEquals(dsi.supportedOptions().size(), 0);
55-
56-
expectThrows(NPE, () -> dsi.setOption(null, null));
57-
expectThrows(NPE, () -> dsi.setOption(null, 1));
58-
expectThrows(UOE, () -> dsi.setOption(SO_RCVBUF, 100));
59-
expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
60-
expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, FALSE));
61-
expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, TRUE));
62-
expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, FALSE));
63-
expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
64-
65-
expectThrows(NPE, () -> dsi.getOption(null));
66-
expectThrows(UOE, () -> dsi.getOption(SO_RCVBUF));
67-
expectThrows(UOE, () -> dsi.getOption(SO_KEEPALIVE));
68-
expectThrows(UOE, () -> dsi.getOption(FAKE_SOCK_OPT));
56+
assertEquals(0, dsi.supportedOptions().size());
57+
58+
assertThrows(NPE, () -> dsi.setOption(null, null));
59+
assertThrows(NPE, () -> dsi.setOption(null, 1));
60+
assertThrows(UOE, () -> dsi.setOption(SO_RCVBUF, 100));
61+
assertThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
62+
assertThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, FALSE));
63+
assertThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, TRUE));
64+
assertThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, FALSE));
65+
assertThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
66+
67+
assertThrows(NPE, () -> dsi.getOption(null));
68+
assertThrows(UOE, () -> dsi.getOption(SO_RCVBUF));
69+
assertThrows(UOE, () -> dsi.getOption(SO_KEEPALIVE));
70+
assertThrows(UOE, () -> dsi.getOption(FAKE_SOCK_OPT));
6971
}
7072

7173
static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() {

test/jdk/java/net/MulticastSocket/Constructor.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, 2026, 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
@@ -24,15 +24,15 @@
2424
/* @test
2525
* @bug 8243999
2626
* @summary Checks to ensure that Multicast constructors behave as expected
27-
* @run testng Constructor
27+
* @run junit ${test.main.class}
2828
*/
2929

30-
import org.testng.annotations.Test;
3130

3231
import java.io.IOException;
3332
import java.net.MulticastSocket;
3433

35-
import static org.testng.Assert.assertTrue;
34+
import org.junit.jupiter.api.Test;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3636

3737
public class Constructor {
3838
@Test

test/jdk/java/net/MulticastSocket/IPMulticastIF.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,41 @@
2727
import java.net.NetworkInterface;
2828
import java.util.ArrayList;
2929
import java.util.List;
30-
import java.util.Optional;
3130

3231
import jdk.test.lib.NetworkConfiguration;
33-
import org.testng.SkipException;
34-
import org.testng.annotations.BeforeTest;
35-
import org.testng.annotations.DataProvider;
36-
import org.testng.annotations.Test;
32+
import static jdk.test.lib.net.IPSupport.diagnoseConfigurationIssue;
3733
import static java.lang.String.format;
3834
import static java.lang.System.out;
3935
import static java.net.StandardSocketOptions.IP_MULTICAST_IF;
4036
import static java.util.stream.Collectors.toList;
41-
import static jdk.test.lib.net.IPSupport.diagnoseConfigurationIssue;
42-
import static org.testng.Assert.assertEquals;
43-
import static org.testng.Assert.assertTrue;
37+
38+
import org.junit.jupiter.api.Assumptions;
39+
import org.junit.jupiter.api.BeforeAll;
40+
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.params.ParameterizedTest;
42+
import org.junit.jupiter.params.provider.MethodSource;
43+
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
4445

4546
/**
4647
* @test
4748
* @bug 8236441
4849
* @summary Bound MulticastSocket fails when setting outbound interface on Windows
4950
* @library /test/lib
50-
* @run testng IPMulticastIF
51-
* @run testng/othervm -Djava.net.preferIPv4Stack=true IPMulticastIF
52-
* @run testng/othervm -Djava.net.preferIPv6Addresses=true IPMulticastIF
53-
* @run testng/othervm -Djava.net.preferIPv6Addresses=true -Djava.net.preferIPv4Stack=true IPMulticastIF
51+
* @run junit ${test.main.class}
52+
* @run junit/othervm -Djava.net.preferIPv4Stack=true ${test.main.class}
53+
* @run junit/othervm -Djava.net.preferIPv6Addresses=true ${test.main.class}
54+
* @run junit/othervm -Djava.net.preferIPv6Addresses=true -Djava.net.preferIPv4Stack=true ${test.main.class}
5455
*/
5556
public class IPMulticastIF {
5657

57-
@BeforeTest
58-
public void sanity() {
59-
Optional<String> configurationIssue = diagnoseConfigurationIssue();
60-
configurationIssue.map(SkipException::new).ifPresent(x -> {
61-
throw x;
62-
});
58+
@BeforeAll
59+
public static void sanity() {
60+
diagnoseConfigurationIssue().ifPresent(Assumptions::abort);
6361
NetworkConfiguration.printSystemConfiguration(out);
6462
}
6563

66-
@DataProvider(name = "scenarios")
67-
public Object[][] positive() throws Exception {
64+
public static Object[][] positive() throws Exception {
6865
List<InetAddress> addrs = List.of(InetAddress.getLocalHost(),
6966
InetAddress.getLoopbackAddress());
7067
List<Object[]> list = new ArrayList<>();
@@ -81,8 +78,7 @@ public Object[][] positive() throws Exception {
8178
return list.stream().toArray(Object[][]::new);
8279
}
8380

84-
@DataProvider(name = "interfaces")
85-
public Object[][] interfaces() throws Exception {
81+
public static Object[][] interfaces() throws Exception {
8682
List<Object[]> list = new ArrayList<>();
8783
NetworkConfiguration nc = NetworkConfiguration.probe();
8884
nc.multicastInterfaces(true)
@@ -92,65 +88,69 @@ public Object[][] interfaces() throws Exception {
9288
return list.stream().toArray(Object[][]::new);
9389
}
9490

95-
@Test(dataProvider = "scenarios")
91+
@ParameterizedTest
92+
@MethodSource("positive")
9693
public void testSetGetInterfaceBound(InetSocketAddress bindAddr, NetworkInterface nif)
9794
throws Exception
9895
{
9996
out.println(format("\n\n--- testSetGetInterfaceBound bindAddr=[%s], nif=[%s]", bindAddr, nif));
10097
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
10198
ms.setNetworkInterface(nif);
10299
NetworkInterface msNetIf = ms.getNetworkInterface();
103-
assertEquals(msNetIf, nif);
100+
assertEquals(nif, msNetIf);
104101
}
105102
}
106103

107-
@Test(dataProvider = "interfaces")
104+
@ParameterizedTest
105+
@MethodSource("interfaces")
108106
public void testSetGetInterfaceUnbound(NetworkInterface nif)
109107
throws Exception
110108
{
111109
out.println(format("\n\n--- testSetGetInterfaceUnbound nif=[%s]", nif));
112110
try (MulticastSocket ms = new MulticastSocket()) {
113111
ms.setNetworkInterface(nif);
114112
NetworkInterface msNetIf = ms.getNetworkInterface();
115-
assertEquals(msNetIf, nif);
113+
assertEquals(nif, msNetIf);
116114
}
117115
}
118116

119-
@Test(dataProvider = "scenarios")
117+
@ParameterizedTest
118+
@MethodSource("positive")
120119
public void testSetGetOptionBound(InetSocketAddress bindAddr, NetworkInterface nif)
121120
throws Exception
122121
{
123122
out.println(format("\n\n--- testSetGetOptionBound bindAddr=[%s], nif=[%s]", bindAddr, nif));
124123
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
125124
ms.setOption(IP_MULTICAST_IF, nif);
126125
NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);
127-
assertEquals(msNetIf, nif);
126+
assertEquals(nif, msNetIf);
128127
}
129128
}
130129

131-
@Test(dataProvider = "interfaces")
130+
@ParameterizedTest
131+
@MethodSource("interfaces")
132132
public void testSetGetOptionUnbound(NetworkInterface nif)
133133
throws Exception
134134
{
135135
out.println(format("\n\n--- testSetGetOptionUnbound nif=[%s]", nif));
136136
try (MulticastSocket ms = new MulticastSocket()) {
137137
ms.setOption(IP_MULTICAST_IF, nif);
138138
NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);
139-
assertEquals(msNetIf, nif);
139+
assertEquals(nif, msNetIf);
140140
}
141141
}
142142

143143
// -- get without set
144144

145-
@DataProvider(name = "bindAddresses")
146-
public Object[][] bindAddresses() throws Exception {
145+
public static Object[][] bindAddresses() throws Exception {
147146
return new Object[][] {
148147
{ new InetSocketAddress(InetAddress.getLocalHost(), 0) },
149148
{ new InetSocketAddress(InetAddress.getLoopbackAddress(), 0) },
150149
};
151150
}
152151

153-
@Test(dataProvider = "bindAddresses")
152+
@ParameterizedTest
153+
@MethodSource("bindAddresses")
154154
public void testGetInterfaceBound(InetSocketAddress bindAddr)
155155
throws Exception
156156
{
@@ -168,29 +168,30 @@ public void testGetInterfaceUnbound() throws Exception {
168168
}
169169
}
170170

171-
@Test(dataProvider = "bindAddresses")
171+
@ParameterizedTest
172+
@MethodSource("bindAddresses")
172173
public void testGetOptionBound(InetSocketAddress bindAddr)
173174
throws Exception
174175
{
175176
out.println(format("\n\n--- testGetOptionBound bindAddr=[%s]", bindAddr));
176177
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
177-
assertEquals(ms.getOption(IP_MULTICAST_IF), null);
178+
assertEquals(null, ms.getOption(IP_MULTICAST_IF));
178179
}
179180
}
180181

181182
@Test
182183
public void testGetOptionUnbound() throws Exception {
183184
out.println("\n\n--- testGetOptionUnbound ");
184185
try (MulticastSocket ms = new MulticastSocket()) {
185-
assertEquals(ms.getOption(IP_MULTICAST_IF), null);
186+
assertEquals(null, ms.getOption(IP_MULTICAST_IF));
186187
}
187188
}
188189

189190
// Asserts that the placeholder NetworkInterface has a single InetAddress
190191
// that represent any local address.
191192
static void assertPlaceHolder(NetworkInterface nif) {
192193
List<InetAddress> addrs = nif.inetAddresses().collect(toList());
193-
assertEquals(addrs.size(), 1);
194+
assertEquals(1, addrs.size());
194195
assertTrue(addrs.get(0).isAnyLocalAddress());
195196
}
196197
}

0 commit comments

Comments
 (0)