package com.googlecode.jslint4java.ant;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Delete;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.googlecode.jslint4java.JSLintResult;

public class JUnitXmlResultFormatterTest {

    private final JUnitXmlResultFormatter formatter = new JUnitXmlResultFormatter();
    private File tmpd;

    private JSLintResult aResult(String name) {
        return new JSLintResult.ResultBuilder(name).build();
    }

    private void formatResult(JSLintResult result) {
        formatter.begin();
        formatter.output(result);
        formatter.end();
    }

    @Before
    public void setUp() throws Exception {
        tmpd = File.createTempFile(JUnitXmlResultFormatter.class.getName() + ".", ".d");
        // I actually wanted a directory, not a file…
        if (!tmpd.delete()) {
            throw new IOException("Can't delete " + tmpd);
        }
        if (!tmpd.mkdir()) {
            throw new IOException("Can't mkdir " + tmpd);
        }
    }

    @After
    public void tearDown() throws Exception {
        // Use the ant task in order to delete a tree of files.
        Delete delete = new Delete();
        delete.setDir(tmpd);
        // I know that this produces messages on stderr, but I have no idea how
        // to tell Delete to just “STFU.” Various combinations of setVerbose()
        // and setQuiet() all fail. I guess it's just noisy. :-(
        delete.execute();
    }

    @Test(expected = BuildException.class)
    public void testFileSetReallyIsFile() throws Exception {
        File foo = new File(tmpd, "foo");
        if (!foo.createNewFile()) {
            throw new IOException("Can't create " + foo);
        }
        JSLintResult result = aResult("foo.js");
        formatter.setFile(foo);
        formatResult(result);
    }

    @Test(expected = BuildException.class)
    public void testNoFileSet() {
        formatResult(aResult("foo.js"));
    }

    @Test
    public void testNormality() {
        // File is set to a pre-existing directory.
        formatter.setFile(tmpd);
        formatResult(aResult("foo.js"));
        File expected = new File(tmpd, "TEST-foo.js.xml");
        assertTrue(expected.exists());
    }

    @Test(expected = BuildException.class)
    public void testReadOnlyFileBlowsUp() throws IOException {
        File output = new File(tmpd, "TEST-foo.js.xml");
        if (!output.createNewFile()) {
            throw new IOException("Can't create " + output);
        }
        assertTrue(output.setReadOnly());
        formatter.setFile(tmpd);
        // Should blow up when write occurs.
        formatResult(aResult("foo.js"));
    }

    @Test
    public void testNonAlphaNumericFilename() {
        formatter.setFile(tmpd);
        formatResult(aResult("a&b.js"));
        File expected = new File(tmpd, "TEST-a_b.js.xml");
        assertTrue(expected.exists());
    }

}

@@ -178,39 +178,17 @@
 org.
-apache.tools.ant.taskdefs.Delet
+junit.Rul
 e;%0Ai
@@ -203,21 +203,20 @@
 g.junit.
-After
+Test
 ;%0Aimport
@@ -230,37 +230,29 @@
 nit.
-Before;%0Aimport org.junit.Test
+rules.TemporaryFolder
 ;%0A%0Ai
@@ -436,25 +436,71 @@
     
-private File tmpd
+@Rule%0A    public TemporaryFolder folder = new TemporaryFolder()
 ;%0A%0A 
@@ -769,1098 +769,106 @@
    @
-Before%0A    public void setUp() throws Exception %7B%0A        tmpd = File.createTempFile(JUnitXmlResultFormatter.class.getName() + %22.%22, %22.d%22);%0A        // I actually wanted a directory, not a file%E2%80%A6%0A        if (!tmpd.delete()) %7B%0A            throw new IOException(%22Can't delete %22 + tmpd);%0A        %7D%0A        if (!tmpd.mkdir()) %7B%0A            throw new IOException(%22Can't mkdir %22 + tmpd);%0A        %7D%0A    %7D%0A%0A    @After%0A    public void tearDown() throws Exception %7B%0A        // Use the ant task in order to delete a tree of files.%0A        Delete delete = new Delete();%0A        delete.setDir(tmpd);%0A        // I know that this produces messages on stderr, but I have no idea how%0A        // to tell Delete to just %E2%80%9CSTFU.%E2%80%9D Various combinations of setVerbose()%0A        // and setQuiet() all fail. I guess it's just noisy. :-(%0A        delete.execute();%0A    %7D%0A%0A    @Test(expected = BuildException.class)%0A    public void testFileSetReallyIsFile() throws Exception %7B%0A        File foo = new File(tmpd, %22foo%22);%0A        if (!foo.createNewFile()) %7B%0A            throw new IOException(%22Can't create %22 + foo);%0A        %7D
+Test(expected = BuildException.class)%0A    public void testFileSetReallyIsFile() throws Exception %7B
 %0A   
@@ -941,17 +941,35 @@
 tFile(fo
-o
+lder.newFile(%22foo%22)
 );%0A     
@@ -1239,36 +1239,48 @@
 rmatter.setFile(
-tmpd
+folder.getRoot()
 );%0A        forma
@@ -1332,36 +1332,48 @@
 cted = new File(
-tmpd
+folder.getRoot()
 , %22TEST-foo.js.x
@@ -1552,31 +1552,31 @@
 utput = 
+folder.
 new
- 
 File(
-tmpd, 
 %22TEST-fo
@@ -1591,118 +1591,8 @@
 %22);%0A
-        if (!output.createNewFile()) %7B%0A            throw new IOException(%22Can't create %22 + output);%0A        %7D%0A
     
@@ -1647,36 +1647,48 @@
 rmatter.setFile(
-tmpd
+folder.getRoot()
 );%0A        // Sh
@@ -1851,20 +1851,32 @@
 setFile(
-tmpd
+folder.getRoot()
 );%0A     
@@ -1948,12 +1948,24 @@
 ile(
-tmpd
+folder.getRoot()
 , %22T

Use JUnit's TemporaryFolder.