1
votes

I'm about to write a test of a custom FileVisitor which requires the filename of a path.

The documentation of Path's getFileName reads

Returns: a path representing the name of the file or directory, or null if this path has zero elements

Apart from using a mock - which I try to avoid - how do I construct a Path with zero elements?

  • Paths.get("") - requires a non-null argument, the empty string is one element
  • new File("").toPath() - requires also a non-null argument, the empty string is one element, too
  • anything like Paths.get(".") or Paths.get("./") also have non-zero elements

For all of these, the filename is never null.

Probably it's a behavior of the WindowsFileSystem implementation in Java. Does anyone know if other implementation of FileSystem behave differently and it's actually possible, that getFileName() returns null?

1

1 Answers

1
votes

(At least on OSX) java.nio.file.Paths.get('/') results in such a zero-element sun.nio.fs.UnixPath.