<commit_before>// Copyright © 2008-2010 <NAME> <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://dimdwarf.sourceforge.net/LICENSE

package net.orfjackal.dimdwarf.test.runner;

import com.google.inject.Module;
import net.orfjackal.dimdwarf.test.util.*;
import org.apache.commons.io.*;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.CloseShieldOutputStream;
import org.slf4j.*;

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class ServerRunner {

    private static final Logger logger = LoggerFactory.getLogger(ServerRunner.class);

    private static final int TIMEOUT = 5;
    private static final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS;

    private static final String JAVA_EXECUTABLE = "java";
    private static final List<String> JAR_TO_EXECUTE = Arrays.asList("-jar", "launcher.jar");
    private final List<String> jvmOptions = new ArrayList<String>();
    private final String host;
    private final int port;

    private File applicationDir;
    private Process serverProcess;
    private StreamWatcher outputWatcher;

    public ServerRunner() {
        this(SocketUtil.anyFreePort());
    }

    public ServerRunner(int port) {
        this("localhost", port);
    }

    public ServerRunner(String host, int port) {
        this.host = host;
        this.port = port;
        addJvmOptions("-ea");
    }

    public void addJvmOptions(String... options) {
        Collections.addAll(jvmOptions, options);
    }

    public void startApplication(Class<? extends Module> application) throws IOException, InterruptedException {
        deployApplication(application);
        startServer();
        waitForServerToStart();
    }

    private void deployApplication(Class<?> application) throws IOException {
        applicationDir = TestEnvironment.createTempDir();
        createAppJar();
        createAppConfig(application);
    }

    private void createAppJar() throws IOException {
        File baseJar = TestEnvironment.getApplicationBaseJar();
        File appLibs = new File(applicationDir, "lib");
        FileUtils.copyFileToDirectory(baseJar, appLibs);
    }

    private void createAppConfig(Class<?> application) throws IOException {
        File configFile = new File(applicationDir, "classes/META-INF/app.properties");
        FileUtils.writeLines(configFile, Arrays.asList(
                "dimdwarf.app.name=" + application.getSimpleName(),
                "dimdwarf.app.module=" + application.getName()
        ));
    }

    private void startServer() throws IOException {
        serverProcess = startProcess(
                TestEnvironment.getDeploymentDir(),
                commandToStartServer()
        );
        OutputStream toWatcher = streamToWatcher();
        redirectStream(serverProcess.getInputStream(), System.out, toWatcher);
        redirectStream(serverProcess.getErrorStream(), System.err, toWatcher);
    }

    private List<String> commandToStartServer() {
        List<String> arguments = Arrays.asList(
                "--port", String.valueOf(port),
                "--app", applicationDir.getPath()
        );

        List<String> command = new ArrayList<String>();
        command.add(JAVA_EXECUTABLE);
        command.addAll(jvmOptions);
        command.addAll(JAR_TO_EXECUTE);
        command.addAll(arguments);
        return command;
    }

    private static Process startProcess(File workingDir, List<String> command) throws IOException {
        logger.info("Starting process: {}\n    in working directory {}", formatForCommandLine(command), workingDir);

        ProcessBuilder builder = new ProcessBuilder();
        builder.directory(workingDir);
        builder.redirectErrorStream(false);
        builder.command(command);
        return builder.start();
    }

    private static String formatForCommandLine(List<String> command) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < command.size(); i++) {
            if (i > 0) {
                sb.append(' ');
            }
            String arg = command.get(i);
            if (arg.contains(" ") || arg.contains("\\")) {<commit_msg>ServerRunner: use the same JRE as for running the tests, not the system's default JRE<commit_after>// Copyright © 2008-2010 <NAME> <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://dimdwarf.sourceforge.net/LICENSE

package net.orfjackal.dimdwarf.test.runner;

import com.google.inject.Module;
import net.orfjackal.dimdwarf.test.util.*;
import org.apache.commons.io.*;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.CloseShieldOutputStream;
import org.slf4j.*;

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class ServerRunner {

    private static final Logger logger = LoggerFactory.getLogger(ServerRunner.class);

    private static final int TIMEOUT = 5;
    private static final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS;

    private static final String JAVA_EXECUTABLE = new File(System.getProperty("java.home"), "bin/java").getAbsolutePath();
    private static final List<String> JAR_TO_EXECUTE = Arrays.asList("-jar", "launcher.jar");
    private final List<String> jvmOptions = new ArrayList<String>();
    private final String host;
    private final int port;

    private File applicationDir;
    private Process serverProcess;
    private StreamWatcher outputWatcher;

    public ServerRunner() {
        this(SocketUtil.anyFreePort());
    }

    public ServerRunner(int port) {
        this("localhost", port);
    }

    public ServerRunner(String host, int port) {
        this.host = host;
        this.port = port;
        addJvmOptions("-ea");
    }

    public void addJvmOptions(String... options) {
        Collections.addAll(jvmOptions, options);
    }

    public void startApplication(Class<? extends Module> application) throws IOException, InterruptedException {
        deployApplication(application);
        startServer();
        waitForServerToStart();
    }

    private void deployApplication(Class<?> application) throws IOException {
        applicationDir = TestEnvironment.createTempDir();
        createAppJar();
        createAppConfig(application);
    }

    private void createAppJar() throws IOException {
        File baseJar = TestEnvironment.getApplicationBaseJar();
        File appLibs = new File(applicationDir, "lib");
        FileUtils.copyFileToDirectory(baseJar, appLibs);
    }

    private void createAppConfig(Class<?> application) throws IOException {
        File configFile = new File(applicationDir, "classes/META-INF/app.properties");
        FileUtils.writeLines(configFile, Arrays.asList(
                "dimdwarf.app.name=" + application.getSimpleName(),
                "dimdwarf.app.module=" + application.getName()
        ));
    }

    private void startServer() throws IOException {
        serverProcess = startProcess(
                TestEnvironment.getDeploymentDir(),
                commandToStartServer()
        );
        OutputStream toWatcher = streamToWatcher();
        redirectStream(serverProcess.getInputStream(), System.out, toWatcher);
        redirectStream(serverProcess.getErrorStream(), System.err, toWatcher);
    }

    private List<String> commandToStartServer() {
        List<String> arguments = Arrays.asList(
                "--port", String.valueOf(port),
                "--app", applicationDir.getPath()
        );

        List<String> command = new ArrayList<String>();
        command.add(JAVA_EXECUTABLE);
        command.addAll(jvmOptions);
        command.addAll(JAR_TO_EXECUTE);
        command.addAll(arguments);
        return command;
    }

    private static Process startProcess(File workingDir, List<String> command) throws IOException {
        logger.info("Starting process in working directory {}\n\t{}", workingDir, formatForCommandLine(command));

        ProcessBuilder builder = new ProcessBuilder();
        builder.directory(workingDir);
        builder.redirectErrorStream(false);
        builder.command(command);
        return builder.start();
    }

    private static String formatForCommandLine(List<String> command) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < command.size(); i++) {
            if (i > 0) {
                sb.append(' ');
            }
            String arg = command.get(i);
            if (arg.contains(" ") || arg.contains("\\")) {
{'fraction_non_alphanumeric': 0.10800744878957169, 'fraction_numerical': 0.0032588454376163874, 'mean_word_length': 2.989322191272052, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894629', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2246, 'n_words': 548}