From 1fe5439baf32af2114958e3cfc3512bd72c84773 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Wed, 13 Feb 2008 05:44:46 +0000 Subject: [PATCH] Applying my patch from LANG-412; fixing Peter Oxenham's report that the appendFixedWidthPadRight and appendFixedWidthPadLeft are not null safe if the nullText has not been set git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@627248 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/text/StrBuilder.java | 6 ++++++ .../apache/commons/lang/text/StrBuilderTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/java/org/apache/commons/lang/text/StrBuilder.java b/src/java/org/apache/commons/lang/text/StrBuilder.java index 13281cefe..fd135fd11 100644 --- a/src/java/org/apache/commons/lang/text/StrBuilder.java +++ b/src/java/org/apache/commons/lang/text/StrBuilder.java @@ -1183,6 +1183,9 @@ public class StrBuilder implements Cloneable { if (width > 0) { ensureCapacity(size + width); String str = (obj == null ? getNullText() : obj.toString()); + if (str == null) { + str = ""; + } int strLen = str.length(); if (strLen >= width) { str.getChars(strLen - width, strLen, buffer, size); @@ -1227,6 +1230,9 @@ public class StrBuilder implements Cloneable { if (width > 0) { ensureCapacity(size + width); String str = (obj == null ? getNullText() : obj.toString()); + if (str == null) { + str = ""; + } int strLen = str.length(); if (strLen >= width) { str.getChars(0, width, buffer, size); diff --git a/src/test/org/apache/commons/lang/text/StrBuilderTest.java b/src/test/org/apache/commons/lang/text/StrBuilderTest.java index 67e1ce1df..d58930805 100644 --- a/src/test/org/apache/commons/lang/text/StrBuilderTest.java +++ b/src/test/org/apache/commons/lang/text/StrBuilderTest.java @@ -1749,4 +1749,17 @@ public class StrBuilderTest extends TestCase { assertEquals( "The indexOf(char) method is looking beyond the end of the string", -1, sb.indexOf('h')); } + //----------------------------------------------------------------------- + public void testLang412Right() { + StrBuilder sb = new StrBuilder(); + sb.appendFixedWidthPadRight(null, 10, '*'); + assertEquals( "Failed to invoke appendFixedWidthPadRight correctly", "**********", sb.toString()); + } + + public void testLang412Left() { + StrBuilder sb = new StrBuilder(); + sb.appendFixedWidthPadLeft(null, 10, '*'); + assertEquals( "Failed to invoke appendFixedWidthPadLeft correctly", "**********", sb.toString()); + } + }