ExceptionToken implements Token { @Override public void render(final LogEntry logEntry, final StringBuilder builder) { Throwable throwable = logEntry.getException(); if (throwable != null) { render(filter(throwable), Collections.<StackTraceElement>emptyList(), builder); } } ExceptionToken(final List<ThrowableFilter> filters); @Override Collection<LogEntryValue> getRequiredLogEntryValues(); @Override void render(final LogEntry logEntry, final StringBuilder builder); @Override void apply(final LogEntry logEntry, final PreparedStatement statement, final int index);  }
@Test public void renderLogEntryWithoutException() { ExceptionToken token = new ExceptionToken(Collections.emptyList()); assertThat(render(token, null)).isEmpty(); }
@Test public void renderExceptionWithoutDescription() { Exception exception = new UnsupportedOperationException(); ExceptionToken token = new ExceptionToken(Collections.emptyList()); assertThat(render(token, exception)) .startsWith(UnsupportedOperationException.class.getName()) .contains(ExceptionTokenTest.class.getName(), "renderExceptionWithoutDescription") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void renderExceptionWithDescription() { Exception exception = new NullPointerException("my message"); ExceptionToken token = new ExceptionToken(Collections.emptyList()); assertThat(render(token, exception)) .startsWith(NullPointerException.class.getName() + ": my message") .contains(ExceptionTokenTest.class.getName(), "renderExceptionWithDescription") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void renderExceptionWithCause() { Exception cause = new IOException("File not found"); Exception exception = new RuntimeException(cause); ExceptionToken token = new ExceptionToken(Collections.emptyList()); assertThat(render(token, exception)) .startsWith(RuntimeException.class.getName()) .contains(IOException.class.getName() + ": File not found") .contains(ExceptionTokenTest.class.getName(), "renderExceptionWithCause") .contains("... " + (cause.getStackTrace().length - 1) + " more") .hasLineCount(exception.getStackTrace().length + 4); }
@Test public void renderyExceptionUsingFilters() { Exception exception = new RuntimeException("Test"); List<ThrowableFilter> filters = Arrays.asList( origin -> new ThrowableStore(origin.getClassName(), origin.getMessage() + "1", origin.getStackTrace(), origin.getCause()), origin -> new ThrowableStore(origin.getClassName(), origin.getMessage() + "2", origin.getStackTrace(), origin.getCause()) ); ExceptionToken token = new ExceptionToken(filters); assertThat(render(token, exception)).startsWith(RuntimeException.class.getName() + ": Test12"); }