1 | package fr.sii.ogham.testing.util; | |
2 | ||
3 | import java.net.DatagramSocket; | |
4 | import java.net.InetAddress; | |
5 | import java.net.ServerSocket; | |
6 | import java.util.SortedSet; | |
7 | ||
8 | import javax.net.ServerSocketFactory; | |
9 | ||
10 | import fr.sii.ogham.testing.util.port.DefaultPortFinder; | |
11 | import fr.sii.ogham.testing.util.port.PortFinder; | |
12 | ||
13 | /** | |
14 | * Simple utility methods for finding available ports on {@code localhost}. | |
15 | * | |
16 | * <p> | |
17 | * Within this class, a TCP port refers to a port for a {@link ServerSocket}; | |
18 | * whereas, a UDP port refers to a port for a {@link DatagramSocket}. | |
19 | * | |
20 | * <strong>NOTE:</strong> This code has been borrowed from Spring Framework. | |
21 | * | |
22 | * @see "https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/util/SocketUtils.java" | |
23 | */ | |
24 | public final class RandomPortUtils { | |
25 | ||
26 | /** | |
27 | * The default minimum value for port ranges used when finding an available | |
28 | * socket port. | |
29 | */ | |
30 | public static final int PORT_RANGE_MIN = 1024; | |
31 | ||
32 | /** | |
33 | * The default maximum value for port ranges used when finding an available | |
34 | * socket port. | |
35 | */ | |
36 | public static final int PORT_RANGE_MAX = 65535; | |
37 | ||
38 | /** | |
39 | * Find an available TCP port randomly selected from the range | |
40 | * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. | |
41 | * | |
42 | * @return an available TCP port number | |
43 | * @throws IllegalStateException | |
44 | * if no available port could be found | |
45 | */ | |
46 | public static int findAvailableTcpPort() { | |
47 |
3
1. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → NO_COVERAGE 2. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → KILLED 3. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → KILLED |
return findAvailableTcpPort(PORT_RANGE_MIN); |
48 | } | |
49 | ||
50 | /** | |
51 | * Find an available TCP port randomly selected from the range | |
52 | * [{@code minPort}, {@value #PORT_RANGE_MAX}]. | |
53 | * | |
54 | * @param minPort | |
55 | * the minimum port number | |
56 | * @return an available TCP port number | |
57 | * @throws IllegalStateException | |
58 | * if no available port could be found | |
59 | */ | |
60 | public static int findAvailableTcpPort(int minPort) { | |
61 |
3
1. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → NO_COVERAGE 2. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → KILLED 3. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → KILLED |
return findAvailableTcpPort(minPort, PORT_RANGE_MAX); |
62 | } | |
63 | ||
64 | /** | |
65 | * Find an available TCP port randomly selected from the range | |
66 | * [{@code minPort}, {@code maxPort}]. | |
67 | * | |
68 | * @param minPort | |
69 | * the minimum port number | |
70 | * @param maxPort | |
71 | * the maximum port number | |
72 | * @return an available TCP port number | |
73 | * @throws IllegalStateException | |
74 | * if no available port could be found | |
75 | */ | |
76 | public static int findAvailableTcpPort(int minPort, int maxPort) { | |
77 |
6
1. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → SURVIVED 2. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → NO_COVERAGE 3. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → TIMED_OUT 4. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → KILLED 5. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → KILLED 6. findAvailableTcpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPort → KILLED |
return SocketType.TCP.findAvailablePort(minPort, maxPort); |
78 | } | |
79 | ||
80 | /** | |
81 | * Find the requested number of available TCP ports, each randomly selected | |
82 | * from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. | |
83 | * | |
84 | * @param numRequested | |
85 | * the number of available ports to find | |
86 | * @return a sorted set of available TCP port numbers | |
87 | * @throws IllegalStateException | |
88 | * if the requested number of available ports could not be found | |
89 | */ | |
90 | public static SortedSet<Integer> findAvailableTcpPorts(int numRequested) { | |
91 |
1
1. findAvailableTcpPorts : replaced return value with null for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPorts → NO_COVERAGE |
return findAvailableTcpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX); |
92 | } | |
93 | ||
94 | /** | |
95 | * Find the requested number of available TCP ports, each randomly selected | |
96 | * from the range [{@code minPort}, {@code maxPort}]. | |
97 | * | |
98 | * @param numRequested | |
99 | * the number of available ports to find | |
100 | * @param minPort | |
101 | * the minimum port number | |
102 | * @param maxPort | |
103 | * the maximum port number | |
104 | * @return a sorted set of available TCP port numbers | |
105 | * @throws IllegalStateException | |
106 | * if the requested number of available ports could not be found | |
107 | */ | |
108 | public static SortedSet<Integer> findAvailableTcpPorts(int numRequested, int minPort, int maxPort) { | |
109 |
1
1. findAvailableTcpPorts : replaced return value with null for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableTcpPorts → NO_COVERAGE |
return SocketType.TCP.findAvailablePorts(numRequested, minPort, maxPort); |
110 | } | |
111 | ||
112 | /** | |
113 | * Find an available UDP port randomly selected from the range | |
114 | * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. | |
115 | * | |
116 | * @return an available UDP port number | |
117 | * @throws IllegalStateException | |
118 | * if no available port could be found | |
119 | */ | |
120 | public static int findAvailableUdpPort() { | |
121 |
1
1. findAvailableUdpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableUdpPort → NO_COVERAGE |
return findAvailableUdpPort(PORT_RANGE_MIN); |
122 | } | |
123 | ||
124 | /** | |
125 | * Find an available UDP port randomly selected from the range | |
126 | * [{@code minPort}, {@value #PORT_RANGE_MAX}]. | |
127 | * | |
128 | * @param minPort | |
129 | * the minimum port number | |
130 | * @return an available UDP port number | |
131 | * @throws IllegalStateException | |
132 | * if no available port could be found | |
133 | */ | |
134 | public static int findAvailableUdpPort(int minPort) { | |
135 |
1
1. findAvailableUdpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableUdpPort → NO_COVERAGE |
return findAvailableUdpPort(minPort, PORT_RANGE_MAX); |
136 | } | |
137 | ||
138 | /** | |
139 | * Find an available UDP port randomly selected from the range | |
140 | * [{@code minPort}, {@code maxPort}]. | |
141 | * | |
142 | * @param minPort | |
143 | * the minimum port number | |
144 | * @param maxPort | |
145 | * the maximum port number | |
146 | * @return an available UDP port number | |
147 | * @throws IllegalStateException | |
148 | * if no available port could be found | |
149 | */ | |
150 | public static int findAvailableUdpPort(int minPort, int maxPort) { | |
151 |
1
1. findAvailableUdpPort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableUdpPort → NO_COVERAGE |
return SocketType.UDP.findAvailablePort(minPort, maxPort); |
152 | } | |
153 | ||
154 | /** | |
155 | * Find the requested number of available UDP ports, each randomly selected | |
156 | * from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. | |
157 | * | |
158 | * @param numRequested | |
159 | * the number of available ports to find | |
160 | * @return a sorted set of available UDP port numbers | |
161 | * @throws IllegalStateException | |
162 | * if the requested number of available ports could not be found | |
163 | */ | |
164 | public static SortedSet<Integer> findAvailableUdpPorts(int numRequested) { | |
165 |
1
1. findAvailableUdpPorts : replaced return value with null for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableUdpPorts → NO_COVERAGE |
return findAvailableUdpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX); |
166 | } | |
167 | ||
168 | /** | |
169 | * Find the requested number of available UDP ports, each randomly selected | |
170 | * from the range [{@code minPort}, {@code maxPort}]. | |
171 | * | |
172 | * @param numRequested | |
173 | * the number of available ports to find | |
174 | * @param minPort | |
175 | * the minimum port number | |
176 | * @param maxPort | |
177 | * the maximum port number | |
178 | * @return a sorted set of available UDP port numbers | |
179 | * @throws IllegalStateException | |
180 | * if the requested number of available ports could not be found | |
181 | */ | |
182 | public static SortedSet<Integer> findAvailableUdpPorts(int numRequested, int minPort, int maxPort) { | |
183 |
1
1. findAvailableUdpPorts : replaced return value with null for fr/sii/ogham/testing/util/RandomPortUtils::findAvailableUdpPorts → NO_COVERAGE |
return SocketType.UDP.findAvailablePorts(numRequested, minPort, maxPort); |
184 | } | |
185 | ||
186 | @SuppressWarnings("squid:IndentationCheck") | |
187 | private enum SocketType implements PortFinder { | |
188 | ||
189 | TCP { | |
190 | @Override | |
191 | protected boolean isPortAvailable(int port) { | |
192 | try { | |
193 | ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost")); | |
194 |
8
1. isPortAvailable : removed call to java/net/ServerSocket::close → NO_COVERAGE 2. isPortAvailable : removed call to java/net/ServerSocket::close → KILLED 3. isPortAvailable : removed call to java/net/ServerSocket::close → KILLED 4. isPortAvailable : removed call to java/net/ServerSocket::close → KILLED 5. isPortAvailable : removed call to java/net/ServerSocket::close → KILLED 6. isPortAvailable : removed call to java/net/ServerSocket::close → KILLED 7. isPortAvailable : removed call to java/net/ServerSocket::close → KILLED 8. isPortAvailable : removed call to java/net/ServerSocket::close → KILLED |
serverSocket.close(); |
195 |
8
1. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → NO_COVERAGE 2. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → TIMED_OUT 3. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → KILLED 4. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → KILLED 5. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → KILLED 6. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → KILLED 7. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → KILLED 8. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → KILLED |
return true; |
196 | } catch (Exception ex) { | |
197 |
1
1. isPortAvailable : replaced boolean return with true for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$1::isPortAvailable → NO_COVERAGE |
return false; |
198 | } | |
199 | } | |
200 | }, | |
201 | ||
202 | UDP { | |
203 | @Override | |
204 | protected boolean isPortAvailable(int port) { | |
205 | try { | |
206 | DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost")); | |
207 |
1
1. isPortAvailable : removed call to java/net/DatagramSocket::close → NO_COVERAGE |
socket.close(); |
208 |
1
1. isPortAvailable : replaced boolean return with false for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$2::isPortAvailable → NO_COVERAGE |
return true; |
209 | } catch (Exception ex) { | |
210 |
1
1. isPortAvailable : replaced boolean return with true for fr/sii/ogham/testing/util/RandomPortUtils$SocketType$2::isPortAvailable → NO_COVERAGE |
return false; |
211 | } | |
212 | } | |
213 | }; | |
214 | | |
215 | private PortFinder delegate; | |
216 | | |
217 | SocketType() { | |
218 | this.delegate = new DefaultPortFinder(name(), this::isPortAvailable); | |
219 | } | |
220 | ||
221 | protected abstract boolean isPortAvailable(int port); | |
222 | | |
223 | /** | |
224 | * Find an available port for this {@code SocketType}, randomly selected | |
225 | * from the range [{@code minPort}, {@code maxPort}]. | |
226 | * | |
227 | * @param minPort | |
228 | * the minimum port number | |
229 | * @param maxPort | |
230 | * the maximum port number | |
231 | * @return an available port number for this socket type | |
232 | * @throws IllegalStateException | |
233 | * if no available port could be found | |
234 | */ | |
235 | @Override | |
236 | public int findAvailablePort(int minPort, int maxPort) { | |
237 |
6
1. findAvailablePort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils$SocketType::findAvailablePort → NO_COVERAGE 2. findAvailablePort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils$SocketType::findAvailablePort → SURVIVED 3. findAvailablePort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils$SocketType::findAvailablePort → TIMED_OUT 4. findAvailablePort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils$SocketType::findAvailablePort → KILLED 5. findAvailablePort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils$SocketType::findAvailablePort → KILLED 6. findAvailablePort : replaced int return with 0 for fr/sii/ogham/testing/util/RandomPortUtils$SocketType::findAvailablePort → KILLED |
return delegate.findAvailablePort(minPort, maxPort); |
238 | } | |
239 | | |
240 | /** | |
241 | * Find the requested number of available ports for this | |
242 | * {@code SocketType}, each randomly selected from the range | |
243 | * [{@code minPort}, {@code maxPort}]. | |
244 | * | |
245 | * @param numRequested | |
246 | * the number of available ports to find | |
247 | * @param minPort | |
248 | * the minimum port number | |
249 | * @param maxPort | |
250 | * the maximum port number | |
251 | * @return a sorted set of available port numbers for this socket type | |
252 | * @throws IllegalStateException | |
253 | * if the requested number of available ports could not be | |
254 | * found | |
255 | */ | |
256 | @Override | |
257 | public SortedSet<Integer> findAvailablePorts(int numRequested, int minPort, int maxPort) { | |
258 |
1
1. findAvailablePorts : replaced return value with null for fr/sii/ogham/testing/util/RandomPortUtils$SocketType::findAvailablePorts → NO_COVERAGE |
return delegate.findAvailablePorts(numRequested, minPort, maxPort); |
259 | } | |
260 | } | |
261 | ||
262 | private RandomPortUtils() { | |
263 | super(); | |
264 | } | |
265 | ||
266 | } | |
Mutations | ||
47 |
1.1 2.2 3.3 |
|
61 |
1.1 2.2 3.3 |
|
77 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
91 |
1.1 |
|
109 |
1.1 |
|
121 |
1.1 |
|
135 |
1.1 |
|
151 |
1.1 |
|
165 |
1.1 |
|
183 |
1.1 |
|
194 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
195 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
197 |
1.1 |
|
207 |
1.1 |
|
208 |
1.1 |
|
210 |
1.1 |
|
237 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
258 |
1.1 |