소스 검색

HADOOP-7526. Add TestPath tests for URI conversion and reserved characters. Contributed by Eli Collins

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1156857 13f79535-47bb-0310-9956-ffa450edef68
Eli Collins 13 년 전
부모
커밋
28fd74c4cc
2개의 변경된 파일45개의 추가작업 그리고 0개의 파일을 삭제
  1. 3 0
      hadoop-common/CHANGES.txt
  2. 42 0
      hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java

+ 3 - 0
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

+ 42 - 0
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");