HADOOP-16957. NodeBase.normalize doesn't removing all trailing slashes. Contributed by Ayush Saxena.

This commit is contained in:
Ayush Saxena 2020-04-30 19:55:20 +05:30
parent b5b45c53a4
commit 6bdab3723e
2 changed files with 16 additions and 1 deletions

View File

@ -20,6 +20,8 @@
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() {
@ -167,6 +170,9 @@ public static String normalize(String path) {
+PATH_SEPARATOR_STR+ ": "+path);
}
// Remove duplicated slashes.
path = SLASHES.matcher(path).replaceAll("/");
int len = path.length();
if (path.charAt(len-1) == PATH_SEPARATOR) {
return path.substring(0, len-1);

View File

@ -234,6 +234,15 @@ public void testChooseRandomExcluded() {
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);