diff options
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java | 59 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java | 70 |
2 files changed, 100 insertions, 29 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 index 5c244bef19d..9fe1e96ecb8 100644 --- a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java @@ -42,53 +42,66 @@ public class Periods implements BatchExtension { } public String label(Snapshot snapshot, int periodIndex) { - String mode = snapshot.getPeriodMode(periodIndex); - String param = snapshot.getPeriodModeParameter(periodIndex); - Date date = snapshot.getPeriodDate(periodIndex); + return label(snapshot.getPeriodMode(periodIndex), snapshot.getPeriodModeParameter(periodIndex), snapshot.getPeriodDate(periodIndex)); + } - return label(mode, param, date); + public String abbreviation(Snapshot snapshot, int periodIndex) { + return abbreviation(snapshot.getPeriodMode(periodIndex), snapshot.getPeriodModeParameter(periodIndex), snapshot.getPeriodDate(periodIndex)); } public String label(int periodIndex) { String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex); PeriodParameters periodParameters = new PeriodParameters(periodProperty); + return label(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); + } - String mode = periodParameters.getMode(); - String param = periodParameters.getParam(); - Date date = periodParameters.getDate(); - - return label(mode, param, date); + public String abbreviation(int periodIndex) { + String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex); + PeriodParameters periodParameters = new PeriodParameters(periodProperty); + return abbreviation(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); } public String label(String mode, String param, Date date) { - String label = ""; + return label(mode, param, date, false); + } + + public String abbreviation(String mode, String param, Date date) { + return label(mode, param, date, true); + } + + private String label(String mode, String param, Date date, boolean shortLabel) { + String label; if (CoreProperties.TIMEMACHINE_MODE_DAYS.equals(mode)) { - label = message("over_x_days", param); + label = label("over_x_days", shortLabel, param); } else if (CoreProperties.TIMEMACHINE_MODE_VERSION.equals(mode)) { - label = message("since_version", param); + label = label("since_version", shortLabel, param); if (date != null) { - label = message("since_version_detailed", param, convertDate(date)); + label = label("since_version_detailed", shortLabel, param, convertDate(date)); } } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(mode)) { - label = message("since_previous_analysis"); + label = label("since_previous_analysis", shortLabel); if (date != null) { - label = message("since_previous_analysis_detailed", convertDate(date)); + label = label("since_previous_analysis_detailed", shortLabel, convertDate(date)); } } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(mode)) { - label = message("since_previous_version"); + label = label("since_previous_version", shortLabel); if (param != null) { - label = message("since_previous_version_detailed", param); + label = label("since_previous_version_detailed", shortLabel, param); } } else if (CoreProperties.TIMEMACHINE_MODE_DATE.equals(mode)) { - label = message("since_x", convertDate(date)); + label = label("since_x", shortLabel, 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 label(String key, boolean shortLabel, Object... parameters) { + String msgKey = key; + if (shortLabel) { + msgKey += ".short"; + } + return i18n.message(getLocale(), msgKey, null, parameters); } private String convertDate(Date date) { @@ -102,9 +115,9 @@ public class Periods implements BatchExtension { private static class PeriodParameters { - private String mode; - private String param; - private Date date; + private String mode = null; + private String param = null; + private Date date = null; public PeriodParameters(String periodProperty) { if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(periodProperty) || CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(periodProperty)) { 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 index 5fd891c4128..0edf7be1a13 100644 --- a/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java @@ -60,7 +60,7 @@ public class PeriodsTest { } @Test - public void shouldReturnSnapshotLabelInModeDays() { + public void label_of_duration_in_days() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DAYS); when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date()); when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); @@ -70,7 +70,17 @@ public class PeriodsTest { } @Test - public void shouldReturnSnapshotLabelInModeVersion() { + public void abbreviation_of_duration_in_days() { + when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DAYS); + when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date()); + when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); + + periods.abbreviation(snapshot, periodIndex); + verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("over_x_days.short"), Mockito.isNull(String.class), Mockito.eq(param)); + } + + @Test + public void label_of_snapshot_version() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION); when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date()); when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); @@ -80,7 +90,17 @@ public class PeriodsTest { } @Test - public void shouldReturnSnapshotLabelInModePreviousAnalysisWithDateNotNull() { + public void abbreviation_of_snapshot_version() { + when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION); + when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date()); + when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); + + periods.abbreviation(snapshot, periodIndex); + verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version_detailed.short"), Mockito.isNull(String.class), Mockito.eq(param), Mockito.anyString()); + } + + @Test + public void label_of_previous_analysis_with_date() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date()); @@ -89,7 +109,7 @@ public class PeriodsTest { } @Test - public void shouldReturnSnapshotLabelInModePreviousAnalysisWithNullDate() { + public void label_of_previous_analysis_without_date() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); when(snapshot.getPeriodDate(periodIndex)).thenReturn(null); when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); @@ -99,6 +119,25 @@ public class PeriodsTest { } @Test + public void abbreviation_of_previous_analysis_with_date() { + when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date()); + + periods.abbreviation(snapshot, periodIndex); + verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis_detailed.short"), Mockito.isNull(String.class), Mockito.anyString()); + } + + @Test + public void abbreviation_of_previous_analysis_without_date() { + when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + when(snapshot.getPeriodDate(periodIndex)).thenReturn(null); + when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); + + periods.abbreviation(snapshot, periodIndex); + verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis.short"), Mockito.isNull(String.class)); + } + + @Test public void shouldReturnSnapshotLabelInModePreviousVersionWithParamNotNull() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); @@ -163,7 +202,7 @@ public class PeriodsTest { } @Test - public void shouldReturnLabelInModePreviousVersion() { + public void label_of_previous_version() { int periodIndex = 1; settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); @@ -172,7 +211,16 @@ public class PeriodsTest { } @Test - public void shouldReturnLabelInModeDate() { + public void abbreviation_of_previous_version() { + int periodIndex = 1; + settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + + periods.abbreviation(periodIndex); + verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version.short"), Mockito.isNull(String.class)); + } + + @Test + public void label_of_date() { int periodIndex = 1; settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "2012-12-12"); @@ -181,6 +229,16 @@ public class PeriodsTest { verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_x"), Mockito.isNull(String.class), Mockito.anyString()); } + @Test + public void abbreviation_of_date() { + int periodIndex = 1; + settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "2012-12-12"); + + periods.abbreviation(periodIndex); + + verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_x.short"), Mockito.isNull(String.class), Mockito.anyString()); + } + @Test(expected = IllegalArgumentException.class) public void shouldNotSupportUnknownModeForLabel() { int periodIndex = 1; |