package com.googlecode.jslint4java.maven;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.io.File;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.googlecode.jslint4java.formatter.JSLintResultFormatter;
import com.googlecode.jslint4java.formatter.JSLintXmlFormatter;

public class ReportWriterTest {

    @Rule
    public TemporaryFolder tmpf = new TemporaryFolder();

    private final JSLintResultFormatter formatter = new JSLintXmlFormatter();

    private ReportWriter createReportWriter(String filename) {
        return new ReportWriter(new File(tmpf.getRoot(), filename), formatter);
    }

    @Test
    public void createsNonEmptyFile() {
        ReportWriter rw = createReportWriter("report.xml");
        rw.open();
        rw.close();
        assertThat(rw.getReportFile().exists(), is(true));
        assertThat(rw.getReportFile().length(), is(not(0L)));
    }

    @Test
    public void makesParentDirectory() {
        File parent = new File(tmpf.getRoot(), "somedir");
        ReportWriter rw = new ReportWriter(new File(parent, "report.xml"), formatter);
        rw.open();
        rw.close();
        assertThat(parent.exists(), is(true));
    }
}

@@ -117,30 +117,285 @@
 .*;%0A
-%0Aimport java.io.File;%0A
+import static org.junit.matchers.JUnitMatchers.*;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.nio.charset.Charset;%0A%0Aimport org.codehaus.plexus.util.IOUtil;
 %0Aimp
@@ -478,16 +478,78 @@
 older;%0A%0A
+import com.googlecode.jslint4java.JSLintResult.ResultBuilder;%0A
 import c
@@ -705,16 +705,76 @@
 Test %7B%0A%0A
+    private static final String REPORT_XML = %22report.xml%22;%0A%0A
     @Rul
@@ -1157,28 +1157,26 @@
 tWriter(
-%22report.xml%22
+REPORT_XML
 );%0A     
@@ -1511,117 +1511,1001 @@
 nt, 
-%22report.xml%22), formatter);%0A        rw.open();%0A        rw.close();%0A        assertThat(parent.exists(), is(true
+REPORT_XML), formatter);%0A        rw.open();%0A        rw.close();%0A        assertThat(parent.exists(), is(true));%0A    %7D%0A%0A    private String readReport(ReportWriter rw) throws FileNotFoundException, IOException %7B%0A        InputStreamReader reader = null;%0A        try %7B%0A            reader = new InputStreamReader(new FileInputStream(rw.getReportFile()),%0A                    Charset.forName(%22UTF-8%22));%0A            return IOUtil.toString(reader);%0A        %7D finally %7B%0A            IOUtil.close(reader);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void reportContentsSanity() throws Exception %7B%0A        ReportWriter rw = createReportWriter(REPORT_XML);%0A        rw.open();%0A        // Create a result with no problems.%0A        rw.report(new ResultBuilder(%22foo.js%22).build());%0A        rw.close();%0A        String report = readReport(rw);%0A        assertThat(report, containsString(%22%3Cjslint%3E%22));%0A        assertThat(report, containsString(%22%3Cfile name='foo.js'%3E%22));%0A        assertThat(report, containsString(%22%3C/jslint%3E%22
 ));%0A

Test report contents.