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 <btell...@linagora.com> Authored: Mon May 14 10:56:56 2018 +0700 Committer: benwa <btell...@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-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org