From: Julien Lancelot Date: Tue, 5 Nov 2013 16:02:58 +0000 (+0100) Subject: SONAR-4700 Refactor the way to get differential periods by loading them only at proje... X-Git-Tag: 4.1-RC1~333 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ab0c1f192ba9619561f9b4a829c581a01575aecd;p=sonarqube.git SONAR-4700 Refactor the way to get differential periods by loading them only at project levels --- diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java index 79aa634f16b..2b080b9092e 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java @@ -20,45 +20,25 @@ 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 issues) { - for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { - int variationIndex = pastSnapshot.getIndex(); + for (Period period : timeMachineConfiguration.periods()) { + int variationIndex = period.getIndex(); Collection 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); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/AbstractNewCoverageFileAnalyzer.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/AbstractNewCoverageFileAnalyzer.java index 4c13b404164..a5677b6a55c 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/AbstractNewCoverageFileAnalyzer.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/AbstractNewCoverageFileAnalyzer.java @@ -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; diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyDecorator.java index 39003a8e688..98629d24ab9 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyDecorator.java @@ -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 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; } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java index c969d44ee6e..8be8b8a77fd 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java @@ -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 pastSnapshots = configuration.getProjectPastSnapshots(); + List pastSnapshots = periodsDefinition.projectPastSnapshots(); for (PastSnapshot pastSnapshot : pastSnapshots) { Snapshot snapshot = session.reattach(Snapshot.class, projectSnapshot.getId()); updatePeriodParams(snapshot, pastSnapshot); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java index c081abb44e8..c952b4fb811 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java @@ -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 projectPastSnapshots) { diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java index 8641611c457..6b24decb2c7 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java @@ -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. emptyList()); + when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.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. emptyList()); + when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.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. newArrayList()); - when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections. emptyList()); + when(issuable.issues()).thenReturn(Lists.newArrayList()); + when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.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. newArrayList()); - when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections. emptyList()); + when(issuable.issues()).thenReturn(Lists.newArrayList()); + when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.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. emptyList()); + when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(Collections.emptyList()); decorator.decorate(resource, context); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java index 9b4c09e8794..51764b8c789 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java @@ -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.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.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()); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java index 888377f435d..80ffb808806 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java @@ -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"); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java index fb3c0508f50..746f5f00d98 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationDecoratorTest.java @@ -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(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java index 24c0ae618c8..eb70abbbc0e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java @@ -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; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java index 0af30a75648..dae7a48cff4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java @@ -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 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 index 00000000000..04bdc89418f --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/components/Period.java @@ -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 index 00000000000..cbbc943e728 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PeriodsDefinition.java @@ -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 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 projectPastSnapshots() { + return projectPastSnapshots; + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java index 89d83f939ea..8ef7570ff6a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java @@ -19,84 +19,49 @@ */ 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 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 periods; + private List 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 periods() { + return periods; } - public List getProjectPastSnapshots() { - return projectPastSnapshots; + /** + * Only used by VariationDecorator + */ + public List modulePastSnapshots() { + return modulePastSnapshots; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index 1cbea0605f8..293ad2c065f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -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; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index 4b8a82fbe4b..c936b06376b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -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); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java index 9348eb38cef..f8d947435c0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java @@ -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 index 00000000000..a5ffc91b1a1 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PeriodsDefinitionTest.java @@ -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() { + @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); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java index dd64e3697ed..78a5b3d2e36 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java @@ -19,50 +19,36 @@ */ 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() { - @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 index 00000000000..c8796e2bb80 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/components/PeriodsDefinitionTest/shared.xml @@ -0,0 +1,12 @@ + + + + + + + + + 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 index c8796e2bb80..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - -