Browse Source

Remove no more used org.sonar.core.timemachine.Periods class

tags/7.8
Julien Lancelot 5 years ago
parent
commit
a03a470e43

+ 0
- 2
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java View File

@@ -77,7 +77,6 @@ import org.sonar.core.platform.Module;
import org.sonar.core.platform.PlatformEditionProvider;
import org.sonar.core.platform.PluginClassloaderFactory;
import org.sonar.core.platform.PluginLoader;
import org.sonar.core.timemachine.Periods;
import org.sonar.core.util.UuidFactoryImpl;
import org.sonar.db.DBSessionsImpl;
import org.sonar.db.DaoModule;
@@ -355,7 +354,6 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer {
container.add(
ResourceTypes.class,
DefaultResourceTypes.get(),
Periods.class,
BillingValidationsProxyImpl.class,

// quality profile

+ 1
- 1
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java View File

@@ -97,7 +97,7 @@ public class ComputeEngineContainerImplTest {
assertThat(picoContainer.getComponentAdapters())
.hasSize(
CONTAINER_ITSELF
+ 69 // level 4
+ 68 // level 4
+ 6 // content of CeConfigurationModule
+ 4 // content of CeQueueModule
+ 3 // content of CeHttpModule

+ 0
- 2
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java View File

@@ -33,7 +33,6 @@ import org.sonar.core.component.DefaultResourceTypes;
import org.sonar.core.extension.CoreExtensionsInstaller;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.core.platform.PlatformEditionProvider;
import org.sonar.core.timemachine.Periods;
import org.sonar.server.authentication.AuthenticationModule;
import org.sonar.server.authentication.LogOAuthWarning;
import org.sonar.server.badge.ws.ProjectBadgesWsModule;
@@ -264,7 +263,6 @@ public class PlatformLevel4 extends PlatformLevel {
DefaultResourceTypes.get(),
SettingsChangeNotifier.class,
PageDecorations.class,
Periods.class,
ServerWs.class,
BackendCleanup.class,
IndexDefinitions.class,

+ 0
- 193
sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java View File

@@ -1,193 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 SonarSource SA
* mailto:info 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.Configuration;
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.LEAK_PERIOD;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION;

public class Periods {

private final Configuration config;
private final I18n i18n;

public Periods(Configuration config, I18n i18n) {
this.config = config;
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 = config.get(LEAK_PERIOD + periodIndex).orElse(null);
PeriodParameters periodParameters = new PeriodParameters(periodProperty);
return label(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate());
}

@CheckForNull
public String abbreviation(int periodIndex) {
String periodProperty = config.get(LEAK_PERIOD + periodIndex).orElse(null);
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 LEAK_PERIOD_MODE_DAYS:
return labelForDays(param, date, shortLabel);
case LEAK_PERIOD_MODE_VERSION:
return labelForVersion(param, date, shortLabel);
case LEAK_PERIOD_MODE_PREVIOUS_VERSION:
return labelForPreviousVersion(param, date, shortLabel);
case LEAK_PERIOD_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 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 (LEAK_PERIOD_MODE_PREVIOUS_VERSION.equals(periodProperty)) {
mode = periodProperty;
} else if (possibleDaysValue != null) {
mode = LEAK_PERIOD_MODE_DAYS;
param = Integer.toString(possibleDaysValue);
} else if (possibleDatesValue != null) {
mode = LEAK_PERIOD_MODE_DATE;
date = possibleDatesValue;
} else {
mode = LEAK_PERIOD_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;
}
}

}

+ 0
- 24
sonar-core/src/main/java/org/sonar/core/timemachine/package-info.java View File

@@ -1,24 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 SonarSource SA
* mailto:info 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;


+ 0
- 248
sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java View File

@@ -1,248 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 SonarSource SA
* mailto:info 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.internal.MapSettings;
import org.sonar.api.i18n.I18n;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.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.LEAK_PERIOD;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION;
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION;

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();
MapSettings settings = new MapSettings();
I18n i18n = mock(I18n.class);
Periods periods = new Periods(settings.asConfig(), i18n);

@Test
public void return_over_x_days_label_when_no_date() {
periods.label(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD + 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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD + 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_version_label_when_no_param() {
periods.label(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD + PERIOD_INDEX, LEAK_PERIOD_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(LEAK_PERIOD_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(LEAK_PERIOD + 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(LEAK_PERIOD_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(LEAK_PERIOD + 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(LEAK_PERIOD + 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(LEAK_PERIOD + PERIOD_INDEX, (String) null);

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Period property should not be empty");

periods.label(PERIOD_INDEX);
}

}

Loading…
Cancel
Save