diff options
author | Keuin <[email protected]> | 2023-01-13 02:25:14 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-01-13 02:25:14 +0800 |
commit | ff7d23b5b915f04c2b5f3614701cb56cfc605228 (patch) | |
tree | f360bb8562d6ed9ef9c763c5ee3aa2307f84220d /src/test/java/com/keuin/kbackupfabric/operation | |
parent | 9d04325a8e059ba4ad3e6e5f75df02563b12a462 (diff) |
Fix possible resource leak and unsafe null de-referencing.
Diffstat (limited to 'src/test/java/com/keuin/kbackupfabric/operation')
-rw-r--r-- | src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java | 28 |
1 files changed, 15 insertions, 13 deletions
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 0928c9e..0741004 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 @@ -17,6 +17,7 @@ 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.*; @@ -135,13 +136,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; } @@ -244,12 +247,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(".") || |