james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [06/24] james-project git commit: JAMES-2366 Forward mappings should be sorted just after Domains
Date Fri, 06 Apr 2018 13:20:25 GMT
JAMES-2366 Forward mappings should be sorted just after Domains


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

Branch: refs/heads/master
Commit: d1b0a112b99054a67ad5f4f2262f7527eed17a85
Parents: 048e362
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Thu Mar 29 15:38:26 2018 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Fri Apr 6 14:39:17 2018 +0200

----------------------------------------------------------------------
 .../java/org/apache/james/rrt/lib/Mapping.java  | 18 +++++++++-----
 .../org/apache/james/rrt/lib/MappingsImpl.java  | 25 +++++++-------------
 .../lib/AbstractRecipientRewriteTableTest.java  |  5 +++-
 .../apache/james/rrt/lib/MappingsImplTest.java  | 13 ++++++++++
 4 files changed, 37 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d1b0a112/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
index 72193dc..bb3ec7e 100644
--- a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
+++ b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
@@ -47,16 +47,18 @@ public interface Mapping {
     String getAddress();
 
     enum Type {
-        Regex("regex:"), 
-        Domain("domain:"), 
-        Error("error:"), 
-        Forward("forward:"),
-        Address(""); 
+        Regex("regex:", 3),
+        Domain("domain:", 1),
+        Error("error:", 3),
+        Forward("forward:", 2),
+        Address("", 3);
 
         private final String asPrefix;
+        private final int order;
 
-        Type(String asPrefix) {
+        Type(String asPrefix, Integer order) {
             this.asPrefix = asPrefix;
+            this.order = order;
         }
 
         public String asPrefix() {
@@ -74,6 +76,10 @@ public interface Mapping {
                 || mapping.startsWith(Error.asPrefix())
                 || mapping.startsWith(Forward.asPrefix());
         }
+
+        public int getOrder() {
+            return order;
+        }
     }
 
     Type getType();

http://git-wip-us.apache.org/repos/asf/james-project/blob/d1b0a112/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 eab5fd8..23c3a3d 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
@@ -44,6 +44,10 @@ public class MappingsImpl implements Mappings, Serializable {
 
     private static final long serialVersionUID = 1L;
 
+    public static final Comparator<Mapping> MAPPING_COMPARATOR = Comparator
+        .<Mapping, Integer>comparing(mapping -> mapping.getType().getOrder())
+        .thenComparing(Mapping::asString);
+
     public static MappingsImpl empty() {
         return builder().build();
     }
@@ -103,7 +107,6 @@ public class MappingsImpl implements Mappings, Serializable {
             mappings.add(mapping);
             return this;
         }
-
         
         public Builder addAll(Mappings mappings) {
             this.mappings.addAll(mappings);
@@ -111,24 +114,12 @@ public class MappingsImpl implements Mappings, Serializable {
         }
         
         public MappingsImpl build() {
-            Comparator<? super Mapping> byTypeComparator = (mapping1, mapping2) ->
{
-                if (mapping1.getType() == mapping2.getType()) {
-                    return mapping1.asString().compareTo(mapping2.asString());
-                }
-                if (mapping1.getType() == Type.Domain) {
-                    return -1;
-                }
-                if (mapping2.getType() == Type.Domain) {
-                    return 1;
-                }
-                return mapping1.asString().compareTo(mapping2.asString());
-            };
-            return new MappingsImpl(mappings.build().stream()
-                    .sorted(byTypeComparator)
-                    .collect(Guavate.toImmutableList()));
+            return new MappingsImpl(mappings.build()
+                .stream()
+                .sorted(MAPPING_COMPARATOR)
+                .collect(Guavate.toImmutableList()));
         }
 
-        
     }
     
     private final ImmutableList<Mapping> mappings;

http://git-wip-us.apache.org/repos/asf/james-project/blob/d1b0a112/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
index 3c69fab..143fb27 100644
--- a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
@@ -223,7 +223,10 @@ public abstract class AbstractRecipientRewriteTableTest {
 
         assertThat(virtualUserTable.getMappings(user, aliasDomain))
             .describedAs("Domain mapped as first, Address mapped as second")
-            .containsExactly(MappingImpl.address(user + "@" + domain), MappingImpl.address(user2
+ "@" + domain));
+            .isEqualTo(MappingsImpl.builder()
+                .add(MappingImpl.address(user + "@" + domain))
+                .add(MappingImpl.address(user2 + "@" + domain))
+                .build());
 
         removeMapping(RecipientRewriteTable.WILDCARD, aliasDomain, user2 + "@" + domain,
Type.Address);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/d1b0a112/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 efb5add..1f8dcd9 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
@@ -326,4 +326,17 @@ public class MappingsImplTest {
                 .build();
         assertThat(mappingsImpl).containsExactly(domainMapping, domain2Mapping, addressMapping,
errorMapping);
     }
+    
+    @Test
+    public void builderShouldPutDomainAliasFirstThenForwardWhenVariousMappings() {
+        MappingImpl regexMapping = MappingImpl.regex("regex");
+        MappingImpl forwardMapping = MappingImpl.forward("forward");
+        MappingImpl domainMapping = MappingImpl.domain(Domain.of("domain"));
+        MappingsImpl mappingsImpl = MappingsImpl.builder()
+                .add(regexMapping)
+                .add(forwardMapping)
+                .add(domainMapping)
+                .build();
+        assertThat(mappingsImpl).containsExactly(domainMapping, forwardMapping, regexMapping);
+    }
 }


---------------------------------------------------------------------
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