james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [18/22] james-project git commit: JAMES-1894 Fix in-memory sorting for SentDate
Date Wed, 11 Jan 2017 16:45:45 GMT
JAMES-1894 Fix in-memory sorting for SentDate


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

Branch: refs/heads/master
Commit: 0ca60fb6a38da4573f64a4574da71a529432822e
Parents: 299addd
Author: Benoit Tellier <btellier@linagora.com>
Authored: Thu Dec 22 16:46:39 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Mon Jan 9 22:01:06 2017 +0700

----------------------------------------------------------------------
 .../comparator/BaseSubjectComparator.java       |  17 +--
 .../search/comparator/CombinedComparator.java   | 116 ++++++++--------
 .../comparator/HeaderDisplayComparator.java     |  31 +----
 .../comparator/HeaderMailboxComparator.java     |  45 +------
 .../comparator/InternalDateComparator.java      |  14 +-
 .../search/comparator/ReverseComparator.java    |  18 ++-
 .../search/comparator/SentDateComparator.java   |  23 +---
 .../store/search/comparator/SizeComparator.java |  15 +--
 .../store/search/comparator/UidComparator.java  |  14 +-
 .../store/search/CombinedComparatorTest.java    | 133 +++++++++++++++++++
 10 files changed, 228 insertions(+), 198 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
index 2a02494..ba38ce3 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
@@ -25,13 +25,7 @@ import org.apache.james.mailbox.store.search.SearchUtil;
 
 public class BaseSubjectComparator extends AbstractHeaderComparator{
 
-
-
-    private final static Comparator<MailboxMessage> BASESUBJECT = new BaseSubjectComparator();
-    private final static Comparator<MailboxMessage> REVERSE_BASESUBJECT = new ReverseComparator(BASESUBJECT);
-
-    
-    
+    public final static Comparator<MailboxMessage> BASESUBJECT = new BaseSubjectComparator();
     private final static String SUBJECT = "subject";
     
     @Override
@@ -41,13 +35,4 @@ public class BaseSubjectComparator extends AbstractHeaderComparator{
 
         return baseSubject1.compareToIgnoreCase(baseSubject2);
     }
-
-
-    public static Comparator<MailboxMessage> baseSubject(boolean reverse){
-        if (reverse) {
-            return REVERSE_BASESUBJECT;
-        } else {
-            return BASESUBJECT;
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
index 002aea2..9295480 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
@@ -18,27 +18,84 @@
  ****************************************************************/
 package org.apache.james.mailbox.store.search.comparator;
 
-import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
 
+import org.apache.commons.lang.NotImplementedException;
 import org.apache.james.mailbox.model.SearchQuery.Sort;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.FluentIterable;
+
 /**
  * {@link Comparator} which takes a Array of other {@link Comparator}'s and use them to compare
two {@link MailboxMessage} instances till one of them
  * return <> 0
- *
  */
 public class CombinedComparator implements Comparator<MailboxMessage>{
 
-    private final Comparator<MailboxMessage>[] comparators;
-    public CombinedComparator(Comparator<MailboxMessage>[] comparators) {
-        if(comparators == null || comparators.length < 1) {
-            throw new IllegalArgumentException();
+    public static CombinedComparator create(List<Sort> sorts) {
+        Preconditions.checkNotNull(sorts);
+        Preconditions.checkArgument(!sorts.isEmpty());
+        return new CombinedComparator(FluentIterable.from(sorts)
+            .transform(toComparator())
+            .toList());
+    }
+
+    private static Function<Sort, Comparator<MailboxMessage>> toComparator()
{
+        return new Function<Sort, Comparator<MailboxMessage>>() {
+            @Override
+            public Comparator<MailboxMessage> apply(Sort input) {
+                return optionalResverse(toComparator(input), input.isReverse());
+            }
+        };
+    }
+
+    private static Comparator<MailboxMessage> toComparator(Sort sort) {
+        switch (sort.getSortClause()) {
+            case Arrival:
+                return InternalDateComparator.INTERNALDATE;
+            case MailboxCc:
+                return HeaderMailboxComparator.CC_COMPARATOR;
+            case MailboxFrom:
+                return HeaderMailboxComparator.FROM_COMPARATOR;
+            case Size:
+                return SizeComparator.SIZE;
+            case BaseSubject:
+                return BaseSubjectComparator.BASESUBJECT;
+            case MailboxTo:
+                return HeaderMailboxComparator.TO_COMPARATOR;
+            case Uid:
+                return UidComparator.UID;
+            case SentDate:
+                return SentDateComparator.SENTDATE;
+            case DisplayFrom:
+                return HeaderDisplayComparator.FROM_COMPARATOR;
+            case DisplayTo:
+                return HeaderDisplayComparator.TO_COMPARATOR;
+            default:
+                throw new NotImplementedException("Combined comparator does not support sort
" + sort.getSortClause());
         }
+    }
+
+    private static Comparator<MailboxMessage> optionalResverse(Comparator<MailboxMessage>
comparator, boolean isReverse) {
+        if (isReverse) {
+            return new ReverseComparator(comparator);
+        }
+        return comparator;
+    }
+
+    private final List<Comparator<MailboxMessage>> comparators;
+
+    public List<Comparator<MailboxMessage>> getComparators() {
+        return comparators;
+    }
+
+    private CombinedComparator(List<Comparator<MailboxMessage>> comparators)
{
         this.comparators = comparators;
     }
+
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         int i = 0;
@@ -50,52 +107,5 @@ public class CombinedComparator implements Comparator<MailboxMessage>{
         }
         return i;
     }
-    
-    @SuppressWarnings("unchecked")
-    public static Comparator<MailboxMessage> create(List<Sort> sorts) {
-        List<Comparator<MailboxMessage>> comps = new ArrayList<Comparator<MailboxMessage>>();
-        for (Sort sort : sorts) {
-            boolean reverse = sort.isReverse();
-            Comparator<MailboxMessage> comparator = null;
-
-            switch (sort.getSortClause()) {
-                case Arrival:
-                    comparator = InternalDateComparator.internalDate(reverse);
-                    break;
-                case MailboxCc:
-                    comparator = HeaderMailboxComparator.cc(reverse);
-                    break;
-                case MailboxFrom:
-                    comparator = HeaderMailboxComparator.from(reverse);
-                    break;
-                case Size:
-                    comparator = SizeComparator.size(reverse);
-                    break;
-                case BaseSubject:
-                    comparator = BaseSubjectComparator.baseSubject(reverse);
-                    break;
-                case MailboxTo:
-                    comparator = HeaderMailboxComparator.to(reverse);
-                    break;
-                case Uid:
-                    comparator = UidComparator.uid(reverse);
-                    break;
-                case SentDate:
-                    comparator = SentDateComparator.sentDate(reverse);
-                case DisplayFrom:
-                    comparator = HeaderDisplayComparator.from(reverse);
-                    break;
-                case DisplayTo:
-                    comparator = HeaderDisplayComparator.to(reverse);
-                    break;
-                default:
-                    break;
-            }
-            if (comparator != null) {
-                comps.add(comparator);
-            }
-        }
-        return new CombinedComparator(comps.toArray(new Comparator[0]));
-    }
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderDisplayComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderDisplayComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderDisplayComparator.java
index 81c474a..bd1790f 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderDisplayComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderDisplayComparator.java
@@ -24,44 +24,19 @@ import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.apache.james.mailbox.store.search.SearchUtil;
 
 public class HeaderDisplayComparator extends AbstractHeaderComparator{
+    public final static Comparator<MailboxMessage> FROM_COMPARATOR = new HeaderDisplayComparator(FROM);
+    public final static Comparator<MailboxMessage> TO_COMPARATOR = new HeaderDisplayComparator(TO);
 
-
-    private final static Comparator<MailboxMessage> FROM_COMPARATOR = new HeaderDisplayComparator(FROM);
-    private final static Comparator<MailboxMessage> REVERSE_FROM_COMPARATOR = new ReverseComparator(FROM_COMPARATOR);
-
-
-    private final static Comparator<MailboxMessage> TO_COMPARATOR = new HeaderDisplayComparator(TO);
-    private final static Comparator<MailboxMessage> REVERSE_TO_COMPARATOR = new ReverseComparator(TO_COMPARATOR);
-
-    
     private final String headerName;
 
     public HeaderDisplayComparator(String headerName) {
         this.headerName = headerName;
     }
-    
+
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         String display1 = SearchUtil.getDisplayAddress(getHeaderValue(headerName, o1));
         String display2 = SearchUtil.getDisplayAddress(getHeaderValue(headerName, o2));
         return display1.compareToIgnoreCase(display2);
     }
-
-    
-    public static Comparator<MailboxMessage> from(boolean reverse) {
-        if (reverse) {
-            return REVERSE_FROM_COMPARATOR;
-        } else {
-            return FROM_COMPARATOR;
-        }
-    }
-
-    
-    public static Comparator<MailboxMessage> to(boolean reverse) {
-        if (reverse) {
-            return REVERSE_TO_COMPARATOR;
-        } else {
-            return TO_COMPARATOR;
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
index 0acb052..f21befd 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
@@ -23,26 +23,19 @@ import java.util.Comparator;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.apache.james.mailbox.store.search.SearchUtil;
 
+import com.google.common.base.Objects;
+
 public class HeaderMailboxComparator extends AbstractHeaderComparator{
+    public final static Comparator<MailboxMessage> FROM_COMPARATOR = new HeaderMailboxComparator(FROM);
+    public final static Comparator<MailboxMessage> TO_COMPARATOR = new HeaderMailboxComparator(TO);
+    public final static Comparator<MailboxMessage> CC_COMPARATOR = new HeaderMailboxComparator(CC);
 
     private final String headerName;
 
-    private final static Comparator<MailboxMessage> FROM_COMPARATOR = new HeaderMailboxComparator(FROM);
-    private final static Comparator<MailboxMessage> REVERSE_FROM_COMPARATOR = new ReverseComparator(FROM_COMPARATOR);
-
-
-    private final static Comparator<MailboxMessage> TO_COMPARATOR = new HeaderMailboxComparator(TO);
-    private final static Comparator<MailboxMessage> REVERSE_TO_COMPARATOR = new ReverseComparator(TO_COMPARATOR);
-
-
-    private final static Comparator<MailboxMessage> CC_COMPARATOR = new HeaderMailboxComparator(CC);
-    private final static Comparator<MailboxMessage> REVERSE_CC_COMPARATOR = new ReverseComparator(CC_COMPARATOR);
-
-    
     public HeaderMailboxComparator(String headerName) {
         this.headerName = headerName;
     }
-    
+
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         String mailbox1 = SearchUtil.getMailboxAddress(getHeaderValue(headerName, o1));
@@ -50,31 +43,5 @@ public class HeaderMailboxComparator extends AbstractHeaderComparator{
 
         return mailbox1.compareToIgnoreCase(mailbox2);
     }
-    
-
-    
-    public static Comparator<MailboxMessage> from(boolean reverse) {
-        if (reverse) {
-            return REVERSE_FROM_COMPARATOR;
-        } else {
-            return FROM_COMPARATOR;
-        }
-    }
-    
-    public static Comparator<MailboxMessage> cc(boolean reverse) {
-        if (reverse) {
-            return REVERSE_CC_COMPARATOR;
-        } else {
-            return CC_COMPARATOR;
-        }
-    }
-    
-    public static Comparator<MailboxMessage> to(boolean reverse) {
-        if (reverse) {
-            return REVERSE_TO_COMPARATOR;
-        } else {
-            return TO_COMPARATOR;
-        }
-    }
 }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
index 4f70337..51de2eb 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
@@ -24,25 +24,13 @@ import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
 /**
  * {@link Comparator} which compares {@link MailboxMessage}'s with their {@link MailboxMessage#getInternalDate()}
value
- *
  */
 public class InternalDateComparator  implements Comparator<MailboxMessage>{
+    public final static Comparator<MailboxMessage> INTERNALDATE = new InternalDateComparator();
 
-
-    private final static Comparator<MailboxMessage> INTERNALDATE = new InternalDateComparator();
-    private final static Comparator<MailboxMessage> REVERSE_INTERNALDATE = new ReverseComparator(INTERNALDATE);
-
-    
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         return (o1.getInternalDate().compareTo(o2.getInternalDate()));
     }
 
-    public static Comparator<MailboxMessage> internalDate(boolean reverse){
-        if (reverse) {
-            return REVERSE_INTERNALDATE;
-        } else {
-            return INTERNALDATE;
-        }
-    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
index 5395482..c8d17ac 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
@@ -22,19 +22,35 @@ import java.util.Comparator;
 
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
+import com.google.common.base.Objects;
+
 /**
  * {@link Comparator} which wraps an other {@link Comparator} and reverse it
- *
  */
 public class ReverseComparator implements Comparator<MailboxMessage>{
 
     private final Comparator<MailboxMessage> comparator;
+
     public ReverseComparator(Comparator<MailboxMessage> comparator) {
         this.comparator = comparator;
     }
+
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         return comparator.compare(o2, o1);
     }
 
+    @Override
+    public boolean equals(Object o) {
+        if (o instanceof ReverseComparator) {
+            ReverseComparator that = (ReverseComparator) o;
+            return Objects.equal(this.comparator, that.comparator);
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(comparator);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
index 1c4e759..8a1373e 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
@@ -32,18 +32,8 @@ import org.apache.james.mime4j.field.datetime.parser.ParseException;
  *
  */
 public class SentDateComparator extends AbstractHeaderComparator {
+    public final static Comparator<MailboxMessage> SENTDATE = new SentDateComparator();
 
-
-
-    private final static Comparator<MailboxMessage> SENTDATE = new SentDateComparator(false);
-    private final static Comparator<MailboxMessage> REVERSE_SENTDATE = new ReverseComparator(new
SentDateComparator(true));
-    
-    private final boolean reverse;
-
-    public SentDateComparator(boolean reverse) {
-        this.reverse = reverse;
-    }
-    
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         Date date1 = getSentDate(o1);
@@ -52,7 +42,7 @@ public class SentDateComparator extends AbstractHeaderComparator {
         
         // sent date was the same so use the uid as tie-breaker
         if (i == 0) {
-            return UidComparator.uid(reverse).compare(o1, o2);
+            return UidComparator.UID.compare(o1, o2);
         }
         return 0;
     }
@@ -68,13 +58,4 @@ public class SentDateComparator extends AbstractHeaderComparator {
             return message.getInternalDate();
         }
     }
-    
-    public static Comparator<MailboxMessage> sentDate(boolean reverse){
-        if (reverse) {
-            return REVERSE_SENTDATE;
-        } else {
-            return SENTDATE;
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
index 959383b..2717226 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
@@ -24,25 +24,12 @@ import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
 /**
  * {@link Comparator} which compares {@link MailboxMessage}'s with their {@link MailboxMessage#getFullContentOctets()}
value
- *
  */
 public class SizeComparator implements Comparator<MailboxMessage>{
+    public final static Comparator<MailboxMessage> SIZE = new SizeComparator();
 
-
-    private final static Comparator<MailboxMessage> SIZE = new SizeComparator();
-    private final static Comparator<MailboxMessage> REVERSE_SIZE = new ReverseComparator(SIZE);
-
-    
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         return (int) (o1.getFullContentOctets() - o2.getFullContentOctets());
     }
-
-    public static Comparator<MailboxMessage> size(boolean reverse) {
-        if (reverse) {
-            return REVERSE_SIZE;
-        } else {
-            return SIZE;
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
index a7a2952..2fa187f 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
@@ -24,25 +24,13 @@ import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
 /**
  * {@link Comparator} which compares {@link MailboxMessage}'s with their {@link MailboxMessage#getUid()}
value
- *
  */
 public class UidComparator implements Comparator<MailboxMessage>{
+    public final static Comparator<MailboxMessage> UID = new UidComparator();
 
-
-    private final static Comparator<MailboxMessage> UID = new UidComparator();
-    private final static Comparator<MailboxMessage> REVERSE_UID = new ReverseComparator(UID);
-
-    
     @Override
     public int compare(MailboxMessage o1, MailboxMessage o2) {
         return o1.getUid().compareTo(o2.getUid());
     }
 
-    public static Comparator<MailboxMessage> uid(boolean reverse){
-        if (reverse) {
-            return REVERSE_UID;
-        } else {
-            return UID;
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ca60fb6/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java
new file mode 100644
index 0000000..be69207
--- /dev/null
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java
@@ -0,0 +1,133 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.store.search;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.commons.lang.NotImplementedException;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.store.search.comparator.BaseSubjectComparator;
+import org.apache.james.mailbox.store.search.comparator.CombinedComparator;
+import org.apache.james.mailbox.store.search.comparator.HeaderDisplayComparator;
+import org.apache.james.mailbox.store.search.comparator.HeaderMailboxComparator;
+import org.apache.james.mailbox.store.search.comparator.InternalDateComparator;
+import org.apache.james.mailbox.store.search.comparator.ReverseComparator;
+import org.apache.james.mailbox.store.search.comparator.SentDateComparator;
+import org.apache.james.mailbox.store.search.comparator.SizeComparator;
+import org.apache.james.mailbox.store.search.comparator.UidComparator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.google.common.collect.ImmutableList;
+
+public class CombinedComparatorTest {
+
+    public static final boolean REVERSE = true;
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void createShouldThrowOnNullListOfSort() {
+        expectedException.expect(NullPointerException.class);
+
+        CombinedComparator.create(null);
+    }
+
+    @Test
+    public void createShouldThrowOnEmptySort() {
+        expectedException.expect(IllegalArgumentException.class);
+
+        CombinedComparator.create(ImmutableList.<SearchQuery.Sort>of());
+    }
+
+    @Test
+    public void createShouldConvertInternalDate() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.Arrival))).getComparators())
+            .containsOnly(InternalDateComparator.INTERNALDATE);
+    }
+
+    @Test
+    public void createShouldConvertCc() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.MailboxCc))).getComparators())
+            .containsOnly(HeaderMailboxComparator.CC_COMPARATOR);
+    }
+
+    @Test
+    public void createShouldConvertFrom() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.MailboxFrom))).getComparators())
+            .containsOnly(HeaderMailboxComparator.FROM_COMPARATOR);
+    }
+
+    @Test
+    public void createShouldConvertTo() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.MailboxTo))).getComparators())
+            .containsOnly(HeaderMailboxComparator.TO_COMPARATOR);
+    }
+
+    @Test
+    public void createShouldConvertSize() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.Size))).getComparators())
+            .containsOnly(SizeComparator.SIZE);
+    }
+
+    @Test
+    public void createShouldConvertBaseSubject() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.BaseSubject))).getComparators())
+            .containsOnly(BaseSubjectComparator.BASESUBJECT);
+    }
+
+    @Test
+    public void createShouldConvertUid() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.Uid))).getComparators())
+            .containsOnly(UidComparator.UID);
+    }
+
+    @Test
+    public void createShouldConvertSentDate() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.SentDate))).getComparators())
+            .containsOnly(SentDateComparator.SENTDATE);
+    }
+
+    @Test
+    public void createShouldConvertDisplayTo() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.DisplayTo))).getComparators())
+            .containsOnly(HeaderDisplayComparator.TO_COMPARATOR);
+    }
+
+    @Test
+    public void createShouldConvertDisplayFrom() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.DisplayFrom))).getComparators())
+            .containsOnly(HeaderDisplayComparator.FROM_COMPARATOR);
+    }
+
+    @Test
+    public void createShouldThrowOnIdConversion() {
+        expectedException.expect(NotImplementedException.class);
+
+        CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.Id)));
+    }
+
+    @Test
+    public void createShouldReverse() {
+        assertThat(CombinedComparator.create(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.DisplayFrom,
REVERSE))).getComparators())
+            .containsOnly(new ReverseComparator(HeaderDisplayComparator.FROM_COMPARATOR));
+    }
+}


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