package org.openengsb.labs.endtoend;

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

import org.openengsb.labs.endtoend.distribution.extractor.DistributionExtractor;
import org.openengsb.labs.endtoend.distribution.resolver.DistributionResolver;
import org.openengsb.labs.endtoend.karaf.CommandTimeoutException;
import org.openengsb.labs.endtoend.karaf.Karaf;
import org.openengsb.labs.endtoend.karaf.shell.RemoteShell;
import org.openengsb.labs.endtoend.karaf.shell.Shell;
import org.openengsb.labs.endtoend.testcontext.TestContext;
import org.openengsb.labs.endtoend.testcontext.loader.TestContextLoader;

public class App {
    private static final String EXTRACTION_DIR = getCurrentDir() + "/tmp";

    public static void main(String[] args) throws Exception {
        new App().exampleTest();
    }

    private static File getCurrentDir() {
        String dir = System.getProperty("user.dir");
        return new File(dir);
    }

    private void exampleTest() {
        DistributionResolver dr = new DistributionResolver();
        DistributionExtractor ds = new DistributionExtractor(new File(EXTRACTION_DIR));

        TestContextLoader testContextLoader = new TestContextLoader(dr, ds);
        testContextLoader.loadContexts();
        TestContext context;

        System.out.println("Run some tests on default context:");
        context = testContextLoader.getDefaultTestContext();
        runTestsWithContext(context);

        System.out.println("Run the same tests on a different context:");
        context = testContextLoader.getTestContext("somethingspecial");
        runTestsWithContext(context);
    }

    private void runTestsWithContext(TestContext context) {
        context.setup();

        Karaf k = context.getDistribution().getKaraf();
        try {
            System.out.println("Starting Karaf...");
            k.start(2L, TimeUnit.MINUTES);
        } catch (CommandTimeoutException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("Executing list command (local shell)...");
        Shell shell = k.getShell();
        try {
            String response = null;
            response = shell.execute("list", 120L, TimeUnit.SECONDS);
            System.out.println(response);
        } catch (CommandTimeoutException e) {
            System.out.println(e.getMessage());
        }

        RemoteShell remoteShell = null;
        try {
            System.out.println("Remote login...");
            remoteShell = k.login("karaf", "", 120L, TimeUnit.SECONDS);
            System.out.println("Executing list command (remote shell)...");
            String response = remoteShell.execute("list", 30L, TimeUnit.SECONDS);
            System.out.println(response);
            System.out.println("Logout...");
            remoteShell.logout(10L, TimeUnit.SECONDS);
        } catch (CommandTimeoutException e) {
            System.out.println(e.getMessage());
        }

        try {
            System.out.println("Stopping Karaf...");
            k.shutdown(10L, TimeUnit.SECONDS);
        } catch (CommandTimeoutException e) {
            System.out.println(e.getMessage());
        }

        context.teardown();

        System.out.println("Finished.");
    }
}

@@ -706,16 +706,130 @@
  %22/tmp%22;
+%0A    private static final Long DEFAULT_TIMEOUT = 2L;%0A    private static final TimeUnit MINUTES = TimeUnit.MINUTES;
 %0A%0A    pu
@@ -1979,21 +1979,25 @@
 art(
-2L, TimeUnit.
+DEFAULT_TIMEOUT, 
 MINU
@@ -2309,37 +2309,39 @@
 %22list%22, 
-120L, TimeUnit.SECOND
+DEFAULT_TIMEOUT, MINUTE
 S);%0A    
@@ -2639,29 +2639,31 @@
 %22%22, 
-120L, TimeUnit.SECOND
+DEFAULT_TIMEOUT, MINUTE
 S);%0A
@@ -2800,28 +2800,31 @@
 t%22, 
-30L, TimeUnit.SECOND
+DEFAULT_TIMEOUT, MINUTE
 S);%0A
@@ -2941,36 +2941,39 @@
 .logout(
-10L, TimeUnit.SECOND
+DEFAULT_TIMEOUT, MINUTE
 S);%0A    
@@ -3167,28 +3167,31 @@
 own(
-10L, TimeUnit.SECOND
+DEFAULT_TIMEOUT, MINUTE
 S);%0A

Use a common timeout for all operations for now