Throwables2 { @SuppressWarnings("unchecked") public static void propagateIfPossible(Throwable exception, Iterable<TypeToken<? extends Throwable>> throwables) throws Throwable { for (TypeToken<? extends Throwable> type : throwables) { Throwable throwable = Throwables2.getFirstThrowableOfType(exception, (Class<Throwable>) type.getRawType()); if (throwable != null) { throw throwable; } } for (Class<Exception> propagatableExceptionType : new Class[] { IllegalStateException.class, AssertionError.class, UnsupportedOperationException.class, IllegalArgumentException.class, AuthorizationException.class, ResourceNotFoundException.class, InsufficientResourcesException.class, HttpResponseException.class }) { Throwable throwable = Throwables2.getFirstThrowableOfType(exception, propagatableExceptionType); if (throwable != null) { throw throwable; } } }  static Predicate<Throwable> containsThrowable(final Class<T> throwableType); @SuppressWarnings("unchecked") static T getFirstThrowableOfType(Throwable from, Class<T> clazz); static T propagateAuthorizationOrOriginalException(Exception e); @SuppressWarnings("unchecked") static void propagateIfPossible(Throwable exception, Iterable<TypeToken<? extends Throwable>> throwables);  }
@Test(expectedExceptions = TestException.class) public void testPropagateExceptionThatsInList() throws Throwable { Exception e = new TestException(); propagateIfPossible(e, ImmutableSet.<TypeToken<? extends Throwable>> of(typeToken(TestException.class))); }
@Test(expectedExceptions = TestException.class) public void testPropagateWrappedExceptionThatsInList() throws Throwable { Exception e = new TestException(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of(typeToken(TestException.class))); }
@Test(expectedExceptions = IllegalStateException.class) public void testPropagateStandardExceptionIllegalStateException() throws Throwable { Exception e = new IllegalStateException(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testPropagateStandardExceptionIllegalArgumentException() throws Throwable { Exception e = new IllegalArgumentException(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testPropagateStandardExceptionUnsupportedOperationException() throws Throwable { Exception e = new UnsupportedOperationException(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = AssertionError.class) public void testPropagateStandardExceptionAssertionError() throws Throwable { AssertionError e = new AssertionError(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = AuthorizationException.class) public void testPropagateStandardExceptionAuthorizationException() throws Throwable { Exception e = new AuthorizationException(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = AuthorizationException.class) public void testPropagateProvisionExceptionAuthorizationException() throws Throwable { Exception e = new AuthorizationException(); propagateIfPossible( new ProvisionException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e))), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = AuthorizationException.class) public void testPropagateCreationExceptionAuthorizationException() throws Throwable { Exception e = new AuthorizationException(); propagateIfPossible( new CreationException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e))), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = InsufficientResourcesException.class) public void testPropagateStandardExceptionInsufficientResourcesException() throws Throwable { Exception e = new InsufficientResourcesException(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testPropagateStandardExceptionResourceNotFoundException() throws Throwable { Exception e = new ResourceNotFoundException(); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = IllegalStateException.class) public void testPropagateStandardExceptionIllegalStateExceptionNestedInHttpResponseException() throws Throwable { Exception e = new IllegalStateException(); propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testPropagateStandardExceptionIllegalArgumentExceptionNestedInHttpResponseException() throws Throwable { Exception e = new IllegalArgumentException(); propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testPropagateStandardExceptionUnsupportedOperationExceptionNestedInHttpResponseException() throws Throwable { Exception e = new UnsupportedOperationException(); propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = AuthorizationException.class) public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Throwable { Exception e = new AuthorizationException(); propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testPropagateStandardExceptionResourceNotFoundExceptionNestedInHttpResponseException() throws Throwable { Exception e = new ResourceNotFoundException(); propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
@Test(expectedExceptions = HttpResponseException.class) public void testPropagateStandardExceptionHttpResponseException() throws Throwable { Exception e = new HttpResponseException("goo", createNiceMock(HttpCommand.class), null); propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }