DropCauseThrowableFilter extends AbstractThrowableFilter { @Override public ThrowableData filter(final ThrowableData origin) { if (getArguments().isEmpty()) { return new ThrowableStore(origin.getClassName(), origin.getMessage(), origin.getStackTrace(), null); } else { String className = origin.getClassName(); for (String filter : getArguments()) { if (className.equals(filter)) { return new ThrowableStore(origin.getClassName(), origin.getMessage(), origin.getStackTrace(), null); } } return origin; } } DropCauseThrowableFilter();  DropCauseThrowableFilter(final String arguments); @Override ThrowableData filter(final ThrowableData origin);  }
@Test public void dropSingleCause() { NullPointerException childException = new NullPointerException("Hello Hell!"); RuntimeException parentException = new RuntimeException("Hello Heaven!", childException); ThrowableFilter filter = new DropCauseThrowableFilter(RuntimeException.class.getName()); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(RuntimeException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello Heaven!"); assertThat(data.getStackTrace()).containsExactly(parentException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void dropNestedCause() { NullPointerException grandChildException = new NullPointerException("Hello Hell!"); RuntimeException childException = new RuntimeException("Hello World!", grandChildException); RuntimeException parentException = new RuntimeException("Hello Heaven!", childException); ThrowableFilter filter = new DropCauseThrowableFilter(RuntimeException.class.getName()); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(RuntimeException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello Heaven!"); assertThat(data.getStackTrace()).containsExactly(parentException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void dropMultipleExceptions() { NullPointerException childException = new NullPointerException("Hello Hell!"); IOException parentException = new IOException("Hello Heaven!", childException); ThrowableFilter filter = new DropCauseThrowableFilter(RuntimeException.class.getName() + " | " + IOException.class.getName()); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(IOException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello Heaven!"); assertThat(data.getStackTrace()).containsExactly(parentException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void dropAll() { NullPointerException childException = new NullPointerException("Hello Hell!"); RuntimeException parentException = new RuntimeException("Hello Heaven!", childException); ThrowableFilter filter = new DropCauseThrowableFilter(); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(RuntimeException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello Heaven!"); assertThat(data.getStackTrace()).containsExactly(parentException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void missingCause() { RuntimeException exception = new RuntimeException("Hello World!"); ThrowableFilter filter = new DropCauseThrowableFilter(RuntimeException.class.getName()); ThrowableData data = filter.filter(new ThrowableWrapper(exception)); assertThat(data.getClassName()).isEqualTo(RuntimeException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello World!"); assertThat(data.getStackTrace()).containsExactly(exception.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void otherException() { NullPointerException childException = new NullPointerException("Hello Hell!"); IOException parentException = new IOException("Hello Heaven!", childException); ThrowableFilter filter = new DropCauseThrowableFilter(RuntimeException.class.getName()); ThrowableData parentData = filter.filter(new ThrowableWrapper(parentException)); assertThat(parentData.getClassName()).isEqualTo(IOException.class.getName()); assertThat(parentData.getMessage()).isEqualTo("Hello Heaven!"); assertThat(parentData.getStackTrace()).containsExactly(parentException.getStackTrace()); assertThat(parentData.getCause()).isNotNull(); ThrowableData causeData = parentData.getCause(); assertThat(causeData.getClassName()).isEqualTo(NullPointerException.class.getName()); assertThat(causeData.getMessage()).isEqualTo("Hello Hell!"); assertThat(causeData.getStackTrace()).containsExactly(childException.getStackTrace()); assertThat(causeData.getCause()).isNull(); }