aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-04-29 08:19:00 +0200
committersimonbrandhof <simon.brandhof@gmail.com>2011-04-29 08:19:15 +0200
commit086a9db53ed24304cb2652032ebc7305693f3625 (patch)
treeba643c37297be918cb57b458d5c2cd700f7c2970
parentf33d88a2158097053b9451905efb1cefdba8b180 (diff)
downloadsonarqube-086a9db53ed24304cb2652032ebc7305693f3625.tar.gz
sonarqube-086a9db53ed24304cb2652032ebc7305693f3625.zip
Fix bug of violation tracking. NPE when past violation has no checksum.
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java8
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java32
2 files changed, 23 insertions, 17 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java
index 2d0c0fb8264..4ad673c0bc5 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java
@@ -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());
}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java
index f0c88e7b52f..51c83a460eb 100644
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java
@@ -19,22 +19,18 @@
*/
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 {
@@ -93,6 +89,15 @@ public class ViolationPersisterDecoratorTest {
}
@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");
RuleFailureModel pastViolation = newPastViolation("message", 2, 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;
}