From 917a1d795b985cad9256cd411308c75d5c967a52 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Thu, 9 Dec 2010 13:14:21 +0000 Subject: [PATCH] SONAR-249 define default values of periods --- .../org/sonar/plugins/core/CorePlugin.java | 19 +++++++++++-------- .../core/timemachine/PastSnapshotFinder.java | 18 +++++++++++++++++- .../timemachine/PastSnapshotFinderByDays.java | 1 + .../timemachine/PastSnapshotFinderTest.java | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index 5e994281585..9cf09450d7f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -116,35 +116,38 @@ import java.util.List; @Property( key = "sonar.timemachine.variation1", name = "Variation 1", - description = "Period used to compare measures and track new violations. Values are : ", + description = "Period used to compare measures and track new violations. Values are : ", project = false, - global = true + global = true, + defaultValue = PastSnapshotFinder.DEFAULT_VALUE_1 ), @Property( key = "sonar.timemachine.variation2", name = "Variation 2", - description = "To be defined. For the moment the number of days", + description = "See the property 'Variation 1'", project = false, - global = true + global = true, + defaultValue = PastSnapshotFinder.DEFAULT_VALUE_2 ), @Property( key = "sonar.timemachine.variation3", name = "Variation 3", - description = "To be defined. For the moment the number of days", + description = "See the property 'Variation 1'", project = false, - global = true + global = true, + defaultValue = PastSnapshotFinder.DEFAULT_VALUE_3 ), @Property( key = "sonar.timemachine.variation4", name = "Variation 4", - description = "To be defined. For the moment the number of days", + description = "Period used to compare measures and track new violations. This property is specific to the project. Values are : ", project = true, global = false ), @Property( key = "sonar.timemachine.variation5", name = "Variation 5", - description = "To be defined. For the moment the number of days", + description = "See the property 'Variation 4'", project = true, global = false ) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java index 927af7ea361..22367050af1 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java @@ -28,6 +28,11 @@ import java.text.SimpleDateFormat; import java.util.Date; public class PastSnapshotFinder implements BatchExtension { + + public static final String DEFAULT_VALUE_1 = PastSnapshotFinderByPreviousAnalysis.MODE; + public static final String DEFAULT_VALUE_2 = "5"; + public static final String DEFAULT_VALUE_3 = "30"; + private PastSnapshotFinderByDays finderByDays; private PastSnapshotFinderByVersion finderByVersion; private PastSnapshotFinderByDate finderByDate; @@ -42,7 +47,18 @@ public class PastSnapshotFinder implements BatchExtension { } public PastSnapshot find(Configuration conf, int index) { - return find(index, conf.getString("sonar.timemachine.variation" + index)); + String propertyValue = getPropertyValue(conf, index); + return find(index, propertyValue); + } + + static String getPropertyValue(Configuration conf, int index) { + String defaultValue = null; + switch (index) { + case 1: defaultValue = DEFAULT_VALUE_1; break; + case 2: defaultValue = DEFAULT_VALUE_2; break; + case 3: defaultValue = DEFAULT_VALUE_3; break; + } + return conf.getString("sonar.timemachine.variation" + index, defaultValue); } public PastSnapshot find(int index, String property) { diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java index 618a539307f..8daea1a6bd1 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java @@ -31,6 +31,7 @@ public class PastSnapshotFinderByDays implements BatchExtension { public static final String MODE = "days"; + private Snapshot projectSnapshot; // TODO replace by PersistenceManager private DatabaseSession session; diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java index 048f34c2b32..6273998b7cc 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java @@ -19,8 +19,10 @@ */ package org.sonar.plugins.core.timemachine; +import org.apache.commons.configuration.PropertiesConfiguration; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.sonar.api.database.model.Snapshot; @@ -161,4 +163,21 @@ public class PastSnapshotFinderTest { when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(new PastSnapshot(PastSnapshotFinderByPreviousAnalysis.MODE, new Snapshot())); // should not be called assertNull(finder.find(2, "foooo")); } + + @Test + public void shouldGetPropertyValue() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty("sonar.timemachine.variation1", "5"); + + assertThat(PastSnapshotFinder.getPropertyValue(conf, 1), is("5")); + assertThat(PastSnapshotFinder.getPropertyValue(conf, 999), nullValue()); + } + + @Test + public void shouldGetDefaultPropertyValue() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty("sonar.timemachine.variation1", "5"); + + assertThat(PastSnapshotFinder.getPropertyValue(conf, 2), is(PastSnapshotFinder.DEFAULT_VALUE_2)); + } } -- 2.39.5