Browse Source

HADOOP-18652. Path.suffix raises NullPointerException (#5653). Contributed by Patrick Grandjean.

Reviewed-by: Wei-Chiu Chuang <weichiu@apache.org>
Signed-off-by: Ayush Saxena <ayushsaxena@apache.org>
Patrick GRANDJEAN 1 năm trước cách đây
mục cha
commit
9029bba5dc

+ 6 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java

@@ -447,7 +447,12 @@ public class Path
    * @return a new path with the suffix added
    */
   public Path suffix(String suffix) {
-    return new Path(getParent(), getName()+suffix);
+    Path parent = getParent();
+    if (parent == null) {
+      return new Path("/", getName() + suffix);
+    }
+
+    return new Path(parent, getName() + suffix);
   }
 
   @Override

+ 7 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java

@@ -528,4 +528,11 @@ public class TestPath {
     }
 
   }
+
+  @Test(timeout = 30000)
+  public void testSuffixFromRoot() {
+    Path root = new Path("/");
+    Assert.assertNull(root.getParent());
+    Assert.assertEquals(new Path("/bar"), root.suffix("bar"));
+  }
 }