]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4221 Remove the property sonar.timemachine.TRK.period4/5 and restore method...
authorJulien Lancelot <julien.lancelot@gmail.com>
Tue, 2 Apr 2013 13:33:59 +0000 (15:33 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Tue, 2 Apr 2013 13:33:59 +0000 (15:33 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java
sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java

index bc8f34a967f60cd629a3df946e83b3cc3383b644..bf865df51a6bc97810f4f1c5eac840bb1857fde8 100644 (file)
@@ -302,7 +302,7 @@ import java.util.List;
     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, " +
@@ -313,17 +313,15 @@ import java.util.List;
     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",
index f416365c5e30c864698e3a5636610f6ca268087f..550907595752e49a17e9cc4c98461a00e9103e6f 100644 (file)
@@ -19,6 +19,8 @@
  */
 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;
@@ -51,33 +53,65 @@ public class PastSnapshotFinder implements BatchExtension {
     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) {
index 1b4ba687767570cf57990979f4d66285cc35814b..090af527d435529b5806fab92e6742c1e96a108f 100644 (file)
@@ -19,6 +19,8 @@
  */
 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;
@@ -66,8 +68,22 @@ public class PastSnapshotFinderTest {
   }
 
   @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()));
 
@@ -80,7 +96,31 @@ public class PastSnapshotFinderTest {
   }
 
   @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");
@@ -93,7 +133,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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);
@@ -101,7 +141,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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()));
@@ -120,7 +160,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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");
@@ -130,7 +170,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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();
@@ -146,7 +186,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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);
@@ -157,7 +197,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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();
@@ -173,7 +213,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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");
@@ -185,7 +225,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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");
@@ -195,7 +235,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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()));
 
@@ -203,7 +243,7 @@ public class PastSnapshotFinderTest {
   }
 
   @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"));
index 7f9a14e051bdcab3a76c802053a39e6d43bc0f39..e55d31b0f766486d00e7ac92a377b5d1557f0d68 100644 (file)
@@ -306,9 +306,7 @@ public interface CoreProperties {
   String GOOGLE_ANALYTICS_ACCOUNT_PROPERTY = "sonar.google-analytics.account";
 
   /* Time machine periods */
-  String TIMEMACHINE_PREFIX = "sonar.timemachine";
-  String TIMEMACHINE_PERIOD_SUFFIX = "period";
-  String TIMEMACHINE_PERIOD_PREFIX = TIMEMACHINE_PREFIX + "." + TIMEMACHINE_PERIOD_SUFFIX;
+  String TIMEMACHINE_PERIOD_PREFIX = "sonar.timemachine.period";
   String TIMEMACHINE_MODE_PREVIOUS_ANALYSIS = "previous_analysis";
   String TIMEMACHINE_MODE_DATE = "date";
   String TIMEMACHINE_MODE_VERSION = "version";