package de.codescape.bitvunit.report;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import de.codescape.bitvunit.rule.Rule;
import de.codescape.bitvunit.rule.Violation;
import de.codescape.bitvunit.rule.Violations;
import de.codescape.bitvunit.ruleset.RuleSet;
import de.codescape.bitvunit.ruleset.XmlRuleSet;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;

import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.PrintStream;

import static de.codescape.bitvunit.util.html.HtmlPageUtil.toHtmlPage;

/**
 * Abstract test class for testing {@link ReportWriter} implementations capturing the console and file output.
 *
 * @author Stefan Glase
 * @since 0.5
 */
public abstract class AbstractReportWriterTest {

    private OutputStream consoleOutput;
    private PrintStream originalConsole;

    @Before
    public void beforeTestsRedirectConsole() throws Exception {
        originalConsole = System.out;
        consoleOutput = new ByteArrayOutputStream();
        System.setOut(new PrintStream(consoleOutput));
    }

    @After
    public void afterTestsUndoConsoleRedirect() throws Exception {
        System.setOut(originalConsole);
    }

    protected String getConsoleOutput() {
        return consoleOutput.toString();
    }

    protected String getFileOutput(String filename) throws Exception {
        return IOUtils.toString(new FileReader(filename));
    }

    protected Violations someViolations() {
        Violations violations = new Violations();
        for (Rule rule : someRuleSet().getRules()) {
            violations.add(new Violation(rule, someHtmlPage().getBody(), "No empty body allowed!"));
        }
        return violations;
    }

    protected RuleSet someRuleSet() {
        return new XmlRuleSet("/rulesets/all-rules.xml");
    }

    protected HtmlPage someHtmlPage() {
        return toHtmlPage("<html><head><title>Hello World</title></head><body></body></html>");
    }

}

@@ -306,24 +306,68 @@
 XmlRuleSet;%0A
+import de.codescape.bitvunit.test.TestPage;%0A
 import org.a
@@ -569,80 +569,8 @@
 m;%0A%0A
-import static de.codescape.bitvunit.util.html.HtmlPageUtil.toHtmlPage;%0A%0A
 /**%0A
@@ -1892,86 +1892,28 @@
 urn 
-toHtmlPage(%22%3Chtml%3E%3Chead%3E%3Ctitle%3EHello World%3C/title%3E%3C/head%3E%3Cbody%3E%3C/body%3E%3C/html%3E%22
+TestPage.asHtmlPage(
 );%0A 

use new TestPage to construct test page