hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject svn commit: r752500 - in /hadoop/core/branches/branch-0.20: CHANGES.txt src/core/org/apache/hadoop/util/StringUtils.java src/test/org/apache/hadoop/util/TestStringUtils.java
Date Wed, 11 Mar 2009 16:22:50 GMT
Author: enis
Date: Wed Mar 11 16:22:49 2009
New Revision: 752500

URL: http://svn.apache.org/viewvc?rev=752500&view=rev
Log:
Merge r 752495:752496 from trunk to branch-0.20. Fixes HADOOP-5307. 

Modified:
    hadoop/core/branches/branch-0.20/CHANGES.txt
    hadoop/core/branches/branch-0.20/src/core/org/apache/hadoop/util/StringUtils.java
    hadoop/core/branches/branch-0.20/src/test/org/apache/hadoop/util/TestStringUtils.java

Modified: hadoop/core/branches/branch-0.20/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.20/CHANGES.txt?rev=752500&r1=752499&r2=752500&view=diff
==============================================================================
--- hadoop/core/branches/branch-0.20/CHANGES.txt (original)
+++ hadoop/core/branches/branch-0.20/CHANGES.txt Wed Mar 11 16:22:49 2009
@@ -719,6 +719,9 @@
 
     HADOOP-5392. Fixes a problem to do with JT crashing during recovery when
     the job files are garbled. (Amar Kamat vi ddas)
+
+    HADOOP-5307. Fix null value handling in StringUtils#arrayToString() and 
+    #getStrings(). (enis)
  
 Release 0.19.1 - Unreleased
 

Modified: hadoop/core/branches/branch-0.20/src/core/org/apache/hadoop/util/StringUtils.java
URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.20/src/core/org/apache/hadoop/util/StringUtils.java?rev=752500&r1=752499&r2=752500&view=diff
==============================================================================
--- hadoop/core/branches/branch-0.20/src/core/org/apache/hadoop/util/StringUtils.java (original)
+++ hadoop/core/branches/branch-0.20/src/core/org/apache/hadoop/util/StringUtils.java Wed
Mar 11 16:22:49 2009
@@ -119,20 +119,24 @@
     return percentFormat.format(rounded / scale);
   }
   
+  private static final String NULL_STR_VALUE = "__null__";
+  
   /**
    * Given an array of strings, return a comma-separated list of its elements.
    * @param strs Array of strings
    * @return Empty string if strs.length is 0, comma separated list of strings
    * otherwise
    */
-  
   public static String arrayToString(String[] strs) {
     if (strs.length == 0) { return ""; }
     StringBuffer sbuf = new StringBuffer();
-    sbuf.append(strs[0]);
-    for (int idx = 1; idx < strs.length; idx++) {
-      sbuf.append(",");
-      sbuf.append(strs[idx]);
+    for (int idx = 0; idx < strs.length; idx++) {
+      if(idx != 0)
+        sbuf.append(",");
+      if(strs[idx] == null)
+        sbuf.append(NULL_STR_VALUE);
+      else
+        sbuf.append(strs[idx]);
     }
     return sbuf.toString();
   }
@@ -314,7 +318,11 @@
     StringTokenizer tokenizer = new StringTokenizer (str,",");
     values = new ArrayList<String>();
     while (tokenizer.hasMoreTokens()) {
-      values.add(tokenizer.nextToken());
+      String value = tokenizer.nextToken();
+      if(value.equals(NULL_STR_VALUE))
+        values.add(null);
+      else
+        values.add(value);
     }
     return values;
   }
@@ -551,6 +559,7 @@
       );
 
     Runtime.getRuntime().addShutdownHook(new Thread() {
+      @Override
       public void run() {
         LOG.info(toStartupShutdownString("SHUTDOWN_MSG: ", new String[]{
           "Shutting down " + classname + " at " + hostname}));

Modified: hadoop/core/branches/branch-0.20/src/test/org/apache/hadoop/util/TestStringUtils.java
URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.20/src/test/org/apache/hadoop/util/TestStringUtils.java?rev=752500&r1=752499&r2=752500&view=diff
==============================================================================
--- hadoop/core/branches/branch-0.20/src/test/org/apache/hadoop/util/TestStringUtils.java
(original)
+++ hadoop/core/branches/branch-0.20/src/test/org/apache/hadoop/util/TestStringUtils.java
Wed Mar 11 16:22:49 2009
@@ -118,4 +118,89 @@
     assertEquals(-1259520L, StringUtils.TraditionalBinaryPrefix.string2long("-1230k"));
     assertEquals(956703965184L, StringUtils.TraditionalBinaryPrefix.string2long("891g"));
   }
+  
+  private <T> void assertEquals(T[] expected, T[] actual) {
+    assertEquals(expected.length, actual.length);
+    for (int i = 0; i < expected.length; i++) {
+      assertEquals(expected[i], actual[i]);
+    }
+  }
+  
+  //internal to StringUtils
+  private static final String NULL_STR_VALUE = "__null__";
+  
+  public void testArrayToString() {
+    
+    //normal test
+    String[] arr1 = new String[] {"1", "2", "3", "4", "5", "6" }; 
+    String expected1 = "1,2,3,4,5,6";
+    assertEquals(expected1, StringUtils.arrayToString(arr1));
+    
+    //test with whitespace
+    String[] arr2 = new String[] {"1 ", "2 ", "3 ", "4 ", "5 ", "6 " }; 
+    String expected2 = "1 ,2 ,3 ,4 ,5 ,6 ";
+    assertEquals(expected2, StringUtils.arrayToString(arr2));
+    
+    //test with empty array
+    String[] emptyArr = new String[0];
+    assertEquals("", StringUtils.arrayToString(emptyArr));
+    
+    //test with null
+    try {
+      StringUtils.arrayToString(null);
+      fail("Should have thrown NPE");
+    }catch(NullPointerException ex) {
+    }
+    
+    //test with one element 
+    assertEquals("1", StringUtils.arrayToString(new String[] {"1"}));
+    assertEquals("", StringUtils.arrayToString(new String[] {""}));
+    assertEquals(" ", StringUtils.arrayToString(new String[] {" "}));
+    
+    
+    assertEquals(" ,1", StringUtils.arrayToString(new String[] {" ", "1"}));
+    
+    //test with null values
+    assertEquals(NULL_STR_VALUE, StringUtils.arrayToString(new String[] {null}));
+    assertEquals(NULL_STR_VALUE+",1", StringUtils.arrayToString(new String[] {null,"1"}));
+    assertEquals("1," + NULL_STR_VALUE, StringUtils.arrayToString(new String[] {"1",null}));
+    
+  }
+  
+  public void testArrayToStringToArray() {
+    
+    //normal test
+    String[] arr1 = new String[] {"1", "2", "3", "4", "5", "6" };
+    assertEquals(arr1, StringUtils.getStrings(StringUtils.arrayToString(arr1)));
+    
+    //test with whitespace
+    String[] arr2 = new String[] {"1 ", "2 ", "3 ", "4 ", "5 ", "6 " }; 
+    assertEquals(arr2, StringUtils.getStrings(StringUtils.arrayToString(arr2)));
+    
+    //test with empty array
+    String[] emptyArr = new String[0];
+    //actually returns null for empty array
+    assertNull(StringUtils.getStrings(StringUtils.arrayToString(emptyArr)));
+    
+    //test with one element 
+    String[] one = new String[] {"1"};
+    String[] empty = new String[] {""};
+    String[] ws = new String[] {" "};
+    assertEquals(one, StringUtils.getStrings(StringUtils.arrayToString(one)));
+    assertNull(StringUtils.getStrings(StringUtils.arrayToString(empty)));
+    assertEquals(ws, StringUtils.getStrings(StringUtils.arrayToString(ws)));
+    
+    String[] wsone = new String[] {" ", "1"};
+    assertEquals(wsone, StringUtils.getStrings(StringUtils.arrayToString(wsone)));
+    
+    //test with null values
+    String[] nullArr = new String[] {null};
+    String[] nullArr2 =  new String[] {null, "1"};
+    String[] nullArr3 =  new String[] {null, "null", null};
+    String[] nullArr4 =  new String[] {null, null, null};
+    assertEquals(nullArr, StringUtils.getStrings(StringUtils.arrayToString(nullArr)));
+    assertEquals(nullArr2, StringUtils.getStrings(StringUtils.arrayToString(nullArr2)));
+    assertEquals(nullArr4, StringUtils.getStrings(StringUtils.arrayToString(nullArr4)));
+  }
+  
 }



Mime
View raw message