diff --git a/src/main/java/teetime/framework/Execution.java b/src/main/java/teetime/framework/Execution.java index 726bb68969e128eda9e5f7f11745673a324e851f..669fd83460b2295362a391e4422f926a51dfe289 100644 --- a/src/main/java/teetime/framework/Execution.java +++ b/src/main/java/teetime/framework/Execution.java @@ -15,7 +15,9 @@ */ package teetime.framework; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -106,13 +108,19 @@ public final class Execution<T extends Configuration> { * @since 2.0 */ public void waitForTermination() { - int numExceptions = 0; configurationContext.waitForConfigurationToTerminate(); - for (Entry<Thread, List<Exception>> entry : configuration.getFactory().getThreadExceptionsMap().entrySet()) { - numExceptions += entry.getValue().size(); + + Map<Thread, List<Exception>> threadExceptionsMap = configuration.getFactory().getThreadExceptionsMap(); + Iterator<Entry<Thread, List<Exception>>> iterator = threadExceptionsMap.entrySet().iterator(); + while (iterator.hasNext()) { + Entry<Thread, List<Exception>> entry = iterator.next(); + if (entry.getValue().isEmpty()) { + iterator.remove(); + } } - if (numExceptions != 0) { - throw new ExecutionException(configuration.getFactory().getThreadExceptionsMap()); + + if (!threadExceptionsMap.isEmpty()) { + throw new ExecutionException(threadExceptionsMap); } } diff --git a/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java b/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java index 0d9a9dfdb85093f14dc993001b08092c2f4c12c5..db21c57b3d672d78077d1a558ff82513441ac0eb 100644 --- a/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java +++ b/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java @@ -15,6 +15,14 @@ */ package teetime.framework.exceptionHandling; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static teetime.testutil.AssertHelper.assertInstanceOf; + +import java.util.List; +import java.util.Map.Entry; + import org.junit.Test; import teetime.framework.Execution; @@ -22,9 +30,20 @@ import teetime.framework.ExecutionException; public class ExceptionHandlingTest { - @Test(expected = ExecutionException.class) + @Test public void testException() { - new Execution<ExceptionPassingTestConfig>(new ExceptionPassingTestConfig()).executeBlocking(); + Execution<ExceptionPassingTestConfig> execution = new Execution<ExceptionPassingTestConfig>(new ExceptionPassingTestConfig()); + try { + execution.executeBlocking(); + } catch (ExecutionException e) { + Entry<Thread, List<Exception>> entry = e.getThrownExceptions().entrySet().iterator().next(); + List<Exception> exceptions = entry.getValue(); + IllegalStateException exception = assertInstanceOf(IllegalStateException.class, exceptions.get(0)); + assertThat(exception.getMessage(), is(equalTo("Correct exception"))); + + assertThat(exceptions.size(), is(1)); + assertThat(e.getThrownExceptions().size(), is(1)); + } } }