diff options
Diffstat (limited to 'src/test/java')
4 files changed, 98 insertions, 24 deletions
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/manager/IncrementalBackupStorageManagerTest.java b/src/test/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManagerTest.java index 2a9420a..7b2d7f0 100644 --- a/src/test/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManagerTest.java +++ b/src/test/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManagerTest.java @@ -18,6 +18,7 @@ import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.*; +import java.util.stream.Stream; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -71,13 +72,16 @@ public class IncrementalBackupStorageManagerTest { throw new IllegalArgumentException("negative count"); int[] c = new int[]{0}; Random rnd = new Random(); - Files.walk(new File(srcRoot).toPath(), 1).map(p -> rnd.nextBoolean() ? p : null) - .filter(Objects::nonNull).forEach(p -> { - if (c[0]++ != count) { - if (!p.toFile().delete() && p.toFile().isFile()) - throw new RuntimeException("Failed to delete file " + p); - } - }); + try (Stream<Path> walk = Files.walk(new File(srcRoot).toPath(), 1)) { + walk.map(p -> rnd.nextBoolean() ? p : null) + .filter(Objects::nonNull) + .forEach(p -> { + if (c[0]++ != count) { + if (!p.toFile().delete() && p.toFile().isFile()) + throw new RuntimeException("Failed to delete file " + p); + } + }); + } } private static String getRandomString(int length) { 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 c69bd11..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,9 +1,13 @@ 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; import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import java.io.*; @@ -13,13 +17,14 @@ import java.nio.file.Paths; import java.time.LocalDateTime; import java.util.*; import java.util.function.Function; +import java.util.logging.Logger; +import java.util.stream.Stream; import static org.apache.commons.io.FileUtils.forceDelete; import static org.junit.Assert.*; public class ConfiguredIncrementalBackupMethodTest { - - private final String testTempPath = (new File("R:\\").isDirectory()) ? "R:\\" : ".\\testfile\\ConfiguredIncrementalBackupMethodTest"; + private String testTempPath; private final String sourceDirectoryName = "source"; private final String destDirectoryName = "destination"; private final String customBackupName = "index"; @@ -30,6 +35,20 @@ public class ConfiguredIncrementalBackupMethodTest { private final int maxRandomFileSizeBytes = 1024 * 1024; private final Function<Integer, Integer> scaleDecayFunc = (x) -> x - 1; + private static final Logger logger = Logger.getLogger(ConfiguredIncrementalBackupMethodTest.class.getName()); + + @Before + public void setUp() throws IOException { + // select temporary directory + testTempPath = TestUtils.getTempDirectory("kb_temp"); + logger.info(String.format("Using temp path: %s", testTempPath)); + } + + @After + public void tearDown() throws IOException { + FileUtils.deleteDirectory(new File(testTempPath)); + } + @Test public void iterationTest() throws IOException { int a = 12; @@ -89,13 +108,15 @@ public class ConfiguredIncrementalBackupMethodTest { int[] success = new int[1]; if (fake) { - Files.walk(sourcePath).filter(path -> path.toFile().isFile()).limit(4).forEach(path -> { - System.out.println("Deleted file `" + path.toFile().getName() + "`."); - if (!path.toFile().delete()) - fail(); - if (!Objects.equals(path.toFile().getName(), BackupMetadata.metadataFileName)) - success[0]++; - }); + try (Stream<Path> walk = Files.walk(sourcePath)) { + walk.filter(path -> path.toFile().isFile()).limit(4).forEach(path -> { + System.out.println("Deleted file `" + path.toFile().getName() + "`."); + if (!path.toFile().delete()) + fail(); + if (!Objects.equals(path.toFile().getName(), BackupMetadata.metadataFileName)) + success[0]++; + }); + } if (success[0] == 0) fake = false; } @@ -198,12 +219,11 @@ public class ConfiguredIncrementalBackupMethodTest { Set<String> ignoredFiles) { File[] fileList = dir.listFiles(); - Arrays.sort(fileList, // Need in reproducible order - new Comparator<File>() { - public int compare(File f1, File f2) { - return f1.getName().compareTo(f2.getName()); - } - }); + if (fileList == null) { + throw new RuntimeException("Failed to list files in directory " + dir); + } + // Need in reproducible order + Arrays.sort(fileList, Comparator.comparing(File::getName)); for (File f : fileList) { if (!includeHiddenFiles && f.getName().startsWith(".") || |