You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/01/04 08:13:18 UTC
[03/21] james-project git commit: JAMES-2272 Add onComplete onFailure
Result composer
JAMES-2272 Add onComplete onFailure Result composer
This make code more fluent.
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1f6f9be7
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1f6f9be7
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1f6f9be7
Branch: refs/heads/master
Commit: 1f6f9be716cba7a9783e6382687c11fe94ac22a4
Parents: b87d49c
Author: benwa <bt...@linagora.com>
Authored: Wed Dec 27 11:19:03 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Jan 4 15:00:43 2018 +0700
----------------------------------------------------------------------
.../main/java/org/apache/james/task/Task.java | 36 ++++++-
.../java/org/apache/james/task/TaskTest.java | 100 +++++++++++++++++++
2 files changed, 135 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/1f6f9be7/server/task/src/main/java/org/apache/james/task/Task.java
----------------------------------------------------------------------
diff --git a/server/task/src/main/java/org/apache/james/task/Task.java b/server/task/src/main/java/org/apache/james/task/Task.java
index 87ce233..fdb6878 100644
--- a/server/task/src/main/java/org/apache/james/task/Task.java
+++ b/server/task/src/main/java/org/apache/james/task/Task.java
@@ -19,11 +19,45 @@
package org.apache.james.task;
+import java.util.Arrays;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
public interface Task {
+ Logger LOGGER = LoggerFactory.getLogger(Task.class);
+
+ interface Operation {
+ void run();
+ }
enum Result {
COMPLETED,
- PARTIAL
+ PARTIAL;
+
+ public Result onComplete(Operation... operation) {
+ try {
+ if (this == COMPLETED) {
+ run(operation);
+ }
+ return this;
+ } catch (Exception e) {
+ LOGGER.error("Error while executing operation", e);
+ return PARTIAL;
+ }
+ }
+
+ public Result onFailure(Operation... operation) {
+ if (this == PARTIAL) {
+ run(operation);
+ }
+ return this;
+ }
+
+ private void run(Operation... operation) {
+ Arrays.stream(operation)
+ .forEach(Operation::run);
+ }
}
static Result combine(Result result1, Result result2) {
http://git-wip-us.apache.org/repos/asf/james-project/blob/1f6f9be7/server/task/src/test/java/org/apache/james/task/TaskTest.java
----------------------------------------------------------------------
diff --git a/server/task/src/test/java/org/apache/james/task/TaskTest.java b/server/task/src/test/java/org/apache/james/task/TaskTest.java
index 55ec2f4..0a7a005 100644
--- a/server/task/src/test/java/org/apache/james/task/TaskTest.java
+++ b/server/task/src/test/java/org/apache/james/task/TaskTest.java
@@ -20,6 +20,9 @@
package org.apache.james.task;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
@@ -49,4 +52,101 @@ public class TaskTest {
.isEqualTo(Task.Result.PARTIAL);
}
+
+ @Test
+ public void onCompleteShouldExecuteOperationWhenCompleted() {
+ AtomicInteger atomicInteger = new AtomicInteger(0);
+
+ Task.Result.COMPLETED
+ .onComplete(atomicInteger::incrementAndGet);
+
+ assertThat(atomicInteger.get())
+ .isEqualTo(1);
+ }
+
+ @Test
+ public void onFailureShouldNotExecuteOperationWhenCompleted() {
+ AtomicInteger atomicInteger = new AtomicInteger(0);
+
+ Task.Result.COMPLETED
+ .onFailure(atomicInteger::incrementAndGet);
+
+ assertThat(atomicInteger.get())
+ .isEqualTo(0);
+ }
+
+ @Test
+ public void onCompleteShouldNotExecuteOperationWhenPartial() {
+ AtomicInteger atomicInteger = new AtomicInteger(0);
+
+ Task.Result.PARTIAL
+ .onComplete(atomicInteger::incrementAndGet);
+
+ assertThat(atomicInteger.get())
+ .isEqualTo(0);
+ }
+
+ @Test
+ public void onFailureShouldExecuteOperationWhenPartial() {
+ AtomicInteger atomicInteger = new AtomicInteger(0);
+
+ Task.Result.PARTIAL
+ .onFailure(atomicInteger::incrementAndGet);
+
+ assertThat(atomicInteger.get())
+ .isEqualTo(1);
+ }
+
+ @Test
+ public void onCompleteShouldReturnPartialWhenPartial() {
+ assertThat(
+ Task.Result.PARTIAL
+ .onComplete(() -> {}))
+ .isEqualTo(Task.Result.PARTIAL);
+ }
+
+ @Test
+ public void onFailureShouldReturnCompletedWhenCompleted() {
+ assertThat(
+ Task.Result.COMPLETED
+ .onFailure(() -> {}))
+ .isEqualTo(Task.Result.COMPLETED);
+ }
+
+ @Test
+ public void onCompleteShouldReturnCompletedWhenCompleted() {
+ assertThat(
+ Task.Result.COMPLETED
+ .onComplete(() -> {}))
+ .isEqualTo(Task.Result.COMPLETED);
+ }
+
+ @Test
+ public void onFailureShouldReturnPartialWhenPartial() {
+ assertThat(
+ Task.Result.PARTIAL
+ .onFailure(() -> {}))
+ .isEqualTo(Task.Result.PARTIAL);
+ }
+
+ @Test
+ public void onCompleteShouldReturnPartialWhenOperationThrows() {
+ assertThat(
+ Task.Result.COMPLETED
+ .onComplete(() -> {
+ throw new RuntimeException();
+ }))
+ .isEqualTo(Task.Result.PARTIAL);
+ }
+
+ @Test
+ public void onFailureShouldPreserveExceptions() {
+ assertThatThrownBy(() ->
+ Task.Result.PARTIAL
+ .onFailure(() -> {
+ throw new RuntimeException();
+ }))
+ .isInstanceOf(RuntimeException.class);
+ }
+
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org