diff --git a/src/main/java/loci/common/Location.java b/src/main/java/loci/common/Location.java index a5fc14e2..3d552dee 100644 --- a/src/main/java/loci/common/Location.java +++ b/src/main/java/loci/common/Location.java @@ -56,8 +56,8 @@ import com.google.common.collect.MapMaker; /** - * Pseudo-extension of java.io.File that supports reading over HTTP (among - * other things). + * Pseudo-extension of {@link java.io.File} that supports reading over HTTP + * (among other things). * It is strongly recommended to use this instead of java.io.File. */ public class Location { @@ -246,7 +246,7 @@ public Location(String parent, String child) { * @see #Location(String, String) */ public Location(Location parent, String child) { - this(parent.getAbsolutePath(), child); + this(parent == null ? (String) null : parent.getAbsolutePath(), child); } // -- Location API methods -- @@ -842,9 +842,13 @@ public String getName() { } /** - * Returns the name of this file's parent directory, i.e. the path name prefix - * and every name in the path name sequence except for the last. - * If this file does not have a parent directory, then null is returned. + * Returns the pathname string of this abstract pathname's parent, or null if + * this pathname does not have a parent directory. + * + * The parent of an abstract pathname consists of the pathname's prefix, if + * any, and each name in the pathname's name sequence except for the last. + * If the name sequence is empty then the pathname does not name a parent + * directory. * * @return see above * @see java.io.File#getParent() @@ -864,13 +868,22 @@ public String getParent() { } /** - * Returns this file's parent directory. + * Returns the abstract pathname of this abstract pathname's parent, or null + * if this pathname does not name a parent directory. + * + * The parent of an abstract pathname consists of the pathname's prefix, if + * any, and each name in the pathname's name sequence except for the last. + * If the name sequence is empty then the pathname does not name a parent + * directory. * - * @return the Location representing {@link #getParent()} + * @return The abstract pathname of the parent directory named by this + * abstract pathname, or null if this pathname does not name a parent * @see java.io.File#getParentFile() */ public Location getParentFile() { - return new Location(getParent()); + String parent = this.getParent(); + if (parent == null) return null; + return new Location(parent); } /** diff --git a/src/test/java/loci/common/utests/LocationTest.java b/src/test/java/loci/common/utests/LocationTest.java index dc6c5371..b9fd4095 100644 --- a/src/test/java/loci/common/utests/LocationTest.java +++ b/src/test/java/loci/common/utests/LocationTest.java @@ -33,6 +33,7 @@ package loci.common.utests; import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNull; import java.io.File; import java.io.IOException; @@ -272,6 +273,14 @@ public void testParentRoot() { } } + @Test + public void testParentNull() { + Location nullParent = new Location((String) null, "nullParentFile"); + assertNull(nullParent.getParentFile()); + nullParent = new Location((Location) null, "nullParentFile"); + assertNull(nullParent.getParentFile()); + } + @Test public void testIsDirectory() { for (int i=0; i