]> source.dussan.org Git - jgit.git/commitdiff
[spotbugs] Fix potential NPEs in FileReftableStackTest 68/173368/5
authorMatthias Sohn <matthias.sohn@sap.com>
Fri, 4 Dec 2020 00:26:23 +0000 (01:26 +0100)
committerChristian Halstrick <christian.halstrick@sap.com>
Thu, 17 Dec 2020 17:42:00 +0000 (18:42 +0100)
File#listFiles can return null. Use Files#list instead to fix the
problem.

Change-Id: I74e0b49aa6dae370219507c64aa43be4d8aa7b82
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileReftableStackTest.java

index 72bff168317dd099b38c20d7387c7a6c41fac610..6c74f0079a155ef53bf164b2a8f8ec049ae29be7 100644 (file)
@@ -18,7 +18,8 @@ import static org.junit.Assert.assertTrue;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.util.Arrays;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -90,8 +91,9 @@ public class FileReftableStackTest {
                                assertEquals(ObjectId.zeroId(), c.getRef().getObjectId());
                        }
 
-                       List<String> files = Arrays.asList(reftableDir.listFiles()).stream()
-                                       .map(File::getName).collect(Collectors.toList());
+                       List<String> files = Files.list(reftableDir.toPath())
+                                       .map(Path::getFileName).map(Path::toString)
+                                       .collect(Collectors.toList());
                        Collections.sort(files);
 
                        assertTrue(files.size() < 20);
@@ -130,11 +132,14 @@ public class FileReftableStackTest {
                                });
                                assertTrue(ok);
 
-                               List<File> files = Arrays.asList(reftableDir.listFiles());
+                               List<Path> files = Files.list(reftableDir.toPath())
+                                               .collect(Collectors.toList());
                                for (int j = 0; j < files.size(); j++) {
-                                       File f = files.get(j);
-                                       if (f.getName().endsWith(".ref")) {
-                                               assertTrue(f.delete());
+                                       Path f = files.get(j);
+                                       Path fileName = f.getFileName();
+                                       if (fileName != null
+                                                       && fileName.toString().endsWith(".ref")) {
+                                               Files.delete(f);
                                                break outer;
                                        }
                                }