File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed
hadoop-common-project/hadoop-common/src
main/java/org/apache/hadoop/net
test/java/org/apache/hadoop/net Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change 2020import org .apache .hadoop .classification .InterfaceAudience ;
2121import org .apache .hadoop .classification .InterfaceStability ;
2222
23+ import java .util .regex .Pattern ;
24+
2325/** A base class that implements interface Node
2426 *
2527 */
@@ -38,6 +40,7 @@ public class NodeBase implements Node {
3840 protected String location ; //string representation of this node's location
3941 protected int level ; //which level of the tree the node resides
4042 protected Node parent ; //its parent
43+ private static final Pattern SLASHES = Pattern .compile ("/+" );
4144
4245 /** Default constructor */
4346 public NodeBase () {
@@ -160,12 +163,15 @@ public static String normalize(String path) {
160163 if (path .length () == 0 ) {
161164 return ROOT ;
162165 }
163-
166+
164167 if (path .charAt (0 ) != PATH_SEPARATOR ) {
165168 throw new IllegalArgumentException (
166169 "Network Location path does not start with "
167170 +PATH_SEPARATOR_STR + ": " +path );
168171 }
172+
173+ // Remove duplicated slashes.
174+ path = SLASHES .matcher (path ).replaceAll ("/" );
169175
170176 int len = path .length ();
171177 if (path .charAt (len -1 ) == PATH_SEPARATOR ) {
Original file line number Diff line number Diff line change @@ -234,6 +234,15 @@ public void testChooseRandomExcluded() {
234234 assertSame ("node3" , node .getName ());
235235 }
236236
237+ @ Test
238+ public void testNodeBaseNormalizeRemoveLeadingSlash () {
239+ assertEquals ("/d1" , NodeBase .normalize ("/d1///" ));
240+ assertEquals ("/d1" , NodeBase .normalize ("/d1/" ));
241+ assertEquals ("/d1" , NodeBase .normalize ("/d1" ));
242+ assertEquals ("" , NodeBase .normalize ("///" ));
243+ assertEquals ("" , NodeBase .normalize ("/" ));
244+ }
245+
237246 private NodeElement getNewNode (String name , String rackLocation ) {
238247 NodeElement node = new NodeElement (name );
239248 node .setNetworkLocation (rackLocation );
You can’t perform that action at this time.
0 commit comments