MessageAndExceptionToken implements Token { @Override public void render(final LogEntry logEntry, final StringBuilder builder) { messageToken.render(logEntry, builder); if (logEntry.getException() != null) { if (logEntry.getMessage() != null) { builder.append(": "); } exceptionToken.render(logEntry, builder); } } MessageAndExceptionToken(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 renderNeitherMessageNorException() { MessageAndExceptionToken token = new MessageAndExceptionToken(Collections.emptyList()); assertThat(render(token, null, null)).isEmpty(); }
@Test public void renderMessageOnly() { MessageAndExceptionToken token = new MessageAndExceptionToken(Collections.emptyList()); assertThat(render(token, "Hello World!", null)).isEqualTo("Hello World!"); }
@Test public void renderExceptionOnly() { Exception exception = new UnsupportedOperationException(); MessageAndExceptionToken token = new MessageAndExceptionToken(Collections.emptyList()); assertThat(render(token, null, exception)) .startsWith(UnsupportedOperationException.class.getName()) .contains(MessageAndExceptionTokenTest.class.getName(), "renderExceptionOnly") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void renderMessageAndException() { Exception exception = new UnsupportedOperationException(); MessageAndExceptionToken token = new MessageAndExceptionToken(Collections.emptyList()); assertThat(render(token, "Hello World!", exception)) .startsWith("Hello World!") .contains(UnsupportedOperationException.class.getName()) .contains(MessageAndExceptionTokenTest.class.getName(), "renderMessageAndException") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void renderExceptionUsingFilters() { Exception exception = new RuntimeException("Test"); List<ThrowableFilter> filters = Collections.singletonList( origin -> new ThrowableStore(origin.getClassName(), origin.getMessage() + "42", origin.getStackTrace(), origin.getCause()) ); MessageAndExceptionToken token = new MessageAndExceptionToken(filters); assertThat(render(token, null, exception)).startsWith(RuntimeException.class.getName() + ": Test42"); }