Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id AB611200BB5 for ; Sun, 6 Nov 2016 10:58:05 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id A9EAF160B10; Sun, 6 Nov 2016 09:58:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id CC78A160B0D for ; Sun, 6 Nov 2016 10:58:04 +0100 (CET) Received: (qmail 27979 invoked by uid 500); 6 Nov 2016 09:57:49 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 26853 invoked by uid 99); 6 Nov 2016 09:57:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 06 Nov 2016 09:57:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 905DFF1730; Sun, 6 Nov 2016 09:57:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: varunsaxena@apache.org To: common-commits@hadoop.apache.org Date: Sun, 06 Nov 2016 09:58:23 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [37/50] [abbrv] hadoop git commit: HADOOP-13792. Stackoverflow for schemeless defaultFS with trailing slash. Contributed by John Zhuge archived-at: Sun, 06 Nov 2016 09:58:05 -0000 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/YARN-5355 Commit: abfc15d5ef966e99e8fe05d155ad2557e8cd67e8 Parents: 0aafc12 Author: Mingliang Liu Authored: Fri Nov 4 10:46:08 2016 -0700 Committer: Mingliang Liu 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