HADOOP-15516. Add test cases to cover FileUtil#readLink. Contributed by 
Giovanni Matteo Fumarola.

(cherry picked from commit 12be8bad7debd67c9ea72b979a39c8cf42c5f37d)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/8526ba9a
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/8526ba9a
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/8526ba9a

Branch: refs/heads/branch-3.1
Commit: 8526ba9a6901ac35b4219cb55a42ce9026d67e3a
Parents: e9367cc
Author: Inigo Goiri <[email protected]>
Authored: Thu Jun 7 13:34:52 2018 -0700
Committer: Inigo Goiri <[email protected]>
Committed: Thu Jun 7 13:35:32 2018 -0700

----------------------------------------------------------------------
 .../java/org/apache/hadoop/fs/FileUtil.java     |  6 +++
 .../java/org/apache/hadoop/fs/TestFileUtil.java | 52 ++++++++++++++++++++
 2 files changed, 58 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/8526ba9a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java
index 5ef78f2..61c30aa 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java
@@ -198,6 +198,12 @@ public class FileUtil {
      * use getCanonicalPath in File to get the target of the symlink but that
      * does not indicate if the given path refers to a symlink.
      */
+
+    if (f == null) {
+      LOG.warn("Can not read a null symLink");
+      return "";
+    }
+
     try {
       return Shell.execCommand(
           Shell.getReadlinkCommand(f.toString())).trim();

http://git-wip-us.apache.org/repos/asf/hadoop/blob/8526ba9a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java
index 7218a1b..f02ea80 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java
@@ -1287,4 +1287,56 @@ public class TestFileUtil {
     }
   }
 
+  /**
+   * This test validates the correctness of {@link FileUtil#readLink(File)} in
+   * case of null pointer inputs.
+   */
+  @Test
+  public void testReadSymlinkWithNullInput() {
+    String result = FileUtil.readLink(null);
+    Assert.assertEquals("", result);
+  }
+
+  /**
+   * This test validates the correctness of {@link FileUtil#readLink(File)}.
+   *
+   * @throws IOException
+   */
+  @Test
+  public void testReadSymlink() throws IOException {
+    Assert.assertFalse(del.exists());
+    del.mkdirs();
+
+    File file = new File(del, FILE);
+    File link = new File(del, "_link");
+
+    // Create a symbolic link
+    FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
+
+    String result = FileUtil.readLink(link);
+    Assert.assertEquals(file.getAbsolutePath(), result);
+
+    file.delete();
+    link.delete();
+  }
+
+  /**
+   * This test validates the correctness of {@link FileUtil#readLink(File)} 
when
+   * it gets a file in input.
+   *
+   * @throws IOException
+   */
+  @Test
+  public void testReadSymlinkWithAFileAsInput() throws IOException {
+    Assert.assertFalse(del.exists());
+    del.mkdirs();
+
+    File file = new File(del, FILE);
+
+    String result = FileUtil.readLink(file);
+    Assert.assertEquals("", result);
+
+    file.delete();
+  }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to