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

This commit is contained in:
Inigo Goiri 2018-06-07 13:34:52 -07:00
parent 7969cc4667
commit 12be8bad7d
2 changed files with 58 additions and 0 deletions

View File

@ -198,6 +198,12 @@ public static String readLink(File f) {
* 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();

View File

@ -1441,4 +1441,56 @@ private void putEntriesInTar(TarArchiveOutputStream tos, File f)
}
}
/**
* 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();
}
}