Return-Path: Delivered-To: apmail-hadoop-core-commits-archive@www.apache.org Received: (qmail 45045 invoked from network); 11 Mar 2009 16:25:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 11 Mar 2009 16:25:31 -0000 Received: (qmail 2134 invoked by uid 500); 11 Mar 2009 16:25:30 -0000 Delivered-To: apmail-hadoop-core-commits-archive@hadoop.apache.org Received: (qmail 2089 invoked by uid 500); 11 Mar 2009 16:25:30 -0000 Mailing-List: contact core-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: core-dev@hadoop.apache.org Delivered-To: mailing list core-commits@hadoop.apache.org Received: (qmail 2075 invoked by uid 99); 11 Mar 2009 16:25:30 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Mar 2009 09:25:30 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Mar 2009 16:25:28 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E3878238889F; Wed, 11 Mar 2009 16:25:06 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r752501 - in /hadoop/core/branches/branch-0.19: CHANGES.txt src/core/org/apache/hadoop/util/StringUtils.java src/test/org/apache/hadoop/util/TestStringUtils.java Date: Wed, 11 Mar 2009 16:25:06 -0000 To: core-commits@hadoop.apache.org From: enis@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090311162506.E3878238889F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: enis Date: Wed Mar 11 16:25:06 2009 New Revision: 752501 URL: http://svn.apache.org/viewvc?rev=752501&view=rev Log: Merge r 752495:752496 from trunk to branch-0.19. Fixes HADOOP-5307. Modified: hadoop/core/branches/branch-0.19/CHANGES.txt hadoop/core/branches/branch-0.19/src/core/org/apache/hadoop/util/StringUtils.java hadoop/core/branches/branch-0.19/src/test/org/apache/hadoop/util/TestStringUtils.java Modified: hadoop/core/branches/branch-0.19/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.19/CHANGES.txt?rev=752501&r1=752500&r2=752501&view=diff ============================================================================== --- hadoop/core/branches/branch-0.19/CHANGES.txt (original) +++ hadoop/core/branches/branch-0.19/CHANGES.txt Wed Mar 11 16:25:06 2009 @@ -51,6 +51,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) HADOOP-5421. Removes the test TestRecoveryManager.java from the 0.19 branch as it has compilation issues. (ddas) Modified: hadoop/core/branches/branch-0.19/src/core/org/apache/hadoop/util/StringUtils.java URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.19/src/core/org/apache/hadoop/util/StringUtils.java?rev=752501&r1=752500&r2=752501&view=diff ============================================================================== --- hadoop/core/branches/branch-0.19/src/core/org/apache/hadoop/util/StringUtils.java (original) +++ hadoop/core/branches/branch-0.19/src/core/org/apache/hadoop/util/StringUtils.java Wed Mar 11 16:25:06 2009 @@ -110,20 +110,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(); } @@ -305,7 +309,11 @@ StringTokenizer tokenizer = new StringTokenizer (str,","); values = new ArrayList(); 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; } @@ -542,6 +550,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.19/src/test/org/apache/hadoop/util/TestStringUtils.java URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.19/src/test/org/apache/hadoop/util/TestStringUtils.java?rev=752501&r1=752500&r2=752501&view=diff ============================================================================== --- hadoop/core/branches/branch-0.19/src/test/org/apache/hadoop/util/TestStringUtils.java (original) +++ hadoop/core/branches/branch-0.19/src/test/org/apache/hadoop/util/TestStringUtils.java Wed Mar 11 16:25:06 2009 @@ -118,4 +118,89 @@ assertEquals(-1259520L, StringUtils.TraditionalBinaryPrefix.string2long("-1230k")); assertEquals(956703965184L, StringUtils.TraditionalBinaryPrefix.string2long("891g")); } + + private 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))); + } + }