From 0cd7d63bf7fa7e910a5501aff1341f9659f70334 Mon Sep 17 00:00:00 2001 From: Christian Wulf <chw@informatik.uni-kiel.de> Date: Wed, 30 Sep 2015 15:43:41 +0200 Subject: [PATCH] fixes #221 --- .../java/teetime/framework/Execution.java | 18 +++++++++++---- .../ExceptionHandlingTest.java | 23 +++++++++++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/main/java/teetime/framework/Execution.java b/src/main/java/teetime/framework/Execution.java index 726bb689..669fd834 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 0d9a9dfd..db21c57b 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)); + } } } -- GitLab