Browse Source

HADOOP-2461. Trim property names in configuration. Contributed by Tsz Wo (Nicholas), SZE.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@652178 13f79535-47bb-0310-9956-ffa450edef68
Konstantin Shvachko 17 years ago
parent
commit
9e020eedb8

+ 3 - 0
CHANGES.txt

@@ -52,6 +52,9 @@ Trunk (unreleased changes)
     HADOOP-3308. Improve QuickSort by excluding values eq the pivot from the
     partition. (cdouglas)
 
+    HADOOP-2461. Trim property names in configuration.
+    (Tsz Wo (Nicholas), SZE via shv)
+
   OPTIMIZATIONS
 
     HADOOP-3274. The default constructor of BytesWritable creates empty 

+ 1 - 1
src/java/org/apache/hadoop/conf/Configuration.java

@@ -892,7 +892,7 @@ public class Configuration implements Iterable<Map.Entry<String,String>> {
             continue;
           Element field = (Element)fieldNode;
           if ("name".equals(field.getTagName()) && field.hasChildNodes())
-            attr = ((Text)field.getFirstChild()).getData();
+            attr = ((Text)field.getFirstChild()).getData().trim();
           if ("value".equals(field.getTagName()) && field.hasChildNodes())
             value = ((Text)field.getFirstChild()).getData();
           if ("final".equals(field.getTagName()) && field.hasChildNodes())

+ 26 - 0
src/test/org/apache/hadoop/conf/TestConfiguration.java

@@ -22,6 +22,7 @@ import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Random;
 
 import junit.framework.TestCase;
 
@@ -33,6 +34,7 @@ public class TestConfiguration extends TestCase {
   private Configuration conf;
   final static String CONFIG = new File("./test-config.xml").getAbsolutePath();
   final static String CONFIG2 = new File("./test-config2.xml").getAbsolutePath();
+  final static Random RAN = new Random();
 
   @Override
   protected void setUp() throws Exception {
@@ -186,6 +188,30 @@ public class TestConfiguration extends TestCase {
     assertEquals("this  contains a comment", conf.get("my.comment"));
   }
   
+  public void testTrim() throws IOException {
+    out=new BufferedWriter(new FileWriter(CONFIG));
+    startConfig();
+    String[] whitespaces = {"", " ", "\n", "\t"};
+    String[] name = new String[100];
+    for(int i = 0; i < name.length; i++) {
+      name[i] = "foo" + i;
+      StringBuilder prefix = new StringBuilder(); 
+      StringBuilder postfix = new StringBuilder(); 
+      for(int j = 0; j < 3; j++) {
+        prefix.append(whitespaces[RAN.nextInt(whitespaces.length)]);
+        postfix.append(whitespaces[RAN.nextInt(whitespaces.length)]);
+      }
+      
+      appendProperty(prefix + name[i] + postfix, name[i] + ".value");
+    }
+    endConfig();
+
+    conf.addResource(new Path(CONFIG));
+    for(String n : name) {
+      assertEquals(n + ".value", conf.get(n));
+    }
+  }
+
   public void testToString() throws IOException {
     out=new BufferedWriter(new FileWriter(CONFIG));
     startConfig();