package org.codehaus.groovy.grails.test.report.junit;

import java.io.*;
import junit.framework.Test;
import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
import org.codehaus.groovy.grails.test.support.TestStacktraceSanitizer;

/**
 * JUnit XML formatter that sanitises the stack traces generated by
 * tests.
 */
public class XMLFormatter extends XMLJUnitResultFormatter {

    public XMLFormatter(File file) {
        try {
            super.setOutput(new BufferedOutputStream(new FileOutputStream(file)));
        }
        catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void setOutput(OutputStream out) {
        throw new IllegalStateException("This should not be called");
    }

    public void addFailure(Test test, Throwable throwable) {
        TestStacktraceSanitizer.sanitize(throwable);
        super.addFailure(test, (Throwable)throwable);
    }

    public void addError(Test test, Throwable throwable) {
        TestStacktraceSanitizer.sanitize(throwable);
        super.addError(test, throwable);
    }
}
