From 6dbf89d865377c91e1ee5aad603a164adc7bc667 Mon Sep 17 00:00:00 2001
From: Nelson Tavares de Sousa <stu103017@mail.uni-kiel.de>
Date: Tue, 25 Aug 2015 15:58:06 +0200
Subject: [PATCH] added a check for exceptions in waitForTermination #221

---
 .../java/teetime/framework/Execution.java     |  9 ++++++++
 .../teetime/framework/ExecutionException.java | 17 +++++++--------
 .../framework/RunnableConsumerStageTest.java  | 21 ++++++++-----------
 .../teetime/stage/InstanceOfFilterTest.java   |  4 +---
 4 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/src/main/java/teetime/framework/Execution.java b/src/main/java/teetime/framework/Execution.java
index e801ab6b..441791ee 100644
--- a/src/main/java/teetime/framework/Execution.java
+++ b/src/main/java/teetime/framework/Execution.java
@@ -15,6 +15,8 @@
  */
 package teetime.framework;
 
+import java.util.List;
+import java.util.Map.Entry;
 import java.util.Set;
 
 import teetime.framework.signal.ValidatingSignal;
@@ -104,7 +106,14 @@ 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();
+		}
+		if (numExceptions != 0) {
+			throw new ExecutionException(configuration.getFactory().getThreadExceptionsMap());
+		}
 	}
 
 	// TODO: implement
diff --git a/src/main/java/teetime/framework/ExecutionException.java b/src/main/java/teetime/framework/ExecutionException.java
index f33901b3..bc6945bc 100644
--- a/src/main/java/teetime/framework/ExecutionException.java
+++ b/src/main/java/teetime/framework/ExecutionException.java
@@ -15,24 +15,23 @@
  */
 package teetime.framework;
 
-import java.util.Collection;
-
-import teetime.util.ThreadThrowableContainer;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Represents a exception, which is thrown by an analysis, if any problems occured within its execution.
  * A collection of thrown exceptions within the analysis can be retrieved with {@link #getThrownExceptions()}.
  *
- * @since 1.1
+ * @since 2.0
  */
 public class ExecutionException extends RuntimeException {
 
 	private static final long serialVersionUID = 7486086437171884298L;
 
-	private final Collection<ThreadThrowableContainer> exceptions;
+	private final Map<Thread, List<Exception>> exceptions;
 
-	public ExecutionException(final Collection<ThreadThrowableContainer> exceptions) {
-		super((exceptions.size() == 1) ? exceptions.toString() : "Error(s) while execution. Check thrown exception(s).");
+	public ExecutionException(final Map<Thread, List<Exception>> exceptions) {
+		super((exceptions.size() == 1) ? exceptions.toString() : exceptions.size() + " error(s) while execution. Check thrown exception(s).");
 		this.exceptions = exceptions;
 	}
 
@@ -40,9 +39,9 @@ public class ExecutionException extends RuntimeException {
 	 * Returns all exceptions thrown within the execution.
 	 * These are passed on as pairs of threads and throwables, to indicate a exception's context.
 	 *
-	 * @return a collection of pairs
+	 * @return a thread-exceptionlist-map
 	 */
-	public Collection<ThreadThrowableContainer> getThrownExceptions() {
+	public Map<Thread, List<Exception>> getThrownExceptions() {
 		return exceptions;
 	}
 
diff --git a/src/test/java/teetime/framework/RunnableConsumerStageTest.java b/src/test/java/teetime/framework/RunnableConsumerStageTest.java
index 251a4961..3984a2fe 100644
--- a/src/test/java/teetime/framework/RunnableConsumerStageTest.java
+++ b/src/test/java/teetime/framework/RunnableConsumerStageTest.java
@@ -18,16 +18,13 @@ package teetime.framework;
 import static org.junit.Assert.assertEquals;
 
 import java.lang.Thread.State;
-import java.util.ArrayList;
-import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.junit.Ignore;
 import org.junit.Test;
 
-import teetime.util.ThreadThrowableContainer;
-
-import com.google.common.base.Joiner;
-
 public class RunnableConsumerStageTest {
 
 	@Test
@@ -118,17 +115,17 @@ public class RunnableConsumerStageTest {
 	}
 
 	private void start(final Execution<?> execution) {
-		Collection<ThreadThrowableContainer> exceptions = new ArrayList<ThreadThrowableContainer>();
+		Map<Thread, List<Exception>> exceptions = new HashMap<Thread, List<Exception>>();
 		try {
 			execution.executeBlocking();
 		} catch (ExecutionException e) {
 			exceptions = e.getThrownExceptions();
 		}
-		for (ThreadThrowableContainer pair : exceptions) {
-			System.err.println(pair.getThrowable());
-			System.err.println(Joiner.on("\n").join(pair.getThrowable().getStackTrace()));
-			throw new AssertionError(pair.getThrowable());
-		}
+		// for (ThreadThrowableContainer pair : exceptions) {
+		// System.err.println(pair.getThrowable());
+		// System.err.println(Joiner.on("\n").join(pair.getThrowable().getStackTrace()));
+		// throw new AssertionError(pair.getThrowable());
+		// }
 		assertEquals(0, exceptions.size());
 	}
 }
diff --git a/src/test/java/teetime/stage/InstanceOfFilterTest.java b/src/test/java/teetime/stage/InstanceOfFilterTest.java
index 03c796a7..8389a8ba 100644
--- a/src/test/java/teetime/stage/InstanceOfFilterTest.java
+++ b/src/test/java/teetime/stage/InstanceOfFilterTest.java
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertThat;
 import static teetime.framework.test.StageTester.test;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
 
 import org.junit.Before;
@@ -32,7 +31,6 @@ import org.junit.Test;
 import teetime.framework.Configuration;
 import teetime.framework.Execution;
 import teetime.framework.ExecutionException;
-import teetime.util.ThreadThrowableContainer;
 
 /**
  * @author Nils Christian Ehmke
@@ -117,7 +115,7 @@ public class InstanceOfFilterTest {
 		try {
 			execution.executeBlocking();
 		} catch (ExecutionException e) {
-			Collection<ThreadThrowableContainer> thrownExceptions = e.getThrownExceptions();
+			// Collection<ThreadThrowableContainer> thrownExceptions = e.getThrownExceptions();
 			// TODO: handle exception
 		}
 	}
-- 
GitLab