private Throwable cleanUp(Throwable testfailure) { try { after(); return testfailure; } catch (Throwable afterFailure) { if (testfailure == null) { // Only after() threw an exception return afterFailure; } // Both TestHelper.awaitOrFail() and after() threw exceptions return new MultipleFailureException(Arrays.asList(testfailure, afterFailure)) { @Override public void printStackTrace(PrintStream out) { int i = 0; for (Throwable t : getFailures()) { out.println("Error " + i + ": " + t.getMessage()); t.printStackTrace(out); out.println(); i++; } } }; } }
private Statement statement(final Statement base, final Description description) { return new Statement() { public void evaluate() throws Throwable { before(description); try { List<Throwable> errors = new ArrayList<>(); try { base.evaluate(); succeededQuietly(description, errors); } catch (Throwable e) { errors.add(e); failedQuietly(e, description, errors); } MultipleFailureException.assertEmpty(errors); } finally { after(); } } }; }
@Override public Statement apply(Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { final List<Throwable> errors = new ArrayList<>(); starting(); try { base.evaluate(); } catch (Throwable e) { errors.add(e); } MultipleFailureException.assertEmpty(errors); } }; }
/** * Implementation based on {@link TestWatcher}. */ @Override public Statement apply(final Statement base, final Description description) { return new Statement() { @Override public void evaluate() throws Throwable { List<Throwable> errors = new ArrayList<>(); startingQuietly(description, errors); try { base.evaluate(); succeededQuietly(description, errors); } catch (AssumptionViolatedException e) { errors.add(e); skippedQuietly(e, description, errors); } catch (Throwable t) { errors.add(t); failedQuietly(t, description, errors); } finally { finishedQuietly(description, errors); } MultipleFailureException.assertEmpty(errors); } }; }
/** * Implementation based on {@link TestWatcher}. */ @Override public Statement apply(final Statement base, final Description description) { return new Statement() { @Override public void evaluate() throws Throwable { List<Throwable> errors = new ArrayList<>(); startingQuietly(description, errors); try { base.evaluate(); succeededQuietly(description, errors); } catch (AssumptionViolatedException e) { errors.add(e); skippedQuietly(e, description, errors); } catch (Throwable t) { errors.add(t); failedQuietly(t, description, errors); } MultipleFailureException.assertEmpty(errors); } }; }
/** * Implementation based on {@link TestWatcher}. */ @Override public Statement apply(final Statement base, final Description description) { return new Statement() { @Override public void evaluate() throws Throwable { List<Throwable> errors = new ArrayList<Throwable>(); startingQuietly(description, errors); try { base.evaluate(); succeededQuietly(description, errors); } catch (AssumptionViolatedException e) { errors.add(e); skippedQuietly(e, description, errors); } catch (Throwable t) { errors.add(t); failedQuietly(t, description, errors); } finally { finishedQuietly(description, errors); } MultipleFailureException.assertEmpty(errors); } }; }
@Override public Statement apply(Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { List<Throwable> errors = new ArrayList<Throwable>(); try { starting(description); base.evaluate(); succeeded(description); } catch (Throwable e) { errors.add(e); failed(e, description); } finally { finished(description); } MultipleFailureException.assertEmpty(errors); } }; }
@Override public Statement apply(final Statement base, final Description description, final Object[] params) { return new Statement() { public void evaluate() throws Throwable { ArrayList<Throwable> errors = new ArrayList<Throwable>(); ParameterizedTestWatcher.this.startingQuietly(description, errors, params); try { base.evaluate(); ParameterizedTestWatcher.this.succeededQuietly(description, errors, params); } catch (AssumptionViolatedException var7) { errors.add(var7); ParameterizedTestWatcher.this.skippedQuietly(var7, description, errors, params); } catch (Throwable var8) { errors.add(var8); ParameterizedTestWatcher.this.failedQuietly(var8, description, errors, params); } finally { ParameterizedTestWatcher.this.finishedQuietly(description, errors, params); } MultipleFailureException.assertEmpty(errors); } }; }
private Exception createTimeoutException(Thread thread) { StackTraceElement[] stackTrace = thread.getStackTrace(); final Thread stuckThread = fLookForStuckThread ? getStuckThread(thread) : null; Exception currThreadException = new TestTimedOutException(fTimeout, fTimeUnit); if (stackTrace != null) { currThreadException.setStackTrace(stackTrace); thread.interrupt(); } if (stuckThread != null) { Exception stuckThreadException = new Exception ("Appears to be stuck in thread " + stuckThread.getName()); stuckThreadException.setStackTrace(getStackTrace(stuckThread)); return new MultipleFailureException (Arrays.<Throwable>asList(currThreadException, stuckThreadException)); } else { return currThreadException; } }
@Test public void assertEmptyThrowsMutipleFailureExceptionForManyThrowables() throws Exception { List<Throwable> errors = new ArrayList<Throwable>(); errors.add(new ExpectedException("basil")); errors.add(new RuntimeException("garlic")); try { MultipleFailureException.assertEmpty(errors); fail(); } catch (MultipleFailureException expected) { assertThat(expected.getFailures(), equalTo(errors)); assertTrue(expected.getMessage().startsWith("There were 2 errors:\n")); assertTrue(expected.getMessage().contains("ExpectedException(basil)\n")); assertTrue(expected.getMessage().contains("RuntimeException(garlic)")); } }
public Statement apply(final Statement base, final Description description) { return new Statement() { @Override public void evaluate() throws Throwable { List<Throwable> errors = new ArrayList<Throwable>(); startingQuietly(description, errors); try { base.evaluate(); succeededQuietly(description, errors); } catch (AssumptionViolatedException e) { errors.add(e); skippedQuietly(e, description, errors); } catch (Throwable t) { errors.add(t); failedQuietly(t, description, errors); } finally { finishedQuietly(description, errors); } MultipleFailureException.assertEmpty(errors); } }; }
@Before public void setUp() throws Throwable { assumeTrue("Test disabled", test.isEnabled()); realm.initialize(new NullConsole(), test); realm.get().createGlobalProperties(new Print(), Print.class); exceptionHandler.setExecutionContext(realm.get().defaultContext()); // Apply scripted conditions scriptConditions(); // Filter disabled tests (may have changed after applying scripted conditions) assumeTrue("Test disabled", test.isEnabled()); if (test.negative) { expected.expect( Matchers.either(StandardErrorHandler.defaultMatcher()).or(ScriptExceptionHandler.defaultMatcher()) .or(Matchers.instanceOf(MultipleFailureException.class))); } else { errorHandler.match(StandardErrorHandler.defaultMatcher()); exceptionHandler.match(ScriptExceptionHandler.defaultMatcher()); } }
@Before public void setUp() throws Throwable { assumeTrue("Test disabled", test.isEnabled()); realm.initialize(new NullConsole(), test); realm.get().createGlobalProperties(new Print(), Print.class); exceptionHandler.setExecutionContext(realm.get().defaultContext()); if (test.negative) { expected.expect( Matchers.either(StandardErrorHandler.defaultMatcher()).or(ScriptExceptionHandler.defaultMatcher()) .or(Matchers.instanceOf(MultipleFailureException.class))); } else { errorHandler.match(StandardErrorHandler.defaultMatcher()); exceptionHandler.match(ScriptExceptionHandler.defaultMatcher()); } }
@Test public void should_report_all_errors() throws Throwable { try { softly.assertThat(1).isEqualTo(1); softly.assertThat(1).isEqualTo(2); softly.assertThat(Lists.newArrayList(1, 2)).containsOnly(1, 3); MultipleFailureException.assertEmpty(softly.errorsCollected()); fail("Should not reach here"); } catch (MultipleFailureException e) { List<Throwable> failures = e.getFailures(); assertThat(failures).hasSize(2); assertThat(failures.get(0).getMessage()).startsWith("expected:<[2]> but was:<[1]>"); assertThat(failures.get(1).getMessage()).startsWith(format("%n" + "Expecting:%n" + " <[1, 2]>%n" + "to contain only:%n" + " <[1, 3]>%n" + "elements not found:%n" + " <[3]>%n" + "and elements not expected:%n" + " <[2]>%n")); } }
@Test public void should_report_all_errors() throws Throwable { try { softly.then(1).isEqualTo(1); softly.then(1).isEqualTo(2); softly.then(Lists.newArrayList(1, 2)).containsOnly(1, 3); MultipleFailureException.assertEmpty(softly.getErrors()); fail("Should not reach here"); } catch (MultipleFailureException e) { List<Throwable> failures = e.getFailures(); assertThat(failures).hasSize(2) .extracting("message") .contains("expected:<[2]> but was:<[1]>", format("%n" + "Expecting:%n" + " <[1, 2]>%n" + "to contain only:%n" + " <[1, 3]>%n" + "elements not found:%n" + " <[3]>%n" + "and elements not expected:%n" + " <[2]>%n")); } }
@Test public void should_report_all_errors() throws Throwable { try { softly.assertThat(1).isEqualTo(1); softly.assertThat(1).isEqualTo(2); softly.assertThat(Lists.newArrayList(1, 2)).containsOnly(1, 3); MultipleFailureException.assertEmpty(softly.errorsCollected()); fail("Should not reach here"); } catch (MultipleFailureException e) { List<Throwable> failures = e.getFailures(); assertThat(failures).hasSize(2); assertThat(failures.get(0)).hasMessageContaining("expected:<[2]> but was:<[1]>"); assertThat(failures.get(1)).hasMessageContaining(format("%n" + "Expecting:%n" + " <[1, 2]>%n" + "to contain only:%n" + " <[1, 3]>%n" + "elements not found:%n" + " <[3]>%n" + "and elements not expected:%n" + " <[2]>%n")); } }
@Override public org.junit.runners.model.Statement apply(final org.junit.runners.model.Statement base, Description description){ return new org.junit.runners.model.Statement(){ @Override public void evaluate() throws Throwable{ try{ setup(); }catch(SQLException e){ throw new SetupFailureException(e); } List<Throwable> errors = new LinkedList<>(); try{ base.evaluate(); }catch(Throwable t){ errors.add(t); } MultipleFailureException.assertEmpty(errors); } }; }
private void closeResultSets() { List<Throwable> t = Lists.newArrayListWithExpectedSize(0); for (ResultSet r : resultSets) { try { if (!r.isClosed()) { r.close(); } } catch (Exception e) { e.printStackTrace(); t.add(e); } } try { MultipleFailureException.assertEmpty(t); } catch (Throwable throwable) { throw new RuntimeException(throwable); } }
@Test public void assertEmptyThrowsMutipleFailureExceptionForManyThrowables() throws Throwable { List<Throwable> errors = new ArrayList<Throwable>(); errors.add(new ExpectedException("basil")); errors.add(new RuntimeException("garlic")); try { MultipleFailureException.assertEmpty(errors); fail(); } catch (MultipleFailureException expected) { assertThat(expected.getFailures(), equalTo(errors)); assertTrue(expected.getMessage().startsWith("There were 2 errors:\n")); assertTrue(expected.getMessage().contains("ExpectedException(basil)\n")); assertTrue(expected.getMessage().contains("RuntimeException(garlic)")); } }
/** {@inheritDoc} */ @Override public Statement apply(Statement base, Description description) { container = containerSupplier.get(); return new Statement() { @Override public void evaluate() throws Throwable { List<Throwable> errors = new ArrayList<>(); try { beforeStart(container); container.start(); afterStart(container); beforeTest(container); base.evaluate(); afterTest(container); } catch (Exception e) { errors.add(e); error(e); } finally { beforeStop(container); container.stop(); afterStop(container); } MultipleFailureException.assertEmpty(errors); } }; }
/** * Adds a new problem. * * @param problem * the {@link Throwable} to be added, must not be {@code null} */ public final void addProblem(final Throwable problem) { if (problem instanceof MultipleTestProblems) { addProblems((MultipleTestProblems) problem); } else if (problem instanceof MultipleFailureException) { addProblems(((MultipleFailureException) problem).getFailures()); } else { problems.add(problem); } }
/** * TestRule implementation that verifies that no proxied assertion methods have failed. */ public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { base.evaluate(); MultipleFailureException.assertEmpty(collector.errors()); } }; }
@Test public void testCancellable() throws Exception { // infinite item loops when(componentDocsIterator.hasNext()).thenReturn(true); when(componentDocsIterator.next()).thenReturn(mock(ODocument.class)); when(assetIterator.hasNext()).thenReturn(true); when(assetIterator.next()).thenReturn(mock(Asset.class)); List<Runnable> cancelables = Arrays.asList( () -> underTest.deleteUnusedComponents(new Date()), () -> underTest.deleteUnusedAssets(new Date()) ); for (Runnable cancelable : cancelables) { AtomicBoolean canceled = new AtomicBoolean(false); Thread t = createTaskThread(cancelable, canceled); t.start(); sleep((long) (Math.random() * 1000)); // sleep for up to a second (emulate task running) canceled.set(true); // cancel the task t.join(5000); // ensure task thread ends if (t.isAlive()) { fail("Task did not cancel"); } if (uncaught.size() > 0) { throw new MultipleFailureException(uncaught); } } }
@Test public void multipleExceptionReportedWhenTheyOccur() { final Async async = new Async(); final IllegalStateException exception1 = new IllegalStateException("exception-1"); final IllegalStateException exception2 = new IllegalStateException("exception-2"); final IllegalStateException exception3 = new IllegalStateException("exception-3"); async.run(() -> { throw exception1; }); async.run(() -> { throw exception2; }); async.run(() -> { throw exception3; }); try { async.verify(); fail("Exception should have been thrown in verify"); } catch (MultipleFailureException mfe) { final Set<String> errorMessages = mfe.getFailures().stream() .map(Throwable::getCause) .map(Throwable::getMessage) .collect(Collectors.toSet()); assertThat(errorMessages).isEqualTo(Sets.newHashSet("exception-1", "exception-2", "exception-3")); assertThat(mfe.getMessage()).contains("Error while running Async: exception-1"); } catch (Throwable e) { fail("Expected MultipleFailureException but got: " + e.getClass().getName()); } }
public void addFailure(Throwable targetException) { if (targetException instanceof MultipleFailureException) { addMultipleFailureException((MultipleFailureException) targetException); } else { fNotifier .fireTestFailure(new Failure(fDescription, targetException)); } }
@Test public void assertEmptyRethrowsSingleRuntimeException() throws Exception { Throwable exception= new ExpectedException("pesto"); List<Throwable> errors= Collections.singletonList(exception); try { MultipleFailureException.assertEmpty(errors); fail(); } catch (ExpectedException e) { assertSame(e, exception); } }
@Test public void assertEmptyRethrowsSingleError() throws Exception { Throwable exception= new AnnotationFormatError("changeo"); List<Throwable> errors= Collections.singletonList(exception); try { MultipleFailureException.assertEmpty(errors); fail(); } catch (AnnotationFormatError e) { assertSame(e, exception); } }
private static <T> void helper(ChannelListener<T> listener, Matcher<? super T> matcher, boolean assertFail) throws Throwable { List<T> received = new ArrayList<>(); while (true) { T msg = listener.messages.poll(5, TimeUnit.SECONDS); if (msg == null) { Description d = new StringDescription(); matcher.describeTo(d); if (!received.isEmpty()) { d.appendText("we received messages:"); } for (T m : received) { matcher.describeMismatch(m, d); } if (assertFail) { listener.throwables.add(new AssertionError("Failing waiting for " + d.toString())); MultipleFailureException.assertEmpty(listener.throwables); } return; } if (matcher.matches(msg)) { if (!listener.throwables.isEmpty()) { MultipleFailureException.assertEmpty(listener.throwables); } return; } received.add(msg); } }
@Override public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { try { base.evaluate(); } catch (Throwable t) { throwables.add(t); } MultipleFailureException.assertEmpty(throwables); } }; }
protected void failed( Throwable e ) throws Throwable { if( scenario.getExecutor().hasFailed() ) { Throwable failedException = scenario.getExecutor().getFailedException(); List<Throwable> errors = Lists.newArrayList( failedException, e ); scenario.getExecutor().setFailedException( new MultipleFailureException( errors ) ); } else { scenario.getExecutor().failed( e ); } scenario.finished(); }
@Override public Statement apply(Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { List<Throwable> errors = new ArrayList<>(); network = networkBuilder.build(); final org.arquillian.cube.docker.impl.client.config.Network dockerNetwork = network.getNetwork(); String networkId = null; try { networkId = dockerClientExecutor.createNetwork(network.getId(), dockerNetwork); dockerNetwork.addMetadata(IsNetworkContainerObject.class, new IsNetworkContainerObject()); base.evaluate(); } catch (Throwable t) { errors.add(t); } finally { if (networkId != null) { dockerClientExecutor.removeNetwork(networkId); } } MultipleFailureException.assertEmpty(errors); } }; }