]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4564 SONAR-4563 fix detection of new issues since previous analysis
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 8 Aug 2013 12:05:22 +0000 (14:05 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 8 Aug 2013 12:05:22 +0000 (14:05 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java
sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssuesTest.java
sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java
sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml
sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java
sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java

index 17ca21170d6d1ec9d422c465c6a5cd7fd8684a4c..adb4ebb59172188cfe39a3542257b1e9cd31471b 100644 (file)
@@ -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) {
index 44318f4f387b07085c96417367c9ac33e1f57123..1320ec45988f2d4091951ab795fbd54b917607d5 100644 (file)
@@ -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
index a2dec38d20b8f589e7635c300f6579d57804fefe..f67ad6713084c8ccbafb247ca86a7676c1ef516d 100644 (file)
@@ -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<DefaultIssue> 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<DefaultIssue> 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
index 3c5a80c53a0c74c14f254605839a6e1af17a9fff..67dd24c8f978cea58658e724321d09ce30e1b3e9 100644 (file)
@@ -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);
index 90ebe53d240601a8df5ec89d55d11860d1d58b6c..8e65de6406d9b280cee8e743759bbcf58cb100ff 100644 (file)
         </foreach>
       </if>
       <if test="query.createdAfter() != null">
-        and i.issue_creation_date &gt;= #{query.createdAfter}
+        and i.issue_creation_date &gt; #{query.createdAfter}
       </if>
       <if test="query.createdBefore() != null">
         and i.issue_creation_date &lt; #{query.createdBefore}
index 1e6a16b25296ef328bdcf796370b9a94393dd972..91ffce8461d83019d06de14b736cefb998c3bd7d 100644 (file)
@@ -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();
   }
 
index da014a9c532889d6216938a4e3727d4d38d3df9a..da84dee6ab9f717b4c0efd5f921da23828299bfc 100644 (file)
@@ -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));
   }
 
 
index 739ec33991890dbf0821af8ff1685ee3526e835a..b739523a0d70bc8f2775f7d0e972df0492213e3d 100644 (file)
@@ -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;
   }