Source:
public void agentLoads() throws IOException, InterruptedException { final String buildDirectory = (String) System.getProperties().get("buildDirectory"); final String finalName = (String) System.getProperties().get("finalName"); final int port = Integer.parseInt((String) System.getProperties().get("it.port")); final String config = getClass().getClassLoader().getResource("test.yml").getFile(); final String javaagent = "-javaagent:" + buildDirectory + "/" + finalName + ".jar=" + port + ":" + config; final String javaHome = System.getenv("JAVA_HOME"); final String java; if (javaHome != null && javaHome.equals("")) { java = javaHome + "/bin/java"; } else { java = "java"; } final Process app = new ProcessBuilder() .command(java, javaagent, "-cp", buildClasspath(), "io.prometheus.jmx.TestApplication") .start(); try { app.getInputStream().read(); URL uri = new URL("http://localhost:" + port + "/metrics"); HttpURLConnection cnx = (HttpURLConnection) uri.openConnection(); InputStream stream = cnx.getInputStream(); BufferedReader contents = new BufferedReader(new InputStreamReader(stream)); boolean found = false; while (!found) { String line = contents.readLine(); if (line == null) { break; } if (line.contains("jmx_scrape_duration_seconds")) { found = true; } } assertThat("Expected metric not found", found); cnx.disconnect(); app.getOutputStream().write('\n'); try { app.getOutputStream().flush(); } catch (IOException ignored) { } } catch (Throwable ex) { assertThat("Expection catched during scraping", false); ex.printStackTrace(); } finally { final int exitcode = app.waitFor(); int len; byte[] buffer = new byte[100]; while ((len = app.getErrorStream().read(buffer)) != -1) { System.out.write(buffer, 0, len); } assertThat("Application did not exit cleanly", exitcode == 0); } }

Target:
public void agentLoads() throws IOException, InterruptedException { final String buildDirectory = (String) System.getProperties().get("buildDirectory"); final String finalName = (String) System.getProperties().get("finalName"); final int port = Integer.parseInt((String) System.getProperties().get("it.port")); final String config = getClass().getClassLoader().getResource("test.yml").getFile(); final String javaagent = "-javaagent:" + buildDirectory + "/" + finalName + ".jar=" + port + ":" + config; final String javaHome = System.getenv("JAVA_HOME"); final String java; if (javaHome != null && javaHome.equals("")) { java = javaHome + "/bin/java"; } else { java = "java"; } final Process app = new ProcessBuilder() .command(java, javaagent, "-cp", buildClasspath(), "io.prometheus.jmx.TestApplication") .start(); try { app.getInputStream().read(); URL uri = new URL("http://localhost:" + port + "/metrics"); HttpURLConnection cnx = (HttpURLConnection) uri.openConnection(); InputStream stream = cnx.getInputStream(); BufferedReader contents = new BufferedReader(new InputStreamReader(stream)); boolean found = false; while (!found) { String line = contents.readLine(); if (line == null) { break; } if (line.contains("jmx_scrape_duration_seconds")) { found = true; } } assertThat("Expected metric not found", found); cnx.disconnect(); app.getOutputStream().write('\n'); try { app.getOutputStream().flush(); } catch (IOException ignored) { } } catch (Throwable ex) { assertThat("Expection caught during scraping", false); ex.printStackTrace(); } finally { final int exitcode = app.waitFor(); int len; byte[] buffer = new byte[100]; while ((len = app.getErrorStream().read(buffer)) != -1) { System.out.write(buffer, 0, len); } assertThat("Application did not exit cleanly", exitcode == 0); } }