]> source.dussan.org Git - sonarqube.git/commitdiff
Fix bug of violation tracking. NPE when past violation has no checksum.
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 29 Apr 2011 06:19:00 +0000 (08:19 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 29 Apr 2011 06:19:15 +0000 (08:19 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java

index 2d0c0fb826437dc320a08428fb23955a0c4bf9fd..4ad673c0bc53acb5851e5ac662480085773821b4 100644 (file)
@@ -156,15 +156,15 @@ public class ViolationPersisterDecorator implements Decorator {
     return null;
   }
 
-  private final boolean isSameChecksum(Violation newViolation, RuleFailureModel pastViolation) {
-    return pastViolation.getChecksum().equals(getChecksumForLine(checksums, newViolation.getLineId()));
+  private boolean isSameChecksum(Violation newViolation, RuleFailureModel pastViolation) {
+    return pastViolation.getChecksum()!=null && StringUtils.equals(pastViolation.getChecksum(), getChecksumForLine(checksums, newViolation.getLineId()));
   }
 
-  private final boolean isSameLine(Violation newViolation, RuleFailureModel pastViolation) {
+  private boolean isSameLine(Violation newViolation, RuleFailureModel pastViolation) {
     return pastViolation.getLine() == newViolation.getLineId();
   }
 
-  private final boolean isSameMessage(Violation newViolation, RuleFailureModel pastViolation) {
+  private boolean isSameMessage(Violation newViolation, RuleFailureModel pastViolation) {
     return StringUtils.equals(RuleFailureModel.abbreviateMessage(newViolation.getMessage()), pastViolation.getMessage());
   }
 
index f0c88e7b52fd2992eb1205c41e644541154ed635..51c83a460ebff1ac75f245cf40ffa891357e357e 100644 (file)
  */
 package org.sonar.plugins.core.timemachine;
 
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.not;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
-
-import java.util.List;
-import java.util.Map;
-
+import com.google.common.collect.Lists;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.database.model.RuleFailureModel;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.Violation;
 
-import com.google.common.collect.Lists;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.assertThat;
 
 public class ViolationPersisterDecoratorTest {
 
@@ -92,6 +88,15 @@ public class ViolationPersisterDecoratorTest {
     assertThat(mapping.get(newViolation), equalTo(pastViolation));
   }
 
+  @Test
+  public void pastMeasureHasNoChecksum() {
+    Violation newViolation = newViolation("message", 1, 50, "checksum1");
+    RuleFailureModel pastViolation = newPastViolation("message", 1, 51, null);
+
+    Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
+    assertThat(mapping.get(newViolation), is(nullValue()));
+  }
+
   @Test
   public void sameRuleAndMessageAndChecksumButDifferentLine() {
     Violation newViolation = newViolation("message", 1, 50, "checksum1");
@@ -133,8 +138,7 @@ public class ViolationPersisterDecoratorTest {
   private Violation newViolation(String message, int lineId, int ruleId) {
     Rule rule = Rule.create().setKey("rule");
     rule.setId(ruleId);
-    Violation violation = Violation.create(rule, null).setLineId(lineId).setMessage(message);
-    return violation;
+    return Violation.create(rule, null).setLineId(lineId).setMessage(message);
   }
 
   private Violation newViolation(String message, int lineId, int ruleId, String lineChecksum) {
@@ -160,7 +164,9 @@ public class ViolationPersisterDecoratorTest {
 
   private RuleFailureModel newPastViolation(String message, int lineId, int ruleId, String lineChecksum) {
     RuleFailureModel pastViolation = newPastViolation(message, lineId, ruleId);
-    pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum(lineChecksum));
+    if (lineChecksum != null) {
+      pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum(lineChecksum));
+    }
     return pastViolation;
   }