From: Julien Lancelot Date: Tue, 27 Oct 2015 15:59:54 +0000 (+0100) Subject: Refactor Periods to increase coverage X-Git-Tag: 5.3-RC1~431 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=03d4cb1c2ac5d548167ec10e48100d7865bfc0cf;p=sonarqube.git Refactor Periods to increase coverage --- diff --git a/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java index 0a9a41ba5d4..2765f3d8764 100644 --- a/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java +++ b/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java @@ -25,11 +25,18 @@ import java.util.Date; import java.util.Locale; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.i18n.I18n; +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.commons.lang.StringUtils.isNotBlank; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_DATE; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_DAYS; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_VERSION; +import static org.sonar.api.CoreProperties.TIMEMACHINE_PERIOD_PREFIX; + public class Periods { private final Settings settings; @@ -40,88 +47,103 @@ public class Periods { this.i18n = i18n; } + private static Locale getLocale() { + return Locale.ENGLISH; + } + + @CheckForNull + private static String convertDate(@Nullable Date date) { + if (date != null) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd"); + return dateFormat.format(date); + } + return null; + } + @CheckForNull public String label(int periodIndex) { - String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex); + String periodProperty = settings.getString(TIMEMACHINE_PERIOD_PREFIX + periodIndex); PeriodParameters periodParameters = new PeriodParameters(periodProperty); return label(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); } @CheckForNull public String abbreviation(int periodIndex) { - String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex); + String periodProperty = settings.getString(TIMEMACHINE_PERIOD_PREFIX + periodIndex); PeriodParameters periodParameters = new PeriodParameters(periodProperty); return abbreviation(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); } @CheckForNull - public String label(String mode, String param, Date date) { + public String label(String mode, @Nullable String param, @Nullable Date date) { return label(mode, param, convertDate(date), false); } @CheckForNull - public String label(String mode, String param, String date) { + public String label(String mode, @Nullable String param, @Nullable String date) { return label(mode, param, date, false); } @CheckForNull - public String abbreviation(String mode, String param, Date date) { + public String abbreviation(String mode, @Nullable String param, @Nullable Date date) { return label(mode, param, convertDate(date), true); } @CheckForNull private String label(String mode, @Nullable String param, @Nullable String date, boolean shortLabel) { - String label; - if (CoreProperties.TIMEMACHINE_MODE_DAYS.equals(mode)) { - label = label("over_x_days", shortLabel, param); - if (date != null) { - label = label("over_x_days_detailed", shortLabel, param, date); - } - } else if (CoreProperties.TIMEMACHINE_MODE_VERSION.equals(mode)) { - label = label("since_version", shortLabel, param); - if (date != null) { - label = label("since_version_detailed", shortLabel, param, date); - } - } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(mode)) { - label = label("since_previous_analysis", shortLabel); - if (date != null) { - label = label("since_previous_analysis_detailed", shortLabel, date); - } - } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(mode)) { - label = label("since_previous_version", shortLabel); - if (param != null) { - label = label("since_previous_version_detailed", shortLabel, param); - if (date != null) { - label = label("since_previous_version_detailed", shortLabel, param, date); - } - } - } else if (CoreProperties.TIMEMACHINE_MODE_DATE.equals(mode)) { - label = label("since_x", shortLabel, date); - } else { - throw new IllegalArgumentException("This mode is not supported : " + mode); + switch (mode) { + case TIMEMACHINE_MODE_DAYS: + return labelForDays(param, date, shortLabel); + case TIMEMACHINE_MODE_VERSION: + return labelForVersion(param, date, shortLabel); + case TIMEMACHINE_MODE_PREVIOUS_ANALYSIS: + return labelForPreviousAnalysis(date, shortLabel); + case TIMEMACHINE_MODE_PREVIOUS_VERSION: + return labelForPreviousVersion(param, date, shortLabel); + case TIMEMACHINE_MODE_DATE: + return label("since_x", shortLabel, date); + default: + throw new IllegalArgumentException("This mode is not supported : " + mode); } - return label; } - private String label(String key, boolean shortLabel, Object... parameters) { - String msgKey = key; - if (shortLabel) { - msgKey += ".short"; + private String labelForDays(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (date == null) { + return label("over_x_days", shortLabel, param); } - return i18n.message(getLocale(), msgKey, null, parameters); + return label("over_x_days_detailed", shortLabel, param, date); } - @CheckForNull - private static String convertDate(Date date) { - if (date != null) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd"); - return dateFormat.format(date); + private String labelForVersion(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (date == null) { + return label("since_version", shortLabel, param); } - return null; + return label("since_version_detailed", shortLabel, param, date); } - private static Locale getLocale() { - return Locale.ENGLISH; + private String labelForPreviousAnalysis(@Nullable String date, boolean shortLabel) { + if (date == null) { + return label("since_previous_analysis", shortLabel); + } + return label("since_previous_analysis_detailed", shortLabel, date); + } + + private String labelForPreviousVersion(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (param == null) { + return label("since_previous_version", shortLabel); + } + if (date == null) { + return label("since_previous_version_detailed", shortLabel, param); + } + return label("since_previous_version_detailed", shortLabel, param, date); + } + + private String label(String key, boolean shortLabel, Object... parameters) { + String msgKey = key; + if (shortLabel) { + msgKey += ".short"; + } + return i18n.message(getLocale(), msgKey, null, parameters); } private static class PeriodParameters { @@ -131,19 +153,20 @@ public class Periods { private Date date = null; public PeriodParameters(String periodProperty) { - if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(periodProperty) || CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(periodProperty)) { + checkArgument(isNotBlank(periodProperty), "Period property should not be empty"); + Integer possibleDaysValue = findByDays(periodProperty); + Date possibleDatesValue = findByDate(periodProperty); + if (TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(periodProperty) || 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 if (possibleDaysValue != null) { + mode = TIMEMACHINE_MODE_DAYS; + param = Integer.toString(possibleDaysValue); + } else if (possibleDatesValue != null) { + mode = TIMEMACHINE_MODE_DATE; + date = possibleDatesValue; } else { - throw new IllegalArgumentException("Unknown period property : " + periodProperty); + mode = TIMEMACHINE_MODE_VERSION; + param = periodProperty; } } diff --git a/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java b/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java index f48e5b3396a..fc1e34b98fe 100644 --- a/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java +++ b/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java @@ -17,100 +17,264 @@ * 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.timemachine; +import java.util.Date; import java.util.Locale; +import org.junit.Rule; import org.junit.Test; -import org.sonar.api.CoreProperties; +import org.junit.rules.ExpectedException; import org.sonar.api.config.Settings; import org.sonar.api.i18n.I18n; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.any; -import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.isNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_DATE; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_DAYS; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION; +import static org.sonar.api.CoreProperties.TIMEMACHINE_MODE_VERSION; +import static org.sonar.api.CoreProperties.TIMEMACHINE_PERIOD_PREFIX; +import static org.sonar.api.utils.DateUtils.parseDate; public class PeriodsTest { + static String NUMBER_OF_DAYS = "5"; + static String STRING_DATE = "2015-01-01"; + static Date DATE = parseDate(STRING_DATE); + static String VERSION = "1.1"; + static int PERIOD_INDEX = 1; + @Rule + public ExpectedException thrown = ExpectedException.none(); Settings settings = new Settings(); I18n i18n = mock(I18n.class); Periods periods = new Periods(settings, i18n); @Test - public void shouldReturnLabelInModeDays() { - int periodIndex = 1; - String days = "5"; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, days); + public void return_over_x_days_label_when_no_date() { + periods.label(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, (String) null); + + verify(i18n).message(any(Locale.class), eq("over_x_days"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void return_over_x_days_abbreviation_when_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, null); + + verify(i18n).message(any(Locale.class), eq("over_x_days.short"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void return_over_x_days_detailed_label_when_date_is_set() { + periods.label(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("over_x_days_detailed"), isNull(String.class), eq(NUMBER_OF_DAYS), eq(STRING_DATE)); + } + + @Test + public void return_over_x_days_detailed_abbreviation_when_date_is_set() { + periods.abbreviation(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, DATE); + + verify(i18n).message(any(Locale.class), eq("over_x_days_detailed.short"), isNull(String.class), eq(NUMBER_OF_DAYS), anyString()); + } + + @Test + public void return_over_x_days_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, NUMBER_OF_DAYS); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("over_x_days"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void return_since_version_label_when_no_date() { + periods.label(TIMEMACHINE_MODE_VERSION, VERSION, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_version"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_version_abbreviation_when_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_VERSION, VERSION, null); + + verify(i18n).message(any(Locale.class), eq("since_version.short"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_version_detailed_label_when_date_is_set() { + periods.label(TIMEMACHINE_MODE_VERSION, VERSION, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_version_detailed"), isNull(String.class), eq(VERSION), eq(STRING_DATE)); + } + + @Test + public void return_since_version_detailed_abbreviation_when_date_is_set() { + periods.abbreviation(TIMEMACHINE_MODE_VERSION, VERSION, DATE); + + verify(i18n).message(any(Locale.class), eq("since_version_detailed.short"), isNull(String.class), eq(VERSION), anyString()); + } + + @Test + public void return_since_version_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, VERSION); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("since_version"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_previous_analysis_label_when_no_date() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis"), isNull(String.class)); + } + + @Test + public void return_since_previous_analysis_abbreviation_when_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, null); - periods.label(periodIndex); - verify(i18n).message(any(Locale.class), eq("over_x_days"), isNull(String.class), eq(days)); + verify(i18n).message(any(Locale.class), eq("since_previous_analysis.short"), isNull(String.class)); } @Test - public void shouldReturnLabelInModeVersion() { - int periodIndex = 1; - String version = "3.5"; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, version); + public void return_since_previous_analysis_detailed_label_when_date_is_set() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, STRING_DATE); - periods.label(periodIndex); - verify(i18n).message(any(Locale.class), eq("since_version"), isNull(String.class), eq(version)); + verify(i18n).message(any(Locale.class), eq("since_previous_analysis_detailed"), isNull(String.class), eq(STRING_DATE)); } @Test - public void shouldReturnLabelInModePreviousAnalysis() { - int periodIndex = 1; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + public void return_since_previous_analysis_detailed_abbreviation_when_date_is_set() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis_detailed.short"), isNull(String.class), anyString()); + } + + @Test + public void return_since_previous_analysis_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + + periods.label(PERIOD_INDEX); - periods.label(periodIndex); verify(i18n).message(any(Locale.class), eq("since_previous_analysis"), isNull(String.class)); } @Test - public void label_of_previous_version() { - int periodIndex = 1; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + public void return_since_previous_version_label_when_no_param() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, (String) null); - periods.label(periodIndex); verify(i18n).message(any(Locale.class), eq("since_previous_version"), isNull(String.class)); } @Test - public void abbreviation_of_previous_version() { - int periodIndex = 1; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + public void return_since_previous_version_abbreviation_when_no_param() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, null); - periods.abbreviation(periodIndex); verify(i18n).message(any(Locale.class), eq("since_previous_version.short"), isNull(String.class)); } @Test - public void label_of_date() { - int periodIndex = 1; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "2012-12-12"); + public void return_since_previous_version_detailed_label_when_param_is_set_and_no_date() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_previous_version_detailed_abbreviation_when_param_is_set_and_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, null); - periods.label(periodIndex); + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed.short"), isNull(String.class), eq(VERSION)); + } - verify(i18n).message(any(Locale.class), eq("since_x"), isNull(String.class), anyString()); + @Test + public void return_since_previous_version_detailed_label_when_param_and_date_are_set() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed"), isNull(String.class), eq(VERSION), eq(STRING_DATE)); + } + + @Test + public void return_since_previous_version_detailed_abbreviation_when_param_and_date_are_set() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed.short"), isNull(String.class), eq(VERSION), anyString()); + } + + @Test + public void return_since_previous_version_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, TIMEMACHINE_MODE_PREVIOUS_VERSION); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("since_previous_version"), isNull(String.class)); + } + + @Test + public void return_since_x_label() { + periods.label(TIMEMACHINE_MODE_DATE, null, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_x"), isNull(String.class), eq(STRING_DATE)); } @Test - public void abbreviation_of_date() { - int periodIndex = 1; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "2012-12-12"); + public void return_since_x_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, STRING_DATE); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("since_x"), isNull(String.class), anyString()); + } - periods.abbreviation(periodIndex); + @Test + public void return_since_x_abbreviation() { + periods.abbreviation(TIMEMACHINE_MODE_DATE, null, DATE); verify(i18n).message(any(Locale.class), eq("since_x.short"), isNull(String.class), anyString()); } - @Test(expected = IllegalArgumentException.class) - public void shouldNotSupportUnknownModeForLabel() { - int periodIndex = 1; - settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, ""); + @Test + public void throw_IAE_when_mode_is_unknown() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("This mode is not supported : unknown"); + + periods.label("unknown", null, (String) null); + } + + @Test + public void return_abbreviation_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, NUMBER_OF_DAYS); + + periods.abbreviation(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("over_x_days.short"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void throw_IAE_when_period_property_is_empty() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, ""); + + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Period property should not be empty"); + + periods.label(PERIOD_INDEX); + } + + @Test + public void throw_IAE_when_period_property_is_null() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, (String) null); + + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Period property should not be empty"); - periods.label(periodIndex); + periods.label(PERIOD_INDEX); } }