You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by ay...@apache.org on 2020/04/30 14:28:55 UTC
[hadoop] branch trunk updated: HADOOP-16957. NodeBase.normalize
doesn't removing all trailing slashes. Contributed by Ayush Saxena.
This is an automated email from the ASF dual-hosted git repository.
ayushsaxena pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git
The following commit(s) were added to refs/heads/trunk by this push:
new 6bdab37 HADOOP-16957. NodeBase.normalize doesn't removing all trailing slashes. Contributed by Ayush Saxena.
6bdab37 is described below
commit 6bdab3723eff78c79aa48c24aad87373b983fe6c
Author: Ayush Saxena <ay...@apache.org>
AuthorDate: Thu Apr 30 19:55:20 2020 +0530
HADOOP-16957. NodeBase.normalize doesn't removing all trailing slashes. Contributed by Ayush Saxena.
---
.../src/main/java/org/apache/hadoop/net/NodeBase.java | 8 +++++++-
.../src/test/java/org/apache/hadoop/net/TestClusterTopology.java | 9 +++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java
index 9da9ca2..cc14df8 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java
@@ -20,6 +20,8 @@ package org.apache.hadoop.net;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
+import java.util.regex.Pattern;
+
/** A base class that implements interface Node
*
*/
@@ -38,6 +40,7 @@ public class NodeBase implements Node {
protected String location; //string representation of this node's location
protected int level; //which level of the tree the node resides
protected Node parent; //its parent
+ private static final Pattern SLASHES = Pattern.compile("/+");
/** Default constructor */
public NodeBase() {
@@ -160,12 +163,15 @@ public class NodeBase implements Node {
if (path.length() == 0) {
return ROOT;
}
-
+
if (path.charAt(0) != PATH_SEPARATOR) {
throw new IllegalArgumentException(
"Network Location path does not start with "
+PATH_SEPARATOR_STR+ ": "+path);
}
+
+ // Remove duplicated slashes.
+ path = SLASHES.matcher(path).replaceAll("/");
int len = path.length();
if (path.charAt(len-1) == PATH_SEPARATOR) {
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java
index 6d9dc77..328cf11 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java
@@ -234,6 +234,15 @@ public class TestClusterTopology extends Assert {
assertSame("node3", node.getName());
}
+ @Test
+ public void testNodeBaseNormalizeRemoveLeadingSlash() {
+ assertEquals("/d1", NodeBase.normalize("/d1///"));
+ assertEquals("/d1", NodeBase.normalize("/d1/"));
+ assertEquals("/d1", NodeBase.normalize("/d1"));
+ assertEquals("", NodeBase.normalize("///"));
+ assertEquals("", NodeBase.normalize("/"));
+ }
+
private NodeElement getNewNode(String name, String rackLocation) {
NodeElement node = new NodeElement(name);
node.setNetworkLocation(rackLocation);
---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org