package nl.hsac.fitnesse.junit;

import fitnesse.ContextConfigurator;
import fitnesse.FitNesseContext;
import fitnesse.components.PluginsClassLoader;
import nl.hsac.fitnesse.fixture.Environment;
import nl.hsac.fitnesse.junit.patchFor486.FitNesseRunner;
import org.apache.commons.lang3.StringUtils;
import org.junit.runners.model.InitializationError;

import java.io.File;

/**
 * JUnit Runner to run a FitNesse suite or page as JUnit test.
 *
 * The suite/page to run must be specified either via the Java property
 * 'fitnesseSuiteToRun', or by adding a FitNesseSuite.Name annotation to the test class.
 * If both are present the environment variable is used.
 *
 * The HTML generated for each page is saved in target/fitnesse-results
 */
public class HsacFitNesseRunner extends FitNesseRunner {
    private final static String suiteOverrideVariableName = "fitnesseSuiteToRun";

    public HsacFitNesseRunner(Class<?> suiteClass) throws InitializationError {
        super(suiteClass);
        try {
            // we include images in output so build server will have single
            // directory containing both HTML results and the images created by the tests
            String outputDir = getOutputDir(suiteClass);
            new File(outputDir).mkdirs();
            Environment.getInstance().setFitNesseRoot(outputDir);
        } catch (Exception e) {
            throw new InitializationError(e);
        }
    }

    @Override
    protected String getSuiteName(Class<?> klass) throws InitializationError {
        String name = System.getProperty(suiteOverrideVariableName);
        if (StringUtils.isEmpty(name)) {
            Suite nameAnnotation = klass.getAnnotation(Suite.class);
            if (nameAnnotation == null) {
                throw new InitializationError("There must be a @Suite annotation");
            }
            name = nameAnnotation.value();
        }
        return name;
    }

    @Override
    protected String getFitNesseDir(Class<?> suiteClass) {
        return "wiki";
    }

    @Override
    protected String getOutputDir(Class<?> klass) throws InitializationError {
        return "target/fitnesse-results";
    }

    @Override
    protected String getFitNesseRoot(Class<?> suiteClass) {
        return ContextConfigurator.DEFAULT_ROOT;
    }

    @Override
    protected FitNesseContext createContext(Class<?> suiteClass) throws Exception {
        new PluginsClassLoader(getFitNesseDir(suiteClass)).addPluginsToClassLoader();

        return super.createContext(suiteClass);
    }
}

@@ -139,24 +139,55 @@
 lassLoader;%0A
+import fitnesse.wiki.WikiPage;%0A
 import nl.hs
@@ -219,16 +219,128 @@
 onment;%0A
+import nl.hsac.fitnesse.fixture.slim.web.SauceLabsSeleniumSetup;%0Aimport nl.hsac.fitnesse.fixture.util.FileUtil;%0A
 import n
@@ -434,16 +434,66 @@
 gUtils;%0A
+import org.junit.runner.notification.RunNotifier;%0A
 import o
@@ -557,16 +557,71 @@
 io.File;
+%0Aimport java.io.FileInputStream;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1119,16 +1119,49 @@
 eToRun%22;
+%0A    private Class%3C?%3E suiteClass;
 %0A%0A    pu
@@ -1253,32 +1253,70 @@
 er(suiteClass);%0A
+        this.suiteClass = suiteClass;%0A
         try %7B%0A  
@@ -2792,16 +2792,16 @@
 der();%0A%0A
-
         
@@ -2845,11 +2845,867 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void runPages(List%3CWikiPage%3E pages, RunNotifier notifier) %7B%0A            super.runPages(pages, notifier);%0A        try %7B%0A            String outputDir = getOutputDir(suiteClass);%0A            String suiteName = getSuiteName(suiteClass);%0A            String filename = suiteName + %22.html%22;%0A            String path = new File(outputDir, filename).getAbsolutePath();%0A            String overviewHtml = FileUtil.streamToString(new FileInputStream(path), path);%0A            if (overviewHtml != null) %7B%0A                String videoEmbed = new SauceLabsSeleniumSetup().videoOfRun();%0A                String newOverview = overviewHtml.replace(%22%3Ctable%22, videoEmbed + %22%3Ctable%22);%0A                FileUtil.writeFile(new File(outputDir, %22index.html%22).getAbsolutePath(), newOverview);%0A            %7D%0A        %7D catch (Exception e) %7B%0A        %7D%0A%0A    %7D
 %0A%7D%0A

Create an overview page containing the video of the last run