public void testThrowIfUnchecked_null() throws SomeCheckedException { try { throwIfUnchecked(null); fail(); } catch (NullPointerException expected) { } }
public void testThrowIfUnchecked_Error() { try { throwIfUnchecked(new SomeError()); fail(); } catch (SomeError expected) { } }
public void testGetCasualChainNull() { try { Throwables.getCausalChain(null); fail("Should have throw NPE"); } catch (NullPointerException expected) { } }
public void testThrowIfUnchecked_Unchecked() { try { throwIfUnchecked(new SomeUncheckedException()); fail(); } catch (SomeUncheckedException expected) { } }
public void testGetRootCause_Loop() { Exception cause = new Exception(); Exception exception = new Exception(cause); cause.initCause(exception); try { Throwables.getRootCause(cause); fail("Should have throw IAE"); } catch (IllegalArgumentException expected) { assertThat(expected).hasCauseThat().isSameAs(cause); } }
public void testGetCasualChainLoop() { Exception cause = new Exception(); Exception exception = new Exception(cause); cause.initCause(exception); try { Throwables.getCausalChain(cause); fail("Should have throw IAE"); } catch (IllegalArgumentException expected) { assertThat(expected).hasCauseThat().isSameAs(cause); } }
@GwtIncompatible // throwIfInstanceOf public void testThrowIfInstanceOf_null() throws SomeCheckedException { try { throwIfInstanceOf(null, SomeCheckedException.class); fail(); } catch (NullPointerException expected) { } }
@GwtIncompatible // throwIfInstanceOf public void testThrowIfInstanceOf_CheckedSubclass() { try { throwIfInstanceOf(new SomeCheckedException() {}, SomeCheckedException.class); fail(); } catch (SomeCheckedException expected) { } }
public void testGetCausalChain() { SomeUncheckedException sue = new SomeUncheckedException(); IllegalArgumentException iae = new IllegalArgumentException(sue); RuntimeException re = new RuntimeException(iae); IllegalStateException ex = new IllegalStateException(re); assertEquals(asList(ex, re, iae, sue), Throwables.getCausalChain(ex)); assertSame(sue, Iterables.getOnlyElement(Throwables.getCausalChain(sue))); List<Throwable> causes = Throwables.getCausalChain(ex); try { causes.add(new RuntimeException()); fail("List should be unmodifiable"); } catch (UnsupportedOperationException expected) { } }
@GwtIncompatible // throwIfInstanceOf public void testPropagateIfInstanceOf_DeclaredThrown() { Sample sample = new Sample() { @Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsChecked(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, SomeCheckedException.class); throw Throwables.propagate(t); } } }; // Expect declared exception to be thrown as-is try { sample.oneDeclared(); fail(); } catch (SomeCheckedException expected) { } }
@GwtIncompatible // throwIfInstanceOf public void testPropagateIfInstanceOf_UncheckedThrown() throws SomeCheckedException { Sample sample = new Sample() { @Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsUnchecked(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, SomeCheckedException.class); throw Throwables.propagate(t); } } }; // Expect unchecked exception to be thrown as-is try { sample.oneDeclared(); fail(); } catch (SomeUncheckedException expected) { } }
@GwtIncompatible // propagateIfPossible public void testPropagateIfPossible_NoneDeclared_UndeclaredThrown() { Sample sample = new Sample() { @Override public void noneDeclared() { try { methodThatThrowsUndeclaredChecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t); throw new SomeChainingException(t); } } }; // Expect the undeclared exception to have been chained inside another try { sample.noneDeclared(); fail(); } catch (SomeChainingException expected) { } }
@GwtIncompatible // propagateIfPossible(Throwable, Class) public void testPropagateIfPossible_OneDeclared_UncheckedThrown() throws SomeCheckedException { Sample sample = new Sample() { @Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsUnchecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t, SomeCheckedException.class); throw new SomeChainingException(t); } } }; // Expect the unchecked exception to propagate as-is try { sample.oneDeclared(); fail(); } catch (SomeUncheckedException expected) { } }
@GwtIncompatible // propagate public void testPropagate_NoneDeclared_ErrorThrown() { Sample sample = new Sample() { @Override public void noneDeclared() { try { methodThatThrowsError(); } catch (Throwable t) { throw Throwables.propagate(t); } } }; // Expect the error to propagate as-is try { sample.noneDeclared(); fail(); } catch (SomeError expected) { } }
@GwtIncompatible // propagate public void testPropagate_NoneDeclared_UncheckedThrown() { Sample sample = new Sample() { @Override public void noneDeclared() { try { methodThatThrowsUnchecked(); } catch (Throwable t) { throw Throwables.propagate(t); } } }; // Expect the unchecked exception to propagate as-is try { sample.noneDeclared(); fail(); } catch (SomeUncheckedException expected) { } }
@GwtIncompatible // propagateIfPossible(Throwable, Class, Class) public void testPropagateIfPossible_TwoDeclared_OtherCheckedThrown() throws SomeCheckedException { Sample sample = new Sample() { @Override public void twoDeclared() throws SomeCheckedException, SomeOtherCheckedException { try { methodThatThrowsOtherChecked(); } catch (Throwable t) { Throwables.propagateIfPossible( t, SomeCheckedException.class, SomeOtherCheckedException.class); throw new SomeChainingException(t); } } }; // Expect the checked exception to propagate as-is try { sample.twoDeclared(); fail(); } catch (SomeOtherCheckedException expected) { } }
@GwtIncompatible // throwIfInstanceOf public void testThrowIfInstanceOf_CheckedSame() { try { throwIfInstanceOf(new SomeCheckedException(), SomeCheckedException.class); fail(); } catch (SomeCheckedException expected) { } }
@GwtIncompatible // lazyStackTrace private void doTestLazyStackTraceFallback() { assertFalse(lazyStackTraceIsLazy()); Exception e = new Exception(); assertThat(lazyStackTrace(e)).containsExactly((Object[]) e.getStackTrace()).inOrder(); try { lazyStackTrace(e).set(0, null); fail(); } catch (UnsupportedOperationException expected) { } e.setStackTrace(new StackTraceElement[0]); assertThat(lazyStackTrace(e)).isEmpty(); }
@GwtIncompatible // lazyStackTrace(Throwable) public void testLazyStackTrace() { Exception e = new Exception(); StackTraceElement[] originalStackTrace = e.getStackTrace(); assertThat(lazyStackTrace(e)).containsExactly((Object[]) originalStackTrace).inOrder(); try { lazyStackTrace(e).set(0, null); fail(); } catch (UnsupportedOperationException expected) { } // Now we test a property that holds only for the lazy implementation. if (!lazyStackTraceIsLazy()) { return; } e.setStackTrace(new StackTraceElement[0]); assertThat(lazyStackTrace(e)).containsExactly((Object[]) originalStackTrace).inOrder(); }
@GwtIncompatible // Throwables.getCauseAs(Throwable, Class) public void testGetCauseAs() { SomeCheckedException cause = new SomeCheckedException(); SomeChainingException thrown = new SomeChainingException(cause); assertThat(thrown).hasCauseThat().isSameAs(cause); assertThat(Throwables.getCauseAs(thrown, SomeCheckedException.class)).isSameAs(cause); assertThat(Throwables.getCauseAs(thrown, Exception.class)).isSameAs(cause); try { Throwables.getCauseAs(thrown, IllegalStateException.class); fail("Should have thrown CCE"); } catch (ClassCastException expected) { assertThat(expected).hasCauseThat().isSameAs(thrown); } }