package nl.hsac.fitnesse.junit;

import fitnesse.components.PluginsClassLoader;
import fitnesse.junit.JUnitHelper;
import fitnesse.junit.JUnitXMLTestListener;

/**
 * Helper to run Fitnesse tests from JUnit tests.
 */
public class FitnesseFromJUnitRunner {
    private boolean loadPlugins = true;
    private String fitnesseRoot = ".";
    private String xmlOutputPath = "../target/failsafe-reports";
    private String htmlOutputPath = "../target/fitnesse-results";

    /**
     * Runs a suite of Fitnesse tests. One JUnit test will be reported for each page executed.
     * @param suiteName name of suite to run (maybe overridden by a system property
     * @throws Exception
     */
    public void assertSuitePasses(String suiteName) throws Exception {
        if (loadPlugins) {
            loadPlugins();
        }
        JUnitXMLTestListener resultsListener = new JUnitXMLTestListener(xmlOutputPath);
        JUnitHelper jUnitHelper = new JUnitHelper(fitnesseRoot, htmlOutputPath, resultsListener);

        jUnitHelper.assertSuitePasses(suiteName);
    }

    /**
     * Adds Fitnesse plugins to classpath.
     */
    protected void loadPlugins() {
        try {
            new PluginsClassLoader().addPluginsToClassLoader();
        } catch (Exception e) {
            throw new RuntimeException("Unable to adds plugins to classpath", e);
        }
    }

    /**
     * @return whether Fitnesse's plugins will be added to classpath of test run.
     */
    public boolean isLoadPlugins() {
        return loadPlugins;
    }

    /**
     * @param loadPlugins whether Fitnesse's plugins will be added to classpath of test run.
     */
    public void setLoadPlugins(boolean loadPlugins) {
        this.loadPlugins = loadPlugins;
    }

    /**
     * @return (relative) location of Fitnesse's wiki pages to current directory of JVM.
     */
    public String getFitnesseRoot() {
        return fitnesseRoot;
    }

    /**
     * @param fitnesseRoot (relative) location of Fitnesse's wiki pages to current directory of JVM.
     */
    public void setFitnesseRoot(String fitnesseRoot) {
        this.fitnesseRoot = fitnesseRoot;
    }

    /**
     * @return (relative) location where XML files will be written describing results of Fitnesse tests.
     */
    public String getXmlOutputPath() {
        return xmlOutputPath;
    }

    /**
     * @param xmlOutputPath (relative) location where XML files will be written describing results of Fitnesse tests.
     */
    public void setXmlOutputPath(String xmlOutputPath) {
        this.xmlOutputPath = xmlOutputPath;
    }

    /**
     * @return (relative) location where HTML files will be written describing results of Fitnesse tests (similar to
     *         output of test execution from Wiki).
     */
    public String getHtmlOutputPath() {
        return htmlOutputPath;
    }

    /**
     * @param htmlOutputPath (relative) location where HTML files will be written describing results of Fitnesse tests
     *                       (similar to output of test execution from Wiki).
     */
    public void setHtmlOutputPath(String htmlOutputPath) {
        this.htmlOutputPath = htmlOutputPath;
    }
}

@@ -151,16 +151,103 @@
 istener;
+%0Aimport fitnesse.junit.JavaFormatter;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;
 %0A%0A/**%0A *
@@ -1090,16 +1090,34 @@
 ener);%0A%0A
+        try %7B%0A    
         
@@ -1154,24 +1154,688 @@
 suiteName);%0A
+        %7D finally %7B%0A            addFilesToHtmlOutput();%0A        %7D%0A    %7D%0A%0A    protected void addFilesToHtmlOutput() throws IOException %7B%0A        copyResourceToHtmlOutput(%22css/fitnesse.css%22);%0A        copyResourceToHtmlOutput(%22css/fitnesse_pages.css%22);%0A        copyResourceToHtmlOutput(%22css/fitnesse_wiki.css%22);%0A        copyResourceToHtmlOutput(%22css/fitnesse_straight.css%22);%0A%0A    %7D%0A%0A    protected void copyResourceToHtmlOutput(String resource) throws IOException %7B%0A        String src = %22/fitnesse/resources/%22 + resource;%0A        String dest = htmlOutputPath + %22/%22 + resource;%0A%0A        File target = new File(dest);%0A        JavaFormatter.FileCopier.copy(src, target);%0A
     %7D%0A%0A    /

Include more css in output of JUnit run