JAMES-1877 Improve TimeConverter
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/03fc53a4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/03fc53a4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/03fc53a4 Branch: refs/heads/master Commit: 03fc53a4760e2d28a312fd57f9768f4ef54e3264 Parents: 788b393 Author: Benoit Tellier <btell...@linagora.com> Authored: Tue Nov 29 11:23:48 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Tue Jan 10 14:35:27 2017 +0700 ---------------------------------------------------------------------- .../org/apache/james/util/TimeConverter.java | 8 +- .../apache/james/util/TimeConverterTest.java | 242 +++++++++++++++++-- 2 files changed, 229 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/03fc53a4/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java ---------------------------------------------------------------------- diff --git a/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java b/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java index 6709de0..2a877b7 100644 --- a/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java +++ b/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java @@ -19,6 +19,7 @@ package org.apache.james.util; import java.util.HashMap; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -32,14 +33,19 @@ public class TimeConverter { static { // add allowed units and their respective multiplier + multipliers.put("ms", 1); multipliers.put("msec", 1); multipliers.put("msecs", 1); + multipliers.put("s", 1000); multipliers.put("sec", 1000); multipliers.put("secs", 1000); + multipliers.put("m", 1000 * 60); multipliers.put("minute", 1000 * 60); multipliers.put("minutes", 1000 * 60); + multipliers.put("h", 1000 * 60 * 60); multipliers.put("hour", 1000 * 60 * 60); multipliers.put("hours", 1000 * 60 * 60); + multipliers.put("d", 1000 * 60 * 60 * 24); multipliers.put("day", 1000 * 60 * 60 * 24); multipliers.put("days", 1000 * 60 * 60 * 24); @@ -63,7 +69,7 @@ public class TimeConverter { * Get thrown if an illegal unit was used */ public static long getMilliSeconds(long amount, String unit) throws NumberFormatException { - Object multiplierObject = multipliers.get(unit); + Object multiplierObject = multipliers.get(unit.toLowerCase(Locale.US)); if (multiplierObject == null) { throw new NumberFormatException("Unknown unit: " + unit); } http://git-wip-us.apache.org/repos/asf/james-project/blob/03fc53a4/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java ---------------------------------------------------------------------- diff --git a/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java b/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java index adf620f..b05a935 100644 --- a/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java +++ b/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java @@ -20,6 +20,8 @@ package org.apache.james.util; import static org.assertj.core.api.Assertions.assertThat; +import java.util.concurrent.TimeUnit; + import org.junit.Test; public class TimeConverterTest { @@ -33,8 +35,8 @@ public class TimeConverterTest { //Then assertThat(actual).isEqualTo(expected); } - - @Test + + @Test public void getMilliSecondsShouldConvertValueWhenMsecAmountAsString() { //Given long expected = 2; @@ -43,6 +45,46 @@ public class TimeConverterTest { //Then assertThat(actual).isEqualTo(expected); } + + @Test + public void getMilliSecondsShouldConvertValueWhenMsUnit() { + //Given + long expected = 2; + //When + long actual = TimeConverter.getMilliSeconds(2, "ms"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenMsAmountAsString() { + //Given + long expected = 2; + //When + long actual = TimeConverter.getMilliSeconds("2 ms"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenMsUnitCapital() { + //Given + long expected = 2; + //When + long actual = TimeConverter.getMilliSeconds(2, "Ms"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenMsCapitalAmountAsString() { + //Given + long expected = 2; + //When + long actual = TimeConverter.getMilliSeconds("2 Ms"); + //Then + assertThat(actual).isEqualTo(expected); + } @Test public void getMilliSecondsShouldConvertValueWhenMsecsUnit() { @@ -63,11 +105,31 @@ public class TimeConverterTest { //Then assertThat(actual).isEqualTo(expected); } + + @Test + public void getMilliSecondsShouldConvertValueWhenSUnit() { + //Given + long expected = TimeUnit.SECONDS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "s"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenSAmountAsString() { + //Given + long expected = TimeUnit.SECONDS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 s"); + //Then + assertThat(actual).isEqualTo(expected); + } @Test public void getMilliSecondsShouldConvertValueWhenSecUnit() { //Given - long expected = 2000; + long expected = TimeUnit.SECONDS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "sec"); //Then @@ -77,16 +139,36 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenSecAmountAsString() { //Given - long expected = 2000; + long expected = TimeUnit.SECONDS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 sec"); //Then assertThat(actual).isEqualTo(expected); } + + @Test + public void getMilliSecondsShouldConvertValueWhenSecCapitalUnit() { + //Given + long expected = TimeUnit.SECONDS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "Sec"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenSecCapitalAmountAsString() { + //Given + long expected = TimeUnit.SECONDS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 Sec"); + //Then + assertThat(actual).isEqualTo(expected); + } @Test public void getMilliSecondsShouldConvertValueWhenSecsUnit() { - long expected = 2000; + long expected = TimeUnit.SECONDS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "secs"); //Then @@ -96,17 +178,37 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenSecsAmountAsString() { //Given - long expected = 2000; + long expected = TimeUnit.SECONDS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 secs"); //Then assertThat(actual).isEqualTo(expected); } - + + @Test + public void getMilliSecondsShouldConvertValueWhenMUnit() { + //Given + long expected = TimeUnit.MINUTES.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "m"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenMAmountAsString() { + //Given + long expected = TimeUnit.MINUTES.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 m"); + //Then + assertThat(actual).isEqualTo(expected); + } + @Test public void getMilliSecondsShouldConvertValueWhenMinuteUnit() { //Given - long expected = 120000; + long expected = TimeUnit.MINUTES.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "minute"); //Then @@ -116,7 +218,7 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenMinuteAmountAsString() { //Given - long expected = 120000; + long expected = TimeUnit.MINUTES.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 minute"); //Then @@ -124,9 +226,29 @@ public class TimeConverterTest { } @Test + public void getMilliSecondsShouldConvertValueWhenMinuteCapitalUnit() { + //Given + long expected = TimeUnit.MINUTES.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "Minute"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenMinuteCapitalAmountAsString() { + //Given + long expected = TimeUnit.MINUTES.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 Minute"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test public void getMilliSecondsShouldConvertValueWhenMinutesUnit() { //Given - long expected = 120000; + long expected = TimeUnit.MINUTES.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "minutes"); //Then @@ -136,17 +258,37 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenMinutesAmountAsString() { //Given - long expected = 120000; + long expected = TimeUnit.MINUTES.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 minutes"); //Then assertThat(actual).isEqualTo(expected); } - + + @Test + public void getMilliSecondsShouldConvertValueWhenHUnit() { + //Given + long expected = TimeUnit.HOURS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "h"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenHAmountAsString() { + //Given + long expected = TimeUnit.HOURS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 h"); + //Then + assertThat(actual).isEqualTo(expected); + } + @Test public void getMilliSecondsShouldConvertValueWhenHourUnit() { //Given - long expected = 7200000; + long expected = TimeUnit.HOURS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "hour"); //Then @@ -156,17 +298,37 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenHourAmountAsString() { //Given - long expected = 7200000; + long expected = TimeUnit.HOURS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 hour"); //Then assertThat(actual).isEqualTo(expected); } + + @Test + public void getMilliSecondsShouldConvertValueWhenHourCapitalUnit() { + //Given + long expected = TimeUnit.HOURS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "Hour"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenHourCapitalAmountAsString() { + //Given + long expected = TimeUnit.HOURS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 Hour"); + //Then + assertThat(actual).isEqualTo(expected); + } @Test public void getMilliSecondsShouldConvertValueWhenHoursUnit() { //Given - long expected = 7200000; + long expected = TimeUnit.HOURS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "hours"); //Then @@ -176,17 +338,37 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenHoursAmountAsString() { //Given - long expected = 7200000; + long expected = TimeUnit.HOURS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 hours"); //Then assertThat(actual).isEqualTo(expected); } + + @Test + public void getMilliSecondsShouldConvertValueWhenDUnit() { + //Given + long expected = TimeUnit.DAYS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "d"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenDAmountAsString() { + //Given + long expected = TimeUnit.DAYS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 d"); + //Then + assertThat(actual).isEqualTo(expected); + } @Test public void getMilliSecondsShouldConvertValueWhenDayUnit() { //Given - long expected = 172800000; + long expected = TimeUnit.DAYS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "day"); //Then @@ -196,17 +378,37 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenDayAmountAsString() { //Given - long expected = 172800000; + long expected = TimeUnit.DAYS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 day"); //Then assertThat(actual).isEqualTo(expected); } + + @Test + public void getMilliSecondsShouldConvertValueWhenDayCapitalUnit() { + //Given + long expected = TimeUnit.DAYS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds(2, "Day"); + //Then + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getMilliSecondsShouldConvertValueWhenDayCapitalAmountAsString() { + //Given + long expected = TimeUnit.DAYS.toMillis(2); + //When + long actual = TimeConverter.getMilliSeconds("2 Day"); + //Then + assertThat(actual).isEqualTo(expected); + } @Test public void getMilliSecondsShouldConvertValueWhenDaysUnit() { //Given - long expected = 172800000; + long expected = TimeUnit.DAYS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds(2, "days"); //Then @@ -216,7 +418,7 @@ public class TimeConverterTest { @Test public void getMilliSecondsShouldConvertValueWhenDaysAmountAsString() { //Given - long expected = 172800000; + long expected = TimeUnit.DAYS.toMillis(2); //When long actual = TimeConverter.getMilliSeconds("2 days"); //Then --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org