From ff7d23b5b915f04c2b5f3614701cb56cfc605228 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 13 Jan 2023 02:25:14 +0800 Subject: Fix possible resource leak and unsafe null de-referencing. --- .../ConfiguredIncrementalBackupMethodTest.java | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src/test/java') 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 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 ignoredFiles) { File[] fileList = dir.listFiles(); - Arrays.sort(fileList, // Need in reproducible order - new Comparator() { - 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(".") || -- cgit v1.2.3