summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2023-01-13 02:25:14 +0800
committerKeuin <[email protected]>2023-01-13 02:25:14 +0800
commitff7d23b5b915f04c2b5f3614701cb56cfc605228 (patch)
treef360bb8562d6ed9ef9c763c5ee3aa2307f84220d
parent9d04325a8e059ba4ad3e6e5f75df02563b12a462 (diff)
Fix possible resource leak and unsafe null de-referencing.
-rw-r--r--src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java28
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(".") ||