diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-04-05 20:42:44 +0600 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-04-05 20:51:21 +0600 |
commit | cc59d04173789942e8c1c1abc704037dcf78db60 (patch) | |
tree | 6f6e3c4ce9daea120f6d43516ced68b3ece29cdf /plugins | |
parent | 19498709fc7672ceaa31a7655489c8022e01f79a (diff) | |
download | sonarqube-cc59d04173789942e8c1c1abc704037dcf78db60.tar.gz sonarqube-cc59d04173789942e8c1c1abc704037dcf78db60.zip |
SONAR-3072 Load sources of previous analysis only when required
Diffstat (limited to 'plugins')
2 files changed, 42 insertions, 46 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 c4fbea8db04..9c3fa3aab7d 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 @@ -30,8 +30,6 @@ import org.sonar.api.resources.Resource; import org.sonar.api.rules.Violation; import org.sonar.api.violations.ViolationQuery; -import javax.annotation.Nullable; - import java.util.*; @DependsUpon({DecoratorBarriers.END_OF_VIOLATIONS_GENERATION, DecoratorBarriers.START_VIOLATION_TRACKING}) @@ -61,7 +59,6 @@ public class ViolationTrackingDecorator implements Decorator { } String source = index.getSource(resource); - String referenceSource = referenceAnalysis.getSource(resource); // Load new violations List<Violation> newViolations = prepareNewViolations(context, source); @@ -69,14 +66,8 @@ public class ViolationTrackingDecorator implements Decorator { // Load reference violations List<RuleFailureModel> referenceViolations = referenceAnalysis.getViolations(resource); - // SONAR-3072 Construct blocks recognizer based on reference source - ViolationTrackingBlocksRecognizer rec = null; - if (source != null && referenceSource != null) { - rec = new ViolationTrackingBlocksRecognizer(referenceSource, source); - } - // Map new violations with old ones - mapViolations(newViolations, referenceViolations, rec); + mapViolations(newViolations, referenceViolations, source, resource); } private List<Violation> prepareNewViolations(DecoratorContext context, String source) { @@ -95,14 +86,11 @@ public class ViolationTrackingDecorator implements Decorator { @VisibleForTesting Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, List<RuleFailureModel> pastViolations) { - return mapViolations(newViolations, pastViolations, null); + return mapViolations(newViolations, pastViolations, null, null); } - /** - * @param rec null, if source code not available - */ @VisibleForTesting - Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, List<RuleFailureModel> pastViolations, @Nullable ViolationTrackingBlocksRecognizer rec) { + Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, List<RuleFailureModel> pastViolations, String source, Resource resource) { Multimap<Integer, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create(); for (RuleFailureModel pastViolation : pastViolations) { pastViolationsByRule.put(pastViolation.getRuleId(), pastViolation); @@ -126,28 +114,33 @@ public class ViolationTrackingDecorator implements Decorator { // If each new violation matches an old one we can stop the matching mechanism if (referenceViolationsMap.size() != newViolations.size()) { - if (rec != null) { - // SONAR-3072 - - 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)); + + // SONAR-3072 + ViolationTrackingBlocksRecognizer rec = null; + if (source != null && resource != null) { + String referenceSource = referenceAnalysis.getSource(resource); + if (referenceSource != null) { + rec = new ViolationTrackingBlocksRecognizer(referenceSource, source); + + 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)); + } } - } - Collections.sort(possiblePairs, ViolationPair.COMPARATOR); - - Set<RuleFailureModel> pp = Sets.newHashSet(pastViolations); - for (ViolationPair pair : possiblePairs) { - Violation newViolation = pair.getNewViolation(); - RuleFailureModel pastViolation = pair.getPastViolation(); - if (isNotAlreadyMapped(newViolation, referenceViolationsMap) && pp.contains(pastViolation)) { - pp.remove(pastViolation); - mapViolation(newViolation, pastViolation, pastViolationsByRule, referenceViolationsMap); + Collections.sort(possiblePairs, ViolationPair.COMPARATOR); + + Set<RuleFailureModel> pp = Sets.newHashSet(pastViolations); + for (ViolationPair pair : possiblePairs) { + Violation newViolation = pair.getNewViolation(); + RuleFailureModel pastViolation = pair.getPastViolation(); + if (isNotAlreadyMapped(newViolation, referenceViolationsMap) && pp.contains(pastViolation)) { + pp.remove(pastViolation); + mapViolation(newViolation, pastViolation, pastViolationsByRule, referenceViolationsMap); + } } } - } // Try then to match violations on same rule with same message and with same checksum 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 d2b4a44c891..74dd8c66b9b 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 @@ -46,11 +46,15 @@ public class ViolationTrackingTest { private ViolationTrackingDecorator decorator; + private Project project; + private ReferenceAnalysis referenceAnalysis; + @Before public void setUp() { - Project project = mock(Project.class); + project = mock(Project.class); when(project.getAnalysisDate()).thenReturn(analysisDate); - decorator = new ViolationTrackingDecorator(project, null, null); + referenceAnalysis = mock(ReferenceAnalysis.class); + decorator = new ViolationTrackingDecorator(project, referenceAnalysis, null); } /** @@ -58,7 +62,8 @@ public class ViolationTrackingTest { */ @Test public void violationNotAssociatedWithLine() throws Exception { - ViolationTrackingBlocksRecognizer rec = newRec("example2"); + when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + String source = load("example2-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("2 branches need to be covered", null, 50); @@ -67,7 +72,7 @@ public class ViolationTrackingTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations( Arrays.asList(newViolation1), Arrays.asList(referenceViolation1), - rec); + source, project); assertThat(newViolation1.isNew(), is(false)); assertThat(mapping.get(newViolation1), equalTo(referenceViolation1)); @@ -78,7 +83,8 @@ public class ViolationTrackingTest { */ @Test public void example1() throws Exception { - ViolationTrackingBlocksRecognizer rec = newRec("example1"); + when(referenceAnalysis.getSource(project)).thenReturn(load("example1-v1")); + String source = load("example1-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("Indentation", 7, 50); RuleFailureModel referenceViolation2 = newReferenceViolation("Indentation", 11, 50); @@ -91,7 +97,7 @@ public class ViolationTrackingTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations( Arrays.asList(newViolation1, newViolation2, newViolation3, newViolation4), Arrays.asList(referenceViolation1, referenceViolation2), - rec); + source, project); assertThat(newViolation1.isNew(), is(true)); assertThat(newViolation2.isNew(), is(true)); @@ -106,7 +112,8 @@ public class ViolationTrackingTest { */ @Test public void example2() throws Exception { - ViolationTrackingBlocksRecognizer rec = newRec("example2"); + when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + String source = load("example2-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("SystemPrintln", 5, 50); @@ -117,7 +124,7 @@ public class ViolationTrackingTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations( Arrays.asList(newViolation1, newViolation2, newViolation3), Arrays.asList(referenceViolation1), - rec); + source, project); assertThat(newViolation1.isNew(), is(true)); assertThat(newViolation2.isNew(), is(false)); @@ -142,10 +149,6 @@ public class ViolationTrackingTest { private int violationId = 0; - private static ViolationTrackingBlocksRecognizer newRec(String name) throws IOException { - return new ViolationTrackingBlocksRecognizer(load(name + "-v1"), load(name + "-v2")); - } - private static String load(String name) throws IOException { return Resources.toString(ViolationTrackingTest.class.getResource("ViolationTrackingTest/" + name + ".txt"), Charsets.UTF_8); } |