From: Simon Brandhof Date: Thu, 8 Aug 2013 12:05:22 +0000 (+0200) Subject: SONAR-4564 SONAR-4563 fix detection of new issues since previous analysis X-Git-Tag: 3.7~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2aa3b8e1cf5259429f69322e5f02f27f0ecfeec4;p=sonarqube.git SONAR-4564 SONAR-4563 fix detection of new issues since previous analysis --- 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 17ca21170d6..adb4ebb5917 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 @@ -44,7 +44,7 @@ import java.util.*; * * @since 3.6 */ -@DependsUpon(DecoratorBarriers.END_OF_VIOLATION_TRACKING) +@DependsUpon(DecoratorBarriers.ISSUES_TRACKED) public class CountUnresolvedIssuesDecorator implements Decorator { private final ResourcePerspectives perspectives; @@ -270,7 +270,7 @@ public class CountUnresolvedIssuesDecorator implements Decorator { } private boolean isAfter(Issue issue, @Nullable Date date) { - return date == null || (issue.creationDate() != null && issue.creationDate().after(date)); + return date == null || (issue.creationDate() != null && DateUtils.truncatedCompareTo(issue.creationDate(), date, Calendar.SECOND)>0); } private boolean shouldSaveNewMetrics(DecoratorContext context) { 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 44318f4f387..1320ec45988 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 @@ -44,10 +44,7 @@ import org.sonar.api.test.IsRuleMeasure; import org.sonar.batch.components.PastSnapshot; import org.sonar.batch.components.TimeMachineConfiguration; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.List; +import java.util.*; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; @@ -68,6 +65,7 @@ public class CountUnresolvedIssuesDecoratorTest { Date rightNow; Date tenDaysAgo; Date fiveDaysAgo; + Date sameSecond; @Before public void before() { @@ -83,6 +81,7 @@ public class CountUnresolvedIssuesDecoratorTest { rightNow = new Date(); tenDaysAgo = DateUtils.addDays(rightNow, -10); fiveDaysAgo = DateUtils.addDays(rightNow, -5); + sameSecond = DateUtils.truncate(rightNow, Calendar.SECOND); PastSnapshot pastSnapshot = mock(PastSnapshot.class); when(pastSnapshot.getIndex()).thenReturn(1); @@ -226,6 +225,7 @@ public class CountUnresolvedIssuesDecoratorTest { assertThat(decorator.countIssuesAfterDate(null, fiveDaysAgo)).isEqualTo(0); assertThat(decorator.countIssuesAfterDate(issues, fiveDaysAgo)).isEqualTo(1); // 1 rightNow assertThat(decorator.countIssuesAfterDate(issues, tenDaysAgo)).isEqualTo(3); // 1 rightNow + 2 fiveDaysAgo + assertThat(decorator.countIssuesAfterDate(issues, sameSecond)).isEqualTo(0); // 0 } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssuesTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssuesTest.java index a2dec38d20b..f67ad671308 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssuesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssuesTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.issue; +import org.apache.commons.lang.time.DateUtils; +import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.sonar.api.issue.internal.DefaultIssue; @@ -33,6 +35,7 @@ import org.sonar.api.rules.Rule; import org.sonar.api.rules.RulePriority; import org.sonar.api.rules.Violation; +import java.util.Calendar; import java.util.Date; import static org.fest.assertions.Assertions.assertThat; @@ -46,6 +49,10 @@ public class ScanIssuesTest { IssueFilters filters = mock(IssueFilters.class); ScanIssues scanIssues = new ScanIssues(qProfile, cache, project, filters); + @Before + public void setUp() { + when(project.getAnalysisDate()).thenReturn(new Date()); + } @Test public void should_ignore_null_active_rule() throws Exception { when(qProfile.getActiveRule(anyString(), anyString())).thenReturn(null); @@ -93,7 +100,7 @@ public class ScanIssuesTest { ArgumentCaptor argument = ArgumentCaptor.forClass(DefaultIssue.class); verify(cache).put(argument.capture()); assertThat(argument.getValue().severity()).isEqualTo(Severity.CRITICAL); - assertThat(argument.getValue().creationDate()).isEqualTo(analysisDate); + assertThat(argument.getValue().creationDate()).isEqualTo(DateUtils.truncate(analysisDate, Calendar.SECOND)); } @Test @@ -114,7 +121,7 @@ public class ScanIssuesTest { ArgumentCaptor argument = ArgumentCaptor.forClass(DefaultIssue.class); verify(cache).put(argument.capture()); assertThat(argument.getValue().severity()).isEqualTo(Severity.INFO); - assertThat(argument.getValue().creationDate()).isEqualTo(analysisDate); + assertThat(argument.getValue().creationDate()).isEqualTo(DateUtils.truncate(analysisDate, Calendar.SECOND)); } @Test diff --git a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java index 3c5a80c53a0..67dd24c8f97 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java @@ -21,6 +21,7 @@ package org.sonar.core.issue; import com.google.common.base.Objects; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.time.DateUtils; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; import org.sonar.api.issue.internal.DefaultIssue; @@ -29,6 +30,7 @@ import org.sonar.api.issue.internal.IssueChangeContext; import javax.annotation.Nullable; +import java.util.Calendar; import java.util.Date; /** @@ -156,7 +158,8 @@ public class IssueUpdater implements BatchComponent, ServerComponent { } public void setCloseDate(DefaultIssue issue, @Nullable Date d, IssueChangeContext context) { - if (!Objects.equal(d, issue.closeDate())) { + Date dateWithoutMilliseconds = (d == null ? null : DateUtils.truncate(d, Calendar.SECOND)); + if (!Objects.equal(dateWithoutMilliseconds, issue.closeDate())) { issue.setCloseDate(d); issue.setUpdateDate(context.date()); issue.setChanged(true); diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 90ebe53d240..8e65de6406d 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -288,7 +288,7 @@ - and i.issue_creation_date >= #{query.createdAfter} + and i.issue_creation_date > #{query.createdAfter} and i.issue_creation_date < #{query.createdBefore} diff --git a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java index 1e6a16b2529..91ffce8461d 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java @@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException; import org.sonar.api.issue.Issue; import org.sonar.api.issue.internal.DefaultIssue; +import java.util.Calendar; import java.util.Date; import static org.fest.assertions.Assertions.assertThat; @@ -92,9 +93,9 @@ public class IssueDtoTest { assertThat(issue.assignee()).isEqualTo("perceval"); assertThat(issue.attribute("key")).isEqualTo("value"); assertThat(issue.authorLogin()).isEqualTo("pierre"); - assertThat(issue.creationDate()).isEqualTo(createdAt); - assertThat(issue.updateDate()).isEqualTo(updatedAt); - assertThat(issue.closeDate()).isEqualTo(closedAt); + assertThat(issue.creationDate()).isEqualTo(DateUtils.truncate(createdAt, Calendar.SECOND)); + assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(updatedAt, Calendar.SECOND)); + assertThat(issue.closeDate()).isEqualTo(DateUtils.truncate(closedAt, Calendar.SECOND)); assertThat(issue.isNew()).isFalse(); } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java b/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java index da014a9c532..da84dee6ab9 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java @@ -21,6 +21,7 @@ package org.sonar.core.issue.workflow; import com.google.common.base.Function; import com.google.common.collect.Collections2; +import org.apache.commons.lang.time.DateUtils; import org.junit.Test; import org.sonar.api.issue.DefaultTransitions; import org.sonar.api.issue.Issue; @@ -31,6 +32,7 @@ import org.sonar.core.issue.IssueUpdater; import javax.annotation.Nullable; +import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.List; @@ -158,7 +160,7 @@ public class IssueWorkflowTest { assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED); assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED); assertThat(issue.closeDate()).isNotNull(); - assertThat(issue.updateDate()).isEqualTo(now); + assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND)); } @Test @@ -176,7 +178,7 @@ public class IssueWorkflowTest { assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED); assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED); assertThat(issue.closeDate()).isNotNull(); - assertThat(issue.updateDate()).isEqualTo(now); + assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND)); } @Test @@ -194,7 +196,7 @@ public class IssueWorkflowTest { assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED); assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED); assertThat(issue.closeDate()).isNotNull(); - assertThat(issue.updateDate()).isEqualTo(now); + assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND)); } @Test @@ -212,7 +214,7 @@ public class IssueWorkflowTest { assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED); assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED); assertThat(issue.closeDate()).isNotNull(); - assertThat(issue.updateDate()).isEqualTo(now); + assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND)); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java index 739ec339918..b739523a0d7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java @@ -29,6 +29,7 @@ import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; +import org.apache.commons.lang.time.DateUtils; import org.sonar.api.issue.Issue; import org.sonar.api.issue.IssueComment; import org.sonar.api.rule.RuleKey; @@ -37,10 +38,7 @@ import org.sonar.api.rule.Severity; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.io.Serializable; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.util.*; /** * PLUGINS MUST NOT BE USED THIS CLASS, EXCEPT FOR UNIT TESTING. @@ -233,7 +231,8 @@ public class DefaultIssue implements Issue { } public DefaultIssue setCreationDate(Date d) { - this.creationDate = d; + // d is not marked as Nullable but we still allow null parameter for unit testing. + this.creationDate = (d != null ? DateUtils.truncate(d, Calendar.SECOND) : null); return this; } @@ -243,7 +242,7 @@ public class DefaultIssue implements Issue { } public DefaultIssue setUpdateDate(@Nullable Date d) { - this.updateDate = d; + this.updateDate = (d != null ? DateUtils.truncate(d, Calendar.SECOND) : null); return this; } @@ -253,7 +252,7 @@ public class DefaultIssue implements Issue { } public DefaultIssue setCloseDate(@Nullable Date d) { - this.closeDate = d; + this.closeDate = (d != null ? DateUtils.truncate(d, Calendar.SECOND) : null); return this; }