SetAndThrowAuthorizationExceptionSupplier implements Supplier<T> { public T get() { if (authException.get() != null) throw authException.get(); try { return delegate.get(); } catch (AuthorizationException e) { authException.set(e); throw e; } catch (Exception e) { AuthorizationException aex = getFirstThrowableOfType(e, AuthorizationException.class); if (aex != null) { authException.set(aex); throw aex; } throw propagate(e); } } SetAndThrowAuthorizationExceptionSupplier(Supplier<T> delegate,
         AtomicReference<AuthorizationException> authException); T get(); @Override String toString();  }
@Test public void testNormal() { AtomicReference<AuthorizationException> authException = Atomics.newReference(); assertEquals( new SetAndThrowAuthorizationExceptionSupplier<String>(Suppliers.ofInstance("foo"), authException).get(), "foo"); assertEquals(authException.get(), null); }
@Test(expectedExceptions = AuthorizationException.class) public void testThrowsAuthorizationExceptionAndAlsoSetsExceptionType() { AtomicReference<AuthorizationException> authException = Atomics.newReference(); try { new SetAndThrowAuthorizationExceptionSupplier<String>(new Supplier<String>() { @Override public String get() { throw new AuthorizationException(); } }, authException).get(); } finally { assertEquals(authException.get().getClass(), AuthorizationException.class); } }
@Test(expectedExceptions = AuthorizationException.class) public void testThrowsAuthorizationExceptionAndAlsoSetsExceptionTypeWhenNested() { AtomicReference<AuthorizationException> authException = Atomics.newReference(); try { new SetAndThrowAuthorizationExceptionSupplier<String>(new Supplier<String>() { @Override public String get() { throw new RuntimeException(new ExecutionException(new AuthorizationException())); } }, authException).get(); } finally { assertEquals(authException.get().getClass(), AuthorizationException.class); } }
@Test(expectedExceptions = RuntimeException.class) public void testThrowsOriginalExceptionAndAlsoSetsExceptionTypeWhenNestedAndNotAuthorizationException() { AtomicReference<AuthorizationException> authException = Atomics.newReference(); try { new SetAndThrowAuthorizationExceptionSupplier<String>(new Supplier<String>() { @Override public String get() { throw new RuntimeException(new IllegalArgumentException("foo")); } }, authException).get(); } finally { assertEquals(authException.get().getClass(), RuntimeException.class); } }