defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_3,
category = CoreProperties.CATEGORY_DIFFERENTIAL_VIEWS),
@Property(
- key = "sonar.timemachine.TRK.period4",
+ key = "sonar.timemachine.period4",
name = "Period 4",
description = "Period used to compare measures and track new violations. This property is specific to the project. Values are : " +
"<ul class='bullet'><li>Number of days before analysis, for example 5.</li><li>A custom date. Format is yyyy-MM-dd, " +
project = true,
global = false,
defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_4,
- category = CoreProperties.CATEGORY_DIFFERENTIAL_VIEWS,
- deprecatedKey = "sonar.timemachine.period4"),
+ category = CoreProperties.CATEGORY_DIFFERENTIAL_VIEWS),
@Property(
- key = "sonar.timemachine.TRK.period5",
+ key = "sonar.timemachine.period5",
name = "Period 5",
description = "See the property 'Period 4'",
project = true,
global = false,
defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_5,
- category = CoreProperties.CATEGORY_DIFFERENTIAL_VIEWS,
- deprecatedKey = "sonar.timemachine.period5"),
+ category = CoreProperties.CATEGORY_DIFFERENTIAL_VIEWS),
@Property(
key = CoreProperties.DRY_RUN,
defaultValue = "false",
*/
package org.sonar.batch.components;
+import com.google.common.base.Strings;
+import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
this.finderByPreviousVersion = finderByPreviousVersion;
}
- public PastSnapshot find(Snapshot projectSnapshot, String rootQualifier, Settings settings, int index) {
- String propertyValue = getPropertyValue(rootQualifier, settings, index);
+ /**
+ * @deprecated since 3.6. Replaced by {@link #find(Snapshot projectSnapshot, String rootQualifier, Settings settings, int index) }
+ */
+ @Deprecated
+ public PastSnapshot find(Snapshot projectSnapshot, Configuration conf, int index) {
+ String propertyValue = getPropertyValue(conf, index);
PastSnapshot pastSnapshot = find(projectSnapshot, index, propertyValue);
if (pastSnapshot == null && StringUtils.isNotBlank(propertyValue)) {
- LOG.debug("Property " + getProperty(rootQualifier, index) + " is not valid: " + propertyValue);
+ LoggerFactory.getLogger(PastSnapshotFinder.class).debug("Property " + CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index + " is not valid: " + propertyValue);
}
return pastSnapshot;
}
/**
- * For retro compatibility for dev cockpit. It will used deprecated periods property sonar.timemachine.periods4 and sonar.timemachine.periods5
+ * @deprecated since 3.6. Replace by {@link #getPropertyValue(String rootQualifier, Settings settings, int index) }
*/
+ @Deprecated
+ static String getPropertyValue(Configuration conf, int index) {
+ String defaultValue = null;
+ switch (index) {
+ case 1:
+ defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_1;
+ break;
+ case 2:
+ defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_2;
+ break;
+ case 3:
+ defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_3;
+ break;
+ case 4:
+ defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_4;
+ break; // NOSONAR false-positive: constant 4 is the same than 5 (empty string)
+ case 5:
+ defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_5;
+ break; // NOSONAR false-positive: constant 5 is the same than 4 (empty string)
+ }
+ return conf.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index, defaultValue);
+ }
+
+ public PastSnapshot find(Snapshot projectSnapshot, String rootQualifier, Settings settings, int index) {
+ String propertyValue = getPropertyValue(rootQualifier, settings, index);
+ PastSnapshot pastSnapshot = find(projectSnapshot, index, propertyValue);
+ if (pastSnapshot == null && StringUtils.isNotBlank(propertyValue)) {
+ LOG.debug("Property " + CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index + " is not valid: " + propertyValue);
+ }
+ return pastSnapshot;
+ }
+
public PastSnapshot find(Snapshot projectSnapshot, Settings settings, int index) {
return find(projectSnapshot, null, settings, index);
}
static String getPropertyValue(String rootQualifier, Settings settings, int index) {
- return settings.getString(getProperty(rootQualifier, index));
- }
-
- static private String getProperty(String rootQualifier, int index) {
- // The check on rootQualifier is for retro compatibility
- if (index <= 3 || rootQualifier == null) {
- return CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index;
- } else {
- return CoreProperties.TIMEMACHINE_PREFIX + "." + rootQualifier + "." + CoreProperties.TIMEMACHINE_PERIOD_SUFFIX + index;
+ String value = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index);
+ // For periods 4 and 5 we're searching for a property prefixed by the qualifier
+ if (index > 3 && Strings.isNullOrEmpty(value)) {
+ value = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index + "." + rootQualifier);
}
+ return value;
}
public PastSnapshot findPreviousAnalysis(Snapshot projectSnapshot) {
*/
package org.sonar.batch.components;
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
}
@Test
- public void shouldFind() {
- Settings settings = new Settings().setProperty("sonar.timemachine.TRK.period5", "1.2");
+ public void should_find() {
+ Settings settings = new Settings().setProperty("sonar.timemachine.period5", "1.2");
+
+ when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot()));
+
+ PastSnapshot variationSnapshot = finder.find(null, null, settings, 5);
+
+ verify(finderByVersion).findByVersion(null, "1.2");
+ assertThat(variationSnapshot.getIndex(), is(5));
+ assertThat(variationSnapshot.getMode(), is("version"));
+ assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
+ }
+
+ @Test
+ public void should_find_with_qualifier_suffix() {
+ Settings settings = new Settings().setProperty("sonar.timemachine.period5.TRK", "1.2");
when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot()));
}
@Test
- public void shouldFindByNumberOfDays() {
+ public void should_find_with_configuration() {
+ Configuration conf = new BaseConfiguration();
+ conf.addProperty("sonar.timemachine.period1", "1.2");
+ conf.addProperty("sonar.timemachine.period2", "1.2");
+ conf.addProperty("sonar.timemachine.period3", "1.2");
+ conf.addProperty("sonar.timemachine.period4", "1.2");
+ conf.addProperty("sonar.timemachine.period5", "1.2");
+
+ when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot()));
+
+ PastSnapshot variationSnapshot = finder.find(null, conf, 1);
+
+ verify(finderByVersion).findByVersion(null, "1.2");
+ assertThat(variationSnapshot.getIndex(), is(1));
+ assertThat(variationSnapshot.getMode(), is("version"));
+ assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
+
+ assertThat(finder.find(null, conf, 2).getIndex(), is(2));
+ assertThat(finder.find(null, conf, 3).getIndex(), is(3));
+ assertThat(finder.find(null, conf, 4).getIndex(), is(4));
+ assertThat(finder.find(null, conf, 5).getIndex(), is(5));
+ }
+
+ @Test
+ public void should_find_by_number_of_days() {
when(finderByDays.findFromDays(null, 30)).thenReturn(new PastSnapshot("days", null).setModeParameter("30"));
PastSnapshot variationSnapshot = finder.find(null, 1, "30");
}
@Test
- public void shouldNotFindByNumberOfDays() {
+ public void should_not_find_by_number_of_days() {
PastSnapshot variationSnapshot = finder.find(null, 1, "30");
verify(finderByDays).findFromDays(null, 30);
}
@Test
- public void shouldFindByDate() throws ParseException {
+ public void should_find_by_date() throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
final Date date = format.parse("2010-05-18");
when(finderByDate.findByDate(null, date)).thenReturn(new PastSnapshot("date", date, new Snapshot()));
}
@Test
- public void shouldNotFindByDate() {
+ public void should_not_find_by_date() {
when(finderByDate.findByDate(any(Snapshot.class), any(Date.class))).thenReturn(null);
PastSnapshot variationSnapshot = finder.find(null, 2, "2010-05-18");
}
@Test
- public void shouldFindByPreviousAnalysis() throws ParseException {
+ public void should_find_by_previous_analysis() throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
final Date date = format.parse("2010-05-18");
Snapshot snapshot = new Snapshot();
}
@Test
- public void shouldNotFindPreviousAnalysis() {
+ public void should_not_find_previous_analysis() {
when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(null);
PastSnapshot variationSnapshot = finder.find(null, 2, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
}
@Test
- public void shouldFindByPreviousVersion() throws ParseException {
+ public void should_find_by_previous_version() throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
final Date date = format.parse("2010-05-18");
Snapshot snapshot = new Snapshot();
}
@Test
- public void shouldFindByVersion() {
+ public void should_find_by_version() {
when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot()));
PastSnapshot variationSnapshot = finder.find(null, 2, "1.2");
}
@Test
- public void shouldNotFindVersion() {
+ public void should_not_find_version() {
when(finderByVersion.findByVersion(null, "1.2")).thenReturn(null);
PastSnapshot variationSnapshot = finder.find(null, 2, "1.2");
}
@Test
- public void shouldNotFailIfUnknownFormat() {
+ public void should_not_fail_if_unknown_format() {
// should not be called
when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Date(), new Snapshot()));
}
@Test
- public void shouldGetPropertyValue() {
+ public void should_get_property_value() {
Settings settings = new Settings().setProperty("sonar.timemachine.period1", "5");
assertThat(PastSnapshotFinder.getPropertyValue("FIL", settings, 1), is("5"));