UnpackThrowableFilter extends AbstractThrowableFilter { @Override public ThrowableData filter(final ThrowableData origin) { ThrowableData cause = origin.getCause(); if (cause != null) { List<String> classNames = getArguments(); if (classNames.isEmpty()) { return filter(cause); } for (String className : classNames) { if (className.equals(origin.getClassName())) { return filter(cause); } } } return origin; } UnpackThrowableFilter();  UnpackThrowableFilter(final String arguments); @Override ThrowableData filter(final ThrowableData origin);  }
@Test public void unpackSingleCause() { NullPointerException childException = new NullPointerException("Hello World!"); RuntimeException parentException = new RuntimeException("Hello Heaven!", childException); ThrowableFilter filter = new UnpackThrowableFilter(RuntimeException.class.getName()); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(NullPointerException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello World!"); assertThat(data.getStackTrace()).containsExactly(childException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void unpackNestedCause() { NullPointerException grandChildException = new NullPointerException("Hello Hell!"); RuntimeException childException = new RuntimeException("Hello World!", grandChildException); RuntimeException parentException = new RuntimeException("Hello Heaven!", childException); ThrowableFilter filter = new UnpackThrowableFilter(RuntimeException.class.getName()); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(NullPointerException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello Hell!"); assertThat(data.getStackTrace()).containsExactly(grandChildException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void unpackMultipleExceptions() { NullPointerException childException = new NullPointerException("Hello World!"); IOException parentException = new IOException("Hello Heaven!", childException); ThrowableFilter filter = new UnpackThrowableFilter(RuntimeException.class.getName() + " | " + IOException.class.getName()); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(NullPointerException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello World!"); assertThat(data.getStackTrace()).containsExactly(childException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void unpackAll() { NullPointerException childException = new NullPointerException("Hello World!"); RuntimeException parentException = new RuntimeException("Hello Heaven!", childException); ThrowableFilter filter = new UnpackThrowableFilter(); ThrowableData data = filter.filter(new ThrowableWrapper(parentException)); assertThat(data.getClassName()).isEqualTo(NullPointerException.class.getName()); assertThat(data.getMessage()).isEqualTo("Hello World!"); assertThat(data.getStackTrace()).containsExactly(childException.getStackTrace()); assertThat(data.getCause()).isNull(); }
@Test public void missingCause() { RuntimeException exception = new RuntimeException("Hello World!"); ThrowableFilter filter = new UnpackThrowableFilter(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 World!"); IOException parentException = new IOException("Hello Heaven!", childException); ThrowableFilter filter = new UnpackThrowableFilter(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 World!"); assertThat(causeData.getStackTrace()).containsExactly(childException.getStackTrace()); assertThat(causeData.getCause()).isNull(); }