Author: mreutegg
Date: Wed Sep 21 08:40:19 2016
New Revision: 1761696

URL: http://svn.apache.org/viewvc?rev=1761696&view=rev
Log:
OAK-4830: StringUtils.estimateMemoryUsage() can throw NullPointerException

Applied patch by Matt Ryan

Modified:
    
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java
    
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java

Modified: 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java?rev=1761696&r1=1761695&r2=1761696&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java
 Wed Sep 21 08:40:19 2016
@@ -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;
     }
 }

Modified: 
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java?rev=1761696&r1=1761695&r2=1761696&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java
 Wed Sep 21 08:40:19 2016
@@ -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 Tes
         }
     }
 
+    public void testEstimateMemoryUsage() {
+        final Map<String, Integer> 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<String, Integer> e : testStrings.entrySet()) {
+            assertEquals(e.getValue().intValue(), 
StringUtils.estimateMemoryUsage(e.getKey()));
+        }
+    }
+
 }


Reply via email to