@Test void shouldThrowAppropriateExceptionIfBothStartAndShutdownFail() { RuntimeException startupError = new RuntimeException(); RuntimeException shutdownError = new RuntimeException(); GraphDatabaseFacadeFactory db = newFaultyGraphDatabaseFacadeFactory( startupError ); doThrow( shutdownError ).when( mockFacade ).shutdown(); RuntimeException initException = assertThrows( RuntimeException.class, () -> db.initFacade( testDirectory.storeDir(), Collections.emptyMap(), deps, mockFacade ) ); assertTrue( initException.getMessage().startsWith( "Error starting " ) ); assertEquals( startupError, initException.getCause() ); assertEquals( shutdownError, initException.getSuppressed()[0] ); }
@Test public void testRollbackThrow() throws Exception { RuntimeException outer = new RuntimeException("Transaction throws!"); RuntimeException inner = new RuntimeException("Rollback throws!"); Mockito.when(c.getAutoCommit()).thenReturn(true); Mockito.when(h.getConnection()).thenReturn(c); Mockito.when(h.rollback()).thenThrow(inner); try { new LocalTransactionHandler().inTransaction(h, x -> { throw outer; }); } catch (RuntimeException e) { assertThat(e).isSameAs(outer); assertThat(e.getSuppressed()).hasSize(1); assertThat(e.getSuppressed()[0]).isSameAs(inner); } }
@Test public void throwTwoRuntimeExceptions() throws Throwable { Exception bdcException = new IllegalStateException("block deletion context exception"); Exception jcException = new IllegalArgumentException("journal context exception"); doThrow(bdcException).when(mMockBDC).close(); doThrow(jcException).when(mMockJC).close(); try { mRpcContext.close(); fail("Expected an exception to be thrown"); } catch (RuntimeException e) { assertEquals(jcException, e); // journal context is closed first, so the block deletion context exception should be // suppressed. assertEquals(bdcException, e.getSuppressed()[0]); } }
assertTrue( failed.hasNext() ); assertEquals( e.getMessage(), failed.next() ); for ( Throwable suppressed : e.getSuppressed() )
if (closeError.getSuppressed().length > 0) { throw closeError;
private static ImmutableSet<ConfigObject<?>> extractRequiredConfig(ConfigData configData, Map<String, TypeToken<?>> required) { RuntimeException badConfig = new IllegalStateException("Failed to build required config items"); ImmutableSet.Builder<ConfigObject<?>> config = ImmutableSet.builder(); for (Map.Entry<String, TypeToken<?>> requiredConfig : required.entrySet()) { String path = requiredConfig.getKey(); TypeToken<?> type = requiredConfig.getValue(); try { config.add(configData.getAsConfigObject(path, type)); } catch (Exception e) { badConfig.addSuppressed(new IllegalStateException("Could not bind config at '" + path + "' to '" + type + "'", e)); } } if (badConfig.getSuppressed().length > 0) { throw badConfig; } else { return config.build(); } }
/** * Loops through all values in the set and collects any exceptions from the consumer * into a Throwable. * @param consumer the consumer to call * @return if not null, contains a CompositeException with all the suppressed exceptions */ public Throwable forEachSuppress(Consumer<? super T> consumer) { RuntimeException ex = null; int count = 0; for (T k : keys) { if (k != null) { try { consumer.accept(k); } catch (Throwable e) { if (ex == null) { ex = new RuntimeException("Multiple exceptions"); } count++; ex.addSuppressed(e); } } } if (count == 1) { return ex.getSuppressed()[0]; } return ex; }
if (errors.getSuppressed().length > 0) { throw errors;
@Override public void abort() { RuntimeException error = new RuntimeException("Exception during rollback"); for (PageBuffer pageBuffer : pageWriter.getPageBuffers()) { try { pageBuffer.getStoragePageSink().rollback(); } catch (Throwable t) { // Self-suppression not permitted if (error != t) { error.addSuppressed(t); } } } if (error.getSuppressed().length > 0) { throw error; } }
@Override public void abort() { RuntimeException error = new RuntimeException("Exception during rollback"); for (PageBuffer pageBuffer : pageWriter.getPageBuffers()) { try { pageBuffer.getStoragePageSink().rollback(); } catch (Throwable t) { // Self-suppression not permitted if (error != t) { error.addSuppressed(t); } } } if (error.getSuppressed().length > 0) { throw error; } }
@Test public void testTryWithResourceApi_Never() throws Exception { try { try (Spy ignored = Sniffer.expectNever()) { executeStatement(); throw new RuntimeException("This is a test exception"); } } catch (RuntimeException e) { assertEquals("This is a test exception", e.getMessage()); assertNotNull(e.getSuppressed()); assertEquals(1, e.getSuppressed().length); assertTrue(WrongNumberOfQueriesError.class.isAssignableFrom(e.getSuppressed()[0].getClass())); } }
@Test public void testExecuteThrowsException() throws Exception { try { Sniffer.expect(1).execute(() -> { throw new RuntimeException(); }); } catch (RuntimeException e) { assertNotNull(e); assertNull(e.getCause()); assertEquals(1, e.getSuppressed().length); assertTrue(WrongNumberOfQueriesError.class.isAssignableFrom(e.getSuppressed()[0].getClass())); } }
@Test public void testRunThrowsException() throws Exception { try { Sniffer.expect(1).run(() -> { throw new RuntimeException(); }); } catch (RuntimeException e) { assertNotNull(e); assertNull(e.getCause()); assertEquals(1, e.getSuppressed().length); assertTrue(WrongNumberOfQueriesError.class.isAssignableFrom(e.getSuppressed()[0].getClass())); } }
@Test public void testCallThrowsException() throws Exception { try { Sniffer.expect(1).call(() -> { throw new RuntimeException(); }); } catch (RuntimeException e) { assertNotNull(e); assertNull(e.getCause()); assertEquals(1, e.getSuppressed().length); assertTrue(WrongNumberOfQueriesError.class.isAssignableFrom(e.getSuppressed()[0].getClass())); } }
if (closeError.getSuppressed().length > 0) { throw closeError;
if (closeError.getSuppressed().length > 0) { throw closeError;
if (closeError.getSuppressed().length > 0) { throw closeError;
} catch (RuntimeException e) { throw ExceptionTestUtils.throwAsUnchecked(e.getSuppressed()[0]);
} catch (RuntimeException e) { throw ExceptionTestUtils.throwAsUnchecked(e.getSuppressed()[0]);