summaryrefslogtreecommitdiff
path: root/src/test/java/com/keuin/kbackupfabric/TestUtils.java
blob: ea98ca1df33f08b9ecad076927864ea7d7e0f3b0 (plain)
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
package com.keuin.kbackupfabric;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class TestUtils {

    public static String getTempDirectory(String subDirectory) throws IOException {
        String testTempPath;
        String path = System.getenv("KB_TEMP_DIR");
        if (path == null || path.isEmpty() || !new File(path).isDirectory()) {
            path = findTempPath();
        }
        return Paths.get(path, subDirectory).toString();
    }

    private static String findTempPath() throws IOException {
        String path;
        if (System.getProperty("os.name").startsWith("Windows")) {
            // Windows
            path = System.getProperty("java.io.tmpdir");
        } else {
            // Unix
            path = System.getenv("XDG_RUNTIME_DIR");
            if (!new File(path).isDirectory()) {
                path = "/tmp";
            }
        }
        if (!new File(path).isDirectory()) {
            throw new IOException("Cannot find suitable temporary path");
        }
        return path;
    }
}