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 // 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(); }
@Override public void noneDeclared() { try { methodThatDoesntThrowAnything(); } catch (Throwable t) { throw Throwables.propagate(t); } } };
public void testThrowIfUnchecked_null() throws SomeCheckedException { try { throwIfUnchecked(null); fail(); } catch (NullPointerException expected) { } }
@Override public void noneDeclared() { try { methodThatThrowsUnchecked(); } catch (Throwable t) { throw Throwables.propagate(t); } } };
@Override public void noneDeclared() { try { methodThatThrowsChecked(); } catch (Throwable t) { throw Throwables.propagate(t); } } };
public void testGetRootCause_NoCause() { SomeCheckedException exception = new SomeCheckedException(); assertSame(exception, Throwables.getRootCause(exception)); }
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsOtherChecked(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, SomeCheckedException.class); throw Throwables.propagate(t); } } };
@AndroidIncompatible // No getJavaLangAccess in Android (at least not in the version we use). @GwtIncompatible // lazyStackTraceIsLazy() public void testLazyStackTraceWorksInProd() { // TODO(b/64442212): Remove this guard once lazyStackTrace() works in Java 9+. Integer javaVersion = Ints.tryParse(JAVA_SPECIFICATION_VERSION.value()); if (javaVersion != null && javaVersion >= 9) { return; } // Obviously this isn't guaranteed in every environment, but it works well enough for now: assertTrue(lazyStackTraceIsLazy()); }
@Override public void noneDeclared() { try { methodThatThrowsError(); } catch (Throwable t) { throw Throwables.propagate(t); } } };
public void testThrowIfUnchecked_Error() { try { throwIfUnchecked(new SomeError()); fail(); } catch (SomeError expected) { } }
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsUnchecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t, SomeCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void twoDeclared() throws SomeCheckedException, SomeOtherCheckedException { try { methodThatThrowsChecked(); } catch (Throwable t) { Throwables.propagateIfPossible( t, SomeCheckedException.class, SomeOtherCheckedException.class); throw new SomeChainingException(t); } } };
public void testGetRootCause_SingleWrapped() { SomeCheckedException cause = new SomeCheckedException(); SomeChainingException exception = new SomeChainingException(cause); assertSame(cause, Throwables.getRootCause(exception)); }
@Override public void twoDeclared() throws SomeCheckedException, SomeOtherCheckedException { try { methodThatThrowsOtherChecked(); } catch (Throwable t) { Throwables.propagateIfPossible( t, SomeCheckedException.class, SomeOtherCheckedException.class); throw new SomeChainingException(t); } } };
@AndroidIncompatible // No getJavaLangAccess in Android (at least not in the version we use). @GwtIncompatible // lazyStackTraceIsLazy() public void testLazyStackTraceWorksInProd() { // TODO(b/64442212): Remove this guard once lazyStackTrace() works in Java 9. if (JAVA_SPECIFICATION_VERSION.value().equals("9")) { return; } // Obviously this isn't guaranteed in every environment, but it works well enough for now: assertTrue(lazyStackTraceIsLazy()); }
@Override public void noneDeclared() { try { methodThatThrowsError(); } catch (Throwable t) { throw Throwables.propagate(t); } } };
public void testGetCasualChainNull() { try { Throwables.getCausalChain(null); fail("Should have throw NPE"); } catch (NullPointerException 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) { } }
@Override public void noneDeclared() { try { methodThatThrowsUnchecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t); throw new SomeChainingException(t); } } };