diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-04-17 13:52:28 +0600 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-04-17 13:59:20 +0600 |
commit | a3db19c89bba0ba750cebec4847f25ac53e50c49 (patch) | |
tree | d9bed8826cabeba665c3f8970af47ee0097fdf6c /plugins/sonar-core-plugin | |
parent | 260fc085cf84e78a860c3c843de55d50c05b78c1 (diff) | |
download | sonarqube-a3db19c89bba0ba750cebec4847f25ac53e50c49.tar.gz sonarqube-a3db19c89bba0ba750cebec4847f25ac53e50c49.zip |
Fix possible NPE during tracking of violations
Diffstat (limited to 'plugins/sonar-core-plugin')
2 files changed, 45 insertions, 3 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java index 9c3fa3aab7d..ee92393da35 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java @@ -124,9 +124,13 @@ public class ViolationTrackingDecorator implements Decorator { List<ViolationPair> possiblePairs = Lists.newArrayList(); for (Violation newViolation : newViolations) { - for (RuleFailureModel pastViolation : pastViolationsByRule.get(newViolation.getRule().getId())) { - int weight = rec.computeLengthOfMaximalBlock(pastViolation.getLine() - 1, newViolation.getLineId() - 1); - possiblePairs.add(new ViolationPair(pastViolation, newViolation, weight)); + if (newViolation.getLineId() != null) { + for (RuleFailureModel pastViolation : pastViolationsByRule.get(newViolation.getRule().getId())) { + if (pastViolation.getLine() != null) { + int weight = rec.computeLengthOfMaximalBlock(pastViolation.getLine() - 1, newViolation.getLineId() - 1); + possiblePairs.add(new ViolationPair(pastViolation, newViolation, weight)); + } + } } } Collections.sort(possiblePairs, ViolationPair.COMPARATOR); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingTest.java index 74dd8c66b9b..a84f402595b 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingTest.java @@ -57,6 +57,44 @@ public class ViolationTrackingTest { decorator = new ViolationTrackingDecorator(project, referenceAnalysis, null); } + @Test + public void pastViolationNotAssiciatedWithLineShouldNotCauseNPE() throws Exception { + when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + String source = load("example2-v2"); + + RuleFailureModel referenceViolation1 = newReferenceViolation("2 branches need to be covered", null, 50); + + Violation newViolation1 = newViolation("Indentation", 9, 50); + newViolation1.setChecksum("foo"); + + Map<Violation, RuleFailureModel> mapping = decorator.mapViolations( + Arrays.asList(newViolation1), + Arrays.asList(referenceViolation1), + source, project); + + assertThat(mapping.isEmpty(), is(true)); + assertThat(newViolation1.isNew(), is(true)); + } + + @Test + public void newViolationNotAssiciatedWithLineShouldNotCauseNPE() throws Exception { + when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + String source = load("example2-v2"); + + RuleFailureModel referenceViolation1 = newReferenceViolation("Indentation", 7, 50); + + Violation newViolation1 = newViolation("1 branch need to be covered", null, 50); + newViolation1.setChecksum("foo"); + + Map<Violation, RuleFailureModel> mapping = decorator.mapViolations( + Arrays.asList(newViolation1), + Arrays.asList(referenceViolation1), + source, project); + + assertThat(mapping.isEmpty(), is(true)); + assertThat(newViolation1.isNew(), is(true)); + } + /** * SONAR-2928 */ |