private boolean isPurlRunning() throws URISyntaxException, HttpException, ClientProtocolException, IOException { DefaultHttpClient client = new DefaultHttpClient(); URI url = new URI( "http://localhost:" + testInfo.getPurlzPort() + IdentifiersTestInfo.PURLZ_REST_LOGIN); HttpPost method = new HttpPost(url); List<NameValuePair> loginParams = new ArrayList<NameValuePair>(); loginParams.add(new BasicNameValuePair("id", IdentifiersTestInfo.PURLZ_USER)); loginParams.add(new BasicNameValuePair("passwd", IdentifiersTestInfo.PURLZ_PASSWORD)); loginParams.add(new BasicNameValuePair("referrer", "/docs/index.html")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(loginParams, "UTF-8"); method.setEntity(entity); try { System.out.println("isPurlRunning connecting to " + url); HttpResponse response = client.execute(method); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("PURL Login: HTTP Status code: " + statusCode); if (statusCode != HttpStatus.SC_OK) { throw new HttpException( " [" + statusCode + ":" + response.getStatusLine().toString() + "]"); } String responseStr = IdentifiersTestInfo.getResponseString(response); if (!responseStr.contains(IdentifiersTestInfo.PURLZ_WELCOME_MSG)) { System.out.println("BAD RESPONSE FROM SERVER [" + responseStr + "]"); return false; } System.out.println("Login to PURL successful..."); CookieStore store = client.getCookieStore(); for (Cookie cookie : store.getCookies()) { if (cookie.getName().equalsIgnoreCase(IdentifiersTestInfo.PURLZ_LOGIN_COOKIE)) { testInfo.setPurlzLoginCookie(cookie); return true; } } } finally { // Release the connection. method.abort(); client.getConnectionManager().shutdown(); } return false; }
@Override public void runStep() throws Exception { String startup = testInfo.getPurlzDirectory().getAbsolutePath() + File.separator + "bin" + File.separator; List<String> command = new ArrayList<String>(); // executable to call if (System.getProperty("os.name").toLowerCase().contains("win")) { command.add("cmd"); command.add("/c"); command.add(startup + "startup.bat"); command.add("run"); } else { command.add(startup + "start.sh"); command.add("run"); } List<String> additionalEnvironment = new ArrayList<String>(); additionalEnvironment.add("PURLZ_BASE_PATH=" + testInfo.getPurlzDirectory().getAbsolutePath()); String[] editedEnvironment = editEnvironment(additionalEnvironment); System.out.println("Command environment:\n"); for (String e : editedEnvironment) { System.out.println(e); } String[] commandArray = command.toArray(new String[command.size()]); try { testInfo.purlzProcess = Runtime.getRuntime().exec(commandArray, editedEnvironment, testInfo.getPurlzDirectory()); new StreamGobbler(testInfo.purlzProcess.getInputStream(), StreamGobbler.TYPE_OUT, System.out) .start(); new StreamGobbler(testInfo.purlzProcess.getErrorStream(), StreamGobbler.TYPE_OUT, System.err) .start(); } catch (Exception ex) { throw new Exception("Error invoking startup process: " + ex.getMessage(), ex); } // start checking for running Exception testException = null; sleep(2000); boolean running = false; int wait = 60; // seconds long waitMs = wait * 1000; long totalTime = 0; int attempt = 1; while (!running && totalTime < waitMs) { long start = System.currentTimeMillis(); System.out.println("Connection attempt " + (attempt)); try { running = isPurlRunning(); } catch (Exception ex) { testException = ex; // ex.printStackTrace(); } sleep(5000); attempt++; totalTime += (System.currentTimeMillis() - start); } if (!running) { if (testException != null) { throw new ContainerException( "Error starting PURLZ: " + testException.getMessage(), testException); } else { throw new ContainerException( "PURLZ non responsive after " + wait + " seconds attempting to connect"); } } }