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 2018/05/15 09:22:47 UTC

[1/4] james-project git commit: MAILBOX-333 Notice should handle size units

Repository: james-project
Updated Branches:
  refs/heads/master 5ee21fec0 -> c2b85a004


MAILBOX-333 Notice should handle size units


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

Branch: refs/heads/master
Commit: 8982f4a69334a62a3f08c1609b5717292e597120
Parents: 5ee21fe
Author: benwa <bt...@linagora.com>
Authored: Thu May 10 09:46:01 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu May 10 09:46:01 2018 +0700

----------------------------------------------------------------------
 .../subscribers/QuotaThresholdNoticeTest.java   | 24 ++++++++++++++++++++
 1 file changed, 24 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8982f4a6/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
index e47adc2..0898b18 100644
--- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
+++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
@@ -202,6 +202,30 @@ class QuotaThresholdNoticeTest {
     }
 
     @Test
+    void generateReportShouldFormatSizeUnits() throws Exception {
+        QuotaThresholdChange sizeThresholdChange = new QuotaThresholdChange(_80, NOW);
+
+        assertThat(QuotaThresholdNotice.builder()
+            .withConfiguration(DEFAULT_CONFIGURATION)
+            .sizeQuota(Quota.<QuotaSize>builder()
+                .used(QuotaSize.size(801 * 1024 * 1024))
+                .computedLimit(QuotaSize.size(1024 * 1024 * 1024))
+                .build())
+            .countQuota(Counts._72_PERCENT)
+            .sizeThreshold(HistoryEvolution.higherThresholdReached(sizeThresholdChange, NotAlreadyReachedDuringGracePeriod))
+            .build()
+            .get()
+            .generateReport(fileSystem))
+            .isEqualTo("You receive this email because you recently exceeded a threshold related to the quotas of your email account.\n" +
+                "\n" +
+                "You currently occupy more than 80 % of the total size allocated to you.\n" +
+                "You currently occupy 801 MB on a total of 1 GB allocated to you.\n" +
+                "\n" +
+                "You need to be aware that actions leading to exceeded quotas will be denied. This will result in a degraded service.\n" +
+                "To mitigate this issue you might reach your administrator in order to increase your configured quota. You might also delete some non important emails.");
+    }
+
+    @Test
     void generateReportShouldOmitSizePartWhenNone() throws Exception {
         QuotaThresholdChange countThresholdChange = new QuotaThresholdChange(_80, NOW);
 


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


[3/4] james-project git commit: MAILBOX-333 Also correct CLI size formatting

Posted by bt...@apache.org.
MAILBOX-333 Also correct CLI size formatting


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

Branch: refs/heads/master
Commit: 6d67d2f108fc87f70b7337548ad436d2eb6237ec
Parents: 31ca264
Author: benwa <bt...@linagora.com>
Authored: Mon May 14 10:56:56 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Tue May 15 16:19:34 2018 +0700

----------------------------------------------------------------------
 mailbox/plugin/quota-mailing/pom.xml            |  4 +
 .../subscribers/QuotaThresholdNotice.java       |  1 +
 .../quota/mailing/subscribers/SizeFormat.java   | 92 --------------------
 .../mailing/subscribers/SizeFormatTest.java     | 89 -------------------
 .../james/cli/QuotaCommandsIntegrationTest.java |  4 +-
 .../java/org/apache/james/cli/ServerCmd.java    |  6 +-
 .../java/org/apache/james/util/SizeFormat.java  | 92 ++++++++++++++++++++
 .../org/apache/james/util/SizeFormatTest.java   | 89 +++++++++++++++++++
 8 files changed, 191 insertions(+), 186 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/mailbox/plugin/quota-mailing/pom.xml
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/pom.xml b/mailbox/plugin/quota-mailing/pom.xml
index 8cfb37f..8f45fb1 100644
--- a/mailbox/plugin/quota-mailing/pom.xml
+++ b/mailbox/plugin/quota-mailing/pom.xml
@@ -78,6 +78,10 @@
             <artifactId>james-server-filesystem-api</artifactId>
         </dependency>
         <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>james-server-util</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.github.spullara.mustache.java</groupId>
             <artifactId>compiler</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
index 52ab097..8e90a12 100644
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
@@ -40,6 +40,7 @@ import org.apache.james.mailbox.quota.mailing.QuotaMailingListenerConfiguration;
 import org.apache.james.mailbox.quota.model.HistoryEvolution;
 import org.apache.james.mailbox.quota.model.QuotaThreshold;
 import org.apache.james.mailbox.quota.model.QuotaThresholdChange;
+import org.apache.james.util.SizeFormat;
 
 import com.github.mustachejava.DefaultMustacheFactory;
 import com.github.mustachejava.Mustache;

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java
deleted file mode 100644
index 9a9ebfb..0000000
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/****************************************************************
- * 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.quota.mailing.subscribers;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.text.DecimalFormat;
-import java.text.DecimalFormatSymbols;
-import java.util.Locale;
-
-import org.apache.commons.io.FileUtils;
-
-import com.google.common.base.Preconditions;
-
-public class SizeFormat {
-
-    public enum Unit {
-        TiB(FileUtils.ONE_TB_BI, "TiB"),
-        GiB(FileUtils.ONE_GB_BI, "GiB"),
-        MiB(FileUtils.ONE_MB_BI, "MiB"),
-        KiB(FileUtils.ONE_KB_BI, "KiB"),
-        Byte(BigInteger.valueOf(1), "bytes");
-
-        static Unit locateUnit(long bytesCount) {
-            if (bytesCount / FileUtils.ONE_TB > 0) {
-                return TiB;
-            }
-            if (bytesCount / FileUtils.ONE_GB > 0) {
-                return GiB;
-            }
-            if (bytesCount / FileUtils.ONE_MB > 0) {
-                return  MiB;
-            }
-            if (bytesCount / FileUtils.ONE_KB > 0) {
-                return  KiB;
-            }
-            return Byte;
-        }
-
-        private static final int SCALE = 2;
-        private static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS = new DecimalFormatSymbols(Locale.US);
-        private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.##", DECIMAL_FORMAT_SYMBOLS);
-
-        private final BigInteger bytesCount;
-        private final String notation;
-
-        Unit(BigInteger bytesCount, String notation) {
-            this.bytesCount = bytesCount;
-            this.notation = notation;
-        }
-
-        public String format(long size) {
-            return format(new BigDecimal(size));
-        }
-
-        public String format(BigDecimal sizeAsDecimal) {
-            return asString(scaleToUnit(sizeAsDecimal)) + " " + notation;
-        }
-
-        public BigDecimal scaleToUnit(BigDecimal sizeAsDecimal) {
-            return sizeAsDecimal.divide(new BigDecimal((bytesCount)), SCALE, BigDecimal.ROUND_FLOOR);
-        }
-
-        private String asString(BigDecimal bigDecimal) {
-            return DECIMAL_FORMAT.format(bigDecimal.doubleValue());
-        }
-    }
-
-    public static String format(long bytesCount) {
-        Preconditions.checkArgument(bytesCount >= 0, "Formatting of a negative size is forbidden");
-
-        return Unit.locateUnit(bytesCount)
-            .format(bytesCount);
-    }
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java
deleted file mode 100644
index fdcb144..0000000
--- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/****************************************************************
- * 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.quota.mailing.subscribers;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-import org.junit.Test;
-
-public class SizeFormatTest {
-
-    @Test
-    public void formatShouldThrowWhenNegative() {
-        assertThatThrownBy(() -> SizeFormat.format(-1))
-            .isInstanceOf(IllegalArgumentException.class);
-    }
-
-    @Test
-    public void formatShouldAcceptZero() {
-        assertThat(SizeFormat.format(0))
-            .isEqualTo("0 bytes");
-    }
-
-    @Test
-    public void formatShouldUseByteWhenAlmostKiB() {
-        assertThat(SizeFormat.format(1023))
-            .isEqualTo("1023 bytes");
-    }
-
-    @Test
-    public void formatShouldUseKiBWhenExactlyOneKiB() {
-        assertThat(SizeFormat.format(1024))
-            .isEqualTo("1 KiB");
-    }
-
-    @Test
-    public void formatShouldHaveTwoDigitPrecision() {
-        assertThat(SizeFormat.format(1024 + 100))
-            .isEqualTo("1.09 KiB");
-    }
-
-    @Test
-    public void formatShouldBeExpressedInKiBWhenAlmostMiB() {
-        assertThat(SizeFormat.format(1024 * 1024 - 1))
-            .isEqualTo("1023.99 KiB");
-    }
-
-    @Test
-    public void formatShouldKeepTwoDigitPrecisionWhenRoundingDown() {
-        assertThat(SizeFormat.format(2 * 1024 * 1024 - 1))
-            .isEqualTo("1.99 MiB");
-    }
-
-    @Test
-    public void formatShouldUseMiBWhenExactlyOneMiB() {
-        assertThat(SizeFormat.format(1024 * 1024))
-            .isEqualTo("1 MiB");
-    }
-
-    @Test
-    public void formatShouldUseGiBWhenExactlyOneGiB() {
-        assertThat(SizeFormat.format(1024 * 1024 * 1024))
-            .isEqualTo("1 GiB");
-    }
-
-    @Test
-    public void formatShouldUseTiBWhenExactlyOneTiB() {
-        assertThat(SizeFormat.format(1024L * 1024L * 1024L * 1024L))
-            .isEqualTo("1 TiB");
-    }
-
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java
index 5de89fc..4898871 100644
--- a/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java
+++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java
@@ -75,7 +75,7 @@ public class QuotaCommandsIntegrationTest {
             outputCapture.getPrintStream());
 
         assertThat(outputCapture.getContent())
-            .containsOnlyOnce("Global Maximum Storage Quota: 36 MB");
+            .containsOnlyOnce("Global Maximum Storage Quota: 36 MiB");
     }
 
     @Test
@@ -111,7 +111,7 @@ public class QuotaCommandsIntegrationTest {
             outputCapture.getPrintStream());
 
         assertThat(outputCapture.getContent())
-            .containsOnlyOnce("Storage space allowed for Quota Root #private&user: 1 GB");
+            .containsOnlyOnce("Storage space allowed for Quota Root #private&user: 1 GiB");
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
----------------------------------------------------------------------
diff --git a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
index 51fd07c..19f0981 100644
--- a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
+++ b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
@@ -33,7 +33,6 @@ import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.PosixParser;
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.time.StopWatch;
 import org.apache.james.cli.exceptions.InvalidArgumentNumberException;
 import org.apache.james.cli.exceptions.JamesCliException;
@@ -57,6 +56,7 @@ import org.apache.james.mailbox.store.probe.SieveProbe;
 import org.apache.james.probe.DataProbe;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.util.Port;
+import org.apache.james.util.SizeFormat;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -393,7 +393,7 @@ public class ServerCmd {
         if (value == SerializableQuota.UNLIMITED) {
             return ValueWithUnit.UNLIMITED;
         }
-        return FileUtils.byteCountToDisplaySize(value);
+        return SizeFormat.format(value);
     }
 
     private String formatStorageValue(SerializableQuotaValue<QuotaSize> value) {
@@ -403,7 +403,7 @@ public class ServerCmd {
             if (size.isUnlimited()) {
                 return ValueWithUnit.UNLIMITED;
             }
-            return FileUtils.byteCountToDisplaySize(size.asLong());
+            return SizeFormat.format(size.asLong());
         }).orElse(ValueWithUnit.UNKNOWN);
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/server/container/util/src/main/java/org/apache/james/util/SizeFormat.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/main/java/org/apache/james/util/SizeFormat.java b/server/container/util/src/main/java/org/apache/james/util/SizeFormat.java
new file mode 100644
index 0000000..4209a95
--- /dev/null
+++ b/server/container/util/src/main/java/org/apache/james/util/SizeFormat.java
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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.util;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+import org.apache.commons.io.FileUtils;
+
+import com.google.common.base.Preconditions;
+
+public class SizeFormat {
+
+    public enum Unit {
+        TiB(FileUtils.ONE_TB_BI, "TiB"),
+        GiB(FileUtils.ONE_GB_BI, "GiB"),
+        MiB(FileUtils.ONE_MB_BI, "MiB"),
+        KiB(FileUtils.ONE_KB_BI, "KiB"),
+        Byte(BigInteger.valueOf(1), "bytes");
+
+        static Unit locateUnit(long bytesCount) {
+            if (bytesCount / FileUtils.ONE_TB > 0) {
+                return TiB;
+            }
+            if (bytesCount / FileUtils.ONE_GB > 0) {
+                return GiB;
+            }
+            if (bytesCount / FileUtils.ONE_MB > 0) {
+                return  MiB;
+            }
+            if (bytesCount / FileUtils.ONE_KB > 0) {
+                return  KiB;
+            }
+            return Byte;
+        }
+
+        private static final int SCALE = 2;
+        private static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS = new DecimalFormatSymbols(Locale.US);
+        private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.##", DECIMAL_FORMAT_SYMBOLS);
+
+        private final BigInteger bytesCount;
+        private final String notation;
+
+        Unit(BigInteger bytesCount, String notation) {
+            this.bytesCount = bytesCount;
+            this.notation = notation;
+        }
+
+        public String format(long size) {
+            return format(new BigDecimal(size));
+        }
+
+        public String format(BigDecimal sizeAsDecimal) {
+            return asString(scaleToUnit(sizeAsDecimal)) + " " + notation;
+        }
+
+        public BigDecimal scaleToUnit(BigDecimal sizeAsDecimal) {
+            return sizeAsDecimal.divide(new BigDecimal((bytesCount)), SCALE, BigDecimal.ROUND_FLOOR);
+        }
+
+        private String asString(BigDecimal bigDecimal) {
+            return DECIMAL_FORMAT.format(bigDecimal.doubleValue());
+        }
+    }
+
+    public static String format(long bytesCount) {
+        Preconditions.checkArgument(bytesCount >= 0, "Formatting of a negative size is forbidden");
+
+        return Unit.locateUnit(bytesCount)
+            .format(bytesCount);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d67d2f1/server/container/util/src/test/java/org/apache/james/util/SizeFormatTest.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/test/java/org/apache/james/util/SizeFormatTest.java b/server/container/util/src/test/java/org/apache/james/util/SizeFormatTest.java
new file mode 100644
index 0000000..f506cc5
--- /dev/null
+++ b/server/container/util/src/test/java/org/apache/james/util/SizeFormatTest.java
@@ -0,0 +1,89 @@
+/****************************************************************
+ * 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.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.Test;
+
+public class SizeFormatTest {
+
+    @Test
+    public void formatShouldThrowWhenNegative() {
+        assertThatThrownBy(() -> SizeFormat.format(-1))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void formatShouldAcceptZero() {
+        assertThat(SizeFormat.format(0))
+            .isEqualTo("0 bytes");
+    }
+
+    @Test
+    public void formatShouldUseByteWhenAlmostKiB() {
+        assertThat(SizeFormat.format(1023))
+            .isEqualTo("1023 bytes");
+    }
+
+    @Test
+    public void formatShouldUseKiBWhenExactlyOneKiB() {
+        assertThat(SizeFormat.format(1024))
+            .isEqualTo("1 KiB");
+    }
+
+    @Test
+    public void formatShouldHaveTwoDigitPrecision() {
+        assertThat(SizeFormat.format(1024 + 100))
+            .isEqualTo("1.09 KiB");
+    }
+
+    @Test
+    public void formatShouldBeExpressedInKiBWhenAlmostMiB() {
+        assertThat(SizeFormat.format(1024 * 1024 - 1))
+            .isEqualTo("1023.99 KiB");
+    }
+
+    @Test
+    public void formatShouldKeepTwoDigitPrecisionWhenRoundingDown() {
+        assertThat(SizeFormat.format(2 * 1024 * 1024 - 1))
+            .isEqualTo("1.99 MiB");
+    }
+
+    @Test
+    public void formatShouldUseMiBWhenExactlyOneMiB() {
+        assertThat(SizeFormat.format(1024 * 1024))
+            .isEqualTo("1 MiB");
+    }
+
+    @Test
+    public void formatShouldUseGiBWhenExactlyOneGiB() {
+        assertThat(SizeFormat.format(1024 * 1024 * 1024))
+            .isEqualTo("1 GiB");
+    }
+
+    @Test
+    public void formatShouldUseTiBWhenExactlyOneTiB() {
+        assertThat(SizeFormat.format(1024L * 1024L * 1024L * 1024L))
+            .isEqualTo("1 TiB");
+    }
+
+}
\ No newline at end of file


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


[4/4] james-project git commit: MAILBOX-331 Fix Eclipse warnings

Posted by bt...@apache.org.
MAILBOX-331 Fix Eclipse warnings


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

Branch: refs/heads/master
Commit: c2b85a004115d4249825a731a92a313bcecc7ced
Parents: 6d67d2f
Author: Antoine Duprat <ad...@linagora.com>
Authored: Mon May 14 12:24:13 2018 +0200
Committer: benwa <bt...@linagora.com>
Committed: Tue May 15 16:21:41 2018 +0700

----------------------------------------------------------------------
 .../test/java/org/apache/james/mailbox/MailboxManagerTest.java  | 5 -----
 .../java/org/apache/james/eventsourcing/CommandDispatcher.java  | 4 +++-
 .../org/apache/james/eventsourcing/EventSourcingSystem.java     | 2 +-
 .../org/apache/james/mailbox/quota/model/QuotaThresholds.java   | 2 +-
 .../org/apache/james/eventsourcing/EventSourcingSystemTest.java | 4 ++--
 5 files changed, 7 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c2b85a00/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
index cda734b..b729b64 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
@@ -23,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThatCode;
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
-import java.time.Instant;
 import java.util.List;
 import java.util.Optional;
 
@@ -44,12 +43,8 @@ import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.MessageId;
 import org.apache.james.mailbox.model.MessageRange;
 import org.apache.james.mailbox.model.MultimailboxesSearchQuery;
-import org.apache.james.mailbox.model.Quota;
-import org.apache.james.mailbox.model.QuotaRoot;
 import org.apache.james.mailbox.model.SearchQuery;
 import org.apache.james.mailbox.model.search.MailboxQuery;
-import org.apache.james.mailbox.quota.QuotaCount;
-import org.apache.james.mailbox.quota.QuotaSize;
 import org.apache.james.mailbox.util.EventCollector;
 import org.apache.james.mime4j.dom.Message;
 import org.assertj.core.api.JUnitSoftAssertions;

http://git-wip-us.apache.org/repos/asf/james-project/blob/c2b85a00/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java
index ef5bfd4..de8b63f 100644
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java
@@ -78,10 +78,11 @@ public class CommandDispatcher {
     }
 
     private final EventBus eventBus;
+    @SuppressWarnings("rawtypes")
     private final Map<Class, CommandHandler> handlers;
 
     @Inject
-    public CommandDispatcher(EventBus eventBus, Collection<CommandHandler> handlers) {
+    public CommandDispatcher(EventBus eventBus, Collection<CommandHandler<?>> handlers) {
         this.eventBus = eventBus;
         this.handlers = handlers.stream()
             .collect(Guavate.toImmutableMap(CommandHandler::handledClass, handler -> handler));
@@ -99,6 +100,7 @@ public class CommandDispatcher {
             .findFirst();
     }
 
+    @SuppressWarnings("unchecked")
     private boolean tryDispatch(Command c) {
         try {
             List<Event> events =

http://git-wip-us.apache.org/repos/asf/james-project/blob/c2b85a00/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java
index e627224..22a2788 100644
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java
@@ -25,7 +25,7 @@ public class EventSourcingSystem {
     private final EventBus eventBus;
     private final CommandDispatcher commandDispatcher;
 
-    public EventSourcingSystem(Set<CommandDispatcher.CommandHandler> handlers, Set<Subscriber> subscribers, EventStore eventStore) {
+    public EventSourcingSystem(Set<CommandDispatcher.CommandHandler<?>> handlers, Set<Subscriber> subscribers, EventStore eventStore) {
         this.eventBus = new EventBus(eventStore, subscribers);
         this.commandDispatcher = new CommandDispatcher(eventBus, handlers);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/c2b85a00/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/model/QuotaThresholds.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/model/QuotaThresholds.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/model/QuotaThresholds.java
index 7a7cff4..042799e 100644
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/model/QuotaThresholds.java
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/model/QuotaThresholds.java
@@ -42,7 +42,7 @@ public class QuotaThresholds {
             .collect(Guavate.toImmutableList());
     }
 
-    public QuotaThreshold highestExceededThreshold(Quota quota) {
+    public QuotaThreshold highestExceededThreshold(Quota<?> quota) {
         return quotaThresholds.stream()
             .filter(quotaLevel -> quotaLevel.isExceeded(quota))
             .findFirst()

http://git-wip-us.apache.org/repos/asf/james-project/blob/c2b85a00/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java
index f0feab2..19c3925 100644
--- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java
+++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java
@@ -22,7 +22,7 @@ package org.apache.james.eventsourcing;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyList;
+import static org.mockito.Matchers.anyListOf;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -87,7 +87,7 @@ public interface EventSourcingSystemTest {
     @Test
     default void throwingStoreShouldNotLeadToPusblishing() {
         EventStore eventStore = mock(EventStore.class);
-        doThrow(new RuntimeException()).when(eventStore).appendAll(anyList());
+        doThrow(new RuntimeException()).when(eventStore).appendAll(anyListOf(Event.class));
         when(eventStore.getEventsOfAggregate(any())).thenReturn(EventStore.History.empty());
 
         DataCollectorSubscriber subscriber = new DataCollectorSubscriber();


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


[2/4] james-project git commit: MAILBOX-333 Solve unit formatting issues

Posted by bt...@apache.org.
MAILBOX-333 Solve unit formatting issues

As referenced in https://issues.apache.org/jira/browse/IO-226,
FileUtils does not round down correctly and loses precisions (1.99 MB will
be Expressed as 1 MB)

Also, https://en.wikipedia.org/wiki/Byte shows the unit should be XiB and not XB.

For these reasons, we implemented our own size formatting logic.


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

Branch: refs/heads/master
Commit: 31ca264027d349a8d3c05c7db51ec28b348fbb0f
Parents: 8982f4a
Author: benwa <bt...@linagora.com>
Authored: Mon May 14 10:47:56 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Tue May 15 16:19:34 2018 +0700

----------------------------------------------------------------------
 .../subscribers/QuotaThresholdNotice.java       |  5 +-
 .../quota/mailing/subscribers/SizeFormat.java   | 92 ++++++++++++++++++++
 .../subscribers/QuotaThresholdNoticeTest.java   | 28 ++++--
 .../mailing/subscribers/SizeFormatTest.java     | 89 +++++++++++++++++++
 4 files changed, 203 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/31ca2640/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
index bc007c5..52ab097 100644
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNotice.java
@@ -30,7 +30,6 @@ import java.util.HashMap;
 import java.util.Objects;
 import java.util.Optional;
 
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.james.core.builder.MimeMessageBuilder;
 import org.apache.james.filesystem.api.FileSystem;
@@ -177,10 +176,10 @@ public class QuotaThresholdNotice {
         sizeThreshold.ifPresent(value -> scopes.put("sizeThreshold", value.getQuotaOccupationRatioAsPercent()));
         countThreshold.ifPresent(value -> scopes.put("countThreshold", value.getQuotaOccupationRatioAsPercent()));
 
-        scopes.put("usedSize", FileUtils.byteCountToDisplaySize(sizeQuota.getUsed().asLong()));
+        scopes.put("usedSize", SizeFormat.format(sizeQuota.getUsed().asLong()));
         scopes.put("hasSizeLimit", sizeQuota.getLimit().isLimited());
         if (sizeQuota.getLimit().isLimited()) {
-            scopes.put("limitSize", FileUtils.byteCountToDisplaySize(sizeQuota.getLimit().asLong()));
+            scopes.put("limitSize", SizeFormat.format(sizeQuota.getLimit().asLong()));
         }
 
         scopes.put("usedCount", countQuota.getUsed().asLong());

http://git-wip-us.apache.org/repos/asf/james-project/blob/31ca2640/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java
new file mode 100644
index 0000000..9a9ebfb
--- /dev/null
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormat.java
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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.quota.mailing.subscribers;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+import org.apache.commons.io.FileUtils;
+
+import com.google.common.base.Preconditions;
+
+public class SizeFormat {
+
+    public enum Unit {
+        TiB(FileUtils.ONE_TB_BI, "TiB"),
+        GiB(FileUtils.ONE_GB_BI, "GiB"),
+        MiB(FileUtils.ONE_MB_BI, "MiB"),
+        KiB(FileUtils.ONE_KB_BI, "KiB"),
+        Byte(BigInteger.valueOf(1), "bytes");
+
+        static Unit locateUnit(long bytesCount) {
+            if (bytesCount / FileUtils.ONE_TB > 0) {
+                return TiB;
+            }
+            if (bytesCount / FileUtils.ONE_GB > 0) {
+                return GiB;
+            }
+            if (bytesCount / FileUtils.ONE_MB > 0) {
+                return  MiB;
+            }
+            if (bytesCount / FileUtils.ONE_KB > 0) {
+                return  KiB;
+            }
+            return Byte;
+        }
+
+        private static final int SCALE = 2;
+        private static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS = new DecimalFormatSymbols(Locale.US);
+        private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.##", DECIMAL_FORMAT_SYMBOLS);
+
+        private final BigInteger bytesCount;
+        private final String notation;
+
+        Unit(BigInteger bytesCount, String notation) {
+            this.bytesCount = bytesCount;
+            this.notation = notation;
+        }
+
+        public String format(long size) {
+            return format(new BigDecimal(size));
+        }
+
+        public String format(BigDecimal sizeAsDecimal) {
+            return asString(scaleToUnit(sizeAsDecimal)) + " " + notation;
+        }
+
+        public BigDecimal scaleToUnit(BigDecimal sizeAsDecimal) {
+            return sizeAsDecimal.divide(new BigDecimal((bytesCount)), SCALE, BigDecimal.ROUND_FLOOR);
+        }
+
+        private String asString(BigDecimal bigDecimal) {
+            return DECIMAL_FORMAT.format(bigDecimal.doubleValue());
+        }
+    }
+
+    public static String format(long bytesCount) {
+        Preconditions.checkArgument(bytesCount >= 0, "Formatting of a negative size is forbidden");
+
+        return Unit.locateUnit(bytesCount)
+            .format(bytesCount);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/31ca2640/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
index 0898b18..3bdc35b 100644
--- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
+++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/QuotaThresholdNoticeTest.java
@@ -209,20 +209,32 @@ class QuotaThresholdNoticeTest {
             .withConfiguration(DEFAULT_CONFIGURATION)
             .sizeQuota(Quota.<QuotaSize>builder()
                 .used(QuotaSize.size(801 * 1024 * 1024))
-                .computedLimit(QuotaSize.size(1024 * 1024 * 1024))
+                .computedLimit(QuotaSize.size(1 * 1024 * 1024 * 1024))
                 .build())
             .countQuota(Counts._72_PERCENT)
             .sizeThreshold(HistoryEvolution.higherThresholdReached(sizeThresholdChange, NotAlreadyReachedDuringGracePeriod))
             .build()
             .get()
             .generateReport(fileSystem))
-            .isEqualTo("You receive this email because you recently exceeded a threshold related to the quotas of your email account.\n" +
-                "\n" +
-                "You currently occupy more than 80 % of the total size allocated to you.\n" +
-                "You currently occupy 801 MB on a total of 1 GB allocated to you.\n" +
-                "\n" +
-                "You need to be aware that actions leading to exceeded quotas will be denied. This will result in a degraded service.\n" +
-                "To mitigate this issue you might reach your administrator in order to increase your configured quota. You might also delete some non important emails.");
+            .contains("You currently occupy 801 MiB on a total of 1 GiB allocated to you.");
+    }
+
+    @Test
+    void generateReportShouldTruncateLowDigitsFormatSizeUnits() throws Exception {
+        QuotaThresholdChange sizeThresholdChange = new QuotaThresholdChange(_80, NOW);
+
+        assertThat(QuotaThresholdNotice.builder()
+            .withConfiguration(DEFAULT_CONFIGURATION)
+            .sizeQuota(Quota.<QuotaSize>builder()
+                .used(QuotaSize.size(801 * 1024 * 1024))
+                .computedLimit(QuotaSize.size((2 * 1024 * 1024 * 1024) - 1))
+                .build())
+            .countQuota(Counts._72_PERCENT)
+            .sizeThreshold(HistoryEvolution.higherThresholdReached(sizeThresholdChange, NotAlreadyReachedDuringGracePeriod))
+            .build()
+            .get()
+            .generateReport(fileSystem))
+            .contains("You currently occupy 801 MiB on a total of 1.99 GiB allocated to you.");
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/31ca2640/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java
new file mode 100644
index 0000000..fdcb144
--- /dev/null
+++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/subscribers/SizeFormatTest.java
@@ -0,0 +1,89 @@
+/****************************************************************
+ * 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.quota.mailing.subscribers;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.Test;
+
+public class SizeFormatTest {
+
+    @Test
+    public void formatShouldThrowWhenNegative() {
+        assertThatThrownBy(() -> SizeFormat.format(-1))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void formatShouldAcceptZero() {
+        assertThat(SizeFormat.format(0))
+            .isEqualTo("0 bytes");
+    }
+
+    @Test
+    public void formatShouldUseByteWhenAlmostKiB() {
+        assertThat(SizeFormat.format(1023))
+            .isEqualTo("1023 bytes");
+    }
+
+    @Test
+    public void formatShouldUseKiBWhenExactlyOneKiB() {
+        assertThat(SizeFormat.format(1024))
+            .isEqualTo("1 KiB");
+    }
+
+    @Test
+    public void formatShouldHaveTwoDigitPrecision() {
+        assertThat(SizeFormat.format(1024 + 100))
+            .isEqualTo("1.09 KiB");
+    }
+
+    @Test
+    public void formatShouldBeExpressedInKiBWhenAlmostMiB() {
+        assertThat(SizeFormat.format(1024 * 1024 - 1))
+            .isEqualTo("1023.99 KiB");
+    }
+
+    @Test
+    public void formatShouldKeepTwoDigitPrecisionWhenRoundingDown() {
+        assertThat(SizeFormat.format(2 * 1024 * 1024 - 1))
+            .isEqualTo("1.99 MiB");
+    }
+
+    @Test
+    public void formatShouldUseMiBWhenExactlyOneMiB() {
+        assertThat(SizeFormat.format(1024 * 1024))
+            .isEqualTo("1 MiB");
+    }
+
+    @Test
+    public void formatShouldUseGiBWhenExactlyOneGiB() {
+        assertThat(SizeFormat.format(1024 * 1024 * 1024))
+            .isEqualTo("1 GiB");
+    }
+
+    @Test
+    public void formatShouldUseTiBWhenExactlyOneTiB() {
+        assertThat(SizeFormat.format(1024L * 1024L * 1024L * 1024L))
+            .isEqualTo("1 TiB");
+    }
+
+}
\ No newline at end of file


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