package io.prometheus.jmx;

import static org.hamcrest.MatcherAssert.assertThat;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

public class JavaAgentIT {
    private List<URL> getClassloaderUrls() {
        return getClassloaderUrls(getClass().getClassLoader());
    }

    private static List<URL> getClassloaderUrls(ClassLoader classLoader) {
        if (classLoader == null) {
            return Collections.emptyList();
        }
        if (!(classLoader instanceof URLClassLoader)) {
            return getClassloaderUrls(classLoader.getParent());
        }
        URLClassLoader u = (URLClassLoader) classLoader;
        List<URL> result = new ArrayList<URL>(Arrays.asList(u.getURLs()));
        result.addAll(getClassloaderUrls(u.getParent()));
        return result;
    }

    private String buildClasspath() {
        StringBuilder sb = new StringBuilder();
        for (URL url : getClassloaderUrls()) {
            if (!url.getProtocol().equals("file")) {
                continue;
            }
            if (sb.length() != 0) {
                sb.append(java.io.File.pathSeparatorChar);
            }
            sb.append(url.getPath());
        }
        return sb.toString();
    }

    @Test
    public void agentLoads() throws IOException, InterruptedException {
        // If not starting the testcase via Maven, set the buildDirectory and finalName system properties manually.
        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 = resolveRelativePathToResource("test.yml");
        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 {
            // Wait for application to start
            app.getInputStream().read();

            InputStream stream = new URL("http://localhost:" + port + "/metrics").openStream();
            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);

            // Tell application to stop
            app.getOutputStream().write('\n');
            try {
                app.getOutputStream().flush();
            } catch (IOException ignored) {
            }
        } finally {
            final int exitcode = app.waitFor();
            // Log any errors printed
            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);
        }
    }

    //trying to avoid the occurrence of any : in the windows path
    private String resolveRelativePathToResource(String resource) {
        final String configwk = new File(getClass().getClassLoader().getResource(resource).getFile()).getAbsolutePath();
        final File workingDir = new File(new File(".").getAbsolutePath());
        return "." + configwk.replace(workingDir.getParentFile().getAbsolutePath(), "");
    }
}

@@ -428,16 +428,148 @@
 entIT %7B%0A
+%0A    private static final String DEFAULT_JAVA_HOME_PATH = %22/bin/java%22;%0A    private static final String DEFAULT_JAVA_PATH = %22java%22;%0A%0A
     priv
@@ -2263,17 +2263,16 @@
 config;%0A
-%0A
         
@@ -2292,15 +2292,25 @@
 java
-Home = 
+ = buildJavaPath(
 Syst
@@ -2335,188 +2335,10 @@
 ME%22)
+)
 ;
-%0A        final String java;%0A        if (javaHome != null && javaHome.equals(%22%22)) %7B%0A            java = javaHome + %22/bin/java%22;%0A        %7D else %7B%0A            java = %22java%22;%0A        %7D
 %0A%0A  
@@ -3774,16 +3774,16 @@
  == 0);%0A
-
         
@@ -3787,24 +3787,239 @@
    %7D%0A    %7D%0A%0A
+    private String buildJavaPath(String javaHome) %7B%0A        if (!(javaHome == null %7C%7C javaHome.isEmpty())) %7B%0A            return javaHome + DEFAULT_JAVA_HOME_PATH;%0A        %7D%0A%0A        return DEFAULT_JAVA_PATH;%0A    %7D%0A%0A
     //trying

Fix incorrect 'java' path when JAVA_HOME isn't empty (#481)