aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2012-12-05 15:23:52 +0100
committerJulien Lancelot <julien.lancelot@gmail.com>2012-12-05 15:24:25 +0100
commit8e264cd428bc6ebcc987340f0521858dd41e1056 (patch)
tree7599d302bb143ae6baa5f682cf958d10f763d9d4 /sonar-core
parent6ff0e33afd08707df5fe9d6717c9db6526ce4b93 (diff)
downloadsonarqube-8e264cd428bc6ebcc987340f0521858dd41e1056.tar.gz
sonarqube-8e264cd428bc6ebcc987340f0521858dd41e1056.zip
Periods is now used to display label periods for global settings and for snapshot periods
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java156
-rw-r--r--sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java193
2 files changed, 349 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java
new file mode 100644
index 00000000000..4d1c34ace3a
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java
@@ -0,0 +1,156 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.core.timemachine;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.BatchExtension;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.i18n.I18n;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+public class Periods implements BatchExtension {
+
+ private final Settings settings;
+ private final I18n i18n;
+
+ public Periods(Settings settings, I18n i18n) {
+ this.settings = settings;
+ this.i18n = i18n;
+ }
+
+ public String label(Snapshot snapshot, int periodIndex) {
+ String mode = snapshot.getPeriodMode(periodIndex);
+ String param = snapshot.getPeriodModeParameter(periodIndex);
+ Date date = snapshot.getPeriodDate(periodIndex);
+
+ return label(mode, param, date);
+ }
+
+ public String label(int periodIndex) {
+ String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex);
+ PeriodParameters periodParameters = new PeriodParameters(periodProperty);
+
+ String mode = periodParameters.getMode();
+ String param = periodParameters.getParam();
+ Date date = periodParameters.getDate();
+
+ return label(mode, param, date);
+ }
+
+ public String label(String mode, String param, Date date) {
+ String label = "";
+ if (CoreProperties.TIMEMACHINE_MODE_DAYS.equals(mode)) {
+ label = message("over_x_days", param);
+ } else if (CoreProperties.TIMEMACHINE_MODE_VERSION.equals(mode)) {
+ label = message("since_version", param);
+ if (date != null) {
+ label = message("since_version_detailed", param, convertDate(date));
+ }
+ } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(mode)) {
+ label = message("since_previous_analysis");
+ if (date != null) {
+ label = message("since_previous_analysis_detailed", convertDate(date));
+ }
+ } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(mode)) {
+ label = message("since_previous_version");
+ if (param != null) {
+ label = message("since_previous_version_detailed", param);
+ }
+ } else if (CoreProperties.TIMEMACHINE_MODE_DATE.equals(mode)) {
+ label = message("since_x", convertDate(date));
+ } else {
+ throw new IllegalArgumentException("This mode is not supported : " + mode);
+ }
+ return label;
+ }
+
+ private String message(String key, Object... parameters) {
+ return i18n.message(getLocale(), key, null, parameters);
+ }
+
+ private String convertDate(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd");
+ return dateFormat.format(date);
+ }
+
+ private Locale getLocale() {
+ return Locale.ENGLISH;
+ }
+
+ private class PeriodParameters {
+
+ private String mode;
+ private String param;
+ private Date date;
+
+ public PeriodParameters(String periodProperty) {
+ if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(periodProperty) || CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(periodProperty)) {
+ mode = periodProperty;
+ } else if (findByDays(periodProperty) != null) {
+ mode = CoreProperties.TIMEMACHINE_MODE_DAYS;
+ param = Integer.toString(findByDays(periodProperty));
+ } else if (findByDate(periodProperty) != null) {
+ mode = CoreProperties.TIMEMACHINE_MODE_DATE;
+ date = findByDate(periodProperty);
+ } else if (StringUtils.isNotBlank(periodProperty)) {
+ mode = CoreProperties.TIMEMACHINE_MODE_VERSION;
+ param = periodProperty;
+ } else {
+ throw new IllegalArgumentException("Unknown period property : " + periodProperty);
+ }
+ }
+
+ private Integer findByDays(String property) {
+ try {
+ return Integer.parseInt(property);
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+
+ private Date findByDate(String property) {
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+ try {
+ return format.parse(property);
+ } catch (ParseException e) {
+ return null;
+ }
+ }
+
+ public String getMode() {
+ return mode;
+ }
+
+ public String getParam() {
+ return param;
+ }
+
+ public Date getDate() {
+ return date;
+ }
+ }
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java
new file mode 100644
index 00000000000..5fd891c4128
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java
@@ -0,0 +1,193 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.core.timemachine;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.i18n.I18n;
+
+import java.util.Date;
+import java.util.Locale;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class PeriodsTest {
+
+ private Periods periods;
+
+ private Snapshot snapshot;
+
+ private Settings settings;
+ private I18n i18n;
+
+ private int periodIndex;
+ private String param;
+
+ @Before
+ public void before() {
+ periodIndex = 1;
+ param = "10";
+
+ snapshot = mock(Snapshot.class);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ settings = new Settings();
+ i18n = mock(I18n.class);
+ periods = new Periods(settings, i18n);
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModeDays() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DAYS);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("over_x_days"), Mockito.isNull(String.class), Mockito.eq(param));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModeVersion() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version_detailed"), Mockito.isNull(String.class), Mockito.eq(param), Mockito.anyString());
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousAnalysisWithDateNotNull() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis_detailed"), Mockito.isNull(String.class), Mockito.anyString());
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousAnalysisWithNullDate() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(null);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousVersionWithParamNotNull() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version_detailed"), Mockito.isNull(String.class), Mockito.eq(param));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousVersionWithParamNull() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(null);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModeDate() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DATE);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+
+ periods.label(snapshot, periodIndex);
+
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_x"), Mockito.isNull(String.class), Mockito.anyString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldNotSupportUnknownModeForSnapshotLabel() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn("Unknown mode");
+
+ periods.label(snapshot, periodIndex);
+ }
+
+ @Test
+ public void shouldReturnLabelInModeDays() {
+ int periodIndex = 1;
+ String days = "5";
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, days);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("over_x_days"), Mockito.isNull(String.class), Mockito.eq(days));
+ }
+
+ @Test
+ public void shouldReturnLabelInModeVersion() {
+ int periodIndex = 1;
+ String version = "3.5";
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, version);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version"), Mockito.isNull(String.class), Mockito.eq(version));
+ }
+
+ @Test
+ public void shouldReturnLabelInModePreviousAnalysis() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnLabelInModePreviousVersion() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnLabelInModeDate() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "2012-12-12");
+
+ periods.label(periodIndex);
+
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_x"), Mockito.isNull(String.class), Mockito.anyString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldNotSupportUnknownModeForLabel() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "");
+
+ periods.label(periodIndex);
+ }
+
+
+}