From beee57f341133e875ad43c952d413f0691be8b98 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 10 Feb 2014 11:26:24 +0000
Subject: [PATCH] StaticUtils.java: Output throwable class names and message in the trace.

---
 opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java         |   21 +++++-----
 opendj-sdk/opendj-core/src/test/java/com/forgerock/opendj/util/StaticUtilsTestCase.java |   54 +++++++++++++++++++++-----
 2 files changed, 53 insertions(+), 22 deletions(-)

diff --git a/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index 7120e3b..03fe385 100644
--- a/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -1483,7 +1483,12 @@
             return;
         }
         if (isFullStack) {
-            buffer.append(throwable);
+            // add class name and message of the exception
+            buffer.append(throwable.getClass().getName());
+            final String message = throwable.getLocalizedMessage();
+            if (message != null && message.length() != 0) {
+                buffer.append(": ").append(message);
+            }
             // add first-level stack trace
             for (StackTraceElement e : throwable.getStackTrace()) {
                 buffer.append(" / ");
@@ -1509,16 +1514,10 @@
                 throwable = throwable.getCause();
             }
             // add class name and message of the exception
-            String message = throwable.getMessage();
-            if ((message == null) || (message.length() == 0)) {
-                String className = throwable.getClass().getName();
-                try {
-                    className = className.substring(className.lastIndexOf('.') + 1);
-                } catch (Exception e) { /* ignored */
-                }
-                buffer.append(className);
-            } else {
-                buffer.append(message);
+            buffer.append(throwable.getClass().getSimpleName());
+            final String message = throwable.getLocalizedMessage();
+            if (message != null && message.length() != 0) {
+                buffer.append(": ").append(message);
             }
             // add first 20 items of the first-level stack trace
             int i = 0;
diff --git a/opendj-sdk/opendj-core/src/test/java/com/forgerock/opendj/util/StaticUtilsTestCase.java b/opendj-sdk/opendj-core/src/test/java/com/forgerock/opendj/util/StaticUtilsTestCase.java
index 92db93d..44c16dd 100644
--- a/opendj-sdk/opendj-core/src/test/java/com/forgerock/opendj/util/StaticUtilsTestCase.java
+++ b/opendj-sdk/opendj-core/src/test/java/com/forgerock/opendj/util/StaticUtilsTestCase.java
@@ -199,39 +199,71 @@
 
     @DataProvider
     public Object[][] stackTraceToSingleLineLimitedStackProvider() {
+        final String noMessageTrace = "RuntimeException (StaticUtilsTestCase.java";
+        final String messageTrace = "RuntimeException: message (StaticUtilsTestCase.java";
         return new Object[][] {
-            { new InvocationTargetException(new RuntimeException("message")), "message (StaticUtilsTestCase.java" },
-            { new RuntimeException("message"), "message (StaticUtilsTestCase.java" },
-            { new RuntimeException(""), "RuntimeException (StaticUtilsTestCase.java" },
-            { new RuntimeException(), "RuntimeException (StaticUtilsTestCase.java" },
+            { null, "" },
+            { new RuntimeException(),   noMessageTrace },
+            { new RuntimeException(""), noMessageTrace },
+            { new RuntimeException("message"), messageTrace },
+            { new InvocationTargetException(new RuntimeException()),   noMessageTrace },
+            { new InvocationTargetException(new RuntimeException("")), noMessageTrace },
+            { new InvocationTargetException(new RuntimeException("message")), messageTrace },
+            {
+                new RuntimeException(new RuntimeException("message")),
+                "RuntimeException: java.lang.RuntimeException: message (StaticUtilsTestCase.java"
+            },
+            { new RuntimeException("message", new RuntimeException()), messageTrace },
+            { new RuntimeException("message", new RuntimeException("message")), messageTrace },
         };
     }
 
     @Test(dataProvider = "stackTraceToSingleLineLimitedStackProvider")
-    public void testStackTraceToSingleLineLimitedStack(Throwable t, String expectedStartWith) {
+    public void testStackTraceToSingleLineLimitedStack1(Throwable t, String expectedStartWith) {
         final String trace = stackTraceToSingleLineString(t, false);
         assertThat(trace).startsWith(expectedStartWith);
-        assertThat(trace).endsWith("...)");
+        if (t != null) {
+            assertThat(trace).endsWith("...)");
+        }
     }
 
     @DataProvider
     public Object[][] stackTraceToSingleLineFullStackStackProvider() {
         return new Object[][] {
+            { null, "", "" },
+            { new RuntimeException(),   "java.lang.RuntimeException / StaticUtilsTestCase.java:", "" },
+            { new RuntimeException(""), "java.lang.RuntimeException / StaticUtilsTestCase.java:", "" },
+            {
+                new RuntimeException("message"),
+                "java.lang.RuntimeException: message / StaticUtilsTestCase.java:", "message"
+            },
             {
                 new InvocationTargetException(new RuntimeException("message")),
                 "java.lang.reflect.InvocationTargetException / StaticUtilsTestCase.java:", "message"
             },
             {
-                new RuntimeException("message"),
-                "java.lang.RuntimeException: message / StaticUtilsTestCase.java:", "message"
+                new RuntimeException(new RuntimeException()),
+                "java.lang.RuntimeException: java.lang.RuntimeException / StaticUtilsTestCase.java:",
+                "java.lang.RuntimeException "
             },
-            { new RuntimeException(""), "java.lang.RuntimeException:  / StaticUtilsTestCase.java:", "" },
-            { new RuntimeException(),   "java.lang.RuntimeException / StaticUtilsTestCase.java:", "" },
+            {
+                new RuntimeException(new RuntimeException("message")),
+                "java.lang.RuntimeException: java.lang.RuntimeException: message / StaticUtilsTestCase.java:",
+                "java.lang.RuntimeException: message"
+            },
+            {
+                new RuntimeException("message", new RuntimeException()),
+                "java.lang.RuntimeException: message / StaticUtilsTestCase.java:", "java.lang.RuntimeException "
+            },
+            {
+                new RuntimeException("message", new RuntimeException("message")),
+                "java.lang.RuntimeException: message / StaticUtilsTestCase.java:", "java.lang.RuntimeException: message"
+            },
         };
     }
 
     @Test(dataProvider = "stackTraceToSingleLineFullStackStackProvider")
-    public void testStackTraceToSingleLineFullStack(Exception throwable, String expectedStartWith,
+    public void testStackTraceToSingleLineFullStack1(Exception throwable, String expectedStartWith,
             String expectedContains) {
         String trace = stackTraceToSingleLineString(throwable, true);
         assertThat(trace).startsWith(expectedStartWith);

--
Gitblit v1.10.0