package hudson.util;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;

import static org.junit.Assert.*;

public class AtomicFileWriterTest {
    File af;
    AtomicFileWriter afw;
    String expectedContent = "hello world";


    @Before
    public void setUp() throws IOException {
        af = File.createTempFile("AtomicFileWriter", ".tmp");
        afw = new AtomicFileWriter(af, Charset.defaultCharset());
    }

    @After
    public void tearDown() throws IOException {
        Files.deleteIfExists(af.toPath());
        Files.deleteIfExists(afw.getTemporaryPath());
    }

    @Test
    public void createFile() throws Exception {
        // Verify the file we created exists
        assertTrue(Files.exists(afw.getTemporaryPath()));
    }

    @Test
    public void writeToAtomicFile() throws Exception {
        // Given
        afw.write(expectedContent, 0, expectedContent.length());

        // When
        afw.flush();

        // Then
        assertTrue("File writer did not properly flush to temporary file",
                Files.size(afw.getTemporaryPath()) == expectedContent.length());
    }

    @Test
    public void commitToFile() throws Exception {
        // Given
        afw.write(expectedContent, 0, expectedContent.length());

        // When
        afw.commit();

        // Then
        assertTrue(Files.size(af.toPath()) == expectedContent.length());
    }

    @Test
    public void abortDeletesTmpFile() throws Exception {
        // Given
        afw.write(expectedContent, 0, expectedContent.length());

        // When
        afw.abort();

        // Then
        assertTrue(Files.notExists(afw.getTemporaryPath()));
    }
}
@@ -81,20 +81,83 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder
 ;%0A%0Aimpor
@@ -416,16 +416,78 @@
 world%22;%0A
+    @Rule public TemporaryFolder tmp = new TemporaryFolder();%0A
 %0A%0A    @B
@@ -554,54 +554,20 @@
 f = 
-File.createTempFile(%22AtomicFileWriter%22, %22.tmp%22
+tmp.newFile(
 );%0A 
@@ -642,171 +642,8 @@
  %7D%0A%0A
-    @After%0A    public void tearDown() throws IOException %7B%0A        Files.deleteIfExists(af.toPath());%0A        Files.deleteIfExists(afw.getTemporaryPath());%0A    %7D%0A%0A
     
@@ -1022,20 +1022,22 @@
   assert
-True
+Equals
 (%22File w
@@ -1100,16 +1100,42 @@
         
+ expectedContent.length(),
  Files.s
@@ -1165,36 +1165,8 @@
 h())
- == expectedContent.length()
 );%0A 
@@ -1387,63 +1387,63 @@
 sert
-True(Files.size(af.toPath()) == expectedContent.length(
+Equals(expectedContent.length(), Files.size(af.toPath()
 ));%0A

Use temporary file, and assertEquals in tests.