From 6fa23e27d3d9943c2aa31ec2c53186f9bf0f142a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 5 Feb 2014 18:54:32 +0100 Subject: [PATCH] SONAR-5047 Calculation of new_coverage measures on delta since X days and since Date is wrong --- .../timemachine/AbstractNewCoverageFileAnalyzer.java | 2 +- .../core/issue/CountUnresolvedIssuesDecoratorTest.java | 2 +- .../technicaldebt/NewTechnicalDebtDecoratorTest.java | 6 +++--- .../java/org/sonar/batch/components/PastSnapshot.java | 9 ++++++++- .../src/main/java/org/sonar/batch/components/Period.java | 8 +------- .../sonar/batch/components/TimeMachineConfiguration.java | 6 ++---- .../PastSnapshotFinderByPreviousAnalysisTest.java | 2 +- 7 files changed, 17 insertions(+), 18 deletions(-) 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 a5677b6a55c..ee9b2e1d97f 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 @@ -52,7 +52,7 @@ public abstract class AbstractNewCoverageFileAnalyzer implements Decorator { public AbstractNewCoverageFileAnalyzer(TimeMachineConfiguration timeMachineConfiguration) { structs = Lists.newArrayList(); for (Period period : timeMachineConfiguration.periods()) { - structs.add(new PeriodStruct(period.getIndex(), period.getTargetDate())); + structs.add(new PeriodStruct(period.getIndex(), period.getDate())); } } 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 12db0e0bd17..d51c224564f 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 @@ -95,7 +95,7 @@ public class CountUnresolvedIssuesDecoratorTest { sameSecond = DateUtils.truncate(rightNow, Calendar.SECOND); timeMachineConfiguration = mock(TimeMachineConfiguration.class); - when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, fiveDaysAgo, afterFiveDaysAgo), new Period(2, tenDaysAgo, afterTenDaysAgo))); + when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, afterFiveDaysAgo), new Period(2, afterTenDaysAgo))); project = mock(Project.class); resource = mock(Resource.class); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/NewTechnicalDebtDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/NewTechnicalDebtDecoratorTest.java index 7880c222538..2dfc4b3df36 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/NewTechnicalDebtDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/NewTechnicalDebtDecoratorTest.java @@ -97,7 +97,7 @@ public class NewTechnicalDebtDecoratorTest { fiveDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -5); fourDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -4); - when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, fiveDaysAgo, fiveDaysAgo), new Period(2, tenDaysAgo, tenDaysAgo))); + when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, fiveDaysAgo), new Period(2, tenDaysAgo))); decorator = new NewTechnicalDebtDecorator(perspectives, timeMachineConfiguration, technicalDebtConverter); } @@ -179,7 +179,7 @@ public class NewTechnicalDebtDecoratorTest { @Test public void save_on_one_issue_with_changelog_and_periods_have_no_dates() { - when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, null, null), new Period(2, null, null))); + when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, null), new Period(2, null))); Issue issue = new DefaultIssue().setKey("A").setCreationDate(tenDaysAgo).setTechnicalDebt(fiveDaysDebt).setChanges( newArrayList( @@ -262,7 +262,7 @@ public class NewTechnicalDebtDecoratorTest { @Test public void save_on_one_issue_without_changelog_and_periods_have_no_dates() { - when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, null, null), new Period(2, null, null))); + when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, null), new Period(2, null))); when(issuable.issues()).thenReturn(newArrayList( (Issue) new DefaultIssue().setKey("A").setCreationDate(nineDaysAgo).setTechnicalDebt(fiveDaysDebt)) 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 51bd4d2f50f..91285fff70a 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 @@ -103,10 +103,17 @@ public class PastSnapshot { return projectSnapshot != null ? projectSnapshot.getQualifier() : null; } - public Date getTargetDate() { + private Date getTargetDate() { return targetDate; } + public PastSnapshot clone(){ + PastSnapshot clone = new PastSnapshot(mode, targetDate, projectSnapshot); + clone.setIndex(index); + clone.setModeParameter(modeParameter); + return clone; + } + @Override public String toString() { if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_VERSION)) { 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 index a152ffae00d..a08a1a872fe 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/Period.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/Period.java @@ -28,12 +28,10 @@ import java.util.Date; public class Period { private int index; - private Date targetDate; private Date date; - public Period(int index, Date targetDate, @Nullable Date date) { + public Period(int index, @Nullable Date date) { this.index = index; - this.targetDate = targetDate; this.date = date; } @@ -41,10 +39,6 @@ public class Period { return index; } - public Date getTargetDate() { - return targetDate; - } - @CheckForNull public Date getDate() { return date; 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 cae6da082c9..23e33c5521f 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 @@ -58,12 +58,10 @@ public class TimeMachineConfiguration implements BatchExtension { for (PastSnapshot projectPastSnapshot : periodsDefinition.getRootProjectPastSnapshots()) { Snapshot snapshot = findSnapshot(projectPastSnapshot.getProjectSnapshot()); - PastSnapshot pastSnapshot = new PastSnapshot(projectPastSnapshot.getMode(), projectPastSnapshot.getTargetDate(), projectPastSnapshot.getProjectSnapshot()); - pastSnapshot.setIndex(projectPastSnapshot.getIndex()); - pastSnapshot.setModeParameter(projectPastSnapshot.getModeParameter()); + PastSnapshot pastSnapshot = projectPastSnapshot.clone(); modulePastSnapshots.add(pastSnapshot); // When no snapshot is found, date of the period is null - periods.add(new Period(pastSnapshot.getIndex(), pastSnapshot.getTargetDate(), snapshot != null ? snapshot.getCreatedAt() : null)); + periods.add(new Period(pastSnapshot.getIndex(), snapshot != null ? snapshot.getCreatedAt() : null)); log(pastSnapshot); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java index dcd60faf5d3..93a18980c91 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java @@ -50,6 +50,6 @@ public class PastSnapshotFinderByPreviousAnalysisTest extends AbstractDbUnitTest PastSnapshot pastSnapshot = finder.findByPreviousAnalysis(projectSnapshot); assertThat(pastSnapshot.isRelatedToSnapshot(), is(false)); assertThat(pastSnapshot.getProjectSnapshot(), nullValue()); - assertThat(pastSnapshot.getTargetDate(), nullValue()); + assertThat(pastSnapshot.getDate(), nullValue()); } } -- 2.39.5