11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3535 */
3636public class SocketUtilsTests {
3737
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
5139
5240 @ Test (expected = IllegalArgumentException .class )
5341 public void findAvailableTcpPortWithZeroMinPort () {
@@ -70,8 +58,7 @@ public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro
7058 int port = SocketUtils .findAvailableTcpPort ();
7159 ServerSocket socket = ServerSocketFactory .getDefault ().createServerSocket (port , 1 , InetAddress .getByName ("localhost" ));
7260 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 );
7562 }
7663 finally {
7764 socket .close ();
@@ -117,17 +104,8 @@ public void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
117104 findAvailableTcpPorts (50 , 45000 , 45010 );
118105 }
119106
120- private void findAvailableTcpPorts (int numRequested ) {
121- SortedSet <Integer > ports = SocketUtils .findAvailableTcpPorts (numRequested );
122- assertAvailablePorts (ports , numRequested , PORT_RANGE_MIN , PORT_RANGE_MAX );
123- }
124107
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
131109
132110 @ Test (expected = IllegalArgumentException .class )
133111 public void findAvailableUdpPortWithZeroMinPort () {
@@ -150,8 +128,8 @@ public void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro
150128 int port = SocketUtils .findAvailableUdpPort ();
151129 DatagramSocket socket = new DatagramSocket (port , InetAddress .getByName ("localhost" ));
152130 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 );
155133 }
156134 finally {
157135 socket .close ();
@@ -197,6 +175,19 @@ public void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
197175 findAvailableUdpPorts (50 , 45000 , 45010 );
198176 }
199177
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+
200191 private void findAvailableUdpPorts (int numRequested ) {
201192 SortedSet <Integer > ports = SocketUtils .findAvailableUdpPorts (numRequested );
202193 assertAvailablePorts (ports , numRequested , PORT_RANGE_MIN , PORT_RANGE_MAX );
@@ -206,5 +197,16 @@ private void findAvailableUdpPorts(int numRequested, int minPort, int maxPort) {
206197 SortedSet <Integer > ports = SocketUtils .findAvailableUdpPorts (numRequested , minPort , maxPort );
207198 assertAvailablePorts (ports , numRequested , minPort , maxPort );
208199 }
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+ }
209211
210212}
0 commit comments