MAILBOX-291 Implementation for Current quota storage with JPA

Contributed by Cung and Vi from Parsserelles numeriques Vietnam


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

Branch: refs/heads/master
Commit: b62c413a6a5e9518cc97d1fc7e08af725492a812
Parents: fcdfc80
Author: benwa <btell...@linagora.com>
Authored: Wed Mar 29 17:34:24 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Fri Mar 31 08:46:17 2017 +0700

----------------------------------------------------------------------
 .../quota/CassandraCurrentQuotaManagerTest.java |  92 ++--------------
 mailbox/jpa/pom.xml                             |   3 +-
 .../jpa/quota/JpaCurrentQuotaManager.java       | 100 +++++++++++++++++
 .../jpa/quota/model/JpaCurrentQuota.java        |  57 ++++++++++
 .../src/main/resources/META-INF/persistence.xml |   1 +
 .../james/mailbox/jpa/JPAMailboxFixture.java    |  10 +-
 .../jpa/quota/JPACurrentQuotaManagerTest.java   |  42 ++++++++
 .../quota/StoreCurrentQuotaManagerTest.java     | 108 +++++++++++++++++++
 8 files changed, 326 insertions(+), 87 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java
 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java
index f57f6f2..acea272 100644
--- 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java
+++ 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java
@@ -19,98 +19,24 @@
 
 package org.apache.james.mailbox.cassandra.quota;
 
-import static org.assertj.core.api.Assertions.assertThat;
-
 import org.apache.james.backends.cassandra.CassandraCluster;
 import org.apache.james.mailbox.cassandra.modules.CassandraQuotaModule;
-import org.apache.james.mailbox.model.QuotaRoot;
-import org.apache.james.mailbox.store.quota.QuotaRootImpl;
+import org.apache.james.mailbox.store.quota.StoreCurrentQuotaManager;
+import org.apache.james.mailbox.store.quota.StoreCurrentQuotaManagerTest;
 import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-public class CassandraCurrentQuotaManagerTest {
 
-    private static final QuotaRoot QUOTA_ROOT = 
QuotaRootImpl.quotaRoot("value");
+public class CassandraCurrentQuotaManagerTest extends 
StoreCurrentQuotaManagerTest {
 
-    private CassandraCluster cassandra;
-    private CassandraCurrentQuotaManager currentQuotaManager;
+    private static final CassandraCluster CASSANDRA_CLUSTER = 
CassandraCluster.create(new CassandraQuotaModule());
 
-    @Before
-    public void setUp() {
-        cassandra = CassandraCluster.create(new CassandraQuotaModule());
-        cassandra.ensureAllTables();
-        currentQuotaManager = new 
CassandraCurrentQuotaManager(cassandra.getConf());
+    @Override
+    protected StoreCurrentQuotaManager provideTestee() {
+        return new CassandraCurrentQuotaManager(CASSANDRA_CLUSTER.getConf());
     }
 
     @After
-    public void cleanUp() {
-        cassandra.clearAllTables();
-    }
-
-    @Test
-    public void getCurrentStorageShouldReturnZeroByDefault() throws Exception {
-        
assertThat(currentQuotaManager.getCurrentStorage(QUOTA_ROOT)).isEqualTo(0);
-    }
-
-    @Test
-    public void getCurrentMessageCountShouldReturnZeroByDefault() throws 
Exception {
-        
assertThat(currentQuotaManager.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(0);
-    }
-
-    @Test
-    public void increaseShouldWork() throws Exception {
-        currentQuotaManager.increase(QUOTA_ROOT, 2, 2000);
-        
assertThat(currentQuotaManager.getCurrentStorage(QUOTA_ROOT)).isEqualTo(2000);
-        
assertThat(currentQuotaManager.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(2);
-    }
-
-    @Test
-    public void decreaseShouldWork() throws Exception {
-        currentQuotaManager.increase(QUOTA_ROOT, 2, 2000);
-        currentQuotaManager.decrease(QUOTA_ROOT, 1, 1000);
-        
assertThat(currentQuotaManager.getCurrentStorage(QUOTA_ROOT)).isEqualTo(1000);
-        
assertThat(currentQuotaManager.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void increaseShouldThrowOnZeroCount() throws Exception {
-        currentQuotaManager.increase(QUOTA_ROOT, 0, 5);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void increaseShouldThrowOnNegativeCount() throws Exception {
-        currentQuotaManager.increase(QUOTA_ROOT, -1, 5);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void increaseShouldThrowOnZeroSize() throws Exception {
-        currentQuotaManager.increase(QUOTA_ROOT, 5, 0);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void increaseShouldThrowOnNegativeSize() throws Exception {
-        currentQuotaManager.increase(QUOTA_ROOT, 5, -1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void decreaseShouldThrowOnZeroCount() throws Exception {
-        currentQuotaManager.decrease(QUOTA_ROOT, 0, 5);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void decreaseShouldThrowOnNegativeCount() throws Exception {
-        currentQuotaManager.decrease(QUOTA_ROOT, -1, 5);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void decreaseShouldThrowOnZeroSize() throws Exception {
-        currentQuotaManager.decrease(QUOTA_ROOT, 5, 0);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void decreaseShouldThrowOnNegativeSize() throws Exception {
-        currentQuotaManager.decrease(QUOTA_ROOT, 5, -1);
+    public void tearDown() {
+        CASSANDRA_CLUSTER.clearAllTables();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/jpa/pom.xml
----------------------------------------------------------------------
diff --git a/mailbox/jpa/pom.xml b/mailbox/jpa/pom.xml
index cc085c7..790b5a1 100644
--- a/mailbox/jpa/pom.xml
+++ b/mailbox/jpa/pom.xml
@@ -142,7 +142,8 @@
                                 
org.apache.james.mailbox.jpa.quota.model.MaxDefaultMessageCount;
                                 
org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage
                                 
org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount
-                                
org.apache.james.mailbox.jpa.quota.model.MaxUserStorage)
+                                
org.apache.james.mailbox.jpa.quota.model.MaxUserStorage
+                                
org.apache.james.mailbox.jpa.quota.model.JpaCurrentQuota)
                             </value>
                         </property>
                     </toolProperties>

http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JpaCurrentQuotaManager.java
----------------------------------------------------------------------
diff --git 
a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JpaCurrentQuotaManager.java
 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JpaCurrentQuotaManager.java
new file mode 100644
index 0000000..1223d9c
--- /dev/null
+++ 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JpaCurrentQuotaManager.java
@@ -0,0 +1,100 @@
+/****************************************************************
+ * 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.jpa.quota;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.jpa.quota.model.JpaCurrentQuota;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.store.quota.StoreCurrentQuotaManager;
+
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+
+public class JpaCurrentQuotaManager implements StoreCurrentQuotaManager {
+
+    public static final long NO_MESSAGES = 0L;
+    public static final long NO_STORED_BYTES = 0L;
+
+    private final EntityManager entityManager;
+
+    public JpaCurrentQuotaManager(EntityManagerFactory entityManagerFactory) {
+        this.entityManager = entityManagerFactory.createEntityManager();
+    }
+
+    @Override
+    public MailboxListener.ListenerType getAssociatedListenerType() {
+        return MailboxListener.ListenerType.ONCE;
+    }
+
+    @Override
+    public long getCurrentMessageCount(QuotaRoot quotaRoot) throws 
MailboxException {
+        JpaCurrentQuota userQuota = retrieveUserQuota(quotaRoot);
+
+        if (userQuota == null) {
+            return NO_STORED_BYTES;
+        }
+        return userQuota.getMessageCount();
+    }
+
+    @Override
+    public long getCurrentStorage(QuotaRoot quotaRoot) throws MailboxException 
{
+        JpaCurrentQuota userQuota = retrieveUserQuota(quotaRoot);
+
+        if (userQuota == null) {
+            return NO_STORED_BYTES;
+        }
+        return userQuota.getSize();
+    }
+
+    @Override
+    public void increase(QuotaRoot quotaRoot, long count, long size) throws 
MailboxException {
+        Preconditions.checkArgument(count > 0, "Counts should be positive");
+        Preconditions.checkArgument(size > 0, "Size should be positive");
+
+        JpaCurrentQuota jpaCurrentQuota = 
Optional.fromNullable(retrieveUserQuota(quotaRoot))
+            .or(new JpaCurrentQuota(quotaRoot.getValue(), NO_MESSAGES, 
NO_STORED_BYTES));
+
+        entityManager.merge(new JpaCurrentQuota(quotaRoot.getValue(),
+            jpaCurrentQuota.getMessageCount() + count,
+            jpaCurrentQuota.getSize() + size));
+    }
+
+    @Override
+    public void decrease(QuotaRoot quotaRoot, long count, long size) throws 
MailboxException {
+        Preconditions.checkArgument(count > 0, "Counts should be positive");
+        Preconditions.checkArgument(size > 0, "Counts should be positive");
+
+        JpaCurrentQuota jpaCurrentQuota = 
Optional.fromNullable(retrieveUserQuota(quotaRoot))
+            .or(new JpaCurrentQuota(quotaRoot.getValue(), NO_MESSAGES, 
NO_STORED_BYTES));
+
+        entityManager.merge(new JpaCurrentQuota(quotaRoot.getValue(),
+            jpaCurrentQuota.getMessageCount() - count,
+            jpaCurrentQuota.getSize() - size));
+    }
+
+    private JpaCurrentQuota retrieveUserQuota(QuotaRoot quotaRoot) throws 
MailboxException {
+        return entityManager.find(JpaCurrentQuota.class, quotaRoot.getValue());
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/JpaCurrentQuota.java
----------------------------------------------------------------------
diff --git 
a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/JpaCurrentQuota.java
 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/JpaCurrentQuota.java
new file mode 100644
index 0000000..260db9b
--- /dev/null
+++ 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/JpaCurrentQuota.java
@@ -0,0 +1,57 @@
+/****************************************************************
+ * 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.jpa.quota.model;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity(name = "CurrentQuota")
+@Table(name = "JAMES_QUOTA_CURRENTQUOTA")
+public class JpaCurrentQuota {
+
+    @Id
+    @Column(name = "CURRENTQUOTA_QUOTAROOT")
+    private String quotaRoot;
+
+    @Column(name = "CURRENTQUOTA_MESSAGECOUNT")
+    private long messageCount;
+
+    @Column(name = "CURRENTQUOTA_SIZE")
+    private long size;
+
+    public JpaCurrentQuota() {
+    }
+
+    public JpaCurrentQuota(String quotaRoot, long messageCount, long size) {
+        this.quotaRoot = quotaRoot;
+        this.messageCount = messageCount;
+        this.size = size;
+    }
+
+    public long getMessageCount() {
+        return messageCount;
+    }
+
+    public long getSize() {
+        return size;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/jpa/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git a/mailbox/jpa/src/main/resources/META-INF/persistence.xml 
b/mailbox/jpa/src/main/resources/META-INF/persistence.xml
index 70082c1..a63e0d3 100644
--- a/mailbox/jpa/src/main/resources/META-INF/persistence.xml
+++ b/mailbox/jpa/src/main/resources/META-INF/persistence.xml
@@ -34,6 +34,7 @@
         
<class>org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage</class>
         
<class>org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount</class>
         <class>org.apache.james.mailbox.jpa.quota.model.MaxUserStorage</class>
+        <class>org.apache.james.mailbox.jpa.quota.model.JpaCurrentQuota</class>
         <properties>
             <property name="openjpa.jdbc.SynchronizeMappings" 
value="buildSchema(ForeignKeys=true)"/>
             <property name="openjpa.jdbc.MappingDefaults" 
value="ForeignKeyDeleteAction=cascade, JoinForeignKeyDeleteAction=cascade"/>

http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java
----------------------------------------------------------------------
diff --git 
a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java 
b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java
index 8beecb5..9749be3 100644
--- 
a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java
+++ 
b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java
@@ -25,6 +25,7 @@ import org.apache.james.mailbox.jpa.mail.model.JPAProperty;
 import org.apache.james.mailbox.jpa.mail.model.JPAUserFlag;
 import 
org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMailboxMessage;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMailboxMessage;
+import org.apache.james.mailbox.jpa.quota.model.JpaCurrentQuota;
 import org.apache.james.mailbox.jpa.quota.model.MaxDefaultMessageCount;
 import org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage;
 import org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount;
@@ -39,13 +40,15 @@ public interface JPAMailboxFixture {
         JPAProperty.class,
         JPAUserFlag.class,
         JPAMailboxAnnotation.class,
-        JPASubscription.class};
+        JPASubscription.class
+    };
 
     Class<?>[] QUOTA_PERSISTANCE_CLASSES = new Class[] {
         MaxDefaultMessageCount.class,
         MaxDefaultStorage.class,
         MaxUserMessageCount.class,
-        MaxUserStorage.class
+        MaxUserStorage.class,
+        JpaCurrentQuota.class
     };
 
     String[] MAILBOX_TABLE_NAMES = new String[] {"JAMES_MAIL_USERFLAG",
@@ -58,6 +61,7 @@ public interface JPAMailboxFixture {
         "JAMES_MAX_DEFAULT_MESSAGE_COUNT",
         "JAMES_MAX_DEFAULT_STORAGE",
         "JAMES_MAX_USER_MESSAGE_COUNT",
-        "JAMES_MAX_USER_STORAGE"
+        "JAMES_MAX_USER_STORAGE",
+        "JAMES_QUOTA_CURRENTQUOTA"
     };
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPACurrentQuotaManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPACurrentQuotaManagerTest.java
 
b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPACurrentQuotaManagerTest.java
new file mode 100644
index 0000000..44f6c0e
--- /dev/null
+++ 
b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPACurrentQuotaManagerTest.java
@@ -0,0 +1,42 @@
+/****************************************************************
+ * 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.jpa.quota;
+
+import org.apache.james.backends.jpa.JpaTestCluster;
+import org.apache.james.mailbox.jpa.JPAMailboxFixture;
+import org.apache.james.mailbox.store.quota.StoreCurrentQuotaManager;
+import org.apache.james.mailbox.store.quota.StoreCurrentQuotaManagerTest;
+import org.junit.After;
+
+public class JPACurrentQuotaManagerTest extends StoreCurrentQuotaManagerTest {
+
+    private static final JpaTestCluster JPA_TEST_CLUSTER = 
JpaTestCluster.create(JPAMailboxFixture.QUOTA_PERSISTANCE_CLASSES);
+
+    @Override
+    protected StoreCurrentQuotaManager provideTestee() {
+        return new 
JpaCurrentQuotaManager(JPA_TEST_CLUSTER.getEntityManagerFactory());
+    }
+
+    @After
+    public void tearDown() {
+        JPA_TEST_CLUSTER.clear(JPAMailboxFixture.QUOTA_TABLES_NAMES);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/b62c413a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManagerTest.java
new file mode 100644
index 0000000..f77dda9
--- /dev/null
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManagerTest.java
@@ -0,0 +1,108 @@
+/****************************************************************
+ * 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.quota;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.junit.Before;
+import org.junit.Test;
+
+public abstract class StoreCurrentQuotaManagerTest {
+    public static final QuotaRoot QUOTA_ROOT = 
QuotaRootImpl.quotaRoot("benwa");
+    protected abstract StoreCurrentQuotaManager provideTestee();
+    private StoreCurrentQuotaManager testee;
+
+    @Before
+    public void setUp() throws Exception {
+        testee = provideTestee();
+    }
+
+    @Test
+    public void getCurrentStorageShouldReturnZeroByDefault() throws Exception {
+        assertThat(testee.getCurrentStorage(QUOTA_ROOT)).isEqualTo(0);
+    }
+
+    @Test
+    public void increaseShouldWork() throws Exception {
+        testee.increase(QUOTA_ROOT, 10, 100);
+
+        assertThat(testee.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(10);
+        assertThat(testee.getCurrentStorage(QUOTA_ROOT)).isEqualTo(100);
+    }
+
+    @Test
+    public void decreaseShouldWork() throws Exception {
+        testee.increase(QUOTA_ROOT, 20, 200);
+
+        testee.decrease(QUOTA_ROOT, 10, 100);
+
+        assertThat(testee.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(10);
+        assertThat(testee.getCurrentStorage(QUOTA_ROOT)).isEqualTo(100);
+    }
+
+    @Test
+    public void decreaseShouldNotFailWhenItLeadsToNegativeValues() throws 
Exception {
+        testee.decrease(QUOTA_ROOT, 10, 100);
+
+        assertThat(testee.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(-10);
+        assertThat(testee.getCurrentStorage(QUOTA_ROOT)).isEqualTo(-100);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnZeroCount() throws Exception {
+        testee.increase(QUOTA_ROOT, 0, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnNegativeCount() throws Exception {
+        testee.increase(QUOTA_ROOT, -1, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnZeroSize() throws Exception {
+        testee.increase(QUOTA_ROOT, 5, 0);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnNegativeSize() throws Exception {
+        testee.increase(QUOTA_ROOT, 5, -1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnZeroCount() throws Exception {
+        testee.decrease(QUOTA_ROOT, 0, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnNegativeCount() throws Exception {
+        testee.decrease(QUOTA_ROOT, -1, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnZeroSize() throws Exception {
+        testee.decrease(QUOTA_ROOT, 5, 0);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnNegativeSize() throws Exception {
+        testee.decrease(QUOTA_ROOT, 5, -1);
+    }
+}


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