diff --git a/src/main/java/com/github/fge/filesystem/path/GenericPath.java b/src/main/java/com/github/fge/filesystem/path/GenericPath.java index ff7fb60..06815da 100644 --- a/src/main/java/com/github/fge/filesystem/path/GenericPath.java +++ b/src/main/java/com/github/fge/filesystem/path/GenericPath.java @@ -129,6 +129,10 @@ public int getNameCount() @Override public Path getName(final int index) { + if (elements.root != null && elements.root.isEmpty() && elements.names.length == 0) { + return new GenericPath(fs, factory, PathElements.EMPTY); + } + final String name; //noinspection ProhibitedExceptionCaught diff --git a/src/test/java/com/github/fge/filesystem/path/GenericPathTest.java b/src/test/java/com/github/fge/filesystem/path/GenericPathTest.java index 7e1519d..44e6e29 100644 --- a/src/test/java/com/github/fge/filesystem/path/GenericPathTest.java +++ b/src/test/java/com/github/fge/filesystem/path/GenericPathTest.java @@ -23,6 +23,7 @@ import com.github.fge.filesystem.driver.FileSystemDriver; import com.github.fge.filesystem.fs.GenericFileSystem; import com.github.fge.filesystem.provider.FileSystemRepository; +import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -120,6 +121,19 @@ public void getFileNameWithNameElementsDoesNotReturnNull() assertPath(path.getFileName()).isNotNull(); } + /* The typical behavior of a Unix path is to return a path with an + * empty root and no names when element 0 is requested from a path + * with an empty root and no names. + */ + @Test + public void getEmptyFileNameReturnsEmptyFirstName() { + final PathElements elements = new PathElements("", new String[] {}); + final Path path = new GenericPath(fs, factory, elements); + + Assert.assertEquals(path.getName(0).toString(), "", + "First name element on empty path didn't equal an empty String"); + } + /* * This test this part of the Path's .relativize() method: * @@ -240,4 +254,4 @@ public void toUriPathReturnsCorrectURI(final String s, final String path, assertThat(p.toUri().toString()).as("generated URI is correct") .isEqualTo(expected); } -} \ No newline at end of file +}