Author: eric
Date: Fri May 29 07:56:38 2015
New Revision: 1682388

URL: http://svn.apache.org/r1682388
Log:
Add more tests in mailbox implementations, contributed by Benoit Tellier 
(MAILBOX-73)

Added:
    
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java
    
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java
    
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMapperProvider.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMailboxMapperTest.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMessageMapperTest.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssert.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssertTests.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MapperProvider.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssert.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssertTest.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssert.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssertTest.java
Modified:
    
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java
    james/mailbox/trunk/cassandra/pom.xml
    
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java
    
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapperTest.java
    james/mailbox/trunk/store/pom.xml

Added: 
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java
 (added)
+++ 
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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;
+
+import javax.mail.Flags;
+
+public class FlagsBuilder {
+
+    private Flags internalFlags;
+
+    public FlagsBuilder() {
+        internalFlags = new Flags();
+    }
+
+    public FlagsBuilder add(Flags.Flag... flags) {
+        for(Flags.Flag flag : flags) {
+            internalFlags.add(flag);
+        }
+        return this;
+    }
+
+    public FlagsBuilder add(String... flags) {
+        for(String userFlag : flags) {
+            internalFlags.add(userFlag);
+        }
+        return this;
+    }
+
+    public FlagsBuilder add(Flags flags) {
+        internalFlags.add(flags);
+        return this;
+    }
+
+    public Flags build() {
+        return new Flags(internalFlags);
+    }
+}

Modified: 
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java?rev=1682388&r1=1682387&r2=1682388&view=diff
==============================================================================
--- 
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java
 (original)
+++ 
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java
 Fri May 29 07:56:38 2015
@@ -169,4 +169,37 @@ public class UpdatedFlags {
             return false;
         }
     }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+        if (!(other instanceof UpdatedFlags)) {
+            return false;
+        }
+
+        UpdatedFlags that = (UpdatedFlags) other;
+
+        if (uid != that.uid) {
+            return false;
+        }
+        if (modSeq != that.modSeq) {
+            return false;
+        }
+        if (oldFlags != null ? !oldFlags.equals(that.oldFlags) : that.oldFlags 
!= null) {
+            return false;
+        }
+        return !(newFlags != null ? !newFlags.equals(that.newFlags) : 
that.newFlags != null);
+
+    }
+
+    @Override
+    public int hashCode() {
+        int result = (int) (uid ^ (uid >>> 32));
+        result = 31 * result + (oldFlags != null ? oldFlags.hashCode() : 0);
+        result = 31 * result + (newFlags != null ? newFlags.hashCode() : 0);
+        result = 31 * result + (int) (modSeq ^ (modSeq >>> 32));
+        return result;
+    }
 }

Modified: james/mailbox/trunk/cassandra/pom.xml
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/pom.xml?rev=1682388&r1=1682387&r2=1682388&view=diff
==============================================================================
--- james/mailbox/trunk/cassandra/pom.xml (original)
+++ james/mailbox/trunk/cassandra/pom.xml Fri May 29 07:56:38 2015
@@ -70,6 +70,12 @@
            <version>${cassandra-driver-core.version}</version>
         </dependency>
         <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>apache-james-mailbox-store</artifactId>
+            <scope>test</scope>
+            <type>test-jar</type>
+        </dependency>
+        <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
         </dependency>

Added: 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java
 (added)
+++ 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,44 @@
+/****************************************************************
+ * 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.cassandra;
+
+import org.apache.james.mailbox.AbstractSubscriptionManagerTest;
+import org.apache.james.mailbox.SubscriptionManager;
+import org.apache.james.mailbox.cassandra.mail.CassandraModSeqProvider;
+import org.apache.james.mailbox.cassandra.mail.CassandraUidProvider;
+
+/**
+ * Test Cassandra subscription against some general purpose written code.
+ */
+public class CassandraSubscriptionManagerTest extends 
AbstractSubscriptionManagerTest {
+
+    private static final CassandraClusterSingleton cassandra = 
CassandraClusterSingleton.build();
+    
+    @Override
+    public SubscriptionManager createSubscriptionManager() {
+        return new CassandraSubscriptionManager(
+                new CassandraMailboxSessionMapperFactory(
+                        new CassandraUidProvider(cassandra.getConf()),
+                        new CassandraModSeqProvider(cassandra.getConf()),
+                        cassandra.getConf()
+                )
+        );
+    }
+}

Modified: 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java?rev=1682388&r1=1682387&r2=1682388&view=diff
==============================================================================
--- 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java
 (original)
+++ 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java
 Fri May 29 07:56:38 2015
@@ -18,225 +18,12 @@
  ****************************************************************/
 package org.apache.james.mailbox.cassandra.mail;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import org.apache.james.mailbox.store.mail.model.AbstractMailboxMapperTest;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
 import java.util.UUID;
 
-import org.apache.james.mailbox.cassandra.CassandraClusterSingleton;
-import org.apache.james.mailbox.exception.MailboxException;
-import org.apache.james.mailbox.exception.MailboxNotFoundException;
-import org.apache.james.mailbox.model.MailboxPath;
-import org.apache.james.mailbox.store.mail.model.Mailbox;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * CassandraMailboxMapper unit tests.
- * 
- */
-public class CassandraMailboxMapperTest {
-
-    private static final Logger LOG = 
LoggerFactory.getLogger(CassandraMailboxMapperTest.class);
-    public static final CassandraClusterSingleton CASSANDRA = 
CassandraClusterSingleton.build();
-    private static CassandraMailboxMapper mapper;
-    private static List<SimpleMailbox<UUID>> mailboxList;
-    private static List<MailboxPath> pathsList;
-    private static final int NAMESPACES = 5;
-    private static final int USERS = 5;
-    private static final int MAILBOX_NO = 5;
-    private static final int MAX_RETRY = 100;
-    private static final char SEPARATOR = '%';
-
-    @Before
-    public void setUp() throws Exception {
-        CASSANDRA.ensureAllTables();
-        fillMailboxList();
-        mapper = new CassandraMailboxMapper(CASSANDRA.getConf(), MAX_RETRY);
-        for (SimpleMailbox<UUID> mailbox : mailboxList) {
-            mapper.save(mailbox);
-        }
-    }
-
-    @After
-    public void cleanUp() {
-        CASSANDRA.clearAllTables();
-    }
-
-    /**
-     * Test an ordered scenario with list, delete... methods.
-     * 
-     * @throws Exception
-     */
-    @Test
-    public void testMailboxMapperScenario() throws Exception {
-        testFindMailboxByPath();
-        testFindMailboxWithPathLike();
-        testList();
-        testSave();
-        testDelete();
-        testHasChildren();
-        // testDeleteAllMemberships(); // Ignore this test
-    }
-
-    /**
-     * Test of findMailboxByPath method, of class CassandraMailboxMapper.
-     */
-    private void testFindMailboxByPath() throws Exception {
-        LOG.info("findMailboxByPath");
-        SimpleMailbox<UUID> mailbox;
-        for (MailboxPath path : pathsList) {
-            LOG.info("Searching for " + path);
-            mailbox = (SimpleMailbox<UUID>) mapper.findMailboxByPath(path);
-            assertEquals(path, new MailboxPath(mailbox.getNamespace(), 
mailbox.getUser(), mailbox.getName()));
-        }
-    }
-
-    /**
-     * Test of findMailboxWithPathLike method, of class CassandraMailboxMapper.
-     */
-    private void testFindMailboxWithPathLike() throws Exception {
-        LOG.info("findMailboxWithPathLike");
-        MailboxPath path = pathsList.get(pathsList.size() / 2);
-
-        List<Mailbox<UUID>> result = mapper.findMailboxWithPathLike(path);
-        assertEquals(1, result.size());
-
-        int start = 3;
-        int end = 7;
-        MailboxPath newPath;
-
-        for (int i = start; i < end; i++) {
-            newPath = new MailboxPath(path);
-            newPath.setName(i + newPath.getName() + " " + i);
-            // test for paths with null user
-            if (i % 2 == 0) {
-                newPath.setUser(null);
-            }
-            addMailbox(new SimpleMailbox<>(newPath, 1234));
-        }
-        result = mapper.findMailboxWithPathLike(path);
-        assertEquals(end - start + 1, result.size());
-    }
-
-    /**
-     * Test of list method, of class CassandraMailboxMapper.
-     */
-    private void testList() throws Exception {
-        LOG.info("list");
-        List<Mailbox<UUID>> result = mapper.list();
-        assertEquals(mailboxList.size(), result.size());
-
-    }
-
-    /**
-     * Test of save method and list method, of class CassandraMailboxMapper.
-     */
-    private void testSave() throws Exception {
-        LOG.info("save and mailboxFromResult");
-        final List<Mailbox<UUID>> mailboxes = mapper.list();
-        final SimpleMailbox<UUID> mlbx = mailboxList.get(mailboxList.size() / 
2);
-        Mailbox<UUID> newValue = mailboxes.get(0);
-
-        for (Mailbox<UUID> mailbox : mailboxes) {
-            if (mlbx.getMailboxId().equals(mailbox.getMailboxId())) {
-                newValue = mailbox;
-            }
-        }
-
-        assertEquals(mlbx, newValue);
-        assertEquals(mlbx.getUser(), newValue.getUser());
-        assertEquals(mlbx.getName(), newValue.getName());
-        assertEquals(mlbx.getNamespace(), newValue.getNamespace());
-        assertEquals(mlbx.getMailboxId(), newValue.getMailboxId());
-    }
-
-    /**
-     * Test of delete method, of class CassandraMailboxMapper.
-     */
-    private void testDelete() throws Exception {
-        LOG.info("delete");
-        // delete last 5 mailboxes from mailboxList
-        int offset = 5;
-        int notFoundCount = 0;
-
-        Iterator<SimpleMailbox<UUID>> iterator = 
mailboxList.subList(mailboxList.size() - offset, mailboxList.size()).iterator();
-
-        while (iterator.hasNext()) {
-            SimpleMailbox<UUID> mailbox = iterator.next();
-            mapper.delete(mailbox);
-            iterator.remove();
-            MailboxPath path = new MailboxPath(mailbox.getNamespace(), 
mailbox.getUser(), mailbox.getName());
-            pathsList.remove(path);
-            LOG.info("Removing mailbox: {}", path);
-            try {
-                mapper.findMailboxByPath(path);
-            } catch (MailboxNotFoundException e) {
-                LOG.info("Succesfully removed {}", mailbox);
-                notFoundCount++;
-            }
-        }
-        assertEquals(offset, notFoundCount);
-        assertEquals(mailboxList.size(), mapper.list().size());
-    }
-
-    /**
-     * Test of hasChildren method, of class CassandraMailboxMapper.
-     */
-    private void testHasChildren() throws Exception {
-        LOG.info("hasChildren");
-        String oldName;
-        for (MailboxPath path : pathsList) {
-            final SimpleMailbox<UUID> mailbox = new SimpleMailbox<>(path, 
12455);
-            oldName = mailbox.getName();
-            if (path.getUser().equals("user3")) {
-                mailbox.setName("test");
-            }
-            boolean result = mapper.hasChildren(mailbox, SEPARATOR);
-            mailbox.setName(oldName);
-            if (path.getUser().equals("user3")) {
-                assertTrue(result);
-            } else {
-                assertFalse(result);
-            }
-
-        }
-    }
-
-    private static void fillMailboxList() {
-        mailboxList = new ArrayList<>();
-        pathsList = new ArrayList<>();
-        MailboxPath path;
-        String name;
-        for (int i = 0; i < NAMESPACES; i++) {
-            for (int j = 0; j < USERS; j++) {
-                for (int k = 0; k < MAILBOX_NO; k++) {
-                    if (j == 3) {
-                        name = "test" + SEPARATOR + "subbox" + k;
-                    } else {
-                        name = "mailbox" + k;
-                    }
-                    path = new MailboxPath("namespace" + i, "user" + j, name);
-                    pathsList.add(path);
-                    mailboxList.add(new SimpleMailbox<>(path, 13));
-                }
-            }
-        }
-        LOG.info("Created test case with {} mailboxes and {} paths", 
mailboxList.size(), pathsList.size());
-    }
-
-    private void addMailbox(SimpleMailbox<UUID> mailbox) throws 
MailboxException {
-        mailboxList.add(mailbox);
-        pathsList.add(new MailboxPath(mailbox.getNamespace(), 
mailbox.getUser(), mailbox.getName()));
-        mapper.save(mailbox);
-        LOG.info("Added new mailbox: {} paths: {}", mailboxList.size(), 
pathsList.size());
+public class CassandraMailboxMapperTest extends 
AbstractMailboxMapperTest<UUID> {
+    public CassandraMailboxMapperTest() {
+        super(new CassandraMapperProvider());
     }
 }

Added: 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMapperProvider.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMapperProvider.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMapperProvider.java
 (added)
+++ 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMapperProvider.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,49 @@
+package org.apache.james.mailbox.cassandra.mail;
+
+import org.apache.james.mailbox.cassandra.CassandraClusterSingleton;
+import org.apache.james.mailbox.cassandra.CassandraMailboxSessionMapperFactory;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.model.MapperProvider;
+
+import java.util.UUID;
+
+public class CassandraMapperProvider implements MapperProvider<UUID> {
+
+    private static final CassandraClusterSingleton cassandra = 
CassandraClusterSingleton.build();
+
+    @Override
+    public MailboxMapper<UUID> createMailboxMapper() throws MailboxException {
+        return new CassandraMailboxSessionMapperFactory(
+            new CassandraUidProvider(cassandra.getConf()),
+            new CassandraModSeqProvider(cassandra.getConf()),
+            cassandra.getConf()
+        ).getMailboxMapper(new MockMailboxSession("benwa"));
+    }
+
+    @Override
+    public MessageMapper<UUID> createMessageMapper() throws MailboxException {
+        return new CassandraMailboxSessionMapperFactory(
+            new CassandraUidProvider(cassandra.getConf()),
+            new CassandraModSeqProvider(cassandra.getConf()),
+            cassandra.getConf()
+        ).getMessageMapper(new MockMailboxSession("benwa"));
+    }
+
+    @Override
+    public UUID generateId() {
+        return UUID.randomUUID();
+    }
+
+    @Override
+    public void clearMapper() throws MailboxException {
+        cassandra.clearAllTables();
+    }
+
+    @Override
+    public void ensureMapperPrepared() throws MailboxException {
+        cassandra.ensureAllTables();
+    }
+}

Modified: 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapperTest.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapperTest.java?rev=1682388&r1=1682387&r2=1682388&view=diff
==============================================================================
--- 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapperTest.java
 (original)
+++ 
james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapperTest.java
 Fri May 29 07:56:38 2015
@@ -18,260 +18,12 @@
  ****************************************************************/
 package org.apache.james.mailbox.cassandra.mail;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import org.apache.james.mailbox.store.mail.model.AbstractMessageMapperTest;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Random;
 import java.util.UUID;
 
-import javax.mail.Flags;
-import javax.mail.internet.SharedInputStream;
-import javax.mail.util.SharedByteArrayInputStream;
-
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.cassandra.CassandraClusterSingleton;
-import org.apache.james.mailbox.exception.MailboxException;
-import org.apache.james.mailbox.mock.MockMailboxSession;
-import org.apache.james.mailbox.model.MailboxPath;
-import org.apache.james.mailbox.model.MessageRange;
-import org.apache.james.mailbox.store.mail.MessageMapper;
-import org.apache.james.mailbox.store.mail.model.Mailbox;
-import org.apache.james.mailbox.store.mail.model.Message;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMessage;
-import org.junit.Before;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.datastax.driver.core.Session;
-
-/**
- * Unit tests for CassandraMessageMapper.
- * 
- */
-public class CassandraMessageMapperTest {
-
-    private static final Logger LOG = 
LoggerFactory.getLogger(CassandraMailboxMapperTest.class);
-    public static final CassandraClusterSingleton CLUSTER = 
CassandraClusterSingleton.build();
-    private static CassandraUidProvider uidProvider;
-    private static CassandraModSeqProvider modSeqProvider;
-    private static CassandraMessageMapper messageMapper;
-    private static final List<MailboxPath> MBOX_PATHS = new 
ArrayList<MailboxPath>();
-    private static final List<Mailbox<UUID>> MBOXES = new 
ArrayList<Mailbox<UUID>>();
-    private static final List<Message<UUID>> MESSAGE_NO = new 
ArrayList<Message<UUID>>();
-    private static final int COUNT = 5;
-    private static Session session;
-    /*
-     * we mock a simple message content
-     */
-    private static final String messageTemplate = "Date: Mon, 7 Feb 1994 
21:52:25 -0800 (PST)\n" + "From: Fred Foobar <foo...@blurdybloop.com>\n" + 
"Subject: Test 02\n" + "To: mo...@owatagu.siam.edu\n" + "Message-Id: 
<b27397-0100...@blurdybloop.com>\n" + "MIME-Version: 1.0\n"
-            + "Content-Type: TEXT/PLAIN; CHARSET=US-ASCII\n" + "\n" + "Test\n" 
+ "\n.";
-
-    private static SharedInputStream content = new 
SharedByteArrayInputStream(messageTemplate.getBytes());
-
-    @Before
-    public void setUp() throws Exception {
-        CLUSTER.ensureAllTables();
-        CLUSTER.clearAllTables();
-        session = CLUSTER.getConf();
-        uidProvider = new CassandraUidProvider(session);
-        modSeqProvider = new CassandraModSeqProvider(session);
-        generateTestData();
-        final MailboxSession mailboxSession = new MockMailboxSession("ieugen");
-        messageMapper = new CassandraMessageMapper(session, uidProvider, 
modSeqProvider, mailboxSession);
-        for (int i = 0; i < MESSAGE_NO.size(); i++) {
-            messageMapper.add(MBOXES.get(1), MESSAGE_NO.get(i));
-        }
-    }
-
-    public static void generateTestData() {
-        final Random random = new Random();
-        MailboxPath mboxPath;
-        final PropertyBuilder propBuilder = new PropertyBuilder();
-        SimpleMailbox<UUID> mailbox;
-
-        for (int i = 0; i < COUNT; i++) {
-            if (i % 2 == 0) {
-                mboxPath = new MailboxPath("gsoc", "ieugen" + i, "INBOX");
-            } else {
-                mboxPath = new MailboxPath("gsoc", "ieugen" + i, "INBOX.box" + 
i);
-            }
-            MBOX_PATHS.add(mboxPath);
-            mailbox = new SimpleMailbox<UUID>(MBOX_PATHS.get(i), 
random.nextLong());
-            mailbox.setMailboxId(UUID.randomUUID());
-            MBOXES.add(mailbox);
-            propBuilder.setProperty("gsoc", "prop" + i, "value");
-        }
-        propBuilder.setMediaType("text");
-        propBuilder.setSubType("html");
-        propBuilder.setTextualLineCount(2L);
-
-        SimpleMessage<UUID> myMsg;
-        final Flags flags = new Flags(Flags.Flag.RECENT);
-        final Date today = new Date();
-
-        for (int i = 0; i < COUNT * 2; i++) {
-            myMsg = new SimpleMessage<UUID>(today, 
messageTemplate.getBytes().length, messageTemplate.getBytes().length - 20, 
content, flags, propBuilder, MBOXES.get(1).getMailboxId());
-            if (i == COUNT * 2 - 1) {
-                flags.add(Flags.Flag.SEEN);
-                flags.remove(Flags.Flag.RECENT);
-                myMsg.setFlags(flags);
-            }
-            MESSAGE_NO.add(myMsg);
-        }
-    }
-
-    /**
-     * Test an ordered scenario with count, find, add... methods.
-     * 
-     * @throws Exception
-     */
-    @Test
-    public void testMessageMapperScenario() throws Exception {
-        testCountMessagesInMailbox();
-        testCountUnseenMessagesInMailbox();
-        testFindFirstUnseenMessageUid();
-        testFindRecentMessageUidsInMailbox();
-        testAdd();
-        testGetLastUid();
-        testGetHighestModSeq();
-        testMessageUpdateReplace();
-        testMessageUpdateAddition();
-    }
-
-    /**
-     * Test message flag replacement
-     */
-    private void testMessageUpdateReplace() throws MailboxException {
-        LOG.info("message update : replace flags");
-        Flags flags = new Flags();
-        flags.add(Flags.Flag.ANSWERED);
-        flags.add(Flags.Flag.DRAFT);
-        messageMapper.updateFlags(MBOXES.get(1), flags, true, true, 
MessageRange.all());
-        Iterator<Message<UUID>> messageIterator = 
messageMapper.findInMailbox(MBOXES.get(1), MessageRange.all(), 
MessageMapper.FetchType.Full, 100);
-        while(messageIterator.hasNext()) {
-            Message<UUID> message = messageIterator.next();
-            assertTrue(message.isAnswered());
-            assertTrue(message.isDraft());
-            assertFalse(message.isDeleted());
-            assertFalse(message.isRecent());
-            assertFalse(message.isSeen());
-            assertFalse(message.isFlagged());
-        }
-    }
-
-    /**
-     * Test message flag set to true
-     */
-    private void testMessageUpdateAddition() throws MailboxException {
-        LOG.info("message update : flag addition");
-        Flags flags = new Flags();
-        flags.add(Flags.Flag.FLAGGED);
-        messageMapper.updateFlags(MBOXES.get(1), flags, true, false, 
MessageRange.all());
-        Iterator<Message<UUID>> messageIterator = 
messageMapper.findInMailbox(MBOXES.get(1), MessageRange.all(), 
MessageMapper.FetchType.Full, 100);
-        while(messageIterator.hasNext()) {
-            Message<UUID> message = messageIterator.next();
-            assertTrue(message.isAnswered());
-            assertTrue(message.isDraft());
-            assertFalse(message.isDeleted());
-            assertFalse(message.isRecent());
-            assertFalse(message.isSeen());
-            assertTrue(message.isFlagged());
-        }
-    }
-
-    /**
-     * Test message flag removal
-     */
-    private void testMessageUpdateRemove() throws MailboxException {
-        LOG.info("message update : flag removal");
-        Flags flags = new Flags();
-        flags.add(Flags.Flag.ANSWERED);
-        messageMapper.updateFlags(MBOXES.get(1), flags, false, false, 
MessageRange.all());
-        Iterator<Message<UUID>> messageIterator = 
messageMapper.findInMailbox(MBOXES.get(1), MessageRange.all(), 
MessageMapper.FetchType.Full, 100);
-        while(messageIterator.hasNext()) {
-            Message<UUID> message = messageIterator.next();
-            assertFalse(message.isAnswered());
-            assertTrue(message.isDraft());
-            assertFalse(message.isDeleted());
-            assertFalse(message.isRecent());
-            assertFalse(message.isSeen());
-            assertTrue(message.isFlagged());
-        }
-    }
-
-    /**
-     * Test of countMessagesInMailbox method, of class CassandraMessageMapper.
-     */
-    private void testCountMessagesInMailbox() throws Exception {
-        LOG.info("countMessagesInMailbox");
-        long messageCount = 
messageMapper.countMessagesInMailbox(MBOXES.get(1));
-        assertEquals(MESSAGE_NO.size(), messageCount);
-    }
-
-    /**
-     * Test of countUnseenMessagesInMailbox method, of class
-     * CassandraMessageMapper.
-     */
-    private void testCountUnseenMessagesInMailbox() throws Exception {
-        LOG.info("countUnseenMessagesInMailbox");
-        long unseen = 
messageMapper.countUnseenMessagesInMailbox(MBOXES.get(1));
-        assertEquals(MESSAGE_NO.size() - 1, unseen);
-    }
-
-    /**
-     * Test of findFirstUnseenMessageUid method, of class
-     * CassandraMessageMapper.
-     */
-    private void testFindFirstUnseenMessageUid() throws Exception {
-        LOG.info("findFirstUnseenMessageUid");
-        final long uid = 
messageMapper.findFirstUnseenMessageUid(MBOXES.get(1));
-        assertEquals(1, uid);
-    }
-
-    /**
-     * Test of findRecentMessageUidsInMailbox method, of class
-     * CassandraMessageMapper.
-     */
-    private void testFindRecentMessageUidsInMailbox() throws Exception {
-        LOG.info("findRecentMessageUidsInMailbox");
-        List<Long> recentMessages = 
messageMapper.findRecentMessageUidsInMailbox(MBOXES.get(1));
-        assertEquals(MESSAGE_NO.size() - 1, recentMessages.size());
-    }
-
-    /**
-     * Test of add method, of class CassandraMessageMapper.
-     */
-    private void testAdd() throws Exception {
-        LOG.info("add");
-        // The tables should be deleted every time the tests run.
-        long msgCount = messageMapper.countMessagesInMailbox(MBOXES.get(1));
-        LOG.info(msgCount + " " + MESSAGE_NO.size());
-        assertEquals(MESSAGE_NO.size(), msgCount);
-    }
-
-    /**
-     * Test of getLastUid method, of class CassandraMessageMapper.
-     */
-    private void testGetLastUid() throws Exception {
-        LOG.info("getLastUid");
-        long lastUid = messageMapper.getLastUid(MBOXES.get(1));
-        assertEquals(MESSAGE_NO.size(), lastUid);
-    }
-
-    /**
-     * Test of getHighestModSeq method, of class CassandraMessageMapper.
-     */
-    private void testGetHighestModSeq() throws Exception {
-        LOG.info("getHighestModSeq");
-        long highestModSeq = messageMapper.getHighestModSeq(MBOXES.get(1));
-        assertEquals(MESSAGE_NO.size(), highestModSeq);
+public class CassandraMessageMapperTest extends 
AbstractMessageMapperTest<UUID> {
+    public CassandraMessageMapperTest() {
+        super(new CassandraMapperProvider());
     }
 }

Modified: james/mailbox/trunk/store/pom.xml
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/pom.xml?rev=1682388&r1=1682387&r2=1682388&view=diff
==============================================================================
--- james/mailbox/trunk/store/pom.xml (original)
+++ james/mailbox/trunk/store/pom.xml Fri May 29 07:56:38 2015
@@ -86,5 +86,10 @@
             <type>test-jar</type>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Added: 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMailboxMapperTest.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMailboxMapperTest.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMailboxMapperTest.java
 (added)
+++ 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMailboxMapperTest.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,248 @@
+/****************************************************************
+ * 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.mail.model;
+
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
+
+/**
+ * Generic purpose tests for your implementation MailboxMapper.
+ * 
+ * You then just need to instantiate your mailbox mapper and an IdGenerator.
+ */
+public abstract class AbstractMailboxMapperTest<Id> {
+    
+    private final static char DELIMITER = ':';
+    private final static char WILDCARD = '%';
+    private final static long UID_VALIDITY = 42;
+
+    private MapperProvider<Id> mapperProvider;
+    
+    private MailboxMapper<Id> mailboxMapper;
+
+    private MailboxPath benwaInboxPath;
+    private SimpleMailbox<Id> benwaInboxMailbox;
+    private MailboxPath benwaWorkPath;
+    private SimpleMailbox<Id> benwaWorkMailbox;
+    private MailboxPath benwaWorkTodoPath;
+    private SimpleMailbox<Id> benwaWorkTodoMailbox;
+    private MailboxPath benwaPersoPath;
+    private SimpleMailbox<Id> benwaPersoMailbox;
+    private MailboxPath benwaWorkDonePath;
+    private SimpleMailbox<Id> benwaWorkDoneMailbox;
+    private MailboxPath bobInboxPath;
+    private SimpleMailbox<Id> bobInboxMailbox;
+    private MailboxPath esnDevGroupInboxPath;
+    private SimpleMailbox<Id> esnDevGroupInboxMailbox;
+    private MailboxPath esnDevGroupHublinPath;
+    private SimpleMailbox<Id> esnDevGroupHublinMailbox;
+    private MailboxPath esnDevGroupJamesPath;
+    private SimpleMailbox<Id> esnDevGroupJamesMailbox;
+    private MailboxPath obmTeamGroupInboxPath;
+    private SimpleMailbox<Id> obmTeamGroupInboxMailbox;
+    private MailboxPath obmTeamGroupOPushPath;
+    private SimpleMailbox<Id> obmTeamGroupOPushMailbox;
+    private MailboxPath obmTeamGroupRoundCubePath;
+    private SimpleMailbox<Id> obmTeamGroupRoundCubeMailbox;
+
+    public AbstractMailboxMapperTest(MapperProvider<Id> mapperProvider) {
+        this.mapperProvider = mapperProvider;
+
+        benwaInboxPath = new MailboxPath("#private", "benwa", "INBOX");
+        benwaWorkPath = new MailboxPath("#private", "benwa", 
"INBOX"+DELIMITER+"work");
+        benwaWorkTodoPath = new MailboxPath("#private", "benwa", 
"INBOX"+DELIMITER+"work"+DELIMITER+"todo");
+        benwaPersoPath = new MailboxPath("#private", "benwa", 
"INBOX"+DELIMITER+"perso");
+        benwaWorkDonePath = new MailboxPath("#private", "benwa", 
"INBOX"+DELIMITER+"work"+DELIMITER+"done");
+        bobInboxPath = new MailboxPath("#private", "bob", "INBOX");
+        esnDevGroupInboxPath = new MailboxPath("#community_ESN_DEV", null, 
"INBOX");
+        esnDevGroupHublinPath = new MailboxPath("#community_ESN_DEV", null, 
"INBOX"+DELIMITER+"hublin");
+        esnDevGroupJamesPath = new MailboxPath("#community_ESN_DEV", null, 
"INBOX"+DELIMITER+"james");
+        obmTeamGroupInboxPath = new MailboxPath("#community_OBM_Core_Team", 
null, "INBOX");
+        obmTeamGroupOPushPath = new MailboxPath("#community_OBM_Core_Team", 
null, "INBOX"+DELIMITER+"OPush");
+        obmTeamGroupRoundCubePath = new 
MailboxPath("#community_OBM_Core_Team", null, "INBOX"+DELIMITER+"roundCube");
+
+        benwaInboxMailbox = createMailbox(benwaInboxPath);
+        benwaWorkMailbox = createMailbox(benwaWorkPath);
+        benwaWorkTodoMailbox = createMailbox(benwaWorkTodoPath);
+        benwaPersoMailbox = createMailbox(benwaPersoPath);
+        benwaWorkDoneMailbox = createMailbox(benwaWorkDonePath);
+        bobInboxMailbox = createMailbox(bobInboxPath);
+        esnDevGroupInboxMailbox = createMailbox(esnDevGroupInboxPath);
+        esnDevGroupHublinMailbox = createMailbox(esnDevGroupHublinPath);
+        esnDevGroupJamesMailbox = createMailbox(esnDevGroupJamesPath);
+        obmTeamGroupInboxMailbox = createMailbox(obmTeamGroupInboxPath);
+        obmTeamGroupOPushMailbox = createMailbox(obmTeamGroupOPushPath);
+        obmTeamGroupRoundCubeMailbox = 
createMailbox(obmTeamGroupRoundCubePath);
+    }
+
+    @Before
+    public void setUp() throws MailboxException {
+        mapperProvider.ensureMapperPrepared();
+        mailboxMapper = mapperProvider.createMailboxMapper();
+    }
+
+    @After
+    public void tearDown() throws MailboxException {
+        mapperProvider.clearMapper();
+    }
+
+    @Test(expected=MailboxNotFoundException.class)
+    public void findMailboxByPathWhenAbsentShouldFail() throws 
MailboxException {
+        mailboxMapper.findMailboxByPath(new MailboxPath("#private", "benwa", 
"INBOX"));
+    }
+    
+    @Test
+    public void saveShouldPersistTheMailbox() throws MailboxException{
+        mailboxMapper.save(benwaInboxMailbox);
+        
MailboxAssert.assertThat(mailboxMapper.findMailboxByPath(benwaInboxPath)).isEqualTo(benwaInboxMailbox);
+    }
+
+    @Test
+    public void saveWithNullUserShouldPersistTheMailbox() throws 
MailboxException{
+        mailboxMapper.save(esnDevGroupInboxMailbox);
+        
MailboxAssert.assertThat(mailboxMapper.findMailboxByPath(esnDevGroupInboxPath)).isEqualTo(esnDevGroupInboxMailbox);
+    }
+    
+    @Test
+    public void listShouldRetrieveAllMailbox() throws MailboxException {
+        saveAll();
+        List<Mailbox<Id>> mailboxes = mailboxMapper.list();
+        assertThat(mailboxes).contains(benwaInboxMailbox, benwaWorkMailbox, 
benwaWorkTodoMailbox, benwaPersoMailbox, benwaWorkDoneMailbox, bobInboxMailbox, 
esnDevGroupInboxMailbox, esnDevGroupHublinMailbox,
+            esnDevGroupJamesMailbox, obmTeamGroupInboxMailbox, 
obmTeamGroupOPushMailbox, obmTeamGroupRoundCubeMailbox);
+    }
+    
+    @Test
+    public void hasChildrenShouldReturnFalseWhenNoChildrenExists() throws 
MailboxException {
+        saveAll();
+        assertThat(mailboxMapper.hasChildren(benwaWorkTodoMailbox, 
DELIMITER)).isFalse();
+    }
+
+    @Test
+    public void hasChildrenShouldReturnTrueWhenChildrenExists() throws 
MailboxException {
+        saveAll();
+        assertThat(mailboxMapper.hasChildren(benwaInboxMailbox, 
DELIMITER)).isTrue();
+    }
+
+    @Test
+    public void hasChildrenWithNullUserShouldReturnFalseWhenNoChildrenExists() 
throws MailboxException {
+        saveAll();
+        assertThat(mailboxMapper.hasChildren(esnDevGroupHublinMailbox, 
DELIMITER)).isFalse();
+    }
+
+    @Test
+    public void hasChildrenWithNullUserShouldReturnTrueWhenChildrenExists() 
throws MailboxException {
+        saveAll();
+        assertThat(mailboxMapper.hasChildren(esnDevGroupInboxMailbox, 
DELIMITER)).isTrue();
+    }
+
+    @Test(expected=MailboxNotFoundException.class)
+    public void deleteShouldEraseTheGivenMailbox() throws MailboxException {
+        try {
+            saveAll();
+            mailboxMapper.delete(benwaInboxMailbox);
+        } catch(MailboxException exception) {
+            fail("Error was not thrown by the appropriate method", exception);
+        }
+        mailboxMapper.findMailboxByPath(benwaInboxPath);
+    }
+
+    @Test(expected=MailboxNotFoundException.class)
+    public void deleteWithNullUserShouldEraseTheGivenMailbox() throws 
MailboxException {
+        try {
+            saveAll();
+            mailboxMapper.delete(esnDevGroupJamesMailbox);
+        } catch(MailboxException exception) {
+            fail("Error was not thrown by the appropriate method", exception);
+        }
+        mailboxMapper.findMailboxByPath(esnDevGroupJamesPath);
+    }
+
+    // findMailboxWithPathLike does not behave as intended in Cassandra 
Mailbox implementation
+    // It matches .*namespace.*:.*user.*:.*name%.* instead of 
namespace:user:name.*
+    // For instance see this method in use line 436 of StoreMailboxManager
+    @Ignore
+    @Test
+    public void findMailboxWithPathLikeWithChildRegexShouldRetrieveChildren() 
throws MailboxException {
+        saveAll();
+        MailboxPath regexPath = new MailboxPath(benwaWorkPath.getNamespace(), 
benwaWorkPath.getUser(), benwaWorkPath.getName() + WILDCARD);
+        
assertThat(mailboxMapper.findMailboxWithPathLike(regexPath)).containsOnly(benwaWorkMailbox,
 benwaWorkTodoMailbox, benwaWorkDoneMailbox);
+    }
+
+    // Same thing
+    @Ignore
+    @Test
+    public void 
findMailboxWithPathLikeWithNullUserWithChildRegexShouldRetrieveChildren() 
throws MailboxException {
+        saveAll();
+        MailboxPath regexPath = new 
MailboxPath(obmTeamGroupInboxPath.getNamespace(), 
obmTeamGroupInboxPath.getUser(), obmTeamGroupInboxPath.getName() + WILDCARD);
+        
assertThat(mailboxMapper.findMailboxWithPathLike(regexPath)).contains(obmTeamGroupInboxMailbox,
 obmTeamGroupOPushMailbox, obmTeamGroupRoundCubeMailbox);
+    }
+
+    // Same thing
+    @Ignore
+    @Test
+    public void 
findMailboxWithPathLikeWithRegexShouldRetrieveCorrespondingMailbox() throws 
MailboxException {
+        saveAll();
+        MailboxPath regexPath = new MailboxPath(benwaInboxPath.getNamespace(), 
benwaInboxPath.getUser(), WILDCARD + "X");
+        
assertThat(mailboxMapper.findMailboxWithPathLike(regexPath)).containsOnly(benwaInboxMailbox);
+    }
+
+    // Same thing
+    @Ignore
+    @Test
+    public void 
findMailboxWithPathLikeWithNullUserWithRegexShouldRetrieveCorrespondingMailbox()
 throws MailboxException {
+        saveAll();
+        MailboxPath regexPath = new 
MailboxPath(esnDevGroupInboxPath.getNamespace(), 
esnDevGroupInboxPath.getUser(), WILDCARD + "X");
+        
assertThat(mailboxMapper.findMailboxWithPathLike(regexPath)).contains(esnDevGroupInboxMailbox);
+    }
+    
+    private void saveAll() throws MailboxException{
+        mailboxMapper.save(benwaInboxMailbox);
+        mailboxMapper.save(benwaWorkMailbox);
+        mailboxMapper.save(benwaWorkTodoMailbox);
+        mailboxMapper.save(benwaPersoMailbox);
+        mailboxMapper.save(benwaWorkDoneMailbox);
+        mailboxMapper.save(bobInboxMailbox);
+        mailboxMapper.save(esnDevGroupInboxMailbox);
+        mailboxMapper.save(esnDevGroupHublinMailbox);
+        mailboxMapper.save(esnDevGroupJamesMailbox);
+        mailboxMapper.save(obmTeamGroupInboxMailbox);
+        mailboxMapper.save(obmTeamGroupOPushMailbox);
+        mailboxMapper.save(obmTeamGroupRoundCubeMailbox);
+    }
+
+    private SimpleMailbox<Id> createMailbox(MailboxPath mailboxPath) {
+        SimpleMailbox<Id> mailbox = new SimpleMailbox<Id>(mailboxPath, 
UID_VALIDITY);
+        mailbox.setMailboxId(mapperProvider.generateId());
+        return mailbox;
+    }
+
+}

Added: 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMessageMapperTest.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMessageMapperTest.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMessageMapperTest.java
 (added)
+++ 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractMessageMapperTest.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,566 @@
+/****************************************************************
+ * 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.mail.model;
+
+import org.apache.james.mailbox.FlagsBuilder;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageMetaData;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.model.UpdatedFlags;
+import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMessage;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import javax.mail.Flags;
+import javax.mail.util.SharedByteArrayInputStream;
+import java.io.IOException;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public abstract class AbstractMessageMapperTest<Id> {
+
+    private final static char DELIMITER = ':';
+    private static final int LIMIT = 10;
+    private static final int BODY_START = 16;
+    public static final int UID_VALIDITY = 42;
+
+    private MapperProvider<Id> mapperProvider;
+    private MessageMapper<Id> messageMapper;
+
+    private SimpleMailbox<Id> benwaInboxMailbox;
+    private SimpleMailbox<Id> benwaWorkMailbox;
+    
+    private SimpleMessage<Id> message1;
+    private SimpleMessage<Id> message2;
+    private SimpleMessage<Id> message3;
+    private SimpleMessage<Id> message4;
+    private SimpleMessage<Id> message5;
+    private SimpleMessage<Id> message6;
+
+    public AbstractMessageMapperTest(MapperProvider<Id> mapperProvider) {
+        this.mapperProvider = mapperProvider;
+    }
+
+    @Before
+    public void setUp() throws MailboxException {
+        mapperProvider.ensureMapperPrepared();
+        messageMapper = mapperProvider.createMessageMapper();
+        benwaInboxMailbox = createMailbox(new MailboxPath("#private", "benwa", 
"INBOX"));
+        benwaWorkMailbox = createMailbox( new MailboxPath("#private", "benwa", 
"INBOX"+DELIMITER+"work"));
+        message1 = createMessage(benwaInboxMailbox, "Subject: Test1 
\n\nBody1\n.\n", BODY_START);
+        message2 = createMessage(benwaInboxMailbox, "Subject: Test2 
\n\nBody2\n.\n", BODY_START);
+        message3 = createMessage(benwaInboxMailbox, "Subject: Test3 
\n\nBody3\n.\n", BODY_START);
+        message4 = createMessage(benwaInboxMailbox, "Subject: Test4 
\n\nBody4\n.\n", BODY_START);
+        message5 = createMessage(benwaInboxMailbox, "Subject: Test5 
\n\nBody5\n.\n", BODY_START);
+        message6 = createMessage(benwaWorkMailbox, "Subject: Test6 
\n\nBody6\n.\n", BODY_START);
+    }
+
+    @After
+    public void tearDown() throws MailboxException {
+        mapperProvider.clearMapper();
+    }
+    
+    @Test
+    public void emptyMailboxShouldHaveZeroMessageCount() throws 
MailboxException {
+        
assertThat(messageMapper.countMessagesInMailbox(benwaInboxMailbox)).isEqualTo(0);
+    }
+    
+    @Test
+    public void mailboxContainingMessagesShouldHaveTheGoodMessageCount() 
throws MailboxException {
+        saveMessages();
+        
assertThat(messageMapper.countMessagesInMailbox(benwaInboxMailbox)).isEqualTo(5);
+    }
+
+    @Test
+    public void mailboxCountShouldBeDecrementedAfterAMessageDelete() throws 
MailboxException {
+        saveMessages();
+        messageMapper.delete(benwaInboxMailbox, message1);
+        
assertThat(messageMapper.countMessagesInMailbox(benwaInboxMailbox)).isEqualTo(4);
+    }
+    
+    @Test
+    public void emptyMailboxShouldNotHaveUnseenMessages() throws 
MailboxException {
+        
assertThat(messageMapper.countUnseenMessagesInMailbox(benwaInboxMailbox)).isEqualTo(0);
+    }
+    
+    @Test
+    public void mailboxContainingMessagesShouldHaveTheGoodUnseenMessageCount() 
throws MailboxException {
+        saveMessages();
+        
assertThat(messageMapper.countUnseenMessagesInMailbox(benwaInboxMailbox)).isEqualTo(5);
+    }
+
+    // We should decrement mailbox unseen count when a message is marked as 
read.
+    @Ignore
+    @Test
+    public void 
mailboxUnSeenCountShouldBeDecrementedAfterAMessageIsMarkedSeen() throws 
MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), true, true, 
MessageRange.one(message1.getUid())).hasNext();
+        
assertThat(messageMapper.countUnseenMessagesInMailbox(benwaInboxMailbox)).isEqualTo(4);
+    }
+
+    @Test
+    public void 
mailboxUnSeenCountShouldBeDecrementedAfterAMessageIsMarkedUnSeen() throws 
MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), true, true, MessageRange.one(message1.getUid()));
+        messageMapper.updateFlags(benwaInboxMailbox, new Flags(), true, true, 
MessageRange.one(message1.getUid()));
+        
assertThat(messageMapper.countUnseenMessagesInMailbox(benwaInboxMailbox)).isEqualTo(5);
+    }
+    
+    @Test
+    public void mailboxUnSeenCountShouldBeDecrementedAfterAMessageDelete() 
throws MailboxException {
+        saveMessages();
+        messageMapper.delete(benwaInboxMailbox, message1);
+        
assertThat(messageMapper.countUnseenMessagesInMailbox(benwaInboxMailbox)).isEqualTo(4);
+    }
+
+    @Test
+    public void deletedMessagesShouldBeRemovedFromStorage() throws 
MailboxException {
+        saveMessages();
+        messageMapper.delete(benwaInboxMailbox, message1);
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message1.getUid()), MessageMapper.FetchType.Metadata, 
LIMIT)).isEmpty();
+    }
+
+    @Test
+    public void deletingUnExistingMessageShouldHaveNoSideEffect() throws 
MailboxException, IOException {
+        saveMessages();
+        message6.setUid(messageMapper.getLastUid(benwaInboxMailbox) + 1);
+        messageMapper.delete(benwaInboxMailbox, message6);
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, LIMIT))
+            .containsOnly(message1, message2, message3, message4, message5);
+    }
+
+    @Test
+    public void noMessageShouldBeRetrievedInEmptyMailbox() throws 
MailboxException {
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message1.getUid()), MessageMapper.FetchType.Metadata, 
LIMIT)).isEmpty();
+    }
+
+    @Test
+    public void messagesCanBeRetrievedInMailboxWithRangeTypeOne() throws 
MailboxException, IOException{
+        saveMessages();
+        MessageMapper.FetchType fetchType = MessageMapper.FetchType.Full;
+        int limit =10;
+        
MessageAssert.assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message1.getUid()), fetchType, limit).next())
+            .isEqualTo(message1, fetchType);
+    }
+
+    // Ranges should be inclusive
+    @Ignore
+    @Test
+    public void messagesCanBeRetrievedInMailboxWithRangeTypeRange() throws 
MailboxException, IOException{
+        saveMessages();
+        Iterator<Message<Id>> retrievedMessageIterator = messageMapper
+                .findInMailbox(benwaInboxMailbox, 
MessageRange.range(message1.getUid(), message4.getUid()), 
MessageMapper.FetchType.Full, LIMIT);
+        assertThat(retrievedMessageIterator).containsOnly(message1, message2, 
message3, message4);
+    }
+
+    // Ranges should be inclusive
+    @Ignore
+    @Test
+    public void 
messagesCanBeRetrievedInMailboxWithRangeTypeRangeContainingAHole() throws 
MailboxException, IOException {
+        saveMessages();
+        messageMapper.delete(benwaInboxMailbox, message3);
+        Iterator<Message<Id>> retrievedMessageIterator = messageMapper
+            .findInMailbox(benwaInboxMailbox, 
MessageRange.range(message1.getUid(), message4.getUid()), 
MessageMapper.FetchType.Full, LIMIT);
+        assertThat(retrievedMessageIterator).containsOnly(message1, message2, 
message4);
+    }
+
+    // Ranges should be inclusive
+    @Ignore
+    @Test
+    public void messagesCanBeRetrievedInMailboxWithRangeTypeFrom() throws 
MailboxException, IOException {
+        saveMessages();
+        Iterator<Message<Id>> retrievedMessageIterator = messageMapper
+                .findInMailbox(benwaInboxMailbox, 
MessageRange.from(message3.getUid()), MessageMapper.FetchType.Full, LIMIT);
+        assertThat(retrievedMessageIterator).containsOnly(message3, message4, 
message5);
+    }
+
+    // Ranges should be inclusive
+    @Ignore
+    @Test
+    public void 
messagesCanBeRetrievedInMailboxWithRangeTypeFromContainingAHole() throws 
MailboxException, IOException {
+        saveMessages();
+        messageMapper.delete(benwaInboxMailbox, message4);
+        Iterator<Message<Id>> retrievedMessageIterator = messageMapper
+                .findInMailbox(benwaInboxMailbox, 
MessageRange.from(message3.getUid()), MessageMapper.FetchType.Full, LIMIT);
+        assertThat(retrievedMessageIterator).containsOnly(message3, message5);
+    }
+
+    @Test
+    public void messagesCanBeRetrievedInMailboxWithRangeTypeAll() throws 
MailboxException, IOException {
+        saveMessages();
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, LIMIT))
+            .containsOnly(message1, message2, message3, message4, message5);
+    }
+
+    @Test
+    public void 
messagesCanBeRetrievedInMailboxWithRangeTypeAllContainingHole() throws 
MailboxException, IOException {
+        saveMessages();
+        messageMapper.delete(benwaInboxMailbox, message1);
+        Iterator<Message<Id>> retrievedMessageIterator = messageMapper
+                .findInMailbox(benwaInboxMailbox, MessageRange.all(), 
MessageMapper.FetchType.Full, LIMIT);
+        assertThat(retrievedMessageIterator).containsOnly(message2, message3, 
message4, message5);
+    }
+    
+    @Test
+    public void 
messagesRetrievedUsingFetchTypeMetadataShouldHaveAtLastMetadataDataLoaded() 
throws MailboxException, IOException{
+        saveMessages();
+        MessageMapper.FetchType fetchType = MessageMapper.FetchType.Metadata;
+        Iterator<Message<Id>> retrievedMessageIterator = 
messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message1.getUid()), fetchType, LIMIT);
+        
MessageAssert.assertThat(retrievedMessageIterator.next()).isEqualTo(message1, 
fetchType);
+        assertThat(retrievedMessageIterator).isEmpty();
+    }
+
+    @Test
+    public void 
messagesRetrievedUsingFetchTypeHeaderShouldHaveHeaderDataLoaded() throws 
MailboxException, IOException{
+        saveMessages();
+        MessageMapper.FetchType fetchType = MessageMapper.FetchType.Headers;
+        Iterator<Message<Id>> retrievedMessageIterator = 
messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message1.getUid()), fetchType, LIMIT);
+        
MessageAssert.assertThat(retrievedMessageIterator.next()).isEqualTo(message1, 
fetchType);
+        assertThat(retrievedMessageIterator).isEmpty();
+    }
+
+    @Test
+    public void messagesRetrievedUsingFetchTypeBodyShouldHaveBodyDataLoaded() 
throws MailboxException, IOException{
+        saveMessages();
+        MessageMapper.FetchType fetchType = MessageMapper.FetchType.Body;
+        Iterator<Message<Id>> retrievedMessageIterator = 
messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message1.getUid()), fetchType, LIMIT);
+        
MessageAssert.assertThat(retrievedMessageIterator.next()).isEqualTo(message1, 
fetchType);
+        assertThat(retrievedMessageIterator).isEmpty();
+    }
+
+    // No limit used for the moment
+    @Ignore
+    @Test
+    public void retrievingMessagesWithALimitShouldLimitTheNumberOfMessages() 
throws MailboxException {
+        int limit = 2;
+        saveMessages();
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, limit)).hasSize(2);
+    }
+    
+    @Test
+    public void 
findRecentUidsInMailboxShouldReturnEmptyListWhenNoMessagesMarkedAsRecentArePresentInMailbox()
 throws MailboxException {
+        
assertThat(messageMapper.findRecentMessageUidsInMailbox(benwaInboxMailbox)).isEmpty();
+    }
+
+    @Test
+    public void 
findRecentUidsInMailboxShouldReturnListOfMessagesHoldingFlagsRecent() throws 
MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.RECENT), true, true, MessageRange.one(message2.getUid()));
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.RECENT), true, true, MessageRange.one(message4.getUid()));
+        messageMapper.updateFlags(benwaWorkMailbox, new 
Flags(Flags.Flag.RECENT), true, true, MessageRange.one(message6.getUid()));
+        
assertThat(messageMapper.findRecentMessageUidsInMailbox(benwaInboxMailbox)).containsOnly(message2.getUid(),
 message4.getUid());
+    }
+    
+    @Test
+    public void 
findFirstUnseenMessageUidShouldReturnNullWhenNoUnseenMessagesCanBeFound() 
throws MailboxException {
+        
assertThat(messageMapper.findFirstUnseenMessageUid(benwaInboxMailbox)).isNull();
+    }
+
+    @Test
+    public void findFirstUnseenMessageUidShouldReturnUid1WhenUid1isNotSeen() 
throws MailboxException {
+        saveMessages();
+        
assertThat(messageMapper.findFirstUnseenMessageUid(benwaInboxMailbox)).isEqualTo(message1.getUid());
+    }
+
+    @Test
+    public void findFirstUnseenMessageUidShouldReturnUid2WhenUid2isSeen() 
throws MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), true, true, MessageRange.one(message1.getUid()));
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), true, true, MessageRange.one(message3.getUid()));
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), true, true, MessageRange.one(message5.getUid()));
+        
assertThat(messageMapper.findFirstUnseenMessageUid(benwaInboxMailbox)).isEqualTo(message2.getUid());
+    }
+    
+    @Test
+    public void 
expungeMarkedForDeletionInMailboxShouldReturnEmptyResultOnEmptyMailbox() throws 
MailboxException, IOException {
+        
assertThat(messageMapper.expungeMarkedForDeletionInMailbox(benwaInboxMailbox, 
MessageRange.all())).isEmpty();
+    }
+
+    @Test
+    public void 
expungeMarkedForDeletionInMailboxShouldReturnEmptyResultWhenNoMessageInMailboxIsDeleted()
 throws MailboxException, IOException {
+        saveMessages();
+        
assertThat(messageMapper.expungeMarkedForDeletionInMailbox(benwaInboxMailbox, 
MessageRange.all())).isEmpty();
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, LIMIT))
+            .containsOnly(message1, message2, message3, message4, message5);
+    }
+
+    @Test
+    public void expungeShouldReturnCorrectMetadataWithRangeAll() throws 
MailboxException, IOException {
+        saveMessages();
+        
MetadataMapAssert.assertThat(markThenPerformExpunge(MessageRange.all()))
+            .hasSize(2)
+            .containsMetadataForMessages(message1, message4);
+    }
+
+    @Test
+    public void expungeShouldModifyUnderlyingStorageWithRangeAll() throws 
MailboxException, IOException {
+        saveMessages();
+        markThenPerformExpunge(MessageRange.all());
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, LIMIT))
+            .containsOnly(message2, message3, message5);
+    }
+
+    @Test
+    public void expungeShouldReturnCorrectMetadataWithRangeOne() throws 
MailboxException, IOException {
+        saveMessages();
+        
MetadataMapAssert.assertThat(markThenPerformExpunge(MessageRange.one(message1.getUid())))
+            .hasSize(1)
+            .containsMetadataForMessages(message1);
+    }
+
+    @Test
+    public void expungeShouldModifyUnderlyingStorageWithRangeOne() throws 
MailboxException, IOException {
+        saveMessages();
+        markThenPerformExpunge(MessageRange.one(message1.getUid()));
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, LIMIT))
+            .containsOnly(message4, message2, message3, message5);
+    }
+
+    @Test
+    public void expungeShouldReturnCorrectMetadataWithRangeFrom() throws 
MailboxException, IOException {
+        saveMessages();
+        
MetadataMapAssert.assertThat(markThenPerformExpunge(MessageRange.from(message3.getUid())))
+            .hasSize(1)
+            .containsMetadataForMessages(message4);
+    }
+
+    @Test
+    public void expungeShouldModifyUnderlyingStorageWithRangeFrom() throws 
MailboxException, IOException {
+        saveMessages();
+        markThenPerformExpunge(MessageRange.from(message3.getUid()));
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, LIMIT))
+            .containsOnly(message1, message2, message3, message5);
+    }
+
+    @Test
+    public void expungeShouldReturnCorrectMetadataWithRange() throws 
MailboxException, IOException {
+        saveMessages();
+        
MetadataMapAssert.assertThat(markThenPerformExpunge(MessageRange.range(message3.getUid(),
 message5.getUid())))
+            .hasSize(1)
+            .containsMetadataForMessages(message4);
+    }
+
+    @Test
+    public void expungeShouldModifyUnderlyingStorageWithRange() throws 
MailboxException, IOException {
+        saveMessages();
+        markThenPerformExpunge(MessageRange.range(message3.getUid(), 
message5.getUid()));
+        assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.all(), MessageMapper.FetchType.Full, LIMIT))
+            .containsOnly(message1, message2, message3, message5);
+    }
+
+    @Test
+    public void getHighestMoseqShouldBeEqualToZeroOnEmptyMailbox() throws 
MailboxException {
+        
assertThat(messageMapper.getHighestModSeq(benwaInboxMailbox)).isEqualTo(0);
+    }
+
+    @Test
+    public void insertingAMessageShouldIncrementModSeq() throws 
MailboxException {
+        messageMapper.add(benwaInboxMailbox, message1);
+        long modSeq = messageMapper.getHighestModSeq(benwaInboxMailbox);
+        assertThat(modSeq).isGreaterThan(0);
+        messageMapper.add(benwaInboxMailbox, message2);
+        
assertThat(messageMapper.getHighestModSeq(benwaInboxMailbox)).isGreaterThan(modSeq);
+    }
+
+    @Test
+    public void getLastUidShouldReturn0OnEmptyMailbox() throws 
MailboxException {
+        assertThat(messageMapper.getLastUid(benwaInboxMailbox)).isEqualTo(0);
+    }
+
+    @Test
+    public void insertingAMessageShouldIncrementLastUid() throws 
MailboxException {
+        messageMapper.add(benwaInboxMailbox, message1);
+        long uid = messageMapper.getLastUid(benwaInboxMailbox);
+        assertThat(uid).isGreaterThan(0);
+        messageMapper.add(benwaInboxMailbox, message2);
+        
assertThat(messageMapper.getLastUid(benwaInboxMailbox)).isGreaterThan(uid);
+    }
+
+    @Test
+    public void copyShouldIncrementUid() throws MailboxException, IOException {
+        saveMessages();
+        long uid = messageMapper.getLastUid(benwaInboxMailbox);
+        messageMapper.copy(benwaInboxMailbox, new 
SimpleMessage<Id>(benwaInboxMailbox, message6));
+        
assertThat(messageMapper.getLastUid(benwaInboxMailbox)).isGreaterThan(uid);
+    }
+
+    // Message count should be modified upon copy
+    @Ignore
+    @Test
+    public void copyShouldIncrementMessageCount() throws MailboxException, 
IOException {
+        saveMessages();
+        messageMapper.copy(benwaInboxMailbox, new 
SimpleMessage<Id>(benwaInboxMailbox, message6));
+        
assertThat(messageMapper.countMessagesInMailbox(benwaInboxMailbox)).isEqualTo(6);
+    }
+
+    // Message unseen count should be modified upon copy.
+    @Ignore
+    @Test
+    public void copyOfUnSeenMessageShouldIncrementUnSeenMessageCount() throws 
MailboxException, IOException {
+        saveMessages();
+        messageMapper.copy(benwaInboxMailbox, new 
SimpleMessage<Id>(benwaInboxMailbox, message6));
+        
assertThat(messageMapper.countUnseenMessagesInMailbox(benwaInboxMailbox)).isEqualTo(6);
+    }
+
+    @Test
+    public void copyShouldIncrementModSeq() throws MailboxException, 
IOException {
+        saveMessages();
+        long modSeq = messageMapper.getHighestModSeq(benwaInboxMailbox);
+        messageMapper.copy(benwaInboxMailbox, new 
SimpleMessage<Id>(benwaInboxMailbox, message6));
+        
assertThat(messageMapper.getHighestModSeq(benwaInboxMailbox)).isGreaterThan(modSeq);
+    }
+
+    // ModSeq of copied messages is not set
+    @Ignore
+    @Test
+    public void copyShouldCreateAMessageInDestination() throws 
MailboxException, IOException {
+        saveMessages();
+        Message<Id> message7 = new SimpleMessage<Id>(benwaInboxMailbox, 
message6);
+        messageMapper.copy(benwaInboxMailbox, message7);
+        message7.setModSeq(messageMapper.getHighestModSeq(benwaInboxMailbox));
+        
MessageAssert.assertThat(messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message7.getUid()), MessageMapper.FetchType.Full, 
LIMIT).next())
+            .isEqualTo(message7, MessageMapper.FetchType.Full);
+    }
+    
+    @Test
+    public void copyOfSeenMessageShouldNotIncrementUnSeenMessageCount() throws 
MailboxException {
+        message6.setFlags(new Flags(Flags.Flag.SEEN));
+        messageMapper.copy(benwaInboxMailbox, new 
SimpleMessage<Id>(benwaInboxMailbox, message6));
+        
assertThat(messageMapper.countUnseenMessagesInMailbox(benwaInboxMailbox)).isEqualTo(0);
+    }
+
+    @Test
+    public void flagsReplacementShouldReplaceStoredMessageFlags() throws 
MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.FLAGGED), true, true, MessageRange.one(message1.getUid()));
+        
MessageAssert.assertThat(retrieveMessageFromStorage(message1)).hasFlags(new 
Flags(Flags.Flag.FLAGGED));
+    }
+
+    @Test
+    public void 
flagsReplacementShouldReturnAnUpdatedFlagHighlightingTheReplacement() throws 
MailboxException {
+        saveMessages();
+        assertThat(messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.FLAGGED), true, true, MessageRange.one(message1.getUid())))
+            .containsOnly(new UpdatedFlags(message1.getUid(), 
messageMapper.getHighestModSeq(benwaInboxMailbox), new Flags(), new 
Flags(Flags.Flag.FLAGGED)));
+    }
+
+    @Test
+    public void 
flagsAdditionShouldReturnAnUpdatedFlagHighlightingTheAddition() throws 
MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.FLAGGED), true, true, MessageRange.one(message1.getUid()));
+        assertThat(messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), true, false, MessageRange.one(message1.getUid())))
+            .containsOnly(new UpdatedFlags(message1.getUid(), 
messageMapper.getHighestModSeq(benwaInboxMailbox), new 
Flags(Flags.Flag.FLAGGED),
+                new FlagsBuilder().add(Flags.Flag.SEEN, 
Flags.Flag.FLAGGED).build()));
+    }
+
+    @Test
+    public void flagsAdditionShouldUpdateStoredMessageFlags() throws 
MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.FLAGGED), true, true, MessageRange.one(message1.getUid()));
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), true, false, MessageRange.one(message1.getUid()));
+        
MessageAssert.assertThat(retrieveMessageFromStorage(message1)).hasFlags(new 
FlagsBuilder().add(Flags.Flag.SEEN, Flags.Flag.FLAGGED).build());
+    }
+
+    @Test
+    public void flagsRemovalShouldReturnAnUpdatedFlagHighlightingTheRemoval() 
throws MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
FlagsBuilder().add(Flags.Flag.FLAGGED, Flags.Flag.SEEN).build(), true, true, 
MessageRange.one(message1.getUid()));
+        assertThat(messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), false, false, MessageRange.one(message1.getUid())))
+            .containsOnly(new UpdatedFlags(message1.getUid(), 
messageMapper.getHighestModSeq(benwaInboxMailbox),
+                new FlagsBuilder().add(Flags.Flag.SEEN, 
Flags.Flag.FLAGGED).build(), new Flags(Flags.Flag.FLAGGED)));
+    }
+
+    @Test
+    public void flagsRemovalShouldUpdateStoredMessageFlags() throws 
MailboxException {
+        saveMessages();
+        messageMapper.updateFlags(benwaInboxMailbox, new 
FlagsBuilder().add(Flags.Flag.FLAGGED, Flags.Flag.SEEN).build(), true, true, 
MessageRange.one(message1.getUid()));
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), false, false, MessageRange.one(message1.getUid()));
+        
MessageAssert.assertThat(retrieveMessageFromStorage(message1)).hasFlags(new 
Flags(Flags.Flag.FLAGGED));
+    }
+
+    // Ranges should be inclusive
+    @Ignore
+    @Test
+    public void updateFlagsOnRangeShouldAffectMessagesContainedInThisRange() 
throws MailboxException {
+        saveMessages();
+        assertThat(messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), false, true, MessageRange.range(message1.getUid(), 
message3.getUid())))
+            .hasSize(3);
+    }
+
+    // Ranges should be inclusive
+    @Ignore
+    @Test
+    public void 
updateFlagsWithRangeFromShouldAffectMessagesContainedInThisRange() throws 
MailboxException {
+        saveMessages();
+        assertThat(messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), false, true, MessageRange.from(message3.getUid())))
+            .hasSize(3);
+    }
+
+    @Test
+    public void updateFlagsWithRangeAllRangeShouldAffectAllMessages() throws 
MailboxException {
+        saveMessages();
+        assertThat(messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.SEEN), false, true, MessageRange.all()))
+            .hasSize(5);
+    }
+    
+    private Map<Long, MessageMetaData> markThenPerformExpunge(MessageRange 
range) throws MailboxException {
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.DELETED), true, true, MessageRange.one(message1.getUid()));
+        messageMapper.updateFlags(benwaInboxMailbox, new 
Flags(Flags.Flag.DELETED), true, true, MessageRange.one(message4.getUid()));
+        return 
messageMapper.expungeMarkedForDeletionInMailbox(benwaInboxMailbox, range);
+    }
+
+    private SimpleMailbox<Id> createMailbox(MailboxPath mailboxPath) {
+        SimpleMailbox<Id> mailbox = new SimpleMailbox<Id>(mailboxPath, 
UID_VALIDITY);
+        Id id = mapperProvider.generateId();
+        mailbox.setMailboxId(id);
+        return mailbox;
+    }
+    
+    private void saveMessages() throws MailboxException {
+        messageMapper.add(benwaInboxMailbox, message1);
+        message1.setModSeq(messageMapper.getHighestModSeq(benwaInboxMailbox));
+        messageMapper.add(benwaInboxMailbox, message2);
+        message2.setModSeq(messageMapper.getHighestModSeq(benwaInboxMailbox));
+        messageMapper.add(benwaInboxMailbox, message3);
+        message3.setModSeq(messageMapper.getHighestModSeq(benwaInboxMailbox));
+        messageMapper.add(benwaInboxMailbox, message4);
+        message4.setModSeq(messageMapper.getHighestModSeq(benwaInboxMailbox));
+        messageMapper.add(benwaInboxMailbox, message5);
+        message5.setModSeq(messageMapper.getHighestModSeq(benwaInboxMailbox));
+        messageMapper.add(benwaWorkMailbox, message6);
+        message6.setModSeq(messageMapper.getHighestModSeq(benwaWorkMailbox));
+    }
+
+    private Message<Id> retrieveMessageFromStorage(Message message) throws 
MailboxException {
+        return messageMapper.findInMailbox(benwaInboxMailbox, 
MessageRange.one(message.getUid()), MessageMapper.FetchType.Metadata, 
LIMIT).next();
+    }
+    
+    private SimpleMessage<Id> createMessage(Mailbox<Id> mailbox, String 
content, int bodyStart) {
+        return new SimpleMessage<Id>(new Date(), content.length(), bodyStart, 
new SharedByteArrayInputStream(content.getBytes()), new Flags(), new 
PropertyBuilder(), mailbox.getMailboxId());
+    }
+}

Added: 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssert.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssert.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssert.java
 (added)
+++ 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssert.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,62 @@
+/****************************************************************
+ * 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.mail.model;
+
+import org.assertj.core.api.AbstractAssert;
+
+public class MailboxAssert extends AbstractAssert<MailboxAssert, Mailbox<?>> {
+    public MailboxAssert(Mailbox<?> actual) {
+        super(actual, MailboxAssert.class);
+    }
+
+    public static MailboxAssert assertThat(Mailbox<?> actual) {
+        return new MailboxAssert(actual);
+    }
+
+    public MailboxAssert isEqualTo(Mailbox<?> expected) {
+        isNotNull();
+        if (!equals(actual.getMailboxId(), expected.getMailboxId())) {
+            failWithMessage("Expected UUID to be <%s> but was <%s>", 
expected.getMailboxId(), actual.getMailboxId());
+        }
+        if (!equals(actual.getNamespace(), expected.getNamespace())) {
+            failWithMessage("Expected NameSpace to be <%s> but was <%s>", 
expected.getNamespace(), actual.getNamespace());
+        }
+        if (!equals(actual.getUser(), expected.getUser())) {
+            failWithMessage("Expected User to be <%s> but was <%s>", 
expected.getUser(), actual.getUser());
+        }
+        if (!equals(actual.getName(), expected.getName())) {
+            failWithMessage("Expected Name to be <%s> but was <%s>", 
expected.getName(), actual.getName());
+        }
+        if (!equals(actual.getACL(), expected.getACL())) {
+            failWithMessage("Expected UUID to be <%s> but was <%s>", 
expected.getACL(), actual.getACL());
+        }
+        if (actual.getUidValidity() != expected.getUidValidity()) {
+            failWithMessage("Expected UID Validity to be <%s> but was <%s>", 
expected.getUidValidity(), actual.getUidValidity());
+        }
+        return this;
+    }
+
+    private boolean equals(Object object1, Object object2) {
+        if ( object1 == null && object2 == null ) {
+            return true;
+        }
+        return ( object1 != null ) && object1.equals(object2);
+    }
+}

Added: 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssertTests.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssertTests.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssertTests.java
 (added)
+++ 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxAssertTests.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,84 @@
+/****************************************************************
+ * 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.mail.model;
+
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import org.junit.Test;
+
+public class MailboxAssertTests {
+
+    private final static long UID_VALIDITY = 42;
+    private final static long MAILBOX_ID = 24;
+
+    @Test
+    public void isEqualToShouldNotFailWithEqualMailbox() {
+        SimpleMailbox<Long> mailbox1 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        SimpleMailbox<Long> mailbox2 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+        MailboxAssert.assertThat(mailbox1).isEqualTo(mailbox2);
+    }
+
+    @Test(expected = AssertionError.class)
+    public void isEqualToShouldFailWithNotEqualNamespace() {
+        SimpleMailbox<Long> mailbox1 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        SimpleMailbox<Long> mailbox2 = new SimpleMailbox<Long>(new 
MailboxPath("other_namespace", "user", "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+        MailboxAssert.assertThat(mailbox1).isEqualTo(mailbox2);
+    }
+
+    @Test(expected = AssertionError.class)
+    public void isEqualToShouldFailWithNotEqualUser() {
+        SimpleMailbox<Long> mailbox1 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        SimpleMailbox<Long> mailbox2 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "other_user", "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+        MailboxAssert.assertThat(mailbox1).isEqualTo(mailbox2);
+    }
+
+    @Test(expected = AssertionError.class)
+    public void isEqualToShouldFailWithNotEqualName() {
+        SimpleMailbox<Long> mailbox1 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        SimpleMailbox<Long> mailbox2 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "other_name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+        MailboxAssert.assertThat(mailbox1).isEqualTo(mailbox2);
+    }
+
+    @Test(expected = AssertionError.class)
+    public void isEqualToShouldFailWithNotEqualId() {
+        SimpleMailbox<Long> mailbox1 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        SimpleMailbox<Long> mailbox2 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID + 1);
+        MailboxAssert.assertThat(mailbox1).isEqualTo(mailbox2);
+    }
+
+    @Test(expected = AssertionError.class)
+    public void isEqualToShouldFailWithNotEqualUidValidity() {
+        SimpleMailbox<Long> mailbox1 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY);
+        SimpleMailbox<Long> mailbox2 = new SimpleMailbox<Long>(new 
MailboxPath("namespace", "user", "name"), UID_VALIDITY + 1);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+        MailboxAssert.assertThat(mailbox1).isEqualTo(mailbox2);
+    }
+}

Added: 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MapperProvider.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MapperProvider.java?rev=1682388&view=auto
==============================================================================
--- 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MapperProvider.java
 (added)
+++ 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MapperProvider.java
 Fri May 29 07:56:38 2015
@@ -0,0 +1,36 @@
+/****************************************************************
+ * 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.mail.model;
+
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.MessageMapper;
+
+public interface MapperProvider<Id> {
+    MailboxMapper<Id> createMailboxMapper() throws MailboxException;
+
+    MessageMapper<Id> createMessageMapper() throws MailboxException;
+
+    Id generateId();
+
+    void clearMapper() throws MailboxException;
+
+    void ensureMapperPrepared() throws MailboxException;
+}



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

Reply via email to