8381359: Refactor java/net/DatagramSocket TestNG tests to use JUnit

Reviewed-by: vyazici
This commit is contained in:
Daniel Fuchs 2026-04-13 09:24:41 +00:00
parent d605b5709a
commit a76e38c7eb
10 changed files with 262 additions and 267 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,11 +21,6 @@
* questions.
*/
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.DatagramSocket;
@ -34,62 +29,64 @@ import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.nio.channels.DatagramChannel;
import java.util.List;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.expectThrows;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
/*
* @test
* @bug 8240533
* @summary Check that DatagramSocket, MulticastSocket and DatagramSocketAdaptor
* throw expected Exception when connecting to port 0
* @run testng/othervm ConnectPortZero
* @run junit/othervm ${test.main.class}
*/
public class ConnectPortZero {
private InetAddress loopbackAddr, wildcardAddr;
private DatagramSocket datagramSocket, datagramSocketAdaptor;
private MulticastSocket multicastSocket;
private static InetAddress loopbackAddr, wildcardAddr;
private static final Class<SocketException> SE = SocketException.class;
private static final Class<UncheckedIOException> UCIOE = UncheckedIOException.class;
@BeforeTest
public void setUp() throws IOException {
@BeforeAll
public static void setUp() throws IOException {
loopbackAddr = InetAddress.getLoopbackAddress();
wildcardAddr = new InetSocketAddress(0).getAddress();
datagramSocket = new DatagramSocket();
multicastSocket = new MulticastSocket();
datagramSocketAdaptor = DatagramChannel.open().socket();
}
@DataProvider(name = "data")
public Object[][] variants() {
return new Object[][]{
{ datagramSocket, loopbackAddr },
{ datagramSocketAdaptor, loopbackAddr },
{ multicastSocket, loopbackAddr },
{ datagramSocket, wildcardAddr },
{ datagramSocketAdaptor, wildcardAddr },
{ multicastSocket, wildcardAddr }
};
public static List<Arguments> testCases() throws IOException {
// Note that Closeable arguments passed to a ParameterizedTest are automatically
// closed by JUnit. We do not want to rely on this, but we do need to
// create a new set of sockets for each invocation of this method, so that
// the next test method invoked doesn't get a closed socket.
return List.of(
Arguments.of(new DatagramSocket(), loopbackAddr),
Arguments.of(DatagramChannel.open().socket(), loopbackAddr),
Arguments.of(new MulticastSocket(), loopbackAddr),
Arguments.of(new DatagramSocket(), wildcardAddr),
Arguments.of(DatagramChannel.open().socket(), wildcardAddr),
Arguments.of(new MulticastSocket(), wildcardAddr)
);
}
@Test(dataProvider = "data")
public void testConnect(DatagramSocket ds, InetAddress addr) {
Throwable t = expectThrows(UCIOE, () -> ds.connect(addr, 0));
assertEquals(t.getCause().getClass(), SE);
assertThrows(SE, () -> ds
.connect(new InetSocketAddress(addr, 0)));
}
@AfterTest
public void tearDown() {
datagramSocket.close();
multicastSocket.close();
datagramSocketAdaptor.close();
@ParameterizedTest
@MethodSource("testCases")
public void testConnect(DatagramSocket socket, InetAddress addr) {
try (var ds = socket) {
assertFalse(ds.isConnected());
assertFalse(ds.isClosed());
Throwable t = assertThrows(UCIOE, () -> ds.connect(addr, 0));
assertSame(SE, t.getCause().getClass());
assertFalse(ds.isConnected());
assertFalse(ds.isClosed());
assertThrows(SE, () -> ds
.connect(new InetSocketAddress(addr, 0)));
assertFalse(ds.isConnected());
assertFalse(ds.isClosed());
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,18 +24,18 @@
/* @test
* @bug 8243507 8243999
* @summary Checks to ensure that DatagramSocket constructors behave as expected
* @run testng Constructor
* @run junit ${test.main.class}
*/
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Constructor {
private static final InetAddress LOOPBACK = InetAddress.getLoopbackAddress();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,23 +26,24 @@
* @bug 4163126 8222829
* @summary Test to see if timeout hangs. Also checks that
* negative timeout value fails as expected.
* @run testng DatagramTimeout
* @run junit ${test.main.class}
*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.nio.channels.DatagramChannel;
import java.util.List;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class DatagramTimeout {
private static final Class<IllegalArgumentException> IAE =
@ -51,49 +52,46 @@ public class DatagramTimeout {
SocketTimeoutException.class;
private static final Class<SocketException> SE = SocketException.class;
private DatagramSocket datagramSocket, multicastSocket,
datagramSocketAdaptor;
@BeforeTest
public void setUp() throws Exception {
datagramSocket = new DatagramSocket();
multicastSocket = new MulticastSocket();
datagramSocketAdaptor = DatagramChannel.open().socket();
public static List<DatagramSocket> sockets() throws IOException {
// Note that Closeable arguments passed to a ParameterizedTest are automatically
// closed by JUnit. We do not want to rely on this, but we do need to
// create a new set of sockets for each invocation of this method, so that
// the next test method invoked doesn't get a closed socket.
return List.of(
new DatagramSocket(),
new MulticastSocket(),
DatagramChannel.open().socket());
}
@DataProvider(name = "data")
public Object[][] variants() {
return new Object[][]{
{ datagramSocket },
{ datagramSocketAdaptor },
{ multicastSocket },
};
@ParameterizedTest
@MethodSource("sockets")
public void testSetNegTimeout(DatagramSocket socket) {
try (var ds = socket) {
assertFalse(ds.isClosed());
assertThrows(IAE, () -> ds.setSoTimeout(-1));
}
}
@Test(dataProvider = "data")
public void testSetNegTimeout(DatagramSocket ds) {
assertThrows(IAE, () -> ds.setSoTimeout(-1));
@ParameterizedTest
@MethodSource("sockets")
public void testSetTimeout(DatagramSocket socket) throws Exception {
try (var ds = socket) {
assertFalse(ds.isClosed());
byte[] buffer = new byte[50];
DatagramPacket pkt = new DatagramPacket(buffer, buffer.length);
ds.setSoTimeout(2);
assertThrows(STE, () -> ds.receive(pkt));
}
}
@Test(dataProvider = "data")
public void testSetTimeout(DatagramSocket ds) throws Exception {
byte[] buffer = new byte[50];
DatagramPacket pkt = new DatagramPacket(buffer, buffer.length);
ds.setSoTimeout(2);
assertThrows(STE, () -> ds.receive(pkt));
}
@Test(dataProvider = "data")
public void testGetTimeout(DatagramSocket ds) throws Exception {
ds.setSoTimeout(10);
assertEquals(10, ds.getSoTimeout());
}
@AfterTest
public void tearDown() {
datagramSocket.close();
multicastSocket.close();
datagramSocketAdaptor.close();
@ParameterizedTest
@MethodSource("sockets")
public void testGetTimeout(DatagramSocket socket) throws Exception {
try (var ds = socket) {
assertFalse(ds.isClosed());
ds.setSoTimeout(10);
assertEquals(10, ds.getSoTimeout());
}
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,15 +25,24 @@
* @test
* @bug 8260428
* @summary Drop support for pre JDK 1.4 DatagramSocketImpl implementations
* @run testng/othervm OldDatagramSocketImplTest
* @run junit/othervm ${test.main.class}
*/
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.DatagramSocketImpl;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.*;
import java.io.*;
import static org.testng.Assert.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class OldDatagramSocketImplTest {
InetAddress LOOPBACK = InetAddress.getLoopbackAddress();
@ -41,22 +50,18 @@ public class OldDatagramSocketImplTest {
@Test
public void testOldImplConnect() {
try (var ds = new DatagramSocket(new OldDatagramSocketImpl()) {}) {
ds.connect(new InetSocketAddress(LOOPBACK, 6667));
throw new RuntimeException("ERROR: test failed");
} catch (SocketException ex) {
assertEquals(ex.getMessage(), "connect not implemented");
System.out.println("PASSED: default implementation of connect has thrown as expected");
SocketException ex = assertThrows(SocketException.class,
() -> ds.connect(new InetSocketAddress(LOOPBACK, 6667)));
assertEquals("connect not implemented", ex.getMessage());
}
}
@Test
public void testOldImplConnectTwoArgs() {
try (var ds = new DatagramSocket(new OldDatagramSocketImpl()) {}) {
ds.connect(LOOPBACK, 6667);
throw new RuntimeException("ERROR: test failed");
} catch (UncheckedIOException ex) {
assertEquals(ex.getMessage(), "connect failed");
System.out.println("PASSED: default implementation of connect has thrown as expected");
UncheckedIOException ex = assertThrows(UncheckedIOException.class,
() -> ds.connect(LOOPBACK, 6667));
assertEquals("connect failed", ex.getMessage());
}
}
@ -64,36 +69,27 @@ public class OldDatagramSocketImplTest {
public void testOldImplDisconnect() {
try (var ds = new DatagramSocket(new OldDatagramSocketImplWithValidConnect()) { }){
ds.connect(LOOPBACK, 6667);
ds.disconnect();
throw new RuntimeException("ERROR: test failed");
} catch (UncheckedIOException ex) {
UncheckedIOException ex = assertThrows(UncheckedIOException.class, () -> ds.disconnect());
var innerException = ex.getCause();
assertEquals(innerException.getClass(), SocketException.class);
assertEquals(innerException.getMessage(), "disconnect not implemented");
System.out.println("PASSED: default implementation of disconnect has thrown as expected");
assertSame(SocketException.class, innerException.getClass());
assertEquals("disconnect not implemented", innerException.getMessage());
}
}
@Test
public void testOldImplPublic() {
try (var ds = new PublicOldDatagramSocketImpl()) {
ds.connect(LOOPBACK, 0);
throw new RuntimeException("ERROR: test failed");
} catch (SocketException ex) {
assertEquals(ex.getMessage(), "connect not implemented");
System.out.println("PASSED: default implementation of disconnect has thrown as expected");
SocketException ex = assertThrows(SocketException.class, () -> ds.connect(LOOPBACK, 0));
assertEquals("connect not implemented", ex.getMessage());
}
}
@Test
public void testOldImplPublicDisconnect() {
try (var ds = new PublicOldDatagramSocketImplWithValidConnect()) {
ds.disconnect();
throw new RuntimeException("ERROR: test failed");
} catch (UncheckedIOException ex) {
UncheckedIOException ex = assertThrows(UncheckedIOException.class, () -> ds.disconnect());
var innerException = ex.getCause();
assertEquals(innerException.getClass(), SocketException.class);
assertEquals(innerException.getMessage(), "disconnect not implemented");
System.out.println("PASSED: default implementation of disconnect has thrown as expected");
assertSame(SocketException.class, innerException.getClass());
assertEquals("disconnect not implemented", innerException.getMessage());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,12 +33,14 @@ import java.nio.channels.DatagramChannel;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.testng.Assert.expectThrows;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
/*
* @test
@ -47,28 +49,24 @@ import static org.testng.Assert.expectThrows;
* DatagramSocketAdaptor and DatagramChannel all
* throw expected Exception when passed a DatagramPacket
* with invalid details
* @run testng SendCheck
* @run junit ${test.main.class}
*/
public class SendCheck {
private InetAddress loopbackAddr, wildcardAddr;
private static InetAddress loopbackAddr, wildcardAddr;
static final Class<IOException> IOE = IOException.class;
static final Class<SocketException> SE = SocketException.class;
static final byte[] buf = {0, 1, 2};
static DatagramSocket socket;
@BeforeTest
public void setUp() {
try {
socket = new DatagramSocket();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
@BeforeAll
public static void setUp() throws Exception {
socket = new DatagramSocket();
}
@AfterTest
public void closeDown() {
@AfterAll
public static void closeDown() {
socket.close();
}
@ -92,7 +90,7 @@ public class SendCheck {
if (address == null) {
description = "<null>:" + port;
} else if (port < 0) {
description = packet.getAddress().toString() + ":" + port;
description = packet.getAddress() + ":" + port;
} else {
description = packet.getSocketAddress().toString();
}
@ -100,8 +98,7 @@ public class SendCheck {
}
}
@DataProvider(name = "packets")
Object[][] providerIO() throws IOException {
static List<Arguments> providerIO() throws IOException {
loopbackAddr = InetAddress.getLoopbackAddress();
wildcardAddr = new InetSocketAddress(0).getAddress();
@ -124,44 +121,46 @@ public class SendCheck {
List<Packet> Packets = List.of(Packet.of(pkt1), Packet.of(pkt2));
List<Sender> senders = List.of(
Sender.of(new DatagramSocket(null)),
Sender.of(new MulticastSocket(null)),
Sender.of(DatagramChannel.open()),
Sender.of(DatagramChannel.open().socket())
);
List<Object[]> testcases = new ArrayList<>();
List<Arguments> testcases = new ArrayList<>();
for (var packet : Packets) {
// Note that Closeable arguments passed to a ParameterizedTest are automatically
// closed by JUnit. We do not want to rely on this, but we do need to
// create a new set of sockets for each invocation of this method, so that
// the next test method invoked doesn't get a closed socket.
List<Sender> senders = List.of(
Sender.of(new DatagramSocket(null)),
Sender.of(new MulticastSocket(null)),
Sender.of(DatagramChannel.open()),
Sender.of(DatagramChannel.open().socket())
);
addTestCaseFor(testcases, senders, packet);
}
return testcases.toArray(new Object[0][0]);
return testcases;
}
static void addTestCaseFor(List<Object[]> testcases,
static void addTestCaseFor(List<Arguments> testcases,
List<Sender> senders, Packet p) {
for (var s : senders) {
Object[] testcase = new Object[]{s, p, s.expectedException()};
testcases.add(testcase);
testcases.add(Arguments.of(s, p, s.expectedException()));
}
}
@Test(dataProvider = "packets")
public static void sendCheck(Sender<IOException> sender,
Packet packet,
Class<? extends Throwable> exception) {
DatagramPacket pkt = packet.packet;
if (exception != null) {
Throwable t = expectThrows(exception, () -> sender.send(pkt));
System.out.printf("%s got expected exception %s%n",
packet.toString(), t);
} else {
try {
sender.send(pkt);
} catch (IOException e) {
throw new AssertionError("Unexpected exception for "
+ sender + " / " + packet, e);
@ParameterizedTest
@MethodSource("providerIO")
public void sendCheck(Sender<IOException> socket,
Packet packet,
Class<? extends Throwable> exception)
throws IOException
{
try (var sender = socket) {
DatagramPacket pkt = packet.packet;
if (exception != null) {
Throwable t = assertThrows(exception, () -> sender.send(pkt));
System.out.printf("%s got expected exception %s%n", packet, t);
} else {
assertDoesNotThrow(() -> sender.send(pkt),
"Unexpected exception for " + sender + " / " + packet);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,39 +21,40 @@
* questions.
*/
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.nio.channels.DatagramChannel;
import static org.testng.Assert.assertThrows;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
/*
* @test
* @bug 8236105 8240533
* @summary Check that DatagramSocket throws expected
* Exception when sending a DatagramPacket with port 0
* @run testng/othervm SendPortZero
* @run junit/othervm ${test.main.class}
*/
public class SendPortZero {
private InetAddress loopbackAddr, wildcardAddr;
private DatagramSocket datagramSocket, datagramSocketAdaptor;
private DatagramPacket loopbackZeroPkt, wildcardZeroPkt, wildcardValidPkt;
private static InetAddress loopbackAddr, wildcardAddr;
private static DatagramSocket datagramSocket, datagramSocketAdaptor;
private static DatagramPacket loopbackZeroPkt, wildcardZeroPkt, wildcardValidPkt;
private static final Class<SocketException> SE = SocketException.class;
@BeforeTest
public void setUp() throws IOException {
@BeforeAll
public static void setUp() throws IOException {
datagramSocket = new DatagramSocket();
datagramSocketAdaptor = DatagramChannel.open().socket();
@ -81,8 +82,13 @@ public class SendPortZero {
wildcardValidPkt.setPort(datagramSocket.getLocalPort());
}
@DataProvider(name = "data")
public Object[][] variants() {
@AfterAll
public static void tearDown() {
datagramSocket.close();
datagramSocketAdaptor.close();
}
public static Object[][] testCases() {
return new Object[][]{
{ datagramSocket, loopbackZeroPkt },
{ datagramSocket, wildcardZeroPkt },
@ -96,14 +102,10 @@ public class SendPortZero {
};
}
@Test(dataProvider = "data")
@ParameterizedTest(autoCloseArguments = false) // closed in tearDown
@MethodSource("testCases")
public void testSend(DatagramSocket ds, DatagramPacket pkt) {
assertFalse(ds.isClosed());
assertThrows(SE, () -> ds.send(pkt));
}
@AfterTest
public void tearDown() {
datagramSocket.close();
datagramSocketAdaptor.close();
}
}

View File

@ -33,7 +33,7 @@
* limit.
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run testng/othervm SendReceiveMaxSize
* @run junit/othervm ${test.main.class}
*/
/*
* @test id=preferIPv4Stack
@ -42,7 +42,7 @@
* maximum size on macOS, using an IPv4 only socket.
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run testng/othervm -Djava.net.preferIPv4Stack=true SendReceiveMaxSize
* @run junit/othervm -Djava.net.preferIPv4Stack=true ${test.main.class}
*/
/*
* @test id=preferIPv6Addresses
@ -52,7 +52,7 @@
* IPv6 addresses.
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run testng/othervm -Djava.net.preferIPv6Addresses=true SendReceiveMaxSize
* @run junit/othervm -Djava.net.preferIPv6Addresses=true ${test.main.class}
*/
/*
* @test id=preferLoopback
@ -62,7 +62,7 @@
* interface.
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run testng/othervm -Dtest.preferLoopback=true SendReceiveMaxSize
* @run junit/othervm -Dtest.preferLoopback=true ${test.main.class}
*/
/*
* @test id=preferIPv6Loopback
@ -72,7 +72,7 @@
* interface.
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run testng/othervm -Dtest.preferLoopback=true -Djava.net.preferIPv6Addresses=true SendReceiveMaxSize
* @run junit/othervm -Dtest.preferLoopback=true -Djava.net.preferIPv6Addresses=true ${test.main.class}
*/
/*
* @test id=preferIPv4Loopback
@ -82,16 +82,12 @@
* loopback interface
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run testng/othervm -Dtest.preferLoopback=true -Djava.net.preferIPv4Stack=true SendReceiveMaxSize
* @run junit/othervm -Dtest.preferLoopback=true -Djava.net.preferIPv4Stack=true ${test.main.class}
*/
import jdk.test.lib.RandomFactory;
import jdk.test.lib.Platform;
import jdk.test.lib.net.IPSupport;
import org.testng.SkipException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.DatagramPacket;
@ -101,21 +97,27 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.nio.channels.DatagramChannel;
import java.util.Optional;
import java.util.Random;
import static java.net.StandardSocketOptions.SO_RCVBUF;
import static jdk.test.lib.net.IPSupport.diagnoseConfigurationIssue;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.expectThrows;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SendReceiveMaxSize {
private final static boolean PREFER_LOOPBACK = Boolean.getBoolean("test.preferLoopback");
private int BUF_LIMIT;
private InetAddress HOST_ADDR;
private static int BUF_LIMIT;
private static InetAddress HOST_ADDR;
private final static int IPV4_SNDBUF = IPSupport.getMaxUDPSendBufSizeIPv4();
private final static int IPV6_SNDBUF = IPSupport.getMaxUDPSendBufSizeIPv6();
private final static Class<IOException> IOE = IOException.class;
@ -126,20 +128,16 @@ public class SendReceiveMaxSize {
}
static DatagramSocketSupplier supplier(DatagramSocketSupplier supplier) { return supplier; }
@BeforeTest
public void setUp() throws IOException {
Optional<String> configurationIssue = diagnoseConfigurationIssue();
configurationIssue.map(SkipException::new).ifPresent(x -> {
throw x;
});
@BeforeAll
public static void setUp() throws IOException {
// skip test if the configuration is not operational
diagnoseConfigurationIssue().ifPresent(Assumptions::abort);
HOST_ADDR = PREFER_LOOPBACK ? InetAddress.getLoopbackAddress() : InetAddress.getLocalHost();
BUF_LIMIT = (HOST_ADDR instanceof Inet6Address) ? IPV6_SNDBUF : IPV4_SNDBUF;
System.out.printf("Host address: %s, Buffer limit: %d%n", HOST_ADDR, BUF_LIMIT);
}
@DataProvider
public Object[][] invariants() {
public static Object[][] testCases() {
var ds = supplier(() -> new DatagramSocket());
var ms = supplier(() -> new MulticastSocket());
var dsa = supplier(() -> DatagramChannel.open().socket());
@ -156,7 +154,8 @@ public class SendReceiveMaxSize {
};
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("testCases")
public void testSendReceiveMaxSize(String name, int capacity,
DatagramSocketSupplier supplier,
Class<? extends Exception> exception) throws IOException {
@ -177,7 +176,7 @@ public class SendReceiveMaxSize {
var sendPkt = new DatagramPacket(testData, capacity, addr);
if (exception != null) {
Exception ex = expectThrows(IOE, () -> sender.send(sendPkt));
Exception ex = assertThrows(exception, () -> sender.send(sendPkt));
System.out.println(name + " got expected exception: " + ex);
} else {
sender.send(sendPkt);
@ -185,8 +184,8 @@ public class SendReceiveMaxSize {
receiver.receive(receivePkt);
// check packet data has been fragmented and re-assembled correctly at receiver
assertEquals(receivePkt.getLength(), capacity);
assertEquals(receivePkt.getData(), testData);
assertEquals(capacity, receivePkt.getLength());
assertArrayEquals(testData, receivePkt.getData());
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,21 +25,20 @@
* @test
* @bug 4173717 8243488
* @summary Check that setReceiveBufferSize and getReceiveBufferSize work as expected
* @run testng SetGetReceiveBufferSize
* @run testng/othervm -Djava.net.preferIPv4Stack=true SetGetReceiveBufferSize
* @run junit ${test.main.class}
* @run junit/othervm -Djava.net.preferIPv4Stack=true ${test.main.class}
*/
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.nio.channels.DatagramChannel;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.expectThrows;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class SetGetReceiveBufferSize {
static final Class<SocketException> SE = SocketException.class;
@ -51,8 +50,7 @@ public class SetGetReceiveBufferSize {
}
static DatagramSocketSupplier supplier(DatagramSocketSupplier supplier) { return supplier; }
@DataProvider
public Object[][] invariants() {
public static Object[][] suppliers() {
return new Object[][]{
{"DatagramSocket", supplier(() -> new DatagramSocket())},
{"MulticastSocket", supplier(() -> new MulticastSocket())},
@ -60,39 +58,42 @@ public class SetGetReceiveBufferSize {
};
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("suppliers")
public void testSetInvalidBufferSize(String name, DatagramSocketSupplier supplier) throws IOException {
var invalidArgs = new int[]{ -1, 0 };
try (var socket = supplier.open()) {
for (int i : invalidArgs) {
Exception ex = expectThrows(IAE, () -> socket.setReceiveBufferSize(i));
Exception ex = assertThrows(IAE, () -> socket.setReceiveBufferSize(i));
System.out.println(name + " got expected exception: " + ex);
}
}
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("suppliers")
public void testSetAndGetBufferSize(String name, DatagramSocketSupplier supplier) throws IOException {
var validArgs = new int[]{ 1234, 2345, 3456 };
try (var socket = supplier.open()) {
for (int i : validArgs) {
socket.setReceiveBufferSize(i);
assertEquals(socket.getReceiveBufferSize(), i, name);
assertEquals(i, socket.getReceiveBufferSize(), name);
}
}
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("suppliers")
public void testSetGetAfterClose(String name, DatagramSocketSupplier supplier) throws IOException {
var socket = supplier.open();
socket.close();
Exception setException = expectThrows(SE, () -> socket.setReceiveBufferSize(2345));
Exception setException = assertThrows(SE, () -> socket.setReceiveBufferSize(2345));
System.out.println(name + " got expected exception: " + setException);
Exception getException = expectThrows(SE, () -> socket.getReceiveBufferSize());
Exception getException = assertThrows(SE, () -> socket.getReceiveBufferSize());
System.out.println(name + " got expected exception: " + getException);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,14 +27,12 @@
* @library /test/lib
* @build jdk.test.lib.Platform
* @summary Check that setSendBufferSize and getSendBufferSize work as expected
* @run testng SetGetSendBufferSize
* @run testng/othervm -Djava.net.preferIPv4Stack=true SetGetSendBufferSize
* @run junit ${test.main.class}
* @run junit/othervm -Djava.net.preferIPv4Stack=true ${test.main.class}
*/
import jdk.test.lib.Platform;
import jdk.test.lib.net.IPSupport;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.DatagramSocket;
@ -42,9 +40,11 @@ import java.net.MulticastSocket;
import java.net.SocketException;
import java.nio.channels.DatagramChannel;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.expectThrows;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SetGetSendBufferSize {
static final Class<SocketException> SE = SocketException.class;
@ -55,8 +55,7 @@ public class SetGetSendBufferSize {
}
static DatagramSocketSupplier supplier(DatagramSocketSupplier supplier) { return supplier; }
@DataProvider
public Object[][] invariants() {
public static Object[][] suppliers() {
return new Object[][]{
{"DatagramSocket", supplier(() -> new DatagramSocket())},
{"MulticastSocket", supplier(() -> new MulticastSocket())},
@ -64,51 +63,55 @@ public class SetGetSendBufferSize {
};
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("suppliers")
public void testSetInvalidBufferSize(String name, DatagramSocketSupplier supplier) throws IOException {
var invalidArgs = new int[]{ -1, 0 };
try (var socket = supplier.open()) {
for (int i : invalidArgs) {
Exception ex = expectThrows(IAE, () -> socket.setSendBufferSize(i));
Exception ex = assertThrows(IAE, () -> socket.setSendBufferSize(i));
System.out.println(name + " got expected exception: " + ex);
}
}
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("suppliers")
public void testSetAndGetBufferSize(String name, DatagramSocketSupplier supplier) throws IOException {
var validArgs = new int[]{ 2345, 3456 };
try (var socket = supplier.open()) {
for (int i : validArgs) {
socket.setSendBufferSize(i);
assertEquals(socket.getSendBufferSize(), i, name);
assertEquals(i, socket.getSendBufferSize(), name);
}
}
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("suppliers")
public void testInitialSendBufferSize(String name, DatagramSocketSupplier supplier) throws IOException {
if (Platform.isOSX()) {
try (var socket = supplier.open()){
assertTrue(socket.getSendBufferSize() >= 65507, name);
if (IPSupport.hasIPv6() && !IPSupport.preferIPv4Stack()) {
assertEquals(socket.getSendBufferSize(), 65527, name);
assertEquals(65527, socket.getSendBufferSize(), name);
}
}
}
}
@Test(dataProvider = "invariants")
@ParameterizedTest
@MethodSource("suppliers")
public void testSetGetAfterClose(String name, DatagramSocketSupplier supplier) throws IOException {
var socket = supplier.open();
socket.close();
Exception setException = expectThrows(SE, () -> socket.setSendBufferSize(2345));
Exception setException = assertThrows(SE, () -> socket.setSendBufferSize(2345));
System.out.println(name + " got expected exception: " + setException);
Exception getException = expectThrows(SE, () -> socket.getSendBufferSize());
Exception getException = assertThrows(SE, () -> socket.getSendBufferSize());
System.out.println(name + " got expected exception: " + getException);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,17 +27,17 @@
* @library /test/lib
* @summary checks that the DatagramSocket supportedOptions set contains all
* MulticastSocket socket options
* @run testng SupportedOptionsCheck
* @run junit ${test.main.class}
*/
import jdk.test.lib.Platform;
import org.testng.annotations.Test;
import java.net.DatagramSocket;
import java.net.StandardSocketOptions;
import java.util.Set;
import static org.testng.Assert.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SupportedOptionsCheck {