From ac675b9c3ab1aae81d757a2eadf867604e7c1bb1 Mon Sep 17 00:00:00 2001
From: Nelson Tavares de Sousa <stu103017@mail.uni-kiel.de>
Date: Mon, 24 Aug 2015 15:15:09 +0200
Subject: [PATCH] introduced a flag which indicates if exceptions should be
 logged

---
 src/main/java/teetime/framework/Stage.java            |  2 +-
 .../exceptionHandling/AbstractExceptionListener.java  | 11 ++++++++++-
 .../exceptionHandling/IgnoringExceptionListener.java  |  4 ++++
 .../exceptionHandling/LoggingExceptionListener.java   |  4 ++++
 .../TerminatingExceptionListener.java                 |  2 +-
 .../framework/exceptionHandling/TestListener.java     |  4 ++++
 6 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/src/main/java/teetime/framework/Stage.java b/src/main/java/teetime/framework/Stage.java
index 4cc0cf76..a768ac37 100644
--- a/src/main/java/teetime/framework/Stage.java
+++ b/src/main/java/teetime/framework/Stage.java
@@ -119,7 +119,7 @@ public abstract class Stage {
 		} catch (TerminateException e) {
 			throw e;
 		} catch (Exception e) {
-			final FurtherExecution furtherExecution = this.exceptionListener.onStageException(e, this);
+			final FurtherExecution furtherExecution = this.exceptionListener.reportException(e, this);
 			if (furtherExecution == FurtherExecution.TERMINATE) {
 				throw TerminateException.INSTANCE;
 			}
diff --git a/src/main/java/teetime/framework/exceptionHandling/AbstractExceptionListener.java b/src/main/java/teetime/framework/exceptionHandling/AbstractExceptionListener.java
index 3cc9dcae..81cc2f02 100644
--- a/src/main/java/teetime/framework/exceptionHandling/AbstractExceptionListener.java
+++ b/src/main/java/teetime/framework/exceptionHandling/AbstractExceptionListener.java
@@ -31,6 +31,7 @@ import teetime.framework.Stage;
 public abstract class AbstractExceptionListener {
 
 	private final List<Exception> exceptionsList = new ArrayList<Exception>();
+	private final boolean logExceptions;
 
 	public enum FurtherExecution {
 		CONTINUE, TERMINATE
@@ -41,8 +42,9 @@ public abstract class AbstractExceptionListener {
 	 */
 	protected final Logger logger;
 
-	public AbstractExceptionListener() {
+	protected AbstractExceptionListener(final boolean shouldLogExceptions) {
 		this.logger = LoggerFactory.getLogger(this.getClass().getCanonicalName());
+		this.logExceptions = shouldLogExceptions;
 	}
 
 	/**
@@ -61,4 +63,11 @@ public abstract class AbstractExceptionListener {
 		return exceptionsList;
 	}
 
+	public FurtherExecution reportException(final Exception e, final Stage stage) {
+		if (logExceptions) {
+			exceptionsList.add(e);
+		}
+		return onStageException(e, stage);
+	}
+
 }
diff --git a/src/main/java/teetime/framework/exceptionHandling/IgnoringExceptionListener.java b/src/main/java/teetime/framework/exceptionHandling/IgnoringExceptionListener.java
index c46c70a2..3eb54451 100644
--- a/src/main/java/teetime/framework/exceptionHandling/IgnoringExceptionListener.java
+++ b/src/main/java/teetime/framework/exceptionHandling/IgnoringExceptionListener.java
@@ -19,6 +19,10 @@ import teetime.framework.Stage;
 
 class IgnoringExceptionListener extends AbstractExceptionListener {
 
+	IgnoringExceptionListener() {
+		super(false);
+	}
+
 	@Override
 	public FurtherExecution onStageException(final Exception e, final Stage throwingStage) {
 		return FurtherExecution.CONTINUE;
diff --git a/src/main/java/teetime/framework/exceptionHandling/LoggingExceptionListener.java b/src/main/java/teetime/framework/exceptionHandling/LoggingExceptionListener.java
index ab7f9176..8fee623c 100644
--- a/src/main/java/teetime/framework/exceptionHandling/LoggingExceptionListener.java
+++ b/src/main/java/teetime/framework/exceptionHandling/LoggingExceptionListener.java
@@ -19,6 +19,10 @@ import teetime.framework.Stage;
 
 class LoggingExceptionListener extends AbstractExceptionListener {
 
+	LoggingExceptionListener() {
+		super(true);
+	}
+
 	@Override
 	public FurtherExecution onStageException(final Exception e, final Stage throwingStage) {
 		logger.warn("Exception occurred in " + throwingStage.getId(), e);
diff --git a/src/main/java/teetime/framework/exceptionHandling/TerminatingExceptionListener.java b/src/main/java/teetime/framework/exceptionHandling/TerminatingExceptionListener.java
index b8bcf36d..2490c0e4 100644
--- a/src/main/java/teetime/framework/exceptionHandling/TerminatingExceptionListener.java
+++ b/src/main/java/teetime/framework/exceptionHandling/TerminatingExceptionListener.java
@@ -25,7 +25,7 @@ class TerminatingExceptionListener extends AbstractExceptionListener {
 	private final List<Exception> exceptions = new ArrayList<Exception>();
 
 	TerminatingExceptionListener() {
-		// should only be instantiated by its factory
+		super(true);
 	}
 
 	@Override
diff --git a/src/test/java/teetime/framework/exceptionHandling/TestListener.java b/src/test/java/teetime/framework/exceptionHandling/TestListener.java
index 7a8826a9..06b32bcd 100644
--- a/src/test/java/teetime/framework/exceptionHandling/TestListener.java
+++ b/src/test/java/teetime/framework/exceptionHandling/TestListener.java
@@ -19,6 +19,10 @@ import teetime.framework.Stage;
 
 public class TestListener extends AbstractExceptionListener {
 
+	protected TestListener() {
+		super(false);
+	}
+
 	private int numExceptionsInvoked;
 
 	@Override
-- 
GitLab