You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2015/09/22 12:19:54 UTC

svn commit: r1704530 - in /james/mailbox/trunk/store/src: main/java/org/apache/james/mailbox/store/quota/StoreQuotaManager.java test/java/org/apache/james/mailbox/store/quota/StoreQuotaManagerTest.java

Author: btellier
Date: Tue Sep 22 10:19:51 2015
New Revision: 1704530

URL: http://svn.apache.org/viewvc?rev=1704530&view=rev
Log:
MAILBOX-64 Store Quota Manager implementation

Added:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreQuotaManager.java
    james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/StoreQuotaManagerTest.java

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreQuotaManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreQuotaManager.java?rev=1704530&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreQuotaManager.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreQuotaManager.java Tue Sep 22 10:19:51 2015
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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 org.apache.james.mailbox.quota.CurrentQuotaManager;
+import org.apache.james.mailbox.quota.MaxQuotaManager;
+import org.apache.james.mailbox.quota.QuotaManager;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.Quota;
+import org.apache.james.mailbox.model.QuotaRoot;
+
+import javax.inject.Inject;
+
+/**
+ * Default implementation for the Quota Manager.
+ *
+ * Relies on the CurrentQuotaManager and MaxQuotaManager provided.
+ */
+public class StoreQuotaManager implements QuotaManager {
+    private CurrentQuotaManager currentQuotaManager;
+    private MaxQuotaManager maxQuotaManager;
+    private boolean calculateWhenUnlimited = false;
+
+    public void setCalculateWhenUnlimited(boolean calculateWhenUnlimited) {
+        this.calculateWhenUnlimited = calculateWhenUnlimited;
+    }
+
+    @Inject
+    public void setMaxQuotaManager(MaxQuotaManager maxQuotaManager) {
+        this.maxQuotaManager = maxQuotaManager;
+    }
+
+    @Inject
+    public void setCurrentQuotaManager(CurrentQuotaManager currentQuotaManager) {
+        this.currentQuotaManager = currentQuotaManager;
+    }
+
+    public Quota getMessageQuota(QuotaRoot quotaRoot) throws MailboxException {
+        long maxValue = maxQuotaManager.getMaxMessage(quotaRoot);
+        if(maxValue == Quota.UNLIMITED && !calculateWhenUnlimited) {
+            return QuotaImpl.quota(Quota.UNKNOWN, Quota.UNLIMITED);
+        }
+        return QuotaImpl.quota(currentQuotaManager.getCurrentMessageCount(quotaRoot), maxValue);
+    }
+
+
+    public Quota getStorageQuota(QuotaRoot quotaRoot) throws MailboxException {
+        long maxValue = maxQuotaManager.getMaxStorage(quotaRoot);
+        if(maxValue == Quota.UNLIMITED && !calculateWhenUnlimited) {
+            return QuotaImpl.quota(Quota.UNKNOWN, Quota.UNLIMITED);
+        }
+        return QuotaImpl.quota(currentQuotaManager.getCurrentStorage(quotaRoot), maxValue);
+    }
+
+}
\ No newline at end of file

Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/StoreQuotaManagerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/StoreQuotaManagerTest.java?rev=1704530&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/StoreQuotaManagerTest.java (added)
+++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/StoreQuotaManagerTest.java Tue Sep 22 10:19:51 2015
@@ -0,0 +1,151 @@
+/****************************************************************
+ * 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 static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.james.mailbox.model.Quota;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.quota.CurrentQuotaManager;
+import org.apache.james.mailbox.quota.MaxQuotaManager;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class StoreQuotaManagerTest {
+
+    private StoreQuotaManager testee;
+    private CurrentQuotaManager mockedCurrentQuotaManager;
+    private MaxQuotaManager mockedMaxQuotaManager;
+    private QuotaRoot quotaRoot;
+
+    @Before
+    public void setUp() {
+        mockedCurrentQuotaManager = mock(CurrentQuotaManager.class);
+        mockedMaxQuotaManager = mock(MaxQuotaManager.class);
+        testee = new StoreQuotaManager();
+        testee.setCurrentQuotaManager(mockedCurrentQuotaManager);
+        testee.setMaxQuotaManager(mockedMaxQuotaManager);
+        quotaRoot = QuotaRootImpl.quotaRoot("benwa");
+    }
+
+    @Test
+    public void test() throws Exception {
+        when(mockedMaxQuotaManager.getMaxMessage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return 360L;
+            }
+        });
+        when(mockedCurrentQuotaManager.getCurrentMessageCount(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return 36L;
+            }
+        });
+        assertThat(testee.getMessageQuota(quotaRoot)).isEqualTo(QuotaImpl.quota(36, 360));
+    }
+
+    @Test
+    public void test2() throws Exception {
+        when(mockedMaxQuotaManager.getMaxStorage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return 360L;
+            }
+        });
+        when(mockedCurrentQuotaManager.getCurrentStorage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return 36L;
+            }
+        });
+        assertThat(testee.getStorageQuota(quotaRoot)).isEqualTo(QuotaImpl.quota(36, 360));
+    }
+
+    @Test
+    public void test3() throws Exception {
+        testee.setCalculateWhenUnlimited(false);
+        when(mockedMaxQuotaManager.getMaxStorage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Quota.UNLIMITED;
+            }
+        });
+        assertThat(testee.getStorageQuota(quotaRoot)).isEqualTo(QuotaImpl.quota(Quota.UNKNOWN, Quota.UNLIMITED));
+        verify(mockedCurrentQuotaManager, never()).getCurrentStorage(quotaRoot);
+    }
+
+    @Test
+    public void test4() throws Exception {
+        testee.setCalculateWhenUnlimited(false);
+        when(mockedMaxQuotaManager.getMaxMessage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Quota.UNLIMITED;
+            }
+        });
+        assertThat(testee.getMessageQuota(quotaRoot)).isEqualTo(QuotaImpl.quota(Quota.UNKNOWN, Quota.UNLIMITED));
+        verify(mockedCurrentQuotaManager, never()).getCurrentMessageCount(quotaRoot);
+    }
+
+    @Test
+    public void test5() throws Exception {
+        testee.setCalculateWhenUnlimited(true);
+        when(mockedMaxQuotaManager.getMaxStorage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Quota.UNLIMITED;
+            }
+        });
+        when(mockedCurrentQuotaManager.getCurrentStorage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return 36L;
+            }
+        });
+        assertThat(testee.getStorageQuota(quotaRoot)).isEqualTo(QuotaImpl.quota(36, Quota.UNLIMITED));
+    }
+
+    @Test
+    public void test6() throws Exception {
+        testee.setCalculateWhenUnlimited(true);
+        when(mockedMaxQuotaManager.getMaxMessage(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Quota.UNLIMITED;
+            }
+        });
+        when(mockedCurrentQuotaManager.getCurrentMessageCount(quotaRoot)).then(new Answer<Long>() {
+            @Override
+            public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return 36L;
+            }
+        });
+        assertThat(testee.getMessageQuota(quotaRoot)).isEqualTo(QuotaImpl.quota(36, Quota.UNLIMITED));
+    }
+
+}



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