diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java index 2786e5a..f7a8cfb 100644 --- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java +++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java @@ -87,6 +87,6 @@ public class StringUtils { * @return the estimated memory usage. */ public static int estimateMemoryUsage(String s) { - return 48 + s.length() * 2; + return s == null ? 0 : 48 + s.length() * 2; } } diff --git a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java index 2c85c21..5075045 100644 --- a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java +++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java @@ -16,8 +16,11 @@ */ package org.apache.jackrabbit.oak.commons; +import com.google.common.collect.Maps; import junit.framework.TestCase; +import java.util.Map; + /** * Test the string utilities. */ @@ -41,4 +44,16 @@ public class StringUtilsTest extends TestCase { } } + public void testEstimateMemoryUsage() { + final Map testStrings = Maps.newHashMap(); + testStrings.put(null, 0); + testStrings.put("", 48); + testStrings.put("a", 50); + testStrings.put("short string", 72); + testStrings.put("a much longer string than the one named 'short string'", 156); + for (final Map.Entry e : testStrings.entrySet()) { + assertEquals(e.getValue().intValue(), StringUtils.estimateMemoryUsage(e.getKey())); + } + } + }