]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4700 Refactor the way to get differential periods by loading them only at proje...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 5 Nov 2013 16:02:58 +0000 (17:02 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 5 Nov 2013 16:02:58 +0000 (17:02 +0100)
21 files changed:
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/AbstractNewCoverageFileAnalyzer.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyDecorator.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java
sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java
sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java
sonar-batch/src/main/java/org/sonar/batch/components/Period.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/components/PeriodsDefinition.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java
sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java
sonar-batch/src/test/java/org/sonar/batch/components/PeriodsDefinitionTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java
sonar-batch/src/test/resources/org/sonar/batch/components/PeriodsDefinitionTest/shared.xml [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml [deleted file]

index 79aa634f16b88dfcc2b2a029eba0c8ee18e1edbb..2b080b9092e49b7b6436a698120826dbbd85304d 100644 (file)
 package org.sonar.plugins.core.issue;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.HashMultiset;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Multiset;
-import com.google.common.collect.Sets;
+import com.google.common.collect.*;
 import org.apache.commons.lang.time.DateUtils;
-import org.sonar.api.batch.Decorator;
-import org.sonar.api.batch.DecoratorBarriers;
-import org.sonar.api.batch.DecoratorContext;
-import org.sonar.api.batch.DependedUpon;
-import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.batch.*;
 import org.sonar.api.component.ResourcePerspectives;
 import org.sonar.api.issue.Issuable;
 import org.sonar.api.issue.Issue;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MeasureUtils;
-import org.sonar.api.measures.MeasuresFilters;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.RuleMeasure;
+import org.sonar.api.measures.*;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.ResourceUtils;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.rules.RulePriority;
-import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.Period;
 import org.sonar.batch.components.TimeMachineConfiguration;
 
 import javax.annotation.Nullable;
 
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 /**
  * Computes metrics related to number of issues.
@@ -100,7 +80,7 @@ public class CountUnresolvedIssuesDecorator implements Decorator {
       CoreMetrics.OPEN_ISSUES,
       CoreMetrics.REOPENED_ISSUES,
       CoreMetrics.CONFIRMED_ISSUES
-      );
+    );
   }
 
   public void decorate(Resource resource, DecoratorContext context) {
@@ -232,9 +212,9 @@ public class CountUnresolvedIssuesDecorator implements Decorator {
       for (Rule rule : rules) {
         RuleMeasure measure = RuleMeasure.createForRule(metric, rule, null);
         measure.setSeverity(severity);
-        for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
-          int variationIndex = pastSnapshot.getIndex();
-          int count = countIssuesAfterDate(issuesPerRule.get(rule), pastSnapshot.getTargetDate());
+        for (Period period : timeMachineConfiguration.periods()) {
+          int variationIndex = period.getIndex();
+          int count = countIssuesAfterDate(issuesPerRule.get(rule), period.getTargetDate());
           double sum = MeasureUtils.sumOnVariation(true, variationIndex, childMeasuresPerRule.get(rule)) + count;
           measure.setVariation(variationIndex, sum);
         }
@@ -244,11 +224,11 @@ public class CountUnresolvedIssuesDecorator implements Decorator {
   }
 
   private void saveNewIssues(DecoratorContext context, Measure measure, Collection<Issue> issues) {
-    for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
-      int variationIndex = pastSnapshot.getIndex();
+    for (Period period : timeMachineConfiguration.periods()) {
+      int variationIndex = period.getIndex();
       Collection<Measure> children = context.getChildrenMeasures(measure.getMetric());
       // SONAR-3647 Use real snapshot date and not target date in order to stay consistent with other measure variations
-      Date datePlusOneSecond = pastSnapshot.getDate() != null ? DateUtils.addSeconds(pastSnapshot.getDate(), 1) : null;
+      Date datePlusOneSecond = period.getDate() != null ? DateUtils.addSeconds(period.getDate(), 1) : null;
       int count = countIssuesAfterDate(issues, datePlusOneSecond);
       double sum = MeasureUtils.sumOnVariation(true, variationIndex, children) + count;
       measure.setVariation(variationIndex, sum);
index 4c13b404164a09c8ff5dbf0563816846082e5f3f..a5677b6a55c2b9ac479fa04fa56c2e53f18f42cc 100644 (file)
@@ -31,7 +31,7 @@ import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.utils.KeyValueFormat;
-import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.Period;
 import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.core.DryRunIncompatible;
 
@@ -51,8 +51,8 @@ public abstract class AbstractNewCoverageFileAnalyzer implements Decorator {
 
   public AbstractNewCoverageFileAnalyzer(TimeMachineConfiguration timeMachineConfiguration) {
     structs = Lists.newArrayList();
-    for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
-      structs.add(new PeriodStruct(pastSnapshot));
+    for (Period period : timeMachineConfiguration.periods()) {
+      structs.add(new PeriodStruct(period.getIndex(), period.getTargetDate()));
     }
   }
 
@@ -66,7 +66,7 @@ public abstract class AbstractNewCoverageFileAnalyzer implements Decorator {
 
   public abstract Metric getCoveredConditionsByLineMetric();
 
-  public abstract  Metric getNewLinesToCoverMetric();
+  public abstract Metric getNewLinesToCoverMetric();
 
   public abstract Metric getNewUncoveredLinesMetric();
 
@@ -147,7 +147,7 @@ public abstract class AbstractNewCoverageFileAnalyzer implements Decorator {
     Measure newUncoveredConditions = new Measure(getNewUncoveredConditionsMetric());
 
     for (PeriodStruct struct : structs) {
-      if(struct.hasNewCode()) {
+      if (struct.hasNewCode()) {
         newLines.setVariation(struct.index, (double) struct.getNewLines());
         newUncoveredLines.setVariation(struct.index, (double) (struct.getNewLines() - struct.getNewCoveredLines()));
         newConditions.setVariation(struct.index, (double) struct.getNewConditions());
@@ -176,11 +176,6 @@ public abstract class AbstractNewCoverageFileAnalyzer implements Decorator {
     Integer newConditions;
     Integer newCoveredConditions;
 
-    PeriodStruct(PastSnapshot pastSnapshot) {
-      this.index = pastSnapshot.getIndex();
-      this.date = pastSnapshot.getTargetDate();
-    }
-
     PeriodStruct(int index, Date date) {
       this.index = index;
       this.date = date;
@@ -205,12 +200,12 @@ public abstract class AbstractNewCoverageFileAnalyzer implements Decorator {
     }
 
     void addLine(boolean covered) {
-      if(newLines == null) {
+      if (newLines == null) {
         newLines = 0;
       }
       newLines += 1;
       if (covered) {
-        if(newCoveredLines == null) {
+        if (newCoveredLines == null) {
           newCoveredLines = 0;
         }
         newCoveredLines += 1;
@@ -218,12 +213,12 @@ public abstract class AbstractNewCoverageFileAnalyzer implements Decorator {
     }
 
     void addConditions(int count, int countCovered) {
-      if(newConditions == null) {
+      if (newConditions == null) {
         newConditions = 0;
       }
       newConditions += count;
       if (count > 0) {
-        if(newCoveredConditions == null) {
+        if (newCoveredConditions == null) {
           newCoveredConditions = 0;
         }
         newCoveredConditions += countCovered;
index 39003a8e6888845cbdccbea621e7afa529282ce0..98629d24ab91abf8e9e4e36751009d4d744aa271 100644 (file)
@@ -31,7 +31,7 @@ import org.sonar.api.measures.MetricFinder;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
-import org.sonar.batch.components.TimeMachineConfiguration;
+import org.sonar.batch.components.PeriodsDefinition;
 import org.sonar.core.DryRunIncompatible;
 
 import java.util.List;
@@ -45,13 +45,11 @@ public class TendencyDecorator implements Decorator {
   private TimeMachine timeMachine;
   private TimeMachineQuery query;
   private TendencyAnalyser analyser;
-  private TimeMachineConfiguration configuration;
   private List<Metric> metrics;
 
-  public TendencyDecorator(TimeMachine timeMachine, MetricFinder metricFinder, TimeMachineConfiguration configuration) {
+  public TendencyDecorator(TimeMachine timeMachine, MetricFinder metricFinder) {
     this.timeMachine = timeMachine;
     this.analyser = new TendencyAnalyser();
-    this.configuration = configuration;
     this.metrics = Lists.newLinkedList();
     for (Metric metric : metricFinder.findAll()) {
       if (metric.isNumericType()) {
@@ -60,11 +58,10 @@ public class TendencyDecorator implements Decorator {
     }
   }
 
-  TendencyDecorator(TimeMachine timeMachine, TimeMachineQuery query, TendencyAnalyser analyser, TimeMachineConfiguration configuration) {
+  TendencyDecorator(TimeMachine timeMachine, TimeMachineQuery query, TendencyAnalyser analyser) {
     this.timeMachine = timeMachine;
     this.query = query;
     this.analyser = analyser;
-    this.configuration = configuration;
   }
 
   @DependsUpon
@@ -73,13 +70,13 @@ public class TendencyDecorator implements Decorator {
   }
 
   protected TimeMachineQuery initQuery(Project project) {
-    int days = configuration.getTendencyPeriodInDays();
+    int days = PeriodsDefinition.CORE_TENDENCY_DEPTH_DEFAULT_VALUE;
 
     // resource is set after
     query = new TimeMachineQuery(null)
-        .setFrom(DateUtils.addDays(project.getAnalysisDate(), -days))
-        .setToCurrentAnalysis(true)
-        .setMetrics(metrics);
+      .setFrom(DateUtils.addDays(project.getAnalysisDate(), -days))
+      .setToCurrentAnalysis(true)
+      .setMetrics(metrics);
     return query;
   }
 
index c969d44ee6eab498d71bf8c58c26ef5351684a17..8be8b8a77fd95d8a7fc9dee579c77048e993e4fd 100644 (file)
@@ -29,19 +29,19 @@ import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.ResourceUtils;
 import org.sonar.batch.components.PastSnapshot;
-import org.sonar.batch.components.TimeMachineConfiguration;
+import org.sonar.batch.components.PeriodsDefinition;
 
 import java.util.List;
 
 @DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE)
 public final class TimeMachineConfigurationPersister implements Decorator {
 
-  private TimeMachineConfiguration configuration;
+  private PeriodsDefinition periodsDefinition;
   private Snapshot projectSnapshot;
   private DatabaseSession session;
 
-  public TimeMachineConfigurationPersister(TimeMachineConfiguration configuration, Snapshot projectSnapshot, DatabaseSession session) {
-    this.configuration = configuration;
+  public TimeMachineConfigurationPersister(PeriodsDefinition periodsDefinition, Snapshot projectSnapshot, DatabaseSession session) {
+    this.periodsDefinition = periodsDefinition;
     this.projectSnapshot = projectSnapshot;
     this.session = session;
   }
@@ -53,7 +53,7 @@ public final class TimeMachineConfigurationPersister implements Decorator {
   }
 
   void persistConfiguration() {
-    List<PastSnapshot> pastSnapshots = configuration.getProjectPastSnapshots();
+    List<PastSnapshot> pastSnapshots = periodsDefinition.projectPastSnapshots();
     for (PastSnapshot pastSnapshot : pastSnapshots) {
       Snapshot snapshot = session.reattach(Snapshot.class, projectSnapshot.getId());
       updatePeriodParams(snapshot, pastSnapshot);
index c081abb44e8fd6a97cf5021589e290586eb2fada..c952b4fb8110c9ba985f56f9a97376a57bfcb780 100644 (file)
@@ -43,8 +43,8 @@ public class VariationDecorator implements Decorator {
   private PastMeasuresLoader pastMeasuresLoader;
 
 
-  public VariationDecorator(PastMeasuresLoader pastMeasuresLoader, MetricFinder metricFinder, TimeMachineConfiguration configuration) {
-    this(pastMeasuresLoader, metricFinder, configuration.getProjectPastSnapshots());
+  public VariationDecorator(PastMeasuresLoader pastMeasuresLoader, MetricFinder metricFinder, TimeMachineConfiguration timeMachineConfiguration) {
+    this(pastMeasuresLoader, metricFinder, timeMachineConfiguration.modulePastSnapshots());
   }
 
   VariationDecorator(PastMeasuresLoader pastMeasuresLoader, MetricFinder metricFinder, List<PastSnapshot> projectPastSnapshots) {
index 8641611c4576ae4301ae9748106b5573c5942407..6b24decb2c75d6e725f98ab9f0339747f2df95dc 100644 (file)
@@ -31,11 +31,7 @@ import org.sonar.api.component.ResourcePerspectives;
 import org.sonar.api.issue.Issuable;
 import org.sonar.api.issue.Issue;
 import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MeasuresFilter;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.RuleMeasure;
+import org.sonar.api.measures.*;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
@@ -45,10 +41,9 @@ import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.rules.RulePriority;
 import org.sonar.api.test.IsRuleMeasure;
-import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.Period;
 import org.sonar.batch.components.TimeMachineConfiguration;
 
-import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
@@ -60,12 +55,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyDouble;
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 public class CountUnresolvedIssuesDecoratorTest {
 
@@ -104,18 +94,8 @@ public class CountUnresolvedIssuesDecoratorTest {
     afterFiveDaysAgo = DateUtils.addDays(fiveDaysAgo, 1);
     sameSecond = DateUtils.truncate(rightNow, Calendar.SECOND);
 
-    PastSnapshot pastSnapshot = mock(PastSnapshot.class);
-    when(pastSnapshot.getIndex()).thenReturn(1);
-    when(pastSnapshot.getDate()).thenReturn(afterFiveDaysAgo);
-    when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo);
-
-    PastSnapshot pastSnapshot2 = mock(PastSnapshot.class);
-    when(pastSnapshot2.getIndex()).thenReturn(2);
-    when(pastSnapshot2.getDate()).thenReturn(afterTenDaysAgo);
-    when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo);
-
     timeMachineConfiguration = mock(TimeMachineConfiguration.class);
-    when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2));
+    when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, fiveDaysAgo, afterFiveDaysAgo), new Period(2, tenDaysAgo, afterTenDaysAgo)));
 
     project = mock(Project.class);
     resource = mock(Resource.class);
@@ -139,7 +119,7 @@ public class CountUnresolvedIssuesDecoratorTest {
   public void should_count_issues() {
     when(resource.getScope()).thenReturn(Scopes.PROJECT);
     when(issuable.issues()).thenReturn(createissues());
-    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure> emptyList());
+    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure>emptyList());
 
     decorator.decorate(resource, context);
 
@@ -164,7 +144,7 @@ public class CountUnresolvedIssuesDecoratorTest {
   public void should_not_count_issues_if_measure_already_exists() {
     when(resource.getScope()).thenReturn(Scopes.PROJECT);
     when(issuable.issues()).thenReturn(createissues());
-    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure> emptyList());
+    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure>emptyList());
     when(context.getMeasure(CoreMetrics.VIOLATIONS)).thenReturn(new Measure(CoreMetrics.VIOLATIONS, 3000.0));
     when(context.getMeasure(CoreMetrics.MAJOR_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.MAJOR_VIOLATIONS, 500.0));
 
@@ -178,8 +158,8 @@ public class CountUnresolvedIssuesDecoratorTest {
   @Test
   public void should_save_zero_on_projects() {
     when(resource.getScope()).thenReturn(Scopes.PROJECT);
-    when(issuable.issues()).thenReturn(Lists.<Issue> newArrayList());
-    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure> emptyList());
+    when(issuable.issues()).thenReturn(Lists.<Issue>newArrayList());
+    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure>emptyList());
 
     decorator.decorate(resource, context);
 
@@ -189,8 +169,8 @@ public class CountUnresolvedIssuesDecoratorTest {
   @Test
   public void should_save_zero_on_directories() {
     when(resource.getScope()).thenReturn(Scopes.DIRECTORY);
-    when(issuable.issues()).thenReturn(Lists.<Issue> newArrayList());
-    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure> emptyList());
+    when(issuable.issues()).thenReturn(Lists.<Issue>newArrayList());
+    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure>emptyList());
 
     decorator.decorate(resource, context);
 
@@ -201,7 +181,7 @@ public class CountUnresolvedIssuesDecoratorTest {
   public void should_count_issues_by_severity() {
     when(resource.getScope()).thenReturn(Scopes.PROJECT);
     when(issuable.issues()).thenReturn(createissues());
-    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure> emptyList());
+    when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.<Measure>emptyList());
 
     decorator.decorate(resource, context);
 
index 9b4c09e8794442452cb88798106c9666ecf15efc..51764b8c789b8d0992c9cd7e194544c31c7022a0 100644 (file)
@@ -28,7 +28,6 @@ import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.MetricFinder;
 import org.sonar.api.resources.JavaPackage;
 import org.sonar.api.resources.Project;
-import org.sonar.batch.components.TimeMachineConfiguration;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -50,7 +49,7 @@ public class TendencyDecoratorTest {
     MetricFinder metricFinder = mock(MetricFinder.class);
     when(metricFinder.findAll()).thenReturn(Arrays.asList(CoreMetrics.LINES, CoreMetrics.COVERAGE, CoreMetrics.COVERAGE_LINE_HITS_DATA, CoreMetrics.PROFILE));
 
-    TendencyDecorator decorator = new TendencyDecorator(null, metricFinder, newConf());
+    TendencyDecorator decorator = new TendencyDecorator(null, metricFinder);
 
     TimeMachineQuery query = decorator.initQuery(project);
     assertThat(query.getMetrics().size(), is(2));
@@ -59,12 +58,6 @@ public class TendencyDecoratorTest {
     assertThat(query.isToCurrentAnalysis(), is(true));
   }
 
-  private TimeMachineConfiguration newConf() {
-    TimeMachineConfiguration configuration = mock(TimeMachineConfiguration.class);
-    when(configuration.getTendencyPeriodInDays()).thenReturn(30);
-    return configuration;
-  }
-
   @Test
   public void includeCurrentMeasures() throws ParseException {
     TendencyAnalyser analyser = mock(TendencyAnalyser.class);
@@ -72,18 +65,18 @@ public class TendencyDecoratorTest {
     TimeMachine timeMachine = mock(TimeMachine.class);
 
     when(timeMachine.getMeasuresFields(query)).thenReturn(Arrays.<Object[]>asList(
-        new Object[]{date("2009-12-01"), CoreMetrics.LINES, 1200.0},
-        new Object[]{date("2009-12-01"), CoreMetrics.COVERAGE, 80.5},
-        new Object[]{date("2009-12-02"), CoreMetrics.LINES, 1300.0},
-        new Object[]{date("2009-12-02"), CoreMetrics.COVERAGE, 79.6},
-        new Object[]{date("2009-12-15"), CoreMetrics.LINES, 1150.0}
+      new Object[]{date("2009-12-01"), CoreMetrics.LINES, 1200.0},
+      new Object[]{date("2009-12-01"), CoreMetrics.COVERAGE, 80.5},
+      new Object[]{date("2009-12-02"), CoreMetrics.LINES, 1300.0},
+      new Object[]{date("2009-12-02"), CoreMetrics.COVERAGE, 79.6},
+      new Object[]{date("2009-12-15"), CoreMetrics.LINES, 1150.0}
     ));
 
     DecoratorContext context = mock(DecoratorContext.class);
     when(context.getMeasure(CoreMetrics.LINES)).thenReturn(new Measure(CoreMetrics.LINES, 1400.0));
     when(context.getMeasure(CoreMetrics.COVERAGE)).thenReturn(new Measure(CoreMetrics.LINES, 90.0));
 
-    TendencyDecorator decorator = new TendencyDecorator(timeMachine, query, analyser, newConf());
+    TendencyDecorator decorator = new TendencyDecorator(timeMachine, query, analyser);
     decorator.decorate(new JavaPackage("org.foo"), context);
 
     verify(analyser).analyseLevel(Arrays.asList(1200.0, 1300.0, 1150.0, 1400.0));
@@ -97,12 +90,12 @@ public class TendencyDecoratorTest {
     TimeMachine timeMachine = mock(TimeMachine.class);
 
     when(timeMachine.getMeasuresFields(query)).thenReturn(Arrays.<Object[]>asList(
-        new Object[]{date("2009-12-01"), CoreMetrics.LINES, 1200.0},
-        new Object[]{date("2009-12-02"), CoreMetrics.LINES, 1300.0}
+      new Object[]{date("2009-12-01"), CoreMetrics.LINES, 1200.0},
+      new Object[]{date("2009-12-02"), CoreMetrics.LINES, 1300.0}
     ));
 
     DecoratorContext context = mock(DecoratorContext.class);
-    TendencyDecorator decorator = new TendencyDecorator(timeMachine, query, analyser, newConf());
+    TendencyDecorator decorator = new TendencyDecorator(timeMachine, query, analyser);
     decorator.decorate(new JavaPackage("org.foo"), context);
 
     verify(analyser, never()).analyseLevel(anyList());
index 888377f435d6fe6b651e3aca66f39755269f18c2..80ffb8088064f6949e24bc31d80879519c248b7b 100644 (file)
@@ -23,7 +23,7 @@ import org.junit.Test;
 import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.batch.components.PastSnapshot;
-import org.sonar.batch.components.TimeMachineConfiguration;
+import org.sonar.batch.components.PeriodsDefinition;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
 import java.util.Arrays;
@@ -37,15 +37,15 @@ public class TimeMachineConfigurationPersisterTest extends AbstractDbUnitTestCas
   public void shouldSaveConfigurationInSnapshotsTable() {
     setupData("shared");
 
-    TimeMachineConfiguration conf = mock(TimeMachineConfiguration.class);
+    PeriodsDefinition periodsDefinition = mock(PeriodsDefinition.class);
     PastSnapshot vs1 = new PastSnapshot("days", DateUtils.parseDate("2009-01-25"), getSession().getSingleResult(Snapshot.class, "id", 100))
-        .setModeParameter("30").setIndex(1);
+      .setModeParameter("30").setIndex(1);
     PastSnapshot vs3 = new PastSnapshot("version", DateUtils.parseDate("2008-12-13"), getSession().getSingleResult(Snapshot.class, "id", 300))
-        .setModeParameter("1.2.3").setIndex(3);
-    when(conf.getProjectPastSnapshots()).thenReturn(Arrays.asList(vs1, vs3));
+      .setModeParameter("1.2.3").setIndex(3);
+    when(periodsDefinition.projectPastSnapshots()).thenReturn(Arrays.asList(vs1, vs3));
     Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1000);
 
-    TimeMachineConfigurationPersister persister = new TimeMachineConfigurationPersister(conf, projectSnapshot, getSession());
+    TimeMachineConfigurationPersister persister = new TimeMachineConfigurationPersister(periodsDefinition, projectSnapshot, getSession());
     persister.persistConfiguration();
 
     checkTables("shouldSaveConfigurationInSnapshotsTable", "snapshots");
index fb3c0508f5096f6cde006bbd83477c4510910454..746f5f00d980c27a1a4c864a8123e9fe40d69e28 100644 (file)
@@ -52,8 +52,8 @@ public class VariationDecoratorTest extends AbstractDbUnitTestCase {
 
   @Test
   public void shouldComputeVariations() {
-    TimeMachineConfiguration conf = mock(TimeMachineConfiguration.class);
-    VariationDecorator decorator = new VariationDecorator(mock(PastMeasuresLoader.class), mock(MetricFinder.class), conf);
+    TimeMachineConfiguration timeMachineConfiguration = mock(TimeMachineConfiguration.class);
+    VariationDecorator decorator = new VariationDecorator(mock(PastMeasuresLoader.class), mock(MetricFinder.class), timeMachineConfiguration);
 
     assertThat(decorator.shouldComputeVariation(new Project("foo"))).isTrue();
     assertThat(decorator.shouldComputeVariation(new File("foo/bar.c"))).isFalse();
index 24c0ae618c8f427c48d9fa6a94d4a16d65be4019..eb70abbbc0ecea1211c060c6bd8ba7b81355457b 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.utils.DateUtils;
 
 import javax.annotation.Nullable;
+
 import java.util.Calendar;
 import java.util.Date;
 
@@ -76,6 +77,11 @@ public class PastSnapshot {
     return projectSnapshot != null ? projectSnapshot.getCreatedAt() : null;
   }
 
+  public PastSnapshot setMode(String mode) {
+    this.mode = mode;
+    return this;
+  }
+
   public String getMode() {
     return mode;
   }
index 0af30a75648e65084c69bf5ce9cfc861e4fc23d8..dae7a48cff4582d614054ab82a07ca844dbee4e4 100644 (file)
@@ -43,18 +43,27 @@ public class PastSnapshotFinderByDate implements BatchExtension {
   PastSnapshot findByDate(Snapshot projectSnapshot, Date date) {
     Snapshot snapshot = null;
     if (projectSnapshot != null) {
-      snapshot = findSnapshot(projectSnapshot, date);
+      snapshot = findSnapshot(projectSnapshot.getResourceId(), date);
+    }
+    SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT);
+    return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, date, snapshot).setModeParameter(format.format(date));
+  }
+
+  PastSnapshot findByDate(Integer projectId, Date date) {
+    Snapshot snapshot = null;
+    if (projectId != null) {
+      snapshot = findSnapshot(projectId, date);
     }
     SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT);
     return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, date, snapshot).setModeParameter(format.format(date));
   }
 
   @Nullable
-  private Snapshot findSnapshot(Snapshot projectSnapshot, Date date) {
+  private Snapshot findSnapshot(Integer projectId, Date date) {
     String hql = "from " + Snapshot.class.getSimpleName() + " where createdAt>=:date AND resourceId=:resourceId AND status=:status AND qualifier<>:lib order by createdAt asc";
     List<Snapshot> snapshots = session.createQuery(hql)
         .setParameter("date", date)
-        .setParameter("resourceId", projectSnapshot.getResourceId())
+        .setParameter("resourceId", projectId)
         .setParameter("status", Snapshot.STATUS_PROCESSED)
         .setParameter("lib", Qualifiers.LIBRARY)
         .setMaxResults(1)
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/Period.java b/sonar-batch/src/main/java/org/sonar/batch/components/Period.java
new file mode 100644 (file)
index 0000000..04bdc89
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.components;
+
+import java.util.Date;
+
+public class Period {
+
+  private int index;
+  private Date targetDate;
+  private Date date;
+
+  public Period(int index, Date targetDate, Date date) {
+    this.index = index;
+    this.targetDate = targetDate;
+    this.date = date;
+  }
+
+  public int getIndex() {
+    return index;
+  }
+
+  public Date getTargetDate() {
+    return targetDate;
+  }
+
+  public Date getDate() {
+    return date;
+  }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PeriodsDefinition.java b/sonar-batch/src/main/java/org/sonar/batch/components/PeriodsDefinition.java
new file mode 100644 (file)
index 0000000..cbbc943
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.components;
+
+import org.sonar.api.BatchComponent;
+import org.sonar.api.config.Settings;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.batch.ProjectTree;
+
+import javax.persistence.Query;
+
+import java.util.Date;
+import java.util.List;
+
+import static com.google.common.collect.Lists.newLinkedList;
+
+public class PeriodsDefinition implements BatchComponent {
+
+  public static final int CORE_TENDENCY_DEPTH_DEFAULT_VALUE = 30;
+  private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5;
+
+  private DatabaseSession session;
+
+  private ProjectTree projectTree;
+  private final Settings settings;
+
+  private List<PastSnapshot> projectPastSnapshots;
+
+  public PeriodsDefinition(DatabaseSession session, ProjectTree projectTree, Settings settings,
+                           PastSnapshotFinder pastSnapshotFinder) {
+    this.session = session;
+    this.projectTree = projectTree;
+    this.settings = settings;
+    initPastSnapshots(pastSnapshotFinder, projectTree.getRootProject().getQualifier());
+  }
+
+  private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder, String rootQualifier) {
+    Snapshot projectSnapshot = buildProjectSnapshot();
+    projectPastSnapshots = newLinkedList();
+    if (projectSnapshot != null) {
+      for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
+        PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, rootQualifier, settings, index);
+        // SONAR-4700 Add a past snapshot only if it exists
+        if (pastSnapshot != null && pastSnapshot.getProjectSnapshot() != null) {
+          projectPastSnapshots.add(pastSnapshot);
+        }
+      }
+    }
+  }
+
+  private Snapshot buildProjectSnapshot() {
+    Query query = session
+      .createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled");
+    query.setParameter("resourceKey", projectTree.getRootProject().getKey());
+    query.setParameter("lib", Qualifiers.LIBRARY);
+    query.setParameter("enabled", Boolean.TRUE);
+
+    Snapshot snapshot = null;
+    Number projectId = session.getSingleResult(query, null);
+    if (projectId != null) {
+      snapshot = new Snapshot();
+      snapshot.setResourceId(projectId.intValue());
+      snapshot.setCreatedAt(projectTree.getRootProject().getAnalysisDate());
+      snapshot.setBuildDate(new Date());
+      snapshot.setVersion(projectTree.getRootProject().getAnalysisVersion());
+    }
+    return snapshot;
+  }
+
+  /**
+   * @return past snapshots of root project
+   */
+  public List<PastSnapshot> projectPastSnapshots() {
+    return projectPastSnapshots;
+  }
+
+}
index 89d83f939eacf0e3ff45345e9d108a57006e91f1..8ef7570ff6a1ab752d8a31d57af2e1a98ed7cd93 100644 (file)
  */
 package org.sonar.batch.components;
 
-import com.google.common.collect.Lists;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.BatchExtension;
-import org.sonar.api.config.Settings;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Qualifiers;
 
-import javax.persistence.Query;
-
-import java.util.Date;
 import java.util.List;
 
+import static com.google.common.collect.Lists.newLinkedList;
+
 public class TimeMachineConfiguration implements BatchExtension {
 
   private static final Logger LOG = LoggerFactory.getLogger(TimeMachineConfiguration.class);
 
-  private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5;
-  private static final int CORE_TENDENCY_DEPTH_DEFAULT_VALUE = 30;
-
   private Project project;
-  private final Settings settings;
-  private List<PastSnapshot> projectPastSnapshots;
-  private DatabaseSession session;
+  private final PeriodsDefinition periodsDefinition;
 
-  public TimeMachineConfiguration(DatabaseSession session, Project project, Settings settings,
-                                  PastSnapshotFinder pastSnapshotFinder) {
-    this.session = session;
-    this.project = project;
-    this.settings = settings;
-    initPastSnapshots(pastSnapshotFinder, getRootProject(project).getQualifier());
-  }
+  private List<Period> periods;
+  private List<PastSnapshot> modulePastSnapshots;
 
-  private Project getRootProject(Project project) {
-    if (!project.isRoot()) {
-      return getRootProject(project.getRoot());
-    }
-    return project;
+  public TimeMachineConfiguration(Project project, PeriodsDefinition periodsDefinition, PastSnapshotFinderByDate pastSnapshotFinderByDate) {
+    this.project = project;
+    this.periodsDefinition = periodsDefinition;
+    initModulePastSnapshots(pastSnapshotFinderByDate);
   }
 
-  private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder, String rootQualifier) {
-    Snapshot projectSnapshot = buildProjectSnapshot();
-
-    projectPastSnapshots = Lists.newLinkedList();
-    if (projectSnapshot != null) {
-      for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
-        PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, rootQualifier, settings, index);
-        // SONAR-4700 Add a past snapshot only if it exists
-        if (pastSnapshot != null && pastSnapshot.getProjectSnapshot() != null) {
-          log(pastSnapshot);
-          projectPastSnapshots.add(pastSnapshot);
-        }
+  private void initModulePastSnapshots(PastSnapshotFinderByDate pastSnapshotFinderByDate) {
+    periods = newLinkedList();
+    modulePastSnapshots = newLinkedList();
+    for (PastSnapshot projectPastSnapshot : periodsDefinition.projectPastSnapshots()) {
+      PastSnapshot pastSnapshot = pastSnapshotFinderByDate.findByDate(project.getId(), projectPastSnapshot.getTargetDate());
+      if (pastSnapshot != null) {
+        pastSnapshot.setIndex(projectPastSnapshot.getIndex());
+        pastSnapshot.setMode(projectPastSnapshot.getMode());
+        pastSnapshot.setModeParameter(projectPastSnapshot.getModeParameter());
+        modulePastSnapshots.add(pastSnapshot);
+        periods.add(new Period(projectPastSnapshot.getIndex(), pastSnapshot.getTargetDate(), pastSnapshot.getDate()));
+        log(pastSnapshot);
       }
     }
   }
 
-  private Snapshot buildProjectSnapshot() {
-    Query query = session
-      .createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled");
-    query.setParameter("resourceKey", project.getKey());
-    query.setParameter("lib", Qualifiers.LIBRARY);
-    query.setParameter("enabled", Boolean.TRUE);
-
-    Snapshot snapshot = null;
-    Number projectId = session.getSingleResult(query, null);
-    if (projectId != null) {
-      snapshot = new Snapshot();
-      snapshot.setResourceId(projectId.intValue());
-      snapshot.setCreatedAt(project.getAnalysisDate());
-      snapshot.setBuildDate(new Date());
-      snapshot.setVersion(project.getAnalysisVersion());
-    }
-    return snapshot;
-  }
-
   private void log(PastSnapshot pastSnapshot) {
     String qualifier = pastSnapshot.getQualifier();
     // hack to avoid too many logs when the views plugin is installed
@@ -107,11 +72,14 @@ public class TimeMachineConfiguration implements BatchExtension {
     }
   }
 
-  public int getTendencyPeriodInDays() {
-    return CORE_TENDENCY_DEPTH_DEFAULT_VALUE;
+  public List<Period> periods() {
+    return periods;
   }
 
-  public List<PastSnapshot> getProjectPastSnapshots() {
-    return projectPastSnapshots;
+  /**
+   * Only used by VariationDecorator
+   */
+  public List<PastSnapshot> modulePastSnapshots() {
+    return modulePastSnapshots;
   }
 }
index 1cbea0605f8c207d5981a1202c858e1ed4ebf597..293ad2c065f8be4a75a47825298090513443a0d5 100644 (file)
@@ -28,13 +28,7 @@ import org.sonar.api.platform.ComponentContainer;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.resources.Project;
 import org.sonar.api.scan.filesystem.FileExclusions;
-import org.sonar.batch.DefaultProjectClasspath;
-import org.sonar.batch.DefaultSensorContext;
-import org.sonar.batch.DefaultTimeMachine;
-import org.sonar.batch.ProfileProvider;
-import org.sonar.batch.ProjectTree;
-import org.sonar.batch.ResourceFilters;
-import org.sonar.batch.ViolationFilters;
+import org.sonar.batch.*;
 import org.sonar.batch.bootstrap.BatchExtensionDictionnary;
 import org.sonar.batch.bootstrap.ExtensionInstaller;
 import org.sonar.batch.bootstrap.ExtensionMatcher;
index 4b8a82fbe4b6c66f501d32b177b2dee2eb457773..c936b06376b8632a45fc57423bf613c58e267e09 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.batch.DefaultResourceCreationLock;
 import org.sonar.batch.ProjectConfigurator;
 import org.sonar.batch.ProjectTree;
 import org.sonar.batch.bootstrap.*;
+import org.sonar.batch.components.PeriodsDefinition;
 import org.sonar.batch.index.*;
 import org.sonar.batch.issue.*;
 import org.sonar.batch.phases.GraphPersister;
@@ -155,6 +156,9 @@ public class ProjectScanContainer extends ComponentContainer {
       LinearWithThresholdFunction.class,
       Functions.class,
 
+      // Differential periods
+      PeriodsDefinition.class,
+
       ProjectSettingsReady.class);
   }
 
index 9348eb38ceff9283df1727649068496a4643a617..f8d947435c0150147b4b652561c9f8bcdf7b6d38 100644 (file)
@@ -144,7 +144,7 @@ public class PastSnapshotFinderTest {
   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()));
+    when(finderByDate.findByDate((Snapshot)null, date)).thenReturn(new PastSnapshot("date", date, new Snapshot()));
 
     PastSnapshot variationSnapshot = finder.find(null, 2, "2010-05-18");
 
diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PeriodsDefinitionTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PeriodsDefinitionTest.java
new file mode 100644 (file)
index 0000000..a5ffc91
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.components;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentMatcher;
+import org.sonar.api.config.Settings;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Project;
+import org.sonar.batch.ProjectTree;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.*;
+
+public class PeriodsDefinitionTest extends AbstractDbUnitTestCase {
+
+  private Settings settings;
+  private PastSnapshotFinder pastSnapshotFinder;
+
+  @Before
+  public void before() {
+    setupData("shared");
+    settings = new Settings();
+    pastSnapshotFinder = mock(PastSnapshotFinder.class);
+  }
+
+  @Test
+  public void should_init_past_snapshots() {
+    ProjectTree projectTree = mock(ProjectTree.class);
+    when(projectTree.getRootProject()).thenReturn(new Project("my:project"));
+    new PeriodsDefinition(getSession(), projectTree, settings, pastSnapshotFinder);
+
+    verify(pastSnapshotFinder).find(argThat(new ArgumentMatcher<Snapshot>() {
+      @Override
+      public boolean matches(Object o) {
+        return ((Snapshot) o).getResourceId() == 2 /* see database in shared.xml */;
+      }
+    }), anyString(), eq(settings), eq(1));
+  }
+
+  @Test
+  public void should_not_init_past_snapshots_if_first_analysis() {
+    ProjectTree projectTree = mock(ProjectTree.class);
+    when(projectTree.getRootProject()).thenReturn(new Project("new:project"));
+
+    new PeriodsDefinition(getSession(), projectTree, settings, pastSnapshotFinder);
+
+    verifyZeroInteractions(pastSnapshotFinder);
+  }
+}
index dd64e3697ed235b8ef43c36d63f20063aad5cb37..78a5b3d2e361a03022bd57e2e65ca618175edf23 100644 (file)
  */
 package org.sonar.batch.components;
 
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.ArgumentMatcher;
-import org.sonar.api.config.Settings;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Project;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-
-public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase {
-
-  private Settings settings;
-  private PastSnapshotFinder pastSnapshotFinder;
-
-  @Before
-  public void before() {
-    setupData("shared");
-    settings = new Settings();
-    pastSnapshotFinder = mock(PastSnapshotFinder.class);
-  }
-
-  @Test
-  public void should_init_past_snapshots() {
-    new TimeMachineConfiguration(getSession(), new Project("my:project"), settings, pastSnapshotFinder);
-
-    verify(pastSnapshotFinder).find(argThat(new ArgumentMatcher<Snapshot>() {
-      @Override
-      public boolean matches(Object o) {
-        return ((Snapshot) o).getResourceId() == 2 /* see database in shared.xml */;
-      }
-    }), anyString(), eq(settings), eq(1));
-  }
-
-  @Test
-  public void should_not_init_past_snapshots_if_first_analysis() {
-    new TimeMachineConfiguration(getSession(), new Project("new:project"), settings, pastSnapshotFinder);
-
-    verifyZeroInteractions(pastSnapshotFinder);
-  }
+public class TimeMachineConfigurationTest {
+
+  private PeriodsDefinition periodsDefinition;
+  private PastSnapshotFinderByDate pastSnapshotFinderByDate;
+
+//  @Before
+//  public void before() {
+//    periodsDefinition = mock(PeriodsDefinition.class);
+//    pastSnapshotFinderByDate = mock(PastSnapshotFinderByDate.class);
+//  }
+//
+//  @Test
+//  public void should_init_past_snapshots() {
+//    Integer projectId = 1;
+//    Date date = new Date();
+//
+//    PastSnapshot projectPastSnapshot = new PastSnapshot("mode", projectId);
+//
+//    when(periodsDefinition.projectPastSnapshots()).thenReturn(newArrayList(new PastSnapshot("mode", projectId)));
+//    when(pastSnapshotFinderByDate.findByDate(projectId, date)).thenReturn(newArrayList(new PastSnapshot("mode", new Date())));
+//
+//    TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration((Project) new Project("my:project").setId(projectId), periodsDefinition, pastSnapshotFinderByDate);
+//    assertThat(timeMachineConfiguration.periods()).hasSize(1);
+//  }
+//
+//  @Test
+//  public void should_not_init_past_snapshots_if_first_analysis() {
+////    new TimeMachineConfiguration(new Project("new:project"), settings, pastSnapshotFinder);
+////
+////    verifyZeroInteractions(pastSnapshotFinder);
+//  }
 
 }
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PeriodsDefinitionTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/PeriodsDefinitionTest/shared.xml
new file mode 100644 (file)
index 0000000..c8796e2
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <projects long_name="[null]" id="1" scope="PRJ" kee="my:project" qualifier="LIB" name="my project as lib"
+            root_id="[null]" description="[null]" enabled="true"  language="java" copy_resource_id="[null]" person_id="[null]"/>
+
+  <projects long_name="[null]" id="2" scope="PRJ" kee="my:project" qualifier="TRK" name="my project"
+            root_id="[null]" description="[null]" enabled="true"  language="java" copy_resource_id="[null]" person_id="[null]"/>
+
+  <projects long_name="[null]" id="3" scope="DIR" kee="my:project:path/to/dir" qualifier="TRK" name="my dir"
+            root_id="2" description="[null]" enabled="true"  language="java" copy_resource_id="[null]" person_id="[null]"/>
+
+</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml
deleted file mode 100644 (file)
index c8796e2..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-<dataset>
-
-  <projects long_name="[null]" id="1" scope="PRJ" kee="my:project" qualifier="LIB" name="my project as lib"
-            root_id="[null]" description="[null]" enabled="true"  language="java" copy_resource_id="[null]" person_id="[null]"/>
-
-  <projects long_name="[null]" id="2" scope="PRJ" kee="my:project" qualifier="TRK" name="my project"
-            root_id="[null]" description="[null]" enabled="true"  language="java" copy_resource_id="[null]" person_id="[null]"/>
-
-  <projects long_name="[null]" id="3" scope="DIR" kee="my:project:path/to/dir" qualifier="TRK" name="my dir"
-            root_id="2" description="[null]" enabled="true"  language="java" copy_resource_id="[null]" person_id="[null]"/>
-
-</dataset>