diff --git a/hadoop-common/CHANGES.txt b/hadoop-common/CHANGES.txt index 51d25674651..d7c00392414 100644 --- a/hadoop-common/CHANGES.txt +++ b/hadoop-common/CHANGES.txt @@ -319,6 +319,9 @@ Trunk (unreleased changes) HADOOP-6158. Move CyclicIteration to HDFS. (eli) + HADOOP-7526. Add TestPath tests for URI conversion and reserved + characters. (eli) + OPTIMIZATIONS HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole diff --git a/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java b/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java index 703e7691848..d36a39edb8f 100644 --- a/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java +++ b/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java @@ -185,6 +185,48 @@ public class TestPath extends TestCase { assertEquals("foo://bar/fud#boo", new Path(new Path(new URI( "foo://bar/baz#bud")), new Path(new URI("/fud#boo"))).toString()); } + + /** Test URIs created from Path objects */ + public void testPathToUriConversion() throws URISyntaxException, IOException { + // Path differs from URI in that it ignores the query part.. + assertEquals(new URI(null, null, "/foo?bar", null, null), new Path("/foo?bar").toUri()); + assertEquals(new URI(null, null, "/foo\"bar", null, null), new Path("/foo\"bar").toUri()); + assertEquals(new URI(null, null, "/foo bar", null, null), new Path("/foo bar").toUri()); + // therefore "foo?bar" is a valid Path, so a URI created from a Path has path "foo?bar" + // where in a straight URI the path part is just "foo" + assertEquals("/foo?bar", new Path("http://localhost/foo?bar").toUri().getPath()); + assertEquals("/foo", new URI("http://localhost/foo?bar").getPath()); + + // The path part handling in Path is equivalent to URI + assertEquals(new URI("/foo;bar").getPath(), new Path("/foo;bar").toUri().getPath()); + assertEquals(new URI("/foo;bar"), new Path("/foo;bar").toUri()); + assertEquals(new URI("/foo+bar"), new Path("/foo+bar").toUri()); + assertEquals(new URI("/foo-bar"), new Path("/foo-bar").toUri()); + assertEquals(new URI("/foo=bar"), new Path("/foo=bar").toUri()); + assertEquals(new URI("/foo,bar"), new Path("/foo,bar").toUri()); + } + + /** Test reserved characters in URIs (and therefore Paths) */ + public void testReservedCharacters() throws URISyntaxException, IOException { + // URI encodes the path + assertEquals("/foo%20bar", new URI(null, null, "/foo bar", null, null).getRawPath()); + // URI#getPath decodes the path + assertEquals("/foo bar", new URI(null, null, "/foo bar", null, null).getPath()); + // URI#toString returns an encoded path + assertEquals("/foo%20bar", new URI(null, null, "/foo bar", null, null).toString()); + assertEquals("/foo%20bar", new Path("/foo bar").toUri().toString()); + // Reserved chars are not encoded + assertEquals("/foo;bar", new URI("/foo;bar").getPath()); + assertEquals("/foo;bar", new URI("/foo;bar").getRawPath()); + assertEquals("/foo+bar", new URI("/foo+bar").getPath()); + assertEquals("/foo+bar", new URI("/foo+bar").getRawPath()); + + // URI#getPath decodes the path part (and URL#getPath does not decode) + assertEquals("/foo bar", new Path("http://localhost/foo bar").toUri().getPath()); + assertEquals("/foo%20bar", new Path("http://localhost/foo bar").toUri().toURL().getPath()); + assertEquals("/foo?bar", new URI("http", "localhost", "/foo?bar", null, null).getPath()); + assertEquals("/foo%3Fbar", new URI("http", "localhost", "/foo?bar", null, null).toURL().getPath()); + } public void testMakeQualified() throws URISyntaxException { URI defaultUri = new URI("hdfs://host1/dir1");