james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [13/24] james-project git commit: JAMES-2366 Improve MappingsImpl generation
Date Tue, 03 Apr 2018 10:02:59 GMT
JAMES-2366 Improve MappingsImpl generation


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/169190c0
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/169190c0
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/169190c0

Branch: refs/heads/master
Commit: 169190c04b7c569f2c281c790f62b01e2f62929e
Parents: dafe72b
Author: benwa <btellier@linagora.com>
Authored: Wed Mar 28 10:36:33 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Apr 3 16:59:44 2018 +0700

----------------------------------------------------------------------
 .../org/apache/james/rrt/lib/MappingsImpl.java  | 11 ++--
 .../apache/james/rrt/lib/MappingsImplTest.java  | 58 ++++++++++++++++++++
 2 files changed, 65 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/169190c0/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
index fa91828..a315d7e 100644
--- a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
+++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
@@ -63,14 +63,13 @@ public class MappingsImpl implements Mappings, Serializable {
     }
     
     public static MappingsImpl fromCollection(Collection<String> mappings) {
-        return mappings.stream()
-            .reduce(builder(), (builder, mapping) -> builder.add(mapping), (builder1,
builder2) -> builder1.addAll(builder2.build()))
-            .build();
+        return fromMappings(mappings.stream()
+            .map(MappingImpl::of));
     }
     
     public static MappingsImpl fromMappings(Stream<Mapping> mappings) {
         return mappings
-            .reduce(builder(), (builder, mapping) -> builder.add(mapping), (builder1,
builder2) -> builder1.addAll(builder2.build()))
+            .reduce(builder(), Builder::add, Builder::merge)
             .build();
     }
     
@@ -85,6 +84,10 @@ public class MappingsImpl implements Mappings, Serializable {
     }
     
     public static class Builder {
+
+        public static Builder merge(Builder builder1, Builder builder2) {
+            return builder1.addAll(builder2.build());
+        }
         
         private final ImmutableList.Builder<Mapping> mappings;
         

http://git-wip-us.apache.org/repos/asf/james-project/blob/169190c0/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
index bb66d38..496c74e 100644
--- a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
@@ -21,6 +21,7 @@
 package org.apache.james.rrt.lib;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import java.util.Optional;
 
@@ -253,4 +254,61 @@ public class MappingsImplTest {
         Mappings mappings = MappingsImpl.fromRawString("toto").union(MappingsImpl.fromRawString("tata"));
         assertThat(mappings).containsExactly(MappingImpl.address("toto"),MappingImpl.address("tata"));
     }
+
+    @Test
+    public void mergeShouldThrowWhenLeftIsNull() {
+        MappingsImpl.Builder left = null;
+        assertThatThrownBy(() -> MappingsImpl.Builder.merge(left, MappingsImpl.builder()))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void mergeShouldThrowWhenRightIsNull() {
+        MappingsImpl.Builder right = null;
+        assertThatThrownBy(() -> MappingsImpl.Builder.merge(MappingsImpl.builder(), right))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void mergeShouldReturnEmptyWhenBothAreEmpty() {
+        MappingsImpl.Builder empty = MappingsImpl.builder();
+        MappingsImpl mappingsImpl = MappingsImpl.Builder
+                .merge(empty, empty)
+                .build();
+        assertThat(mappingsImpl.isEmpty()).isTrue();
+    }
+
+    @Test
+    public void mergeShouldReturnLeftWhenRightIsEmpty() {
+        MappingImpl expectedMapping = MappingImpl.address("toto");
+        MappingsImpl.Builder left = MappingsImpl.builder().add(expectedMapping);
+        MappingsImpl.Builder empty = MappingsImpl.builder();
+        MappingsImpl mappingsImpl = MappingsImpl.Builder
+                .merge(left, empty)
+                .build();
+        assertThat(mappingsImpl).containsExactly(expectedMapping);
+    }
+
+    @Test
+    public void mergeShouldReturnRightWhenLeftIsEmpty() {
+        MappingImpl expectedMapping = MappingImpl.address("toto");
+        MappingsImpl.Builder right = MappingsImpl.builder().add(expectedMapping);
+        MappingsImpl.Builder empty = MappingsImpl.builder();
+        MappingsImpl mappingsImpl = MappingsImpl.Builder
+                .merge(empty, right)
+                .build();
+        assertThat(mappingsImpl).containsExactly(expectedMapping);
+    }
+
+    @Test
+    public void mergeShouldReturnBothWhenBothAreNotEmpty() {
+        MappingImpl leftMapping = MappingImpl.address("toto");
+        MappingsImpl.Builder left = MappingsImpl.builder().add(leftMapping);
+        MappingImpl rightMapping = MappingImpl.address("titi");
+        MappingsImpl.Builder right = MappingsImpl.builder().add(rightMapping);
+        MappingsImpl mappingsImpl = MappingsImpl.Builder
+                .merge(left, right)
+                .build();
+        assertThat(mappingsImpl).containsExactly(leftMapping, rightMapping);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message