|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
28 | 28 | import static org.assertj.core.api.Assertions.assertThat;
|
29 | 29 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
30 | 30 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
31 |
| -import static org.springframework.util.SocketUtils.PORT_RANGE_MAX; |
32 |
| -import static org.springframework.util.SocketUtils.PORT_RANGE_MIN; |
33 | 31 |
|
34 | 32 | /**
|
35 | 33 | * Unit tests for {@link SocketUtils}.
|
36 | 34 | *
|
37 | 35 | * @author Sam Brannen
|
38 | 36 | * @author Gary Russell
|
39 | 37 | */
|
| 38 | +@SuppressWarnings("deprecation") |
40 | 39 | class SocketUtilsTests {
|
41 | 40 |
|
42 | 41 | @Test
|
43 | 42 | void canBeInstantiated() {
|
44 | 43 | // Just making sure somebody doesn't try to make SocketUtils abstract,
|
45 | 44 | // since that would be a breaking change due to the intentional public
|
46 | 45 | // constructor.
|
47 |
| - new SocketUtils(); |
| 46 | + new org.springframework.util.SocketUtils(); |
48 | 47 | }
|
49 | 48 |
|
50 | 49 | // TCP
|
51 | 50 |
|
52 | 51 | @Test
|
53 | 52 | void findAvailableTcpPortWithZeroMinPort() {
|
54 | 53 | assertThatIllegalArgumentException().isThrownBy(() ->
|
55 |
| - SocketUtils.findAvailableTcpPort(0)); |
| 54 | + org.springframework.util.SocketUtils.findAvailableTcpPort(0)); |
56 | 55 | }
|
57 | 56 |
|
58 | 57 | @Test
|
59 | 58 | void findAvailableTcpPortWithNegativeMinPort() {
|
60 | 59 | assertThatIllegalArgumentException().isThrownBy(() ->
|
61 |
| - SocketUtils.findAvailableTcpPort(-500)); |
| 60 | + org.springframework.util.SocketUtils.findAvailableTcpPort(-500)); |
62 | 61 | }
|
63 | 62 |
|
64 | 63 | @Test
|
65 | 64 | void findAvailableTcpPort() {
|
66 |
| - int port = SocketUtils.findAvailableTcpPort(); |
67 |
| - assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX); |
| 65 | + int port = org.springframework.util.SocketUtils.findAvailableTcpPort(); |
| 66 | + assertPortInRange(port, org.springframework.util.SocketUtils.PORT_RANGE_MIN, |
| 67 | + org.springframework.util.SocketUtils.PORT_RANGE_MAX); |
68 | 68 | }
|
69 | 69 |
|
70 | 70 | @Test
|
71 | 71 | void findAvailableTcpPortWithMinPortEqualToMaxPort() {
|
72 |
| - int minMaxPort = SocketUtils.findAvailableTcpPort(); |
73 |
| - int port = SocketUtils.findAvailableTcpPort(minMaxPort, minMaxPort); |
| 72 | + int minMaxPort = org.springframework.util.SocketUtils.findAvailableTcpPort(); |
| 73 | + int port = org.springframework.util.SocketUtils.findAvailableTcpPort(minMaxPort, minMaxPort); |
74 | 74 | assertThat(port).isEqualTo(minMaxPort);
|
75 | 75 | }
|
76 | 76 |
|
77 | 77 | @Test
|
78 | 78 | void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
|
79 |
| - int port = SocketUtils.findAvailableTcpPort(); |
| 79 | + int port = org.springframework.util.SocketUtils.findAvailableTcpPort(); |
80 | 80 | try (ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"))) {
|
81 | 81 | assertThat(socket).isNotNull();
|
82 | 82 | // will only look for the exact port
|
83 | 83 | assertThatIllegalStateException().isThrownBy(() ->
|
84 |
| - SocketUtils.findAvailableTcpPort(port, port)) |
| 84 | + org.springframework.util.SocketUtils.findAvailableTcpPort(port, port)) |
85 | 85 | .withMessageStartingWith("Could not find an available TCP port")
|
86 | 86 | .withMessageEndingWith("after 1 attempts");
|
87 | 87 | }
|
88 | 88 | }
|
89 | 89 |
|
90 | 90 | @Test
|
91 | 91 | void findAvailableTcpPortWithMin() {
|
92 |
| - int port = SocketUtils.findAvailableTcpPort(50000); |
93 |
| - assertPortInRange(port, 50000, PORT_RANGE_MAX); |
| 92 | + int port = org.springframework.util.SocketUtils.findAvailableTcpPort(50000); |
| 93 | + assertPortInRange(port, 50000, org.springframework.util.SocketUtils.PORT_RANGE_MAX); |
94 | 94 | }
|
95 | 95 |
|
96 | 96 | @Test
|
97 | 97 | void findAvailableTcpPortInRange() {
|
98 | 98 | int minPort = 20000;
|
99 | 99 | int maxPort = minPort + 1000;
|
100 |
| - int port = SocketUtils.findAvailableTcpPort(minPort, maxPort); |
| 100 | + int port = org.springframework.util.SocketUtils.findAvailableTcpPort(minPort, maxPort); |
101 | 101 | assertPortInRange(port, minPort, maxPort);
|
102 | 102 | }
|
103 | 103 |
|
@@ -133,45 +133,46 @@ void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
|
133 | 133 | @Test
|
134 | 134 | void findAvailableUdpPortWithZeroMinPort() {
|
135 | 135 | assertThatIllegalArgumentException().isThrownBy(() ->
|
136 |
| - SocketUtils.findAvailableUdpPort(0)); |
| 136 | + org.springframework.util.SocketUtils.findAvailableUdpPort(0)); |
137 | 137 | }
|
138 | 138 |
|
139 | 139 | @Test
|
140 | 140 | void findAvailableUdpPortWithNegativeMinPort() {
|
141 | 141 | assertThatIllegalArgumentException().isThrownBy(() ->
|
142 |
| - SocketUtils.findAvailableUdpPort(-500)); |
| 142 | + org.springframework.util.SocketUtils.findAvailableUdpPort(-500)); |
143 | 143 | }
|
144 | 144 |
|
145 | 145 | @Test
|
146 | 146 | void findAvailableUdpPort() {
|
147 |
| - int port = SocketUtils.findAvailableUdpPort(); |
148 |
| - assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX); |
| 147 | + int port = org.springframework.util.SocketUtils.findAvailableUdpPort(); |
| 148 | + assertPortInRange(port, org.springframework.util.SocketUtils.PORT_RANGE_MIN, |
| 149 | + org.springframework.util.SocketUtils.PORT_RANGE_MAX); |
149 | 150 | }
|
150 | 151 |
|
151 | 152 | @Test
|
152 | 153 | void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
|
153 |
| - int port = SocketUtils.findAvailableUdpPort(); |
| 154 | + int port = org.springframework.util.SocketUtils.findAvailableUdpPort(); |
154 | 155 | try (DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"))) {
|
155 | 156 | assertThat(socket).isNotNull();
|
156 | 157 | // will only look for the exact port
|
157 | 158 | assertThatIllegalStateException().isThrownBy(() ->
|
158 |
| - SocketUtils.findAvailableUdpPort(port, port)) |
| 159 | + org.springframework.util.SocketUtils.findAvailableUdpPort(port, port)) |
159 | 160 | .withMessageStartingWith("Could not find an available UDP port")
|
160 | 161 | .withMessageEndingWith("after 1 attempts");
|
161 | 162 | }
|
162 | 163 | }
|
163 | 164 |
|
164 | 165 | @Test
|
165 | 166 | void findAvailableUdpPortWithMin() {
|
166 |
| - int port = SocketUtils.findAvailableUdpPort(50000); |
167 |
| - assertPortInRange(port, 50000, PORT_RANGE_MAX); |
| 167 | + int port = org.springframework.util.SocketUtils.findAvailableUdpPort(50000); |
| 168 | + assertPortInRange(port, 50000, org.springframework.util.SocketUtils.PORT_RANGE_MAX); |
168 | 169 | }
|
169 | 170 |
|
170 | 171 | @Test
|
171 | 172 | void findAvailableUdpPortInRange() {
|
172 | 173 | int minPort = 20000;
|
173 | 174 | int maxPort = minPort + 1000;
|
174 |
| - int port = SocketUtils.findAvailableUdpPort(minPort, maxPort); |
| 175 | + int port = org.springframework.util.SocketUtils.findAvailableUdpPort(minPort, maxPort); |
175 | 176 | assertPortInRange(port, minPort, maxPort);
|
176 | 177 | }
|
177 | 178 |
|
@@ -205,22 +206,24 @@ void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
|
205 | 206 | // Helpers
|
206 | 207 |
|
207 | 208 | private void findAvailableTcpPorts(int numRequested) {
|
208 |
| - SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested); |
209 |
| - assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX); |
| 209 | + SortedSet<Integer> ports = org.springframework.util.SocketUtils.findAvailableTcpPorts(numRequested); |
| 210 | + assertAvailablePorts(ports, numRequested, org.springframework.util.SocketUtils.PORT_RANGE_MIN, |
| 211 | + org.springframework.util.SocketUtils.PORT_RANGE_MAX); |
210 | 212 | }
|
211 | 213 |
|
212 | 214 | private void findAvailableTcpPorts(int numRequested, int minPort, int maxPort) {
|
213 |
| - SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested, minPort, maxPort); |
| 215 | + SortedSet<Integer> ports = org.springframework.util.SocketUtils.findAvailableTcpPorts(numRequested, minPort, maxPort); |
214 | 216 | assertAvailablePorts(ports, numRequested, minPort, maxPort);
|
215 | 217 | }
|
216 | 218 |
|
217 | 219 | private void findAvailableUdpPorts(int numRequested) {
|
218 |
| - SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested); |
219 |
| - assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX); |
| 220 | + SortedSet<Integer> ports = org.springframework.util.SocketUtils.findAvailableUdpPorts(numRequested); |
| 221 | + assertAvailablePorts(ports, numRequested, org.springframework.util.SocketUtils.PORT_RANGE_MIN, |
| 222 | + org.springframework.util.SocketUtils.PORT_RANGE_MAX); |
220 | 223 | }
|
221 | 224 |
|
222 | 225 | private void findAvailableUdpPorts(int numRequested, int minPort, int maxPort) {
|
223 |
| - SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort); |
| 226 | + SortedSet<Integer> ports = org.springframework.util.SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort); |
224 | 227 | assertAvailablePorts(ports, numRequested, minPort, maxPort);
|
225 | 228 | }
|
226 | 229 | private void assertPortInRange(int port, int minPort, int maxPort) {
|
|
0 commit comments