From 56550bf2779990da78d2bac1d8287c8a693c6e4b Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Mon, 14 Dec 2009 07:51:05 +0000 Subject: [PATCH] Applying fix for LANG-552. StringUtils.replaceEach(String, String[], String[]) no longer NPEs when null appears in the last String[] git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@890212 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/commons/lang3/StringUtils.java | 3 +++ src/test/org/apache/commons/lang3/StringUtilsTest.java | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/java/org/apache/commons/lang3/StringUtils.java b/src/java/org/apache/commons/lang3/StringUtils.java index 14563aa66..f6cabeed8 100644 --- a/src/java/org/apache/commons/lang3/StringUtils.java +++ b/src/java/org/apache/commons/lang3/StringUtils.java @@ -3673,6 +3673,9 @@ public class StringUtils { // count the replacement text elements that are larger than their corresponding text being replaced for (int i = 0; i < searchList.length; i++) { + if (searchList[i] == null || replacementList[i] == null) { + continue; + } int greater = replacementList[i].length() - searchList[i].length(); if (greater > 0) { increase += 3 * greater; // assume 3 matches diff --git a/src/test/org/apache/commons/lang3/StringUtilsTest.java b/src/test/org/apache/commons/lang3/StringUtilsTest.java index bb0acf831..7a74598a6 100644 --- a/src/test/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang3/StringUtilsTest.java @@ -1033,6 +1033,10 @@ public class StringUtilsTest extends TestCase { "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "5", "6", "7", "8", "9", "1", "2", "3", "4"})); + + // Test null safety inside arrays - LANG-552 + assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{null}),"aba"); + assertEquals(StringUtils.replaceEach("aba", new String[]{"a", "b"}, new String[]{"c", null}),"cbc"); } /**