hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lium...@apache.org
Subject hadoop git commit: HADOOP-13792. Stackoverflow for schemeless defaultFS with trailing slash. Contributed by John Zhuge
Date Fri, 04 Nov 2016 17:51:04 GMT
Repository: hadoop
Updated Branches:
  refs/heads/trunk 0aafc122d -> abfc15d5e


HADOOP-13792. Stackoverflow for schemeless defaultFS with trailing slash. Contributed by John
Zhuge


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/abfc15d5
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/abfc15d5
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/abfc15d5

Branch: refs/heads/trunk
Commit: abfc15d5ef966e99e8fe05d155ad2557e8cd67e8
Parents: 0aafc12
Author: Mingliang Liu <liuml07@apache.org>
Authored: Fri Nov 4 10:46:08 2016 -0700
Committer: Mingliang Liu <liuml07@apache.org>
Committed: Fri Nov 4 10:46:27 2016 -0700

----------------------------------------------------------------------
 .../java/org/apache/hadoop/fs/FileSystem.java   |   6 +-
 .../org/apache/hadoop/fs/TestDefaultUri.java    | 115 +++++++++++++++++++
 2 files changed, 120 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/abfc15d5/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java
index 39b5b95..375af90 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java
@@ -188,7 +188,11 @@ public abstract class FileSystem extends Configured implements Closeable
{
    * @return the uri of the default filesystem
    */
   public static URI getDefaultUri(Configuration conf) {
-    return URI.create(fixName(conf.get(FS_DEFAULT_NAME_KEY, DEFAULT_FS)));
+    URI uri = URI.create(fixName(conf.get(FS_DEFAULT_NAME_KEY, DEFAULT_FS)));
+    if (uri.getScheme() == null) {
+      throw new IllegalArgumentException("No scheme in default FS: " + uri);
+    }
+    return uri;
   }
 
   /** Set the default filesystem URI in a configuration.

http://git-wip-us.apache.org/repos/asf/hadoop/blob/abfc15d5/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDefaultUri.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDefaultUri.java
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDefaultUri.java
new file mode 100644
index 0000000..f232735
--- /dev/null
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDefaultUri.java
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.fs;
+
+import static org.apache.hadoop.fs.FileSystem.FS_DEFAULT_NAME_KEY;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.test.GenericTestUtils;
+import org.junit.Test;
+
+/**
+ * Test default URI related APIs in {@link FileSystem}.
+ */
+public class TestDefaultUri {
+  private Configuration conf = new Configuration();
+
+  @Test
+  public void tetGetDefaultUri() {
+    conf.set(FS_DEFAULT_NAME_KEY, "hdfs://nn_host");
+    URI uri = FileSystem.getDefaultUri(conf);
+    assertThat(uri.getScheme(), is("hdfs"));
+    assertThat(uri.getAuthority(), is("nn_host"));
+  }
+
+  @Test
+  public void tetGetDefaultUriWithPort() {
+    conf.set(FS_DEFAULT_NAME_KEY, "hdfs://nn_host:5432");
+    URI uri = FileSystem.getDefaultUri(conf);
+    assertThat(uri.getScheme(), is("hdfs"));
+    assertThat(uri.getAuthority(), is("nn_host:5432"));
+  }
+
+  @Test
+  public void tetGetDefaultUriTrailingSlash() {
+    conf.set(FS_DEFAULT_NAME_KEY, "hdfs://nn_host/");
+    URI uri = FileSystem.getDefaultUri(conf);
+    assertThat(uri.getScheme(), is("hdfs"));
+    assertThat(uri.getAuthority(), is("nn_host"));
+  }
+
+  @Test
+  public void tetGetDefaultUriNoScheme() {
+    conf.set(FS_DEFAULT_NAME_KEY, "nn_host");
+    URI uri = FileSystem.getDefaultUri(conf);
+    assertThat(uri.getScheme(), is("hdfs"));
+    assertThat(uri.getAuthority(), is("nn_host"));
+  }
+
+  @Test
+  public void tetGetDefaultUriNoSchemeTrailingSlash() {
+    conf.set(FS_DEFAULT_NAME_KEY, "nn_host/");
+    try {
+      FileSystem.getDefaultUri(conf);
+      fail("Expect IAE: No scheme in default FS");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "No scheme in default FS", e);
+    }
+  }
+
+  @Test
+  public void tetFsGet() throws IOException {
+    conf.set(FS_DEFAULT_NAME_KEY, "file:///");
+    FileSystem fs = FileSystem.get(conf);
+    assertThat(fs, instanceOf(LocalFileSystem.class));
+  }
+
+  @Test
+  public void tetFsGetNoScheme() throws IOException {
+    // Bare host name or address indicates hdfs scheme
+    conf.set(FS_DEFAULT_NAME_KEY, "nn_host");
+    try {
+      FileSystem.get(conf);
+      fail("Expect IOE: No FileSystem for scheme: hdfs");
+    } catch (IOException e) {
+      GenericTestUtils.assertExceptionContains(
+          "No FileSystem for scheme: hdfs", e);
+    }
+  }
+
+  @Test
+  public void tetFsGetNoSchemeTrailingSlash() throws IOException {
+    // Bare host name or address with trailing slash is invalid
+    conf.set(FS_DEFAULT_NAME_KEY, "nn_host/");
+    try {
+      FileSystem.get(conf);
+      fail("Expect IAE: No scheme in default FS");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "No scheme in default FS", e);
+    }
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org


Mime
View raw message