diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-03-06 12:15:15 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-03-06 12:15:22 +0100 |
commit | 94ed63a7c64ddebf9056604535e4439a82ceecae (patch) | |
tree | b8a2bb09bc2fe2653e80f8dda358da0795e8a176 /sonar-core | |
parent | 975dacb594135e449c621d254081f6e179ec51c3 (diff) | |
download | sonarqube-94ed63a7c64ddebf9056604535e4439a82ceecae.tar.gz sonarqube-94ed63a7c64ddebf9056604535e4439a82ceecae.zip |
SONAR-5056 Create new Durations API to format Duration and convert String to Duration
Diffstat (limited to 'sonar-core')
4 files changed, 4 insertions, 285 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/DefaultI18n.java b/sonar-core/src/main/java/org/sonar/core/i18n/DefaultI18n.java index eebe7325cd7..23fe352d0a8 100644 --- a/sonar-core/src/main/java/org/sonar/core/i18n/DefaultI18n.java +++ b/sonar-core/src/main/java/org/sonar/core/i18n/DefaultI18n.java @@ -30,7 +30,6 @@ import org.sonar.api.ServerExtension; import org.sonar.api.i18n.I18n; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; -import org.sonar.api.utils.Duration; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.System2; @@ -48,7 +47,6 @@ public class DefaultI18n implements I18n, ServerExtension, BatchExtension, Start private static final Logger LOG = LoggerFactory.getLogger(DefaultI18n.class); public static final String BUNDLE_PACKAGE = "org.sonar.l10n."; - private final WorkDurationFormatter workDurationFormatter; private PluginRepository pluginRepository; private I18nClassloader i18nClassloader; @@ -56,14 +54,13 @@ public class DefaultI18n implements I18n, ServerExtension, BatchExtension, Start private final ResourceBundle.Control control; private final System2 system2; - public DefaultI18n(PluginRepository pluginRepository, WorkDurationFormatter workDurationFormatter) { - this(pluginRepository, workDurationFormatter, System2.INSTANCE); + public DefaultI18n(PluginRepository pluginRepository) { + this(pluginRepository, System2.INSTANCE); } @VisibleForTesting - DefaultI18n(PluginRepository pluginRepository, WorkDurationFormatter workDurationFormatter, System2 system2) { + DefaultI18n(PluginRepository pluginRepository, System2 system2) { this.pluginRepository = pluginRepository; - this.workDurationFormatter = workDurationFormatter; this.system2 = system2; // SONAR-2927 this.control = new ResourceBundle.Control() { @@ -148,24 +145,6 @@ public class DefaultI18n implements I18n, ServerExtension, BatchExtension, Start return DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date); } - @Override - public String formatWorkDuration(Locale locale, Duration duration) { - Long durationInMinutes = duration.toMinutes(); - if (durationInMinutes == 0) { - return "0"; - } - List<WorkDurationFormatter.Result> results = workDurationFormatter.format(durationInMinutes); - StringBuilder message = new StringBuilder(); - for (WorkDurationFormatter.Result result : results) { - if (" ".equals(result.key())) { - message.append(" "); - } else { - message.append(message(locale, result.key(), null, result.value())); - } - } - return message.toString(); - } - /** * Only the given locale is searched. Contrary to java.util.ResourceBundle, no strategy for locating the bundle is implemented in * this method. diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/WorkDurationFormatter.java b/sonar-core/src/main/java/org/sonar/core/i18n/WorkDurationFormatter.java deleted file mode 100644 index f760bf44802..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/i18n/WorkDurationFormatter.java +++ /dev/null @@ -1,139 +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.core.i18n; - -import org.apache.commons.lang.builder.ReflectionToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.BatchExtension; -import org.sonar.api.ServerComponent; -import org.sonar.api.utils.internal.WorkDuration; -import org.sonar.api.utils.internal.WorkDurationFactory; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import java.util.List; - -import static com.google.common.collect.Lists.newArrayList; - -public class WorkDurationFormatter implements ServerComponent, BatchExtension { - - private final WorkDurationFactory workDurationFactory; - - public WorkDurationFormatter(WorkDurationFactory workDurationFactory) { - this.workDurationFactory = workDurationFactory; - } - - public List<Result> format(long durationInMinutes) { - if (durationInMinutes == 0) { - return newArrayList(new Result("0", null)); - } - boolean isNegative = durationInMinutes < 0; - Long absDuration = Math.abs(durationInMinutes); - return format(workDurationFactory.createFromMinutes(absDuration), isNegative); - } - - private List<Result> format(WorkDuration workDuration, boolean isNegative) { - List<Result> results = newArrayList(); - if (workDuration.days() > 0) { - results.add(message("work_duration.x_days", isNegative ? -1 * workDuration.days() : workDuration.days())); - } - if (displayHours(workDuration)) { - addSpaceIfNeeded(results); - results.add(message("work_duration.x_hours", isNegative && results.isEmpty() ? -1 * workDuration.hours() : workDuration.hours())); - } - if (displayMinutes(workDuration)) { - addSpaceIfNeeded(results); - results.add(message("work_duration.x_minutes", isNegative && results.isEmpty() ? -1 * workDuration.minutes() : workDuration.minutes())); - } - return results; - } - - private boolean displayHours(WorkDuration workDuration){ - return workDuration.hours() > 0 && workDuration.days() < 10; - } - - private boolean displayMinutes(WorkDuration workDuration){ - return workDuration.minutes() > 0 && workDuration.hours() < 10 && workDuration.days() == 0; - } - - private void addSpaceIfNeeded(List<Result> results){ - if (!results.isEmpty()) { - results.add(new Result(" ", null)); - } - } - - private Result message(String key, @CheckForNull Object parameter) { - return new Result(key, parameter); - } - - static class Result { - private String key; - private Object value; - - Result(String key, @Nullable Object value) { - this.key = key; - this.value = value; - } - - String key() { - return key; - } - - @CheckForNull - Object value() { - return value; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Result result = (Result) o; - - if (key != null ? !key.equals(result.key) : result.key != null) { - return false; - } - if (value != null ? !value.equals(result.value) : result.value != null) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - int result = key != null ? key.hashCode() : 0; - result = 31 * result + (value != null ? value.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); - } - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/i18n/DefaultI18nTest.java b/sonar-core/src/test/java/org/sonar/core/i18n/DefaultI18nTest.java index 954f4802092..3c5bc0d6d48 100644 --- a/sonar-core/src/test/java/org/sonar/core/i18n/DefaultI18nTest.java +++ b/sonar-core/src/test/java/org/sonar/core/i18n/DefaultI18nTest.java @@ -27,7 +27,6 @@ import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; import org.sonar.api.utils.DateUtils; -import org.sonar.api.utils.Duration; import org.sonar.api.utils.System2; import java.net.URL; @@ -36,7 +35,6 @@ import java.util.Arrays; import java.util.List; import java.util.Locale; -import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -47,10 +45,6 @@ public class DefaultI18nTest { @Mock System2 system2; - @Mock - WorkDurationFormatter workDurationFormatter; - - DefaultI18n manager; @Before @@ -62,7 +56,7 @@ public class DefaultI18nTest { I18nClassloader i18nClassloader = new I18nClassloader(new ClassLoader[]{ newCoreClassloader(), newFrenchPackClassloader(), newSqaleClassloader(), newCheckstyleClassloader() }); - manager = new DefaultI18n(pluginRepository, workDurationFormatter, system2); + manager = new DefaultI18n(pluginRepository, system2); manager.doStart(i18nClassloader); } @@ -200,23 +194,6 @@ public class DefaultI18nTest { assertThat(manager.formatDate(Locale.ENGLISH, DateUtils.parseDate("2014-01-22"))).isEqualTo("Jan 22, 2014"); } - @Test - public void format_work_duration() { - when(workDurationFormatter.format(10)).thenReturn(newArrayList( - new WorkDurationFormatter.Result("work_duration.x_days", 5), - new WorkDurationFormatter.Result(" ", null), - new WorkDurationFormatter.Result("work_duration.x_hours", 2), - new WorkDurationFormatter.Result(" ", null), - new WorkDurationFormatter.Result("work_duration.x_minutes", 1) - )); - assertThat(manager.formatWorkDuration(Locale.ENGLISH, Duration.create(10))).isEqualTo("5d 2h 1min"); - } - - @Test - public void format_work_duration_when_0() { - assertThat(manager.formatWorkDuration(Locale.ENGLISH, Duration.create(0))).isEqualTo("0"); - } - static URLClassLoader newCoreClassloader() { return newClassLoader("/org/sonar/core/i18n/corePlugin/"); } diff --git a/sonar-core/src/test/java/org/sonar/core/i18n/WorkDurationFormatterTest.java b/sonar-core/src/test/java/org/sonar/core/i18n/WorkDurationFormatterTest.java deleted file mode 100644 index 984b2f78364..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/i18n/WorkDurationFormatterTest.java +++ /dev/null @@ -1,98 +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.core.i18n; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.CoreProperties; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.internal.WorkDurationFactory; - -import static com.google.common.collect.Lists.newArrayList; -import static org.fest.assertions.Assertions.assertThat; - -public class WorkDurationFormatterTest { - - static final int HOURS_IN_DAY = 8; - - static final long ONE_MINUTE = 1L; - static final long ONE_HOUR = ONE_MINUTE * 60; - static final long ONE_DAY = HOURS_IN_DAY * ONE_HOUR; - - Settings settings; - - WorkDurationFormatter formatter; - - @Before - public void setUp() throws Exception { - settings = new Settings(); - settings.setProperty(CoreProperties.HOURS_IN_DAY, Integer.toString(HOURS_IN_DAY)); - formatter = new WorkDurationFormatter(new WorkDurationFactory(settings)); - } - - @Test - public void format() { - assertThat(formatter.format(5 * ONE_DAY)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_days", 5))); - assertThat(formatter.format(2 * ONE_HOUR)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_hours", 2))); - assertThat(formatter.format(ONE_MINUTE)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_minutes", 1))); - - assertThat(formatter.format(5 * ONE_DAY + 2 * ONE_HOUR)).isEqualTo(newArrayList( - new WorkDurationFormatter.Result("work_duration.x_days", 5), - new WorkDurationFormatter.Result(" ", null), - new WorkDurationFormatter.Result("work_duration.x_hours", 2) - )); - - assertThat(formatter.format(2 * ONE_HOUR + ONE_MINUTE)).isEqualTo(newArrayList( - new WorkDurationFormatter.Result("work_duration.x_hours", 2), - new WorkDurationFormatter.Result(" ", null), - new WorkDurationFormatter.Result("work_duration.x_minutes", 1) - )); - - assertThat(formatter.format(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE)).isEqualTo(newArrayList( - new WorkDurationFormatter.Result("work_duration.x_days", 5), - new WorkDurationFormatter.Result(" ", null), - new WorkDurationFormatter.Result("work_duration.x_hours", 2) - )); - } - - @Test - public void not_display_following_element_when_bigger_than_ten() { - int hoursInDay = 15; - settings.setProperty(CoreProperties.HOURS_IN_DAY, Integer.toString(hoursInDay)); - - assertThat(formatter.format(15 * hoursInDay * ONE_HOUR + 2 * ONE_HOUR + ONE_MINUTE)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_days", 15))); - - assertThat(formatter.format(12 * ONE_HOUR + ONE_MINUTE)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_hours", 12))); - } - - @Test - public void display_zero_without_unit() { - assertThat(formatter.format(0)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("0", null))); - } - - @Test - public void display_negative_duration() { - assertThat(formatter.format(-5 * ONE_DAY)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_days", -5))); - assertThat(formatter.format(-2 * ONE_HOUR)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_hours", -2))); - assertThat(formatter.format(-1 * ONE_MINUTE)).isEqualTo(newArrayList(new WorkDurationFormatter.Result("work_duration.x_minutes", -1))); - } - -} |