mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
10.26.2014 36d32f4c95f818f76c6dfc6353cd20fd9fa24cb2
StaticUtils.java:
Output throwable class names and message in the trace.

StaticUtilsTestCase.java:
Added more tests + fixed the existing ones.
2 files modified
73 ■■■■ changed files
opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java 21 ●●●● patch | view | raw | blame | history
opendj-core/src/test/java/com/forgerock/opendj/util/StaticUtilsTestCase.java 52 ●●●● patch | view | raw | blame | history
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;
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);
        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);