diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-06-03 09:03:42 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-06-06 09:41:01 +0200 |
commit | 056b5b562e0c97e5e8c8fe598883c94b07a2bb27 (patch) | |
tree | 1eca1ad8256c664e222fa4945ce5ab6cede04c1e /sonar-core | |
parent | 50c0a193b504d2eef6c57cabe4747ba11ded8657 (diff) | |
download | sonarqube-056b5b562e0c97e5e8c8fe598883c94b07a2bb27.tar.gz sonarqube-056b5b562e0c97e5e8c8fe598883c94b07a2bb27.zip |
Move org.sonar.core.user classes outside sonar-db
Diffstat (limited to 'sonar-core')
3 files changed, 513 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..914278a9955 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java @@ -0,0 +1,203 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.timemachine; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.sonar.api.config.Settings; +import org.sonar.api.i18n.I18n; + +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Locale.ENGLISH; +import static org.apache.commons.lang.StringUtils.isNotBlank; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DAYS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_PERIOD_PREFIX; + +public class Periods { + + private final Settings settings; + private final I18n i18n; + + public Periods(Settings settings, I18n i18n) { + this.settings = settings; + this.i18n = i18n; + } + + @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(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(TIMEMACHINE_PERIOD_PREFIX + periodIndex); + PeriodParameters periodParameters = new PeriodParameters(periodProperty); + return abbreviation(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); + } + + @CheckForNull + public String label(String mode, @Nullable String param, @Nullable Date date) { + return label(mode, param, convertDate(date), false); + } + + @CheckForNull + public String label(String mode, @Nullable String param, @Nullable String date) { + return label(mode, param, date, false); + } + + @CheckForNull + 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) { + 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); + } + } + + private String labelForDays(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (date == null) { + return label("over_x_days", shortLabel, param); + } + return label("over_x_days_detailed", shortLabel, param, date); + } + + private String labelForVersion(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (date == null) { + return label("since_version", shortLabel, param); + } + return label("since_version_detailed", shortLabel, param, date); + } + + 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 && date == null) { + return label("since_previous_version", shortLabel); + } + if (param == null) { + // Special case when no snapshot for previous version is found. The first analysis is then returned -> Display only the date. + return label("since_previous_version_with_only_date", shortLabel, date); + } + 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(ENGLISH, msgKey, null, parameters); + } + + private static class PeriodParameters { + + private String mode = null; + private String param = null; + private Date date = null; + + public PeriodParameters(String 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 (possibleDaysValue != null) { + mode = TIMEMACHINE_MODE_DAYS; + param = Integer.toString(possibleDaysValue); + } else if (possibleDatesValue != null) { + mode = TIMEMACHINE_MODE_DATE; + date = possibleDatesValue; + } else { + mode = TIMEMACHINE_MODE_VERSION; + param = periodProperty; + } + } + + private static Integer findByDays(String property) { + try { + return Integer.parseInt(property); + } catch (NumberFormatException e) { + return null; + } + } + + private static 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/main/java/org/sonar/core/timemachine/package-info.java b/sonar-core/src/main/java/org/sonar/core/timemachine/package-info.java new file mode 100644 index 00000000000..7d65a931ce8 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.core.timemachine; + +import javax.annotation.ParametersAreNonnullByDefault; + 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..67867b40f7f --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java @@ -0,0 +1,286 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.timemachine; + +import java.util.Date; +import java.util.Locale; +import org.junit.Rule; +import org.junit.Test; +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.eq; +import static org.mockito.Mockito.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.sonar.api.utils.DateUtils.parseDate; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DAYS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_PERIOD_PREFIX; + +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 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); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis.short"), isNull(String.class)); + } + + @Test + public void return_since_previous_analysis_detailed_label_when_date_is_set() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis_detailed"), isNull(String.class), eq(STRING_DATE)); + } + + @Test + 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); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis"), isNull(String.class)); + } + + @Test + public void return_since_previous_version_label_when_no_param() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_previous_version"), isNull(String.class)); + } + + @Test + public void return_since_previous_version_abbreviation_when_no_param() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, null); + + verify(i18n).message(any(Locale.class), eq("since_previous_version.short"), isNull(String.class)); + } + + @Test + 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); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed.short"), isNull(String.class), eq(VERSION)); + } + + @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_with_only_date_label_when_no_param_and_date_is_set() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_with_only_date"), isNull(String.class), 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 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()); + } + + @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 + 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(PERIOD_INDEX); + } + +} |