Applying Ilya's patch from LANG-703 fixing an NPE when toString returns null

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1142381 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2011-07-03 07:04:38 +00:00
parent f5cb67acd9
commit a80f11cf9d
2 changed files with 17 additions and 12 deletions

View File

@ -3290,13 +3290,12 @@ public static String join(Object[] array, char separator, int startIndex, int en
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 static String join(Object[] array, char separator, int startIndex, int en
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 static String join(Object[] array, String separator, int startIndex, int
// 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) {

View File

@ -67,6 +67,14 @@ public class StringUtilsTest extends TestCase {
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 void testJoin_Objectarray() {
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 void testJoin_ArrayChar() {
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));