// Path: src/test/java/scaffolding/Photocopier.java
// public class Photocopier {
// 
//     public static File sampleDir() {
//         return new File(separatorsToSystem("sample-apps"));
//     }
// 
//     public static File copySampleAppToTempDir(String sampleAppName) throws IOException {
//         File target = folderForSampleProject(sampleAppName);
//         copySampleAppToDir(sampleAppName, target);
//         return target;
//     }
// 
//     public static void copySampleAppToDir(String sampleAppName, File target) throws IOException {
//         String pathname = FilenameUtils.concat(fullPath(sampleDir()), sampleAppName);
//         File source = new File(pathname);
//         if (!source.isDirectory()) {
//             source = new File(separatorsToSystem("../") + pathname);
//         }
//         if (!source.isDirectory()) {
//             throw new RuntimeException("Could not find module " + sampleAppName + " at " + new File(pathname) + " nor " + fullPath(source));
//         }
//         FileUtils.copyDirectory(source, target);
//     }
// 
//     public static File folderForSampleProject(String moduleName) {
//         return new File(separatorsToSystem("target/samples/" + UUID.randomUUID() + "/" + moduleName));
//     }
// 
//     public static File tempFile(String name) throws IOException {
//         File file = new File(separatorsToSystem("target/temp/" + UUID.randomUUID() + "/" + name));
//         FileUtils.touch(file);
//         return file;
//     }
// }
// 
// Path: src/main/java/com/danielflower/apprunner/mgmt/AppManager.java
// public static int getAFreePort() {
//     try {
//         try (ServerSocket serverSocket = new ServerSocket(0)) {
//             return serverSocket.getLocalPort();
//         }
//     } catch (IOException e) {
//         throw new AppRunnerException("Unable to get a port", e);
//     }
// }
// 
// Path: src/test/java/scaffolding/TestConfig.java
// public static final Config config;


import org.apache.commons.io.output.StringBuilderWriter;
import org.junit.BeforeClass;
import org.junit.Test;
import scaffolding.Photocopier;
import java.util.Optional;
import static com.danielflower.apprunner.mgmt.AppManager.getAFreePort;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeTrue;
import static scaffolding.TestConfig.config;
package com.danielflower.apprunner.runners;





public class GradleRunnerTest {
    private static GradleRunnerFactory runnerFactory;
    private final StringBuilderWriter buildLog = new StringBuilderWriter();
    private final StringBuilderWriter consoleLog = new StringBuilderWriter();

    @BeforeClass
    public static void ignoreTestIfNotSupported() throws Exception {
        Optional<GradleRunnerFactory> runnerFactoryMaybe = GradleRunnerFactory.createIfAvailable(config);
        assumeTrue("Skipping test because Gradle and/or Java not detected", runnerFactoryMaybe.isPresent());
        runnerFactory = runnerFactoryMaybe.get();
    }

    @Test
    public void canStartAGradleProcessByPackagingAndRunning() throws Exception {
        String appName = "gradle";
        AppRunner runner = runnerFactory.appRunner(Photocopier.copySampleAppToTempDir(appName));
        int port = getAFreePort();
        startAndStop(1, appName, runner, port);
        startAndStop(2, appName, runner, port);
    }

    @Test
    public void theVersionIsReported() {
// Path: src/test/java/scaffolding/Photocopier.java
// public class Photocopier {
// 
//     public static File sampleDir() {
//         return new File(separatorsToSystem("sample-apps"));
//     }
// 
//     public static File copySampleAppToTempDir(String sampleAppName) throws IOException {
//         File target = folderForSampleProject(sampleAppName);
//         copySampleAppToDir(sampleAppName, target);
//         return target;
//     }
// 
//     public static void copySampleAppToDir(String sampleAppName, File target) throws IOException {
//         String pathname = FilenameUtils.concat(fullPath(sampleDir()), sampleAppName);
//         File source = new File(pathname);
//         if (!source.isDirectory()) {
//             source = new File(separatorsToSystem("../") + pathname);
//         }
//         if (!source.isDirectory()) {
//             throw new RuntimeException("Could not find module " + sampleAppName + " at " + new File(pathname) + " nor " + fullPath(source));
//         }
//         FileUtils.copyDirectory(source, target);
//     }
// 
//     public static File folderForSampleProject(String moduleName) {
//         return new File(separatorsToSystem("target/samples/" + UUID.randomUUID() + "/" + moduleName));
//     }
// 
//     public static File tempFile(String name) throws IOException {
//         File file = new File(separatorsToSystem("target/temp/" + UUID.randomUUID() + "/" + name));
//         FileUtils.touch(file);
//         return file;
//     }
// }
// 
// Path: src/main/java/com/danielflower/apprunner/mgmt/AppManager.java
// public static int getAFreePort() {
//     try {
//         try (ServerSocket serverSocket = new ServerSocket(0)) {
//             return serverSocket.getLocalPort();
//         }
//     } catch (IOException e) {
//         throw new AppRunnerException("Unable to get a port", e);
//     }
// }
// 
// Path: src/test/java/scaffolding/TestConfig.java
// public static final Config config;


// Path: src/test/java/com/danielflower/apprunner/runners/GradleRunnerTest.java
import org.apache.commons.io.output.StringBuilderWriter;
import org.junit.BeforeClass;
import org.junit.Test;
import scaffolding.Photocopier;
import java.util.Optional;
import static com.danielflower.apprunner.mgmt.AppManager.getAFreePort;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeTrue;
import static scaffolding.TestConfig.config;

package com.danielflower.apprunner.runners;





public class GradleRunnerTest {
    private static GradleRunnerFactory runnerFactory;
    private final StringBuilderWriter buildLog = new StringBuilderWriter();
    private final StringBuilderWriter consoleLog = new StringBuilderWriter();

    @BeforeClass
    public static void ignoreTestIfNotSupported() throws Exception {
        Optional<GradleRunnerFactory> runnerFactoryMaybe = GradleRunnerFactory.createIfAvailable(config);
        assumeTrue("Skipping test because Gradle and/or Java not detected", runnerFactoryMaybe.isPresent());
        runnerFactory = runnerFactoryMaybe.get();
    }

    @Test
    public void canStartAGradleProcessByPackagingAndRunning() throws Exception {
        String appName = "gradle";
        AppRunner runner = runnerFactory.appRunner(Photocopier.copySampleAppToTempDir(appName));
        int port = getAFreePort();
        startAndStop(1, appName, runner, port);
        startAndStop(2, appName, runner, port);
    }

    @Test
    public void theVersionIsReported() {
        assertThat(runnerFactory.versionInfo(), containsString("Gradle"));