From 556bb9e43c9b5119c8830bf4abcffef8c56256a3 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 5 Mar 2014 10:28:40 +0100 Subject: Move WorkDuration API to internal package as it will be refactored is 4.3 --- .../batch/internal/DefaultRequirementTest.java | 2 +- .../server/internal/DefaultCharacteristicTest.java | 2 +- .../sonar/api/utils/WorkDurationFactoryTest.java | 67 ------- .../java/org/sonar/api/utils/WorkDurationTest.java | 201 --------------------- .../utils/internal/WorkDurationFactoryTest.java | 67 +++++++ .../sonar/api/utils/internal/WorkDurationTest.java | 201 +++++++++++++++++++++ 6 files changed, 270 insertions(+), 270 deletions(-) delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationFactoryTest.java delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationTest.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationFactoryTest.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationTest.java (limited to 'sonar-plugin-api/src/test') diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java index 57cb94e3ec6..5420f55fa73 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java @@ -22,8 +22,8 @@ package org.sonar.api.technicaldebt.batch.internal; import org.junit.Test; import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.WorkDuration; import org.sonar.api.utils.WorkUnit; +import org.sonar.api.utils.internal.WorkDuration; import java.text.SimpleDateFormat; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java index 4ae4d77241b..fbba16605f3 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java @@ -22,8 +22,8 @@ package org.sonar.api.technicaldebt.server.internal; import org.junit.Test; import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.WorkDuration; import org.sonar.api.utils.WorkUnit; +import org.sonar.api.utils.internal.WorkDuration; import static org.fest.assertions.Assertions.assertThat; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationFactoryTest.java deleted file mode 100644 index c9b1652903d..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationFactoryTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.api.utils; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.CoreProperties; -import org.sonar.api.config.Settings; - -import static org.fest.assertions.Assertions.assertThat; - -public class WorkDurationFactoryTest { - - WorkDurationFactory factory; - - static final int HOURS_IN_DAY = 8; - static final Long ONE_HOUR_IN_MINUTES = 1L * 60; - - @Before - public void setUp() throws Exception { - Settings settings = new Settings(); - settings.setProperty(CoreProperties.HOURS_IN_DAY, HOURS_IN_DAY); - factory = new WorkDurationFactory(settings); - } - - @Test - public void create_from_working_value() throws Exception { - // 1 working day -> 8 hours - assertThat(factory.createFromWorkingValue(1, WorkDuration.UNIT.DAYS).toMinutes()).isEqualTo(8L * ONE_HOUR_IN_MINUTES); - // 8 hours - assertThat(factory.createFromWorkingValue(8, WorkDuration.UNIT.HOURS).toMinutes()).isEqualTo(8L * ONE_HOUR_IN_MINUTES); - } - - @Test - public void create_from_working_long() throws Exception { - WorkDuration workDuration = factory.createFromWorkingLong(1l); - assertThat(workDuration.days()).isEqualTo(0); - assertThat(workDuration.hours()).isEqualTo(0); - assertThat(workDuration.minutes()).isEqualTo(1); - } - - @Test - public void create_from_seconds() throws Exception { - WorkDuration workDuration = factory.createFromMinutes(8L * ONE_HOUR_IN_MINUTES); - assertThat(workDuration.days()).isEqualTo(1); - assertThat(workDuration.hours()).isEqualTo(0); - assertThat(workDuration.minutes()).isEqualTo(0); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationTest.java deleted file mode 100644 index ddf60903360..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkDurationTest.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.api.utils; - -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -public class WorkDurationTest { - - static final int HOURS_IN_DAY = 8; - - static final Long ONE_MINUTE = 1L; - static final Long ONE_HOUR_IN_MINUTES = ONE_MINUTE * 60; - static final Long ONE_DAY_IN_MINUTES = ONE_HOUR_IN_MINUTES * HOURS_IN_DAY; - - @Test - public void create_from_days_hours_minutes() throws Exception { - WorkDuration workDuration = WorkDuration.create(1, 1, 1, HOURS_IN_DAY); - assertThat(workDuration.days()).isEqualTo(1); - assertThat(workDuration.hours()).isEqualTo(1); - assertThat(workDuration.minutes()).isEqualTo(1); - assertThat(workDuration.toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE); - assertThat(workDuration.hoursInDay()).isEqualTo(HOURS_IN_DAY); - } - - @Test - public void create_from_value_and_unit() throws Exception { - WorkDuration result = WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.DAYS, HOURS_IN_DAY); - assertThat(result.days()).isEqualTo(1); - assertThat(result.hours()).isEqualTo(0); - assertThat(result.minutes()).isEqualTo(0); - assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); - assertThat(result.toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES); - - assertThat(WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES); - assertThat(WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toMinutes()).isEqualTo(ONE_HOUR_IN_MINUTES); - assertThat(WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toMinutes()).isEqualTo(ONE_MINUTE); - } - - @Test - public void create_from_minutes() throws Exception { - WorkDuration workDuration = WorkDuration.createFromMinutes(ONE_MINUTE, HOURS_IN_DAY); - assertThat(workDuration.days()).isEqualTo(0); - assertThat(workDuration.hours()).isEqualTo(0); - assertThat(workDuration.minutes()).isEqualTo(1); - - workDuration = WorkDuration.createFromMinutes(ONE_HOUR_IN_MINUTES, HOURS_IN_DAY); - assertThat(workDuration.days()).isEqualTo(0); - assertThat(workDuration.hours()).isEqualTo(1); - assertThat(workDuration.minutes()).isEqualTo(0); - - workDuration = WorkDuration.createFromMinutes(ONE_DAY_IN_MINUTES, HOURS_IN_DAY); - assertThat(workDuration.days()).isEqualTo(1); - assertThat(workDuration.hours()).isEqualTo(0); - assertThat(workDuration.minutes()).isEqualTo(0); - } - - @Test - public void create_from_working_long() throws Exception { - // 1 minute - WorkDuration workDuration = WorkDuration.createFromLong(1L, HOURS_IN_DAY); - assertThat(workDuration.days()).isEqualTo(0); - assertThat(workDuration.hours()).isEqualTo(0); - assertThat(workDuration.minutes()).isEqualTo(1); - - // 1 hour - workDuration = WorkDuration.createFromLong(100L, HOURS_IN_DAY); - assertThat(workDuration.days()).isEqualTo(0); - assertThat(workDuration.hours()).isEqualTo(1); - assertThat(workDuration.minutes()).isEqualTo(0); - - // 1 day - workDuration = WorkDuration.createFromLong(10000L, HOURS_IN_DAY); - assertThat(workDuration.days()).isEqualTo(1); - assertThat(workDuration.hours()).isEqualTo(0); - assertThat(workDuration.minutes()).isEqualTo(0); - } - - @Test - public void convert_to_seconds() throws Exception { - assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toMinutes()).isEqualTo(2L * ONE_MINUTE); - assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toMinutes()).isEqualTo(2L * ONE_HOUR_IN_MINUTES); - assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toMinutes()).isEqualTo(2L * ONE_DAY_IN_MINUTES); - } - - @Test - public void convert_to_working_days() throws Exception { - assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toWorkingDays()).isEqualTo(2d / 60d / 8d); - assertThat(WorkDuration.createFromValueAndUnit(240, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toWorkingDays()).isEqualTo(0.5); - assertThat(WorkDuration.createFromValueAndUnit(4, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(0.5); - assertThat(WorkDuration.createFromValueAndUnit(8, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(1d); - assertThat(WorkDuration.createFromValueAndUnit(16, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(2d); - assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(2d); - } - - @Test - public void convert_to_working_long() throws Exception { - assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toLong()).isEqualTo(2l); - assertThat(WorkDuration.createFromValueAndUnit(4, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toLong()).isEqualTo(400l); - assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toLong()).isEqualTo(10200l); - assertThat(WorkDuration.createFromValueAndUnit(8, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toLong()).isEqualTo(10000l); - assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toLong()).isEqualTo(20000l); - } - - @Test - public void add() throws Exception { - // 4h + 5h = 1d 1h - WorkDuration result = WorkDuration.createFromValueAndUnit(4, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).add(WorkDuration.createFromValueAndUnit(5, WorkDuration.UNIT.HOURS, HOURS_IN_DAY)); - assertThat(result.days()).isEqualTo(1); - assertThat(result.hours()).isEqualTo(1); - assertThat(result.minutes()).isEqualTo(0); - assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); - - // 40 m + 30m = 1h 10m - result = WorkDuration.createFromValueAndUnit(40, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).add(WorkDuration.createFromValueAndUnit(30, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY)); - assertThat(result.days()).isEqualTo(0); - assertThat(result.hours()).isEqualTo(1); - assertThat(result.minutes()).isEqualTo(10); - assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); - - // 10 m + 20m = 30m - assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).add( - WorkDuration.createFromValueAndUnit(20, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY) - ).minutes()).isEqualTo(30); - - assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).add(null).minutes()).isEqualTo(10); - } - - @Test - public void subtract() throws Exception { - // 1d 1h - 5h = 4h - WorkDuration result = WorkDuration.create(1, 1, 0, HOURS_IN_DAY).subtract(WorkDuration.createFromValueAndUnit(5, WorkDuration.UNIT.HOURS, HOURS_IN_DAY)); - assertThat(result.days()).isEqualTo(0); - assertThat(result.hours()).isEqualTo(4); - assertThat(result.minutes()).isEqualTo(0); - assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); - - // 1h 10m - 30m = 40m - result = WorkDuration.create(0, 1, 10, HOURS_IN_DAY).subtract(WorkDuration.createFromValueAndUnit(30, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY)); - assertThat(result.days()).isEqualTo(0); - assertThat(result.hours()).isEqualTo(0); - assertThat(result.minutes()).isEqualTo(40); - assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); - - // 30m - 20m = 10m - assertThat(WorkDuration.createFromValueAndUnit(30, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).subtract(WorkDuration.createFromValueAndUnit(20, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY)) - .minutes()).isEqualTo(10); - - assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).subtract(null).minutes()).isEqualTo(10); - } - - @Test - public void multiply() throws Exception { - // 5h * 2 = 1d 2h - WorkDuration result = WorkDuration.createFromValueAndUnit(5, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).multiply(2); - assertThat(result.days()).isEqualTo(1); - assertThat(result.hours()).isEqualTo(2); - assertThat(result.minutes()).isEqualTo(0); - assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); - } - - @Test - public void test_equals_and_hashcode() throws Exception { - WorkDuration duration = WorkDuration.createFromLong(28800, HOURS_IN_DAY); - WorkDuration durationWithSameValue = WorkDuration.createFromLong(28800, HOURS_IN_DAY); - WorkDuration durationWithDifferentValue = WorkDuration.createFromLong(14400, HOURS_IN_DAY); - - assertThat(duration).isEqualTo(duration); - assertThat(durationWithSameValue).isEqualTo(duration); - assertThat(durationWithDifferentValue).isNotEqualTo(duration); - assertThat(duration).isNotEqualTo(null); - - assertThat(duration.hashCode()).isEqualTo(duration.hashCode()); - assertThat(durationWithSameValue.hashCode()).isEqualTo(duration.hashCode()); - assertThat(durationWithDifferentValue.hashCode()).isNotEqualTo(duration.hashCode()); - } - - @Test - public void test_toString() throws Exception { - assertThat(WorkDuration.createFromLong(28800, HOURS_IN_DAY).toString()).isNotNull(); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationFactoryTest.java new file mode 100644 index 00000000000..735ab46fc8c --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationFactoryTest.java @@ -0,0 +1,67 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.api.utils.internal; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; + +import static org.fest.assertions.Assertions.assertThat; + +public class WorkDurationFactoryTest { + + WorkDurationFactory factory; + + static final int HOURS_IN_DAY = 8; + static final Long ONE_HOUR_IN_MINUTES = 1L * 60; + + @Before + public void setUp() throws Exception { + Settings settings = new Settings(); + settings.setProperty(CoreProperties.HOURS_IN_DAY, HOURS_IN_DAY); + factory = new WorkDurationFactory(settings); + } + + @Test + public void create_from_working_value() throws Exception { + // 1 working day -> 8 hours + assertThat(factory.createFromWorkingValue(1, WorkDuration.UNIT.DAYS).toMinutes()).isEqualTo(8L * ONE_HOUR_IN_MINUTES); + // 8 hours + assertThat(factory.createFromWorkingValue(8, WorkDuration.UNIT.HOURS).toMinutes()).isEqualTo(8L * ONE_HOUR_IN_MINUTES); + } + + @Test + public void create_from_working_long() throws Exception { + WorkDuration workDuration = factory.createFromWorkingLong(1l); + assertThat(workDuration.days()).isEqualTo(0); + assertThat(workDuration.hours()).isEqualTo(0); + assertThat(workDuration.minutes()).isEqualTo(1); + } + + @Test + public void create_from_seconds() throws Exception { + WorkDuration workDuration = factory.createFromMinutes(8L * ONE_HOUR_IN_MINUTES); + assertThat(workDuration.days()).isEqualTo(1); + assertThat(workDuration.hours()).isEqualTo(0); + assertThat(workDuration.minutes()).isEqualTo(0); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationTest.java new file mode 100644 index 00000000000..c52aac96b8e --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/WorkDurationTest.java @@ -0,0 +1,201 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.api.utils.internal; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class WorkDurationTest { + + static final int HOURS_IN_DAY = 8; + + static final Long ONE_MINUTE = 1L; + static final Long ONE_HOUR_IN_MINUTES = ONE_MINUTE * 60; + static final Long ONE_DAY_IN_MINUTES = ONE_HOUR_IN_MINUTES * HOURS_IN_DAY; + + @Test + public void create_from_days_hours_minutes() throws Exception { + WorkDuration workDuration = WorkDuration.create(1, 1, 1, HOURS_IN_DAY); + assertThat(workDuration.days()).isEqualTo(1); + assertThat(workDuration.hours()).isEqualTo(1); + assertThat(workDuration.minutes()).isEqualTo(1); + assertThat(workDuration.toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE); + assertThat(workDuration.hoursInDay()).isEqualTo(HOURS_IN_DAY); + } + + @Test + public void create_from_value_and_unit() throws Exception { + WorkDuration result = WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.DAYS, HOURS_IN_DAY); + assertThat(result.days()).isEqualTo(1); + assertThat(result.hours()).isEqualTo(0); + assertThat(result.minutes()).isEqualTo(0); + assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); + assertThat(result.toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES); + + assertThat(WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES); + assertThat(WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toMinutes()).isEqualTo(ONE_HOUR_IN_MINUTES); + assertThat(WorkDuration.createFromValueAndUnit(1, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toMinutes()).isEqualTo(ONE_MINUTE); + } + + @Test + public void create_from_minutes() throws Exception { + WorkDuration workDuration = WorkDuration.createFromMinutes(ONE_MINUTE, HOURS_IN_DAY); + assertThat(workDuration.days()).isEqualTo(0); + assertThat(workDuration.hours()).isEqualTo(0); + assertThat(workDuration.minutes()).isEqualTo(1); + + workDuration = WorkDuration.createFromMinutes(ONE_HOUR_IN_MINUTES, HOURS_IN_DAY); + assertThat(workDuration.days()).isEqualTo(0); + assertThat(workDuration.hours()).isEqualTo(1); + assertThat(workDuration.minutes()).isEqualTo(0); + + workDuration = WorkDuration.createFromMinutes(ONE_DAY_IN_MINUTES, HOURS_IN_DAY); + assertThat(workDuration.days()).isEqualTo(1); + assertThat(workDuration.hours()).isEqualTo(0); + assertThat(workDuration.minutes()).isEqualTo(0); + } + + @Test + public void create_from_working_long() throws Exception { + // 1 minute + WorkDuration workDuration = WorkDuration.createFromLong(1L, HOURS_IN_DAY); + assertThat(workDuration.days()).isEqualTo(0); + assertThat(workDuration.hours()).isEqualTo(0); + assertThat(workDuration.minutes()).isEqualTo(1); + + // 1 hour + workDuration = WorkDuration.createFromLong(100L, HOURS_IN_DAY); + assertThat(workDuration.days()).isEqualTo(0); + assertThat(workDuration.hours()).isEqualTo(1); + assertThat(workDuration.minutes()).isEqualTo(0); + + // 1 day + workDuration = WorkDuration.createFromLong(10000L, HOURS_IN_DAY); + assertThat(workDuration.days()).isEqualTo(1); + assertThat(workDuration.hours()).isEqualTo(0); + assertThat(workDuration.minutes()).isEqualTo(0); + } + + @Test + public void convert_to_seconds() throws Exception { + assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toMinutes()).isEqualTo(2L * ONE_MINUTE); + assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toMinutes()).isEqualTo(2L * ONE_HOUR_IN_MINUTES); + assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toMinutes()).isEqualTo(2L * ONE_DAY_IN_MINUTES); + } + + @Test + public void convert_to_working_days() throws Exception { + assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toWorkingDays()).isEqualTo(2d / 60d / 8d); + assertThat(WorkDuration.createFromValueAndUnit(240, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toWorkingDays()).isEqualTo(0.5); + assertThat(WorkDuration.createFromValueAndUnit(4, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(0.5); + assertThat(WorkDuration.createFromValueAndUnit(8, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(1d); + assertThat(WorkDuration.createFromValueAndUnit(16, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(2d); + assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toWorkingDays()).isEqualTo(2d); + } + + @Test + public void convert_to_working_long() throws Exception { + assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).toLong()).isEqualTo(2l); + assertThat(WorkDuration.createFromValueAndUnit(4, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toLong()).isEqualTo(400l); + assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toLong()).isEqualTo(10200l); + assertThat(WorkDuration.createFromValueAndUnit(8, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).toLong()).isEqualTo(10000l); + assertThat(WorkDuration.createFromValueAndUnit(2, WorkDuration.UNIT.DAYS, HOURS_IN_DAY).toLong()).isEqualTo(20000l); + } + + @Test + public void add() throws Exception { + // 4h + 5h = 1d 1h + WorkDuration result = WorkDuration.createFromValueAndUnit(4, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).add(WorkDuration.createFromValueAndUnit(5, WorkDuration.UNIT.HOURS, HOURS_IN_DAY)); + assertThat(result.days()).isEqualTo(1); + assertThat(result.hours()).isEqualTo(1); + assertThat(result.minutes()).isEqualTo(0); + assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); + + // 40 m + 30m = 1h 10m + result = WorkDuration.createFromValueAndUnit(40, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).add(WorkDuration.createFromValueAndUnit(30, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY)); + assertThat(result.days()).isEqualTo(0); + assertThat(result.hours()).isEqualTo(1); + assertThat(result.minutes()).isEqualTo(10); + assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); + + // 10 m + 20m = 30m + assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).add( + WorkDuration.createFromValueAndUnit(20, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY) + ).minutes()).isEqualTo(30); + + assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).add(null).minutes()).isEqualTo(10); + } + + @Test + public void subtract() throws Exception { + // 1d 1h - 5h = 4h + WorkDuration result = WorkDuration.create(1, 1, 0, HOURS_IN_DAY).subtract(WorkDuration.createFromValueAndUnit(5, WorkDuration.UNIT.HOURS, HOURS_IN_DAY)); + assertThat(result.days()).isEqualTo(0); + assertThat(result.hours()).isEqualTo(4); + assertThat(result.minutes()).isEqualTo(0); + assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); + + // 1h 10m - 30m = 40m + result = WorkDuration.create(0, 1, 10, HOURS_IN_DAY).subtract(WorkDuration.createFromValueAndUnit(30, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY)); + assertThat(result.days()).isEqualTo(0); + assertThat(result.hours()).isEqualTo(0); + assertThat(result.minutes()).isEqualTo(40); + assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); + + // 30m - 20m = 10m + assertThat(WorkDuration.createFromValueAndUnit(30, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).subtract(WorkDuration.createFromValueAndUnit(20, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY)) + .minutes()).isEqualTo(10); + + assertThat(WorkDuration.createFromValueAndUnit(10, WorkDuration.UNIT.MINUTES, HOURS_IN_DAY).subtract(null).minutes()).isEqualTo(10); + } + + @Test + public void multiply() throws Exception { + // 5h * 2 = 1d 2h + WorkDuration result = WorkDuration.createFromValueAndUnit(5, WorkDuration.UNIT.HOURS, HOURS_IN_DAY).multiply(2); + assertThat(result.days()).isEqualTo(1); + assertThat(result.hours()).isEqualTo(2); + assertThat(result.minutes()).isEqualTo(0); + assertThat(result.hoursInDay()).isEqualTo(HOURS_IN_DAY); + } + + @Test + public void test_equals_and_hashcode() throws Exception { + WorkDuration duration = WorkDuration.createFromLong(28800, HOURS_IN_DAY); + WorkDuration durationWithSameValue = WorkDuration.createFromLong(28800, HOURS_IN_DAY); + WorkDuration durationWithDifferentValue = WorkDuration.createFromLong(14400, HOURS_IN_DAY); + + assertThat(duration).isEqualTo(duration); + assertThat(durationWithSameValue).isEqualTo(duration); + assertThat(durationWithDifferentValue).isNotEqualTo(duration); + assertThat(duration).isNotEqualTo(null); + + assertThat(duration.hashCode()).isEqualTo(duration.hashCode()); + assertThat(durationWithSameValue.hashCode()).isEqualTo(duration.hashCode()); + assertThat(durationWithDifferentValue.hashCode()).isNotEqualTo(duration.hashCode()); + } + + @Test + public void test_toString() throws Exception { + assertThat(WorkDuration.createFromLong(28800, HOURS_IN_DAY).toString()).isNotNull(); + } +} -- cgit v1.2.3