Path implements IOReadableWritable, Serializable { public Path getParent() { final String path = uri.getPath(); final int lastSlash = path.lastIndexOf('/'); final int start = hasWindowsDrive(path, true) ? 3 : 0; if ((path.length() == start) || (lastSlash == start && path.length() == start + 1)) { return null; } String parent; if (lastSlash == -1) { parent = CUR_DIR; } else { final int end = hasWindowsDrive(path, true) ? 3 : 0; parent = path.substring(0, lastSlash == end ? end + 1 : lastSlash); } return new Path(uri.getScheme(), uri.getAuthority(), parent); } Path();  Path(URI uri);  Path(String parent, String child);  Path(Path parent, String child);  Path(String parent, Path child);  Path(Path parent, Path child);  Path(String pathString);  Path(String scheme, String authority, String path); URI toUri(); FileSystem getFileSystem(); boolean isAbsolute(); String getName(); Path getParent(); Path suffix(String suffix); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); int compareTo(Object o); int depth(); Path makeQualified(FileSystem fs); @Override void read(DataInputView in); @Override void write(DataOutputView out); static final String SEPARATOR; static final char SEPARATOR_CHAR; static final String CUR_DIR; }
@Test public void testGetParent() { Path p = new Path("/my/fancy/path"); assertEquals("/my/fancy", p.getParent().toUri().getPath()); p = new Path("/my/other/fancy/path/"); assertEquals("/my/other/fancy", p.getParent().toUri().getPath()); p = new Path("hdfs: assertEquals("/my", p.getParent().toUri().getPath()); p = new Path("hdfs: assertEquals("/", p.getParent().toUri().getPath()); p = new Path("/"); assertNull(p.getParent()); p = new Path("C:/my/windows/path"); assertEquals("/C:/my/windows", p.getParent().toUri().getPath()); }