ExceptionToken implements Token { @Override public void apply(final LogEntry logEntry, final PreparedStatement statement, final int index) throws SQLException { Throwable throwable = logEntry.getException(); if (throwable == null) { statement.setString(index, null); } else { StringBuilder builder = new StringBuilder(); render(filter(throwable), Collections.<StackTraceElement>emptyList(), builder); statement.setString(index, builder.toString()); } } 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 applyLogEntryWithoutException() throws SQLException { ExceptionToken token = new ExceptionToken(Collections.emptyList()); PreparedStatement statement = mock(PreparedStatement.class); token.apply(LogEntryBuilder.empty().create(), statement, 1); verify(statement).setString(1, null); }
@Test public void applyExceptionWithoutDescription() throws SQLException { Exception exception = new UnsupportedOperationException(); PreparedStatement statement = mock(PreparedStatement.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); new ExceptionToken(Collections.emptyList()).apply(createLogEntry(exception), statement, 1); verify(statement).setString(eq(1), captor.capture()); assertThat(captor.getValue()) .startsWith(UnsupportedOperationException.class.getName()) .contains(ExceptionTokenTest.class.getName(), "applyExceptionWithoutDescription") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void applyExceptionWithDescription() throws SQLException { Exception exception = new NullPointerException("my message"); PreparedStatement statement = mock(PreparedStatement.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); new ExceptionToken(Collections.emptyList()).apply(createLogEntry(exception), statement, 1); verify(statement).setString(eq(1), captor.capture()); assertThat(captor.getValue()) .startsWith(NullPointerException.class.getName() + ": my message") .contains(ExceptionTokenTest.class.getName(), "applyExceptionWithDescription") .hasLineCount(exception.getStackTrace().length + 1); }
@Test public void applyExceptionWithCause() throws SQLException { Exception cause = new IOException("File not found"); Exception exception = new RuntimeException(cause); PreparedStatement statement = mock(PreparedStatement.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); new ExceptionToken(Collections.emptyList()).apply(createLogEntry(exception), statement, 1); verify(statement).setString(eq(1), captor.capture()); assertThat(captor.getValue()) .startsWith(RuntimeException.class.getName()) .contains(IOException.class.getName() + ": File not found") .contains(ExceptionTokenTest.class.getName(), "applyExceptionWithCause") .contains("... " + (cause.getStackTrace().length - 1) + " more") .hasLineCount(exception.getStackTrace().length + 4); }
@Test public void applyyExceptionUsingFilters() throws SQLException { 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()) ); PreparedStatement statement = mock(PreparedStatement.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); new ExceptionToken(filters).apply(createLogEntry(exception), statement, 1); verify(statement).setString(eq(1), captor.capture()); assertThat(captor.getValue()).startsWith(RuntimeException.class.getName() + ": Test12"); }