HADOOP-10876. The constructor of Path should not take an empty URL as a parameter. Contributed by Zhihai Xu.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1614231 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Wang 2014-07-29 00:44:38 +00:00
parent 8155f7c6eb
commit e61396405a
3 changed files with 42 additions and 1 deletions

View File

@ -80,6 +80,9 @@ Release 2.6.0 - UNRELEASED
HADOOP-10830. Missing lock in JavaKeyStoreProvider.createCredentialEntry. HADOOP-10830. Missing lock in JavaKeyStoreProvider.createCredentialEntry.
(Benoy Antony via umamahesh) (Benoy Antony via umamahesh)
HADOOP-10876. The constructor of Path should not take an empty URL as a
parameter. (Zhihai Xu via wang)
Release 2.5.0 - UNRELEASED Release 2.5.0 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -129,6 +129,19 @@ public class Path implements Comparable {
} }
} }
/** check URI parameter of Path constructor. */
private void checkPathArg(URI aUri) throws IllegalArgumentException {
// disallow construction of a Path from an empty URI
if (aUri == null) {
throw new IllegalArgumentException(
"Can not create a Path from a null URI");
}
if (aUri.toString().isEmpty()) {
throw new IllegalArgumentException(
"Can not create a Path from an empty URI");
}
}
/** Construct a path from a String. Path strings are URIs, but with /** Construct a path from a String. Path strings are URIs, but with
* unescaped elements and some additional normalization. */ * unescaped elements and some additional normalization. */
public Path(String pathString) throws IllegalArgumentException { public Path(String pathString) throws IllegalArgumentException {
@ -176,6 +189,7 @@ public class Path implements Comparable {
* Construct a path from a URI * Construct a path from a URI
*/ */
public Path(URI aUri) { public Path(URI aUri) {
checkPathArg(aUri);
uri = aUri.normalize(); uri = aUri.normalize();
} }

View File

@ -26,11 +26,13 @@ import java.util.Arrays;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.AvroTestUtil; import org.apache.hadoop.io.AvroTestUtil;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.util.Shell; import org.apache.hadoop.util.Shell;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import junit.framework.TestCase; import junit.framework.TestCase;
import static org.junit.Assert.fail;
public class TestPath extends TestCase { public class TestPath extends TestCase {
/** /**
@ -305,6 +307,28 @@ public class TestPath extends TestCase {
// if the child uri is absolute path // if the child uri is absolute path
assertEquals("foo://bar/fud#boo", new Path(new Path(new URI( assertEquals("foo://bar/fud#boo", new Path(new Path(new URI(
"foo://bar/baz#bud")), new Path(new URI("/fud#boo"))).toString()); "foo://bar/baz#bud")), new Path(new URI("/fud#boo"))).toString());
// empty URI
URI uri3 = new URI("");
assertEquals("", uri3.toString());
try {
path = new Path(uri3);
fail("Expected exception for empty URI");
} catch (IllegalArgumentException e) {
// expect to receive an IllegalArgumentException
GenericTestUtils.assertExceptionContains("Can not create a Path"
+ " from an empty URI", e);
}
// null URI
uri3 = null;
try {
path = new Path(uri3);
fail("Expected exception for null URI");
} catch (IllegalArgumentException e) {
// expect to receive an IllegalArgumentException
GenericTestUtils.assertExceptionContains("Can not create a Path"
+ " from a null URI", e);
}
} }
/** Test URIs created from Path objects */ /** Test URIs created from Path objects */