HADOOP-16957. NodeBase.normalize doesn't removing all trailing slashes. Contributed by Ayush Saxena.
This commit is contained in:
parent
b5b45c53a4
commit
6bdab3723e
@ -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() {
|
||||
@ -160,12 +163,15 @@ public static String normalize(String path) {
|
||||
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) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user