From 97e028f605d1aa060e4a4640a3dd683e51eaef3a Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 13 Jan 2023 03:10:48 +0800 Subject: Move temporary files to standard path in IncBackupBackwardCompatibilityTest. --- .../java/com/keuin/kbackupfabric/TestUtils.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/test/java/com/keuin/kbackupfabric/TestUtils.java (limited to 'src/test/java/com/keuin/kbackupfabric/TestUtils.java') diff --git a/src/test/java/com/keuin/kbackupfabric/TestUtils.java b/src/test/java/com/keuin/kbackupfabric/TestUtils.java new file mode 100644 index 0000000..ea98ca1 --- /dev/null +++ b/src/test/java/com/keuin/kbackupfabric/TestUtils.java @@ -0,0 +1,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; + } +} -- cgit v1.2.3