MessageAndExceptionToken implements Token { @Override public void apply(final LogEntry logEntry, final PreparedStatement statement, final int index) throws SQLException { if (logEntry.getException() == null) { statement.setString(index, logEntry.getMessage()); } else { StringBuilder builder = new StringBuilder(); render(logEntry, builder); statement.setString(index, builder.toString()); } } 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 applyNeitherMessageNorException() throws SQLException { MessageAndExceptionToken token = new MessageAndExceptionToken(Collections.emptyList()); PreparedStatement statement = mock(PreparedStatement.class); token.apply(createLogEntry(null, null), statement, 1); verify(statement).setString(1, null); }
@Test public void applyMessageOnly() throws SQLException { MessageAndExceptionToken token = new MessageAndExceptionToken(Collections.emptyList()); PreparedStatement statement = mock(PreparedStatement.class); token.apply(createLogEntry("Hello World!", null), statement, 1); verify(statement).setString(1, "Hello World!"); }
@Test public void applyExceptionOnly() throws SQLException { Exception exception = new UnsupportedOperationException(); PreparedStatement statement = mock(PreparedStatement.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); new MessageAndExceptionToken(Collections.emptyList()).apply(createLogEntry(null, exception), statement, 1); verify(statement).setString(eq(1), captor.capture()); assertThat(captor.getValue()) .startsWith(UnsupportedOperationException.class.getName()) .contains(MessageAndExceptionTokenTest.class.getName(), "applyExceptionOnly") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void applyMessageAndException() throws SQLException { Exception exception = new UnsupportedOperationException(); PreparedStatement statement = mock(PreparedStatement.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); new MessageAndExceptionToken(Collections.emptyList()).apply(createLogEntry("Hello World!", exception), statement, 1); verify(statement).setString(eq(1), captor.capture()); assertThat(captor.getValue()) .startsWith("Hello World!") .contains(UnsupportedOperationException.class.getName()) .contains(MessageAndExceptionTokenTest.class.getName(), "applyMessageAndException") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void applyExceptionUsingFilters() throws SQLException { Exception exception = new RuntimeException("Test"); List<ThrowableFilter> filters = Collections.singletonList( origin -> new ThrowableStore(origin.getClassName(), origin.getMessage() + "42", origin.getStackTrace(), origin.getCause()) ); PreparedStatement statement = mock(PreparedStatement.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); new MessageAndExceptionToken(filters).apply(createLogEntry(null, exception), statement, 1); verify(statement).setString(eq(1), captor.capture()); assertThat(captor.getValue()).startsWith(RuntimeException.class.getName() + ": Test42"); }