package nl.hsac.fitnesse.junit;

import fitnesse.ContextConfigurator;
import fitnesse.FitNesseContext;
import fitnesse.components.PluginsClassLoader;
import fitnesse.wiki.WikiPage;
import nl.hsac.fitnesse.fixture.Environment;
import nl.hsac.fitnesse.fixture.util.FileUtil;
import nl.hsac.fitnesse.junit.patchFor486.FitNesseRunner;
import org.apache.commons.lang3.StringUtils;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * 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);
    }

    @Override
    protected void runPages(List<WikiPage> pages, RunNotifier notifier) {
        super.runPages(pages, notifier);
        try {
            Class<?> suiteClass = getTestClass().getJavaClass();
            String outputDir = getOutputDir(suiteClass);
            String suiteName = getSuiteName(suiteClass);
            String filename = suiteName + ".html";
            String path = new File(outputDir, filename).getAbsolutePath();
            String overviewHtml = FileUtil.streamToString(new FileInputStream(path), path);
            if (overviewHtml != null) {
                FileUtil.writeFile(new File(outputDir, "index.html").getAbsolutePath(), overviewHtml);
            }
        } catch (Exception e) {
        }

    }
}

@@ -3283,32 +3283,102 @@
 Html != null) %7B%0A
+                String indexHtml = getIndexHtmlContent(overviewHtml);%0A
                 
@@ -3449,24 +3449,21 @@
 Path(), 
-overview
+index
 Html);%0A 
@@ -3513,21 +3513,120 @@
 %7B%0A        %7D%0A
+    %7D%0A%0A    protected String getIndexHtmlContent(String overviewHtml) %7B%0A        return overviewHtml;
 %0A    %7D%0A%7D%0A

Create method to allow manipulation of body content