NetUtil { public static boolean pingUrl(String address) { try { URL urlObj = new URL(address); HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.setConnectTimeout(DEFAULT_TIMEOUT_IN_SECONDS); connection.setReadTimeout(DEFAULT_TIMEOUT_IN_SECONDS); int statusCode = connection.getResponseCode(); cleanUpConnection(connection); return (200 <= statusCode && statusCode <= 399); } catch (Throwable ignore) { } return false; }  static boolean pingUrl(String address);  }
@Test public void testPingUrlWithStatusCode200() throws Exception { String someResponse = "some response"; ContextHandler handler = mockServerHandler(HttpServletResponse.SC_OK, someResponse); startServerWithHandlers(handler); assertTrue(NetUtil.pingUrl("http: }
@Test public void testPingUrlWithStatusCode404() throws Exception { String someResponse = "some response"; startServerWithHandlers(mockServerHandler(HttpServletResponse.SC_NOT_FOUND, someResponse)); assertFalse(NetUtil.pingUrl("http: }
@Test public void testPingUrlWithServerNotStarted() throws Exception { assertFalse(NetUtil.pingUrl("http: }