aboutsummaryrefslogtreecommitdiff
path: root/app/src/test/java/invalid/lena/scrcpy/AtomicFilesTest.java
blob: 1cafb0c426cd7c67ddef7a7652038a2de4a0294b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package invalid.lena.scrcpy;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;

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

import java.io.File;
import java.nio.file.Files;

public class AtomicFilesTest {

    @Rule
    public TemporaryFolder dir = new TemporaryFolder();

    @Test
    public void writeCreatesFile() throws Exception {
        File f = new File(dir.getRoot(), "x");
        AtomicFiles.write(f, "hello".getBytes());
        assertArrayEquals("hello".getBytes(), Files.readAllBytes(f.toPath()));
    }

    @Test
    public void writeReplacesExistingWhole() throws Exception {
        File f = new File(dir.getRoot(), "x");
        AtomicFiles.write(f, "old".getBytes());
        AtomicFiles.write(f, "new-and-longer".getBytes());
        assertArrayEquals("new-and-longer".getBytes(), Files.readAllBytes(f.toPath()));
    }

    @Test
    public void writeLeavesNoTempBehind() throws Exception {
        File f = new File(dir.getRoot(), "x");
        AtomicFiles.write(f, "data".getBytes());
        assertFalse(new File(dir.getRoot(), "x.tmp").exists());
    }

    @Test
    public void staleTempDoesNotCorruptDestination() throws Exception {
        // A crash after staging but before the rename leaves a partial ".tmp".
        // The destination must still hold the previous good content, and the
        // next successful write must overwrite the stale temp cleanly.
        File f = new File(dir.getRoot(), "x");
        AtomicFiles.write(f, "good".getBytes());
        Files.write(new File(dir.getRoot(), "x.tmp").toPath(), "partial".getBytes());
        assertArrayEquals("good".getBytes(), Files.readAllBytes(f.toPath()));

        AtomicFiles.write(f, "good2".getBytes());
        assertArrayEquals("good2".getBytes(), Files.readAllBytes(f.toPath()));
        assertFalse(new File(dir.getRoot(), "x.tmp").exists());
    }

    @Test
    public void writeRemovesEveryStaleTemp() throws Exception {
        File f = new File(dir.getRoot(), "x");
        Files.write(new File(dir.getRoot(), "x.tmp-old").toPath(), new byte[]{1});
        Files.write(new File(dir.getRoot(), "x.tmp-new").toPath(), new byte[]{2});

        AtomicFiles.write(f, "good".getBytes());

        assertFalse(new File(dir.getRoot(), "x.tmp-old").exists());
        assertFalse(new File(dir.getRoot(), "x.tmp-new").exists());
    }
}