SocketUtilsTests.java (spring-framework-5.3.23) | : | SocketUtilsTests.java (spring-framework-5.3.24) | ||
---|---|---|---|---|
skipping to change at line 53 | skipping to change at line 53 | |||
// Just making sure somebody doesn't try to make SocketUtils abst ract, | // Just making sure somebody doesn't try to make SocketUtils abst ract, | |||
// since that would be a breaking change due to the intentional p ublic | // since that would be a breaking change due to the intentional p ublic | |||
// constructor. | // constructor. | |||
new org.springframework.util.SocketUtils(); | new org.springframework.util.SocketUtils(); | |||
} | } | |||
// TCP | // TCP | |||
@Test | @Test | |||
void findAvailableTcpPortWithZeroMinPort() { | void findAvailableTcpPortWithZeroMinPort() { | |||
assertThatIllegalArgumentException().isThrownBy(() -> | assertThatIllegalArgumentException().isThrownBy( | |||
org.springframework.util.SocketUtils.findAvailabl | () -> org.springframework.util.SocketUtils.findAv | |||
eTcpPort(0)); | ailableTcpPort(0)); | |||
} | } | |||
@Test | @Test | |||
void findAvailableTcpPortWithNegativeMinPort() { | void findAvailableTcpPortWithNegativeMinPort() { | |||
assertThatIllegalArgumentException().isThrownBy(() -> | assertThatIllegalArgumentException().isThrownBy( | |||
org.springframework.util.SocketUtils.findAvailabl | () -> org.springframework.util.SocketUtils.findAv | |||
eTcpPort(-500)); | ailableTcpPort(-500)); | |||
} | } | |||
@Test | @Test | |||
void findAvailableTcpPort() { | void findAvailableTcpPort() { | |||
int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(); | int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(); | |||
assertPortInRange(port, org.springframework.util.SocketUtils.PORT _RANGE_MIN, | assertPortInRange(port, org.springframework.util.SocketUtils.PORT _RANGE_MIN, | |||
org.springframework.util.SocketUtils.PORT_RANGE_M AX); | org.springframework.util.SocketUtils.PORT_RANGE_M AX); | |||
} | } | |||
@Test | @Test | |||
skipping to change at line 83 | skipping to change at line 83 | |||
int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(minMaxPort, minMaxPort); | int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(minMaxPort, minMaxPort); | |||
assertThat(port).isEqualTo(minMaxPort); | assertThat(port).isEqualTo(minMaxPort); | |||
} | } | |||
@Test | @Test | |||
void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro ws Exception { | void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro ws Exception { | |||
int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(); | int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(); | |||
try (ServerSocket socket = ServerSocketFactory.getDefault().creat eServerSocket(port, 1, InetAddress.getByName("localhost"))) { | try (ServerSocket socket = ServerSocketFactory.getDefault().creat eServerSocket(port, 1, InetAddress.getByName("localhost"))) { | |||
assertThat(socket).isNotNull(); | assertThat(socket).isNotNull(); | |||
// will only look for the exact port | // will only look for the exact port | |||
assertThatIllegalStateException().isThrownBy(() -> | assertThatIllegalStateException().isThrownBy( | |||
org.springframework.util.SocketUtils.find | () -> org.springframework.util.SocketUtil | |||
AvailableTcpPort(port, port)) | s.findAvailableTcpPort(port, port)) | |||
.withMessageStartingWith("Could not find an avail able TCP port") | .withMessageStartingWith("Could not find an avail able TCP port") | |||
.withMessageEndingWith("after 1 attempts"); | .withMessageEndingWith("after 1 attempts"); | |||
} | } | |||
} | } | |||
@Test | @Test | |||
void findAvailableTcpPortWithMin() { | void findAvailableTcpPortWithMin() { | |||
int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(50000); | int port = org.springframework.util.SocketUtils.findAvailableTcpP ort(50000); | |||
assertPortInRange(port, 50000, org.springframework.util.SocketUti ls.PORT_RANGE_MAX); | assertPortInRange(port, 50000, org.springframework.util.SocketUti ls.PORT_RANGE_MAX); | |||
} | } | |||
skipping to change at line 126 | skipping to change at line 126 | |||
findAvailableTcpPorts(4, 30000, 35000); | findAvailableTcpPorts(4, 30000, 35000); | |||
} | } | |||
@Test | @Test | |||
void find50AvailableTcpPortsInRange() { | void find50AvailableTcpPortsInRange() { | |||
findAvailableTcpPorts(50, 40000, 45000); | findAvailableTcpPorts(50, 40000, 45000); | |||
} | } | |||
@Test | @Test | |||
void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() { | void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() { | |||
assertThatIllegalArgumentException().isThrownBy(() -> | assertThatIllegalArgumentException().isThrownBy(() -> findAvailab | |||
findAvailableTcpPorts(50, 45000, 45010)); | leTcpPorts(50, 45000, 45010)); | |||
} | } | |||
// UDP | // UDP | |||
@Test | @Test | |||
void findAvailableUdpPortWithZeroMinPort() { | void findAvailableUdpPortWithZeroMinPort() { | |||
assertThatIllegalArgumentException().isThrownBy(() -> | assertThatIllegalArgumentException().isThrownBy( | |||
org.springframework.util.SocketUtils.findAvailabl | () -> org.springframework.util.SocketUtils.findAv | |||
eUdpPort(0)); | ailableUdpPort(0)); | |||
} | } | |||
@Test | @Test | |||
void findAvailableUdpPortWithNegativeMinPort() { | void findAvailableUdpPortWithNegativeMinPort() { | |||
assertThatIllegalArgumentException().isThrownBy(() -> | assertThatIllegalArgumentException().isThrownBy( | |||
org.springframework.util.SocketUtils.findAvailabl | () -> org.springframework.util.SocketUtils.findAv | |||
eUdpPort(-500)); | ailableUdpPort(-500)); | |||
} | } | |||
@Test | @Test | |||
void findAvailableUdpPort() { | void findAvailableUdpPort() { | |||
int port = org.springframework.util.SocketUtils.findAvailableUdpP ort(); | int port = org.springframework.util.SocketUtils.findAvailableUdpP ort(); | |||
assertPortInRange(port, org.springframework.util.SocketUtils.PORT _RANGE_MIN, | assertPortInRange(port, org.springframework.util.SocketUtils.PORT _RANGE_MIN, | |||
org.springframework.util.SocketUtils.PORT_RANGE_M AX); | org.springframework.util.SocketUtils.PORT_RANGE_M AX); | |||
} | } | |||
@Test | @Test | |||
void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro ws Exception { | void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro ws Exception { | |||
int port = org.springframework.util.SocketUtils.findAvailableUdpP ort(); | int port = org.springframework.util.SocketUtils.findAvailableUdpP ort(); | |||
try (DatagramSocket socket = new DatagramSocket(port, InetAddress .getByName("localhost"))) { | try (DatagramSocket socket = new DatagramSocket(port, InetAddress .getByName("localhost"))) { | |||
assertThat(socket).isNotNull(); | assertThat(socket).isNotNull(); | |||
// will only look for the exact port | // will only look for the exact port | |||
assertThatIllegalStateException().isThrownBy(() -> | assertThatIllegalStateException().isThrownBy( | |||
org.springframework.util.SocketUtils.find | () -> org.springframework.util.SocketUtil | |||
AvailableUdpPort(port, port)) | s.findAvailableUdpPort(port, port)) | |||
.withMessageStartingWith("Could not find an avail able UDP port") | .withMessageStartingWith("Could not find an avail able UDP port") | |||
.withMessageEndingWith("after 1 attempts"); | .withMessageEndingWith("after 1 attempts"); | |||
} | } | |||
} | } | |||
@Test | @Test | |||
void findAvailableUdpPortWithMin() { | void findAvailableUdpPortWithMin() { | |||
int port = org.springframework.util.SocketUtils.findAvailableUdpP ort(50000); | int port = org.springframework.util.SocketUtils.findAvailableUdpP ort(50000); | |||
assertPortInRange(port, 50000, org.springframework.util.SocketUti ls.PORT_RANGE_MAX); | assertPortInRange(port, 50000, org.springframework.util.SocketUti ls.PORT_RANGE_MAX); | |||
} | } | |||
skipping to change at line 200 | skipping to change at line 199 | |||
findAvailableUdpPorts(4, 30000, 35000); | findAvailableUdpPorts(4, 30000, 35000); | |||
} | } | |||
@Test | @Test | |||
void find50AvailableUdpPortsInRange() { | void find50AvailableUdpPortsInRange() { | |||
findAvailableUdpPorts(50, 40000, 45000); | findAvailableUdpPorts(50, 40000, 45000); | |||
} | } | |||
@Test | @Test | |||
void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() { | void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() { | |||
assertThatIllegalArgumentException().isThrownBy(() -> | assertThatIllegalArgumentException().isThrownBy(() -> findAvailab | |||
findAvailableUdpPorts(50, 45000, 45010)); | leUdpPorts(50, 45000, 45010)); | |||
} | } | |||
// Helpers | // Helpers | |||
private void findAvailableTcpPorts(int numRequested) { | private void findAvailableTcpPorts(int numRequested) { | |||
SortedSet<Integer> ports = org.springframework.util.SocketUtils.f indAvailableTcpPorts(numRequested); | SortedSet<Integer> ports = org.springframework.util.SocketUtils.f indAvailableTcpPorts(numRequested); | |||
assertAvailablePorts(ports, numRequested, org.springframework.uti l.SocketUtils.PORT_RANGE_MIN, | assertAvailablePorts(ports, numRequested, org.springframework.uti l.SocketUtils.PORT_RANGE_MIN, | |||
org.springframework.util.SocketUtils.PORT_RANGE_M AX); | org.springframework.util.SocketUtils.PORT_RANGE_M AX); | |||
} | } | |||
skipping to change at line 227 | skipping to change at line 225 | |||
private void findAvailableUdpPorts(int numRequested) { | private void findAvailableUdpPorts(int numRequested) { | |||
SortedSet<Integer> ports = org.springframework.util.SocketUtils.f indAvailableUdpPorts(numRequested); | SortedSet<Integer> ports = org.springframework.util.SocketUtils.f indAvailableUdpPorts(numRequested); | |||
assertAvailablePorts(ports, numRequested, org.springframework.uti l.SocketUtils.PORT_RANGE_MIN, | assertAvailablePorts(ports, numRequested, org.springframework.uti l.SocketUtils.PORT_RANGE_MIN, | |||
org.springframework.util.SocketUtils.PORT_RANGE_M AX); | org.springframework.util.SocketUtils.PORT_RANGE_M AX); | |||
} | } | |||
private void findAvailableUdpPorts(int numRequested, int minPort, int max Port) { | private void findAvailableUdpPorts(int numRequested, int minPort, int max Port) { | |||
SortedSet<Integer> ports = org.springframework.util.SocketUtils.f indAvailableUdpPorts(numRequested, minPort, maxPort); | SortedSet<Integer> ports = org.springframework.util.SocketUtils.f indAvailableUdpPorts(numRequested, minPort, maxPort); | |||
assertAvailablePorts(ports, numRequested, minPort, maxPort); | assertAvailablePorts(ports, numRequested, minPort, maxPort); | |||
} | } | |||
private void assertPortInRange(int port, int minPort, int maxPort) { | private void assertPortInRange(int port, int minPort, int maxPort) { | |||
assertThat(port >= minPort).as("port [" + port + "] >= " + minPor | assertThat(port).as("port").isBetween(minPort, maxPort); | |||
t).isTrue(); | ||||
assertThat(port <= maxPort).as("port [" + port + "] <= " + maxPor | ||||
t).isTrue(); | ||||
} | } | |||
private void assertAvailablePorts(SortedSet<Integer> ports, int numReques ted, int minPort, int maxPort) { | private void assertAvailablePorts(SortedSet<Integer> ports, int numReques ted, int minPort, int maxPort) { | |||
assertThat(ports.size()).as("number of ports requested").isEqualT o(numRequested); | assertThat(ports).as("number of ports requested").hasSize(numRequ ested); | |||
for (int port : ports) { | for (int port : ports) { | |||
assertPortInRange(port, minPort, maxPort); | assertPortInRange(port, minPort, maxPort); | |||
} | } | |||
} | } | |||
} | } | |||
End of changes. 11 change blocks. | ||||
27 lines changed or deleted | 25 lines changed or added |