]> source.dussan.org Git - jgit.git/commitdiff
[spotbugs] Fix potential NPE in FileBasedConfigTest 66/173366/4
authorMatthias Sohn <matthias.sohn@sap.com>
Thu, 3 Dec 2020 00:54:25 +0000 (01:54 +0100)
committerChristian Halstrick <christian.halstrick@sap.com>
Thu, 17 Dec 2020 17:42:00 +0000 (18:42 +0100)
Path#getParent can return null. Fix the warning by implementing a helper
method which asserts the parent is not null.

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

index 803ff108f8e2ccb1aea60aeb07b9e97d00508724..b8b503cdcd12e2192c6d334be35ae89bbec15c5a 100644 (file)
@@ -13,6 +13,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.eclipse.jgit.util.FileUtils.pathToString;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -178,7 +179,7 @@ public class FileBasedConfigTest {
                final Path includedFile = createFile(CONTENT1.getBytes(UTF_8), "dir1");
                final ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bos.write("[include]\npath=".getBytes(UTF_8));
-               bos.write(("../" + includedFile.getParent().getFileName() + "/"
+               bos.write(("../" + parent(includedFile).getFileName() + "/"
                                + includedFile.getFileName()).getBytes(UTF_8));
 
                final Path file = createFile(bos.toByteArray(), "dir2");
@@ -213,7 +214,7 @@ public class FileBasedConfigTest {
 
                final Path file = createFile(bos.toByteArray(), "repo");
                final FS fs = FS.DETECTED.newInstance();
-               fs.setUserHome(includedFile.getParent().toFile());
+               fs.setUserHome(parent(includedFile).toFile());
 
                final FileBasedConfig config = new FileBasedConfig(file.toFile(), fs);
                config.load();
@@ -231,7 +232,7 @@ public class FileBasedConfigTest {
                FileBasedConfig config = new FileBasedConfig(file.toFile(),
                                FS.DETECTED);
                config.setString("include", null, "path",
-                               ("../" + includedFile.getParent().getFileName() + "/"
+                               ("../" + parent(includedFile).getFileName() + "/"
                                                + includedFile.getFileName()));
 
                // just by setting the include.path, it won't be included
@@ -280,4 +281,10 @@ public class FileBasedConfigTest {
                }
                return f;
        }
+
+       private Path parent(Path file) {
+               Path parent = file.getParent();
+               assertNotNull(parent);
+               return parent;
+       }
 }