From: simonbrandhof Date: Fri, 29 Apr 2011 06:19:00 +0000 (+0200) Subject: Fix bug of violation tracking. NPE when past violation has no checksum. X-Git-Tag: 2.8~77 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=086a9db53ed24304cb2652032ebc7305693f3625;p=sonarqube.git Fix bug of violation tracking. NPE when past violation has no checksum. --- 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 { @@ -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 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; }