From 2fb0906dda8369cdfcd1fd858808d13912ec7c1e Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 30 Jan 2002 18:21:15 +0000 Subject: [PATCH] Made joinEscaped() handle null array elements by converting them to the empty string. git-svn-id: https://samskivert.googlecode.com/svn/trunk@550 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/util/StringUtil.java | 8 +++++--- .../src/java/com/samskivert/util/StringUtilTest.java | 7 ++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 8ed19e21..ea2dac03 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -1,5 +1,5 @@ // -// $Id: StringUtil.java,v 1.19 2002/01/30 18:11:27 mdb Exp $ +// $Id: StringUtil.java,v 1.20 2002/01/30 18:21:14 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -355,7 +355,8 @@ public class StringUtil * Joins an array of strings into a single string, separated by * commas, and escaping commas that occur in the individual string * values such that a subsequent call to {@link #parseStringArray} - * would recreated the string array properly. + * would recreated the string array properly. Any elements in the + * values array that are null will be treated as an empty string. */ public static String joinEscaped (String[] values) { @@ -365,7 +366,8 @@ public class StringUtil if (i > 0) { buf.append(", "); } - buf.append(replace(values[i], ",", ",,")); + String value = (values[i] == null) ? "" : values[i]; + buf.append(replace(value, ",", ",,")); } return buf.toString(); } diff --git a/projects/samskivert/tests/src/java/com/samskivert/util/StringUtilTest.java b/projects/samskivert/tests/src/java/com/samskivert/util/StringUtilTest.java index 53ef902d..d83a80e6 100644 --- a/projects/samskivert/tests/src/java/com/samskivert/util/StringUtilTest.java +++ b/projects/samskivert/tests/src/java/com/samskivert/util/StringUtilTest.java @@ -1,5 +1,5 @@ // -// $Id: StringUtilTest.java,v 1.1 2002/01/30 18:11:27 mdb Exp $ +// $Id: StringUtilTest.java,v 1.2 2002/01/30 18:21:15 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -43,6 +43,11 @@ public class StringUtilTest extends TestCase // now join them back together String joined = StringUtil.joinEscaped(tokens); assert("joined.equals(source)", joined.equals(source)); + + // make sure null to empty string works + tokens = new String[] { "this", null, "is", null, "a", null, "test" }; + joined = StringUtil.joinEscaped(tokens); + assert("null elements work", joined.equals("this, , is, , a, , test")); } public static Test suite ()