commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r1142381 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/StringUtils.java test/java/org/apache/commons/lang3/StringUtilsTest.java
Date Sun, 03 Jul 2011 07:04:38 GMT
Author: bayard
Date: Sun Jul  3 07:04:38 2011
New Revision: 1142381

URL: http://svn.apache.org/viewvc?rev=1142381&view=rev
Log:
Applying Ilya's patch from LANG-703 fixing an NPE when toString returns null

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1142381&r1=1142380&r2=1142381&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Sun
Jul  3 07:04:38 2011
@@ -3290,13 +3290,12 @@ public class StringUtils {
         if (array == null) {
             return null;
         }
-        int bufSize = (endIndex - startIndex);
-        if (bufSize <= 0) {
+        int noOfItems = (endIndex - startIndex);
+        if (noOfItems <= 0) {
             return EMPTY;
         }
-
-        bufSize *= ((array[startIndex] == null ? 16 : array[startIndex].toString().length())
+ 1);
-        StringBuilder buf = new StringBuilder(bufSize);
+        
+        StringBuilder buf = new StringBuilder(noOfItems * 16);
 
         for (int i = startIndex; i < endIndex; i++) {
             if (i > startIndex) {
@@ -3309,7 +3308,6 @@ public class StringUtils {
         return buf.toString();
     }
 
-
     /**
      * <p>Joins the elements of the provided array into a single String
      * containing the provided list of elements.</p>
@@ -3377,15 +3375,12 @@ public class StringUtils {
 
         // endIndex - startIndex > 0:   Len = NofStrings *(len(firstString) + len(separator))
         //           (Assuming that all Strings are roughly equally long)
-        int bufSize = (endIndex - startIndex);
-        if (bufSize <= 0) {
+        int noOfItems = (endIndex - startIndex);
+        if (noOfItems <= 0) {
             return EMPTY;
         }
 
-        bufSize *= ((array[startIndex] == null ? 16 : array[startIndex].toString().length())
-                        + separator.length());
-
-        StringBuilder buf = new StringBuilder(bufSize);
+        StringBuilder buf = new StringBuilder(noOfItems * 16);
 
         for (int i = startIndex; i < endIndex; i++) {
             if (i > startIndex) {

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=1142381&r1=1142380&r2=1142381&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Sun Jul  3 07:04:38 2011
@@ -67,6 +67,14 @@ public class StringUtilsTest extends Tes
     private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
     private static final String[] EMPTY_ARRAY_LIST = {};
     private static final String[] NULL_ARRAY_LIST = {null};
+    private static final Object[] NULL_TO_STRING_LIST = {
+    	new Object(){
+    		@Override
+    		public String toString() {
+    			return null;
+    		}
+    	}
+    };
     private static final String[] MIXED_ARRAY_LIST = {null, "", "foo"};
     private static final Object[] MIXED_TYPE_LIST = {"foo", Long.valueOf(2L)};
 
@@ -182,6 +190,7 @@ public class StringUtilsTest extends Tes
 
         assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST));
         assertEquals("", StringUtils.join(NULL_ARRAY_LIST));
+        assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST));
         assertEquals("abc", StringUtils.join(new String[] {"a", "b", "c"}));
         assertEquals("a", StringUtils.join(new String[] {null, "a", ""}));
         assertEquals("foo", StringUtils.join(MIXED_ARRAY_LIST));
@@ -197,6 +206,7 @@ public class StringUtilsTest extends Tes
 
         assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, '/', 0, MIXED_ARRAY_LIST.length-1));
         assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 1));
+        assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST,'/', 0, 1));
         assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 2));
         assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, '/', 1, 2));
         assertEquals("", StringUtils.join(MIXED_TYPE_LIST, '/', 2, 1));



Mime
View raw message