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 ++++++++++++++++++++++ .../IncBakupBackwardCompatibilityTest.java | 19 ++++++++++-- .../ConfiguredIncrementalBackupMethodTest.java | 34 ++------------------- 3 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 src/test/java/com/keuin/kbackupfabric/TestUtils.java (limited to 'src/test/java/com') 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; + } +} diff --git a/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java b/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java index 3de75b9..fc72e9a 100644 --- a/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java +++ b/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java @@ -1,10 +1,14 @@ package com.keuin.kbackupfabric.backup.incremental.serializer; +import com.keuin.kbackupfabric.TestUtils; import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionFactory; import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionSerializer; import com.keuin.kbackupfabric.backup.incremental.identifier.Sha256Identifier; import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import java.io.File; @@ -21,8 +25,19 @@ public class IncBakupBackwardCompatibilityTest { private final String customName = "test_backup"; private final LocalDateTime backupTime = LocalDateTime.of(2000, 1, 1, 1, 1, 1, 1); - private final Path testRoot = Paths.get(".\\testfile\\IncBackupBackwardCompatibilityTest"); - private final File indexFile = new File(testRoot.toString(), IncrementalBackupFileNameEncoder.INSTANCE.encode(customName, backupTime)); + private Path testRoot; + private File indexFile; + + @Before + public void setUp() throws IOException { + testRoot = Paths.get(TestUtils.getTempDirectory("IncBackupBackwardCompatibilityTest")); + indexFile = new File(testRoot.toString(), IncrementalBackupFileNameEncoder.INSTANCE.encode(customName, backupTime)); + } + + @After + public void tearDown() throws IOException { + FileUtils.deleteDirectory(new File(testRoot.toString())); + } @Test public void testBackwardCompatibility() throws IOException { diff --git a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java index 0741004..1fb3414 100644 --- a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java +++ b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java @@ -1,5 +1,6 @@ package com.keuin.kbackupfabric.operation.backup.method; +import com.keuin.kbackupfabric.TestUtils; import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; import com.keuin.kbackupfabric.metadata.BackupMetadata; import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback; @@ -23,8 +24,6 @@ import static org.apache.commons.io.FileUtils.forceDelete; import static org.junit.Assert.*; public class ConfiguredIncrementalBackupMethodTest { - - public static final String SUBDIRECTORY = "kb_temp"; private String testTempPath; private final String sourceDirectoryName = "source"; private final String destDirectoryName = "destination"; @@ -41,40 +40,13 @@ public class ConfiguredIncrementalBackupMethodTest { @Before public void setUp() throws IOException { // select temporary directory - String path = System.getenv("KB_TEMP_DIR"); - if (path != null && !path.isEmpty() && new File(path).isDirectory()) { - testTempPath = Paths.get(path, SUBDIRECTORY).toString(); - } else { - testTempPath = findTempPath(); - } + testTempPath = TestUtils.getTempDirectory("kb_temp"); logger.info(String.format("Using temp path: %s", testTempPath)); } - 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"); - } - path = Paths.get(path, SUBDIRECTORY).toString(); - return path; - } - @After public void tearDown() throws IOException { - if (testTempPath.endsWith(SUBDIRECTORY)) { - // recursive delete with safeguard - FileUtils.deleteDirectory(new File(testTempPath)); - } + FileUtils.deleteDirectory(new File(testTempPath)); } @Test -- cgit v1.2.3