1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
35
35
*/
36
36
public class SocketUtilsTests {
37
37
38
- private void assertPortInRange (int port , int minPort , int maxPort ) {
39
- assertTrue ("port [" + port + "] >= " + minPort , port >= minPort );
40
- assertTrue ("port [" + port + "] <= " + maxPort , port <= maxPort );
41
- }
42
-
43
- private void assertAvailablePorts (SortedSet <Integer > ports , int numRequested , int minPort , int maxPort ) {
44
- assertEquals ("number of ports requested" , numRequested , ports .size ());
45
- for (int port : ports ) {
46
- assertPortInRange (port , minPort , maxPort );
47
- }
48
- }
49
-
50
- // --- TCP -----------------------------------------------------------------
38
+ // TCP
51
39
52
40
@ Test (expected = IllegalArgumentException .class )
53
41
public void findAvailableTcpPortWithZeroMinPort () {
@@ -70,8 +58,7 @@ public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro
70
58
int port = SocketUtils .findAvailableTcpPort ();
71
59
ServerSocket socket = ServerSocketFactory .getDefault ().createServerSocket (port , 1 , InetAddress .getByName ("localhost" ));
72
60
try {
73
- // will only look for the exact port, since random.nextInt(1) always returns 0
74
- SocketUtils .findAvailableTcpPort (port , port + 1 );
61
+ SocketUtils .findAvailableTcpPort (port , port );
75
62
}
76
63
finally {
77
64
socket .close ();
@@ -117,17 +104,8 @@ public void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
117
104
findAvailableTcpPorts (50 , 45000 , 45010 );
118
105
}
119
106
120
- private void findAvailableTcpPorts (int numRequested ) {
121
- SortedSet <Integer > ports = SocketUtils .findAvailableTcpPorts (numRequested );
122
- assertAvailablePorts (ports , numRequested , PORT_RANGE_MIN , PORT_RANGE_MAX );
123
- }
124
107
125
- private void findAvailableTcpPorts (int numRequested , int minPort , int maxPort ) {
126
- SortedSet <Integer > ports = SocketUtils .findAvailableTcpPorts (numRequested , minPort , maxPort );
127
- assertAvailablePorts (ports , numRequested , minPort , maxPort );
128
- }
129
-
130
- // --- UDP -----------------------------------------------------------------
108
+ // UDP
131
109
132
110
@ Test (expected = IllegalArgumentException .class )
133
111
public void findAvailableUdpPortWithZeroMinPort () {
@@ -150,8 +128,8 @@ public void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro
150
128
int port = SocketUtils .findAvailableUdpPort ();
151
129
DatagramSocket socket = new DatagramSocket (port , InetAddress .getByName ("localhost" ));
152
130
try {
153
- // will only look for the exact port, since random.nextInt(1) always returns 0
154
- SocketUtils .findAvailableUdpPort (port , port + 1 );
131
+ // will only look for the exact port
132
+ SocketUtils .findAvailableUdpPort (port , port );
155
133
}
156
134
finally {
157
135
socket .close ();
@@ -197,6 +175,19 @@ public void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
197
175
findAvailableUdpPorts (50 , 45000 , 45010 );
198
176
}
199
177
178
+
179
+ // Helpers
180
+
181
+ private void findAvailableTcpPorts (int numRequested ) {
182
+ SortedSet <Integer > ports = SocketUtils .findAvailableTcpPorts (numRequested );
183
+ assertAvailablePorts (ports , numRequested , PORT_RANGE_MIN , PORT_RANGE_MAX );
184
+ }
185
+
186
+ private void findAvailableTcpPorts (int numRequested , int minPort , int maxPort ) {
187
+ SortedSet <Integer > ports = SocketUtils .findAvailableTcpPorts (numRequested , minPort , maxPort );
188
+ assertAvailablePorts (ports , numRequested , minPort , maxPort );
189
+ }
190
+
200
191
private void findAvailableUdpPorts (int numRequested ) {
201
192
SortedSet <Integer > ports = SocketUtils .findAvailableUdpPorts (numRequested );
202
193
assertAvailablePorts (ports , numRequested , PORT_RANGE_MIN , PORT_RANGE_MAX );
@@ -206,5 +197,16 @@ private void findAvailableUdpPorts(int numRequested, int minPort, int maxPort) {
206
197
SortedSet <Integer > ports = SocketUtils .findAvailableUdpPorts (numRequested , minPort , maxPort );
207
198
assertAvailablePorts (ports , numRequested , minPort , maxPort );
208
199
}
200
+ private void assertPortInRange (int port , int minPort , int maxPort ) {
201
+ assertTrue ("port [" + port + "] >= " + minPort , port >= minPort );
202
+ assertTrue ("port [" + port + "] <= " + maxPort , port <= maxPort );
203
+ }
204
+
205
+ private void assertAvailablePorts (SortedSet <Integer > ports , int numRequested , int minPort , int maxPort ) {
206
+ assertEquals ("number of ports requested" , numRequested , ports .size ());
207
+ for (int port : ports ) {
208
+ assertPortInRange (port , minPort , maxPort );
209
+ }
210
+ }
209
211
210
212
}
0 commit comments