2525
2626import java .io .IOException ;
2727import java .net .*;
28+ import java .time .Duration ;
2829import java .util .List ;
2930import java .util .Observable ;
3031import java .util .Observer ;
@@ -81,6 +82,18 @@ public static void assertOnline(String host, int time, TimeUnit unit) {
8182 assertTrue (isOnline (host , time , unit ), host + " is offline" );
8283 }
8384
85+ /**
86+ * Asserts, that the given host is online. The given time is the maximal
87+ * time we try to connect to the given host.
88+ *
89+ * @param host hostname or ip address
90+ * @param timeout how long should we try to connect to host?
91+ * @since 2.6
92+ */
93+ public static void assertOnline (String host , Duration timeout ) {
94+ assertTrue (isOnline (host , timeout ), host + " is offline" );
95+ }
96+
8497 /**
8598 * Checks if the given host is online. The given time is the maximal
8699 * time we try to connect to the given host.
@@ -96,6 +109,19 @@ public static boolean isOnline(String host, int time, TimeUnit unit) {
96109 return scanner .openPortDetected ();
97110 }
98111
112+ /**
113+ * Checks if the given host is online. The given time is the maximal
114+ * time we try to connect to the given host.
115+ *
116+ * @param host hostname or ip address
117+ * @param timeout how long should we try to connect to host?
118+ * @return true if host is online
119+ * @since 2.6
120+ */
121+ public static boolean isOnline (String host , Duration timeout ) {
122+ return isOnline (host , (int ) timeout .toMillis (), TimeUnit .MILLISECONDS );
123+ }
124+
99125 private static PortScanner scanPortsOf (String host , int timeout , TimeUnit unit ) {
100126 PortScanner scanner = new PortScanner (host );
101127 scanner .scanPorts (timeout , unit );
@@ -142,6 +168,17 @@ public static void assertOnline(InetAddress host, int time, TimeUnit unit) {
142168 assertOnline (host .getHostName (), time , unit );
143169 }
144170
171+ /**
172+ * Asserts, that the given host is online. The given time is the maximal
173+ * time we try to connect to the given host.
174+ *
175+ * @param host hostname or ip address
176+ * @param timeout how long should we try to connect to host?
177+ * @since 2.6
178+ */
179+ public static void assertOnline (InetAddress host , Duration timeout ) {
180+ assertOnline (host .getHostName (), timeout );
181+ }
145182
146183 /**
147184 * Asserts, that the given host is online at the given port.
@@ -209,6 +246,19 @@ public static boolean isOnline(String host, int port, long timeout, TimeUnit uni
209246 }
210247 }
211248
249+ /**
250+ * Checks if the given host is online in the given time distance.
251+ *
252+ * @param host the hostname or IP address
253+ * @param port the port between 0 and 0xFFFF
254+ * @param timeout timeout
255+ * @return true if host is online
256+ * @since 2.6
257+ */
258+ public static boolean isOnline (String host , int port , Duration timeout ) {
259+ return isOnline (host , port , timeout .toMillis (), TimeUnit .MILLISECONDS );
260+ }
261+
212262 /**
213263 * Checks if the given URI is online. In contrast to {@link #exists(URI)}
214264 * it is not checked if the URI is present but if the service behind the
@@ -229,27 +279,27 @@ private static int getPortOf(URI uri) {
229279 if (port > 0 ) {
230280 return port ;
231281 }
232- switch (uri .getScheme ().toLowerCase ()) {
233- case "ftp" : return 21 ;
234- case "ssh" : return 22 ;
235- case "telnet" : return 23 ;
236- case "http" : return 80 ;
237- case "auth" : return 113 ;
238- case "sftp" : return 115 ;
239- case "ntp" : return 123 ;
240- case "snmp" : return 161 ;
241- case "irc" : return 194 ;
242- case "ldap" : return 389 ;
243- case "https" : return 443 ;
244- case "rtsp" : return 554 ;
245- case "ipp" : return 631 ;
246- case "ldaps" : return 636 ;
247- case "ftps" : return 990 ;
248- case "ircs" : return 994 ;
249- case "nfs" : return 2049 ;
250- case "svn" : return 3690 ;
251- default : return port ;
252- }
282+ return switch (uri .getScheme ().toLowerCase ()) {
283+ case "ftp" -> 21 ;
284+ case "ssh" -> 22 ;
285+ case "telnet" -> 23 ;
286+ case "http" -> 80 ;
287+ case "auth" -> 113 ;
288+ case "sftp" -> 115 ;
289+ case "ntp" -> 123 ;
290+ case "snmp" -> 161 ;
291+ case "irc" -> 194 ;
292+ case "ldap" -> 389 ;
293+ case "https" -> 443 ;
294+ case "rtsp" -> 554 ;
295+ case "ipp" -> 631 ;
296+ case "ldaps" -> 636 ;
297+ case "ftps" -> 990 ;
298+ case "ircs" -> 994 ;
299+ case "nfs" -> 2049 ;
300+ case "svn" -> 3690 ;
301+ default -> port ;
302+ };
253303 }
254304
255305 /**
@@ -290,6 +340,20 @@ public static void assertOffline(String host, int time, TimeUnit unit) {
290340 assertFalse (scanner .openPortDetected (), host + " is online" );
291341 }
292342
343+ /**
344+ * Asserts, that the given host is offline. The given time is the maximal
345+ * time we try to connect to the given host. Normally it takes about at
346+ * least 8 minutes to realize that a host is offline. So if you want to
347+ * wait a shorter time use this method.
348+ *
349+ * @param host hostname or ip address
350+ * @param timeout how long should we try to connect to host?
351+ * @since 2.6
352+ */
353+ public static void assertOffline (String host , Duration timeout ) {
354+ assertOffline (host , (int ) timeout .toMillis (), TimeUnit .MILLISECONDS );
355+ }
356+
293357 /**
294358 * Asserts, that the port of the given host is offline.
295359 *
0 commit comments