diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-19 00:04:01 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-19 00:09:44 +0100 |
commit | 91cfedbfd44100916f813231c72d0b4202bef4ef (patch) | |
tree | 9535836350d4427e2b5439cc083767420f1ecd1c /plugins | |
parent | 16fcb63b9884486db7b97f0aa0e9d0d00cb29266 (diff) | |
download | sonarqube-91cfedbfd44100916f813231c72d0b4202bef4ef.tar.gz sonarqube-91cfedbfd44100916f813231c72d0b4202bef4ef.zip |
SONAR-3317 download last analysis on dry run
Diffstat (limited to 'plugins')
6 files changed, 83 insertions, 210 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index c9c65815691..2e42100b525 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -78,7 +78,6 @@ import org.sonar.plugins.core.timemachine.NewCoverageFileAnalyzer; import org.sonar.plugins.core.timemachine.NewItCoverageFileAnalyzer; import org.sonar.plugins.core.timemachine.NewOverallCoverageFileAnalyzer; import org.sonar.plugins.core.timemachine.NewViolationsDecorator; -import org.sonar.plugins.core.timemachine.ReferenceAnalysis; import org.sonar.plugins.core.timemachine.TendencyDecorator; import org.sonar.plugins.core.timemachine.TimeMachineConfigurationPersister; import org.sonar.plugins.core.timemachine.VariationDecorator; @@ -488,7 +487,6 @@ public final class CorePlugin extends SonarPlugin { FilesDecorator.class, ReviewNotifications.class, ReviewWorkflowDecorator.class, - ReferenceAnalysis.class, ManualMeasureDecorator.class, ManualViolationInjector.class, ViolationSeverityUpdater.class, diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ReferenceAnalysis.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ReferenceAnalysis.java deleted file mode 100644 index c5a44fd82b0..00000000000 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ReferenceAnalysis.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.plugins.core.timemachine; - -import org.sonar.api.BatchExtension; -import org.sonar.api.database.DatabaseSession; -import org.sonar.api.database.model.ResourceModel; -import org.sonar.api.database.model.RuleFailureModel; -import org.sonar.api.database.model.Snapshot; -import org.sonar.api.database.model.SnapshotSource; -import org.sonar.api.resources.Resource; - -import javax.persistence.Query; - -import java.util.Collections; -import java.util.List; - -public class ReferenceAnalysis implements BatchExtension { - - private DatabaseSession session; - - public ReferenceAnalysis(DatabaseSession session) { - this.session = session; - } - - public List<RuleFailureModel> getViolations(Resource resource) { - Snapshot snapshot = getSnapshot(resource); - if (snapshot != null) { - return session.getResults(RuleFailureModel.class, "snapshotId", snapshot.getId()); - } - return Collections.emptyList(); - } - - public String getSource(Resource resource) { - Snapshot snapshot = getSnapshot(resource); - if (snapshot != null) { - SnapshotSource source = session.getSingleResult(SnapshotSource.class, "snapshotId", snapshot.getId()); - if (source != null) { - return source.getData(); - } - } - return ""; - } - - private Snapshot getSnapshot(Resource resource) { - Query query = session.createQuery("from " + Snapshot.class.getSimpleName() + " s where s.last=:last and s.resourceId=(select r.id from " - + ResourceModel.class.getSimpleName() + " r where r.key=:key)"); - query.setParameter("key", resource.getEffectiveKey()); - query.setParameter("last", Boolean.TRUE); - return session.getSingleResult(query, null); - } -} 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 e2e52396996..9cd1e10a075 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 @@ -39,6 +39,7 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.rules.Violation; import org.sonar.api.violations.ViolationQuery; +import org.sonar.batch.scan.LastSnapshots; import org.sonar.plugins.core.timemachine.tracking.HashedSequence; import org.sonar.plugins.core.timemachine.tracking.HashedSequenceComparator; import org.sonar.plugins.core.timemachine.tracking.RollingHashSequence; @@ -46,6 +47,8 @@ import org.sonar.plugins.core.timemachine.tracking.RollingHashSequenceComparator import org.sonar.plugins.core.timemachine.tracking.StringText; import org.sonar.plugins.core.timemachine.tracking.StringTextComparator; +import javax.annotation.Nullable; + import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -56,7 +59,7 @@ import java.util.Set; @DependsUpon({DecoratorBarriers.END_OF_VIOLATIONS_GENERATION, DecoratorBarriers.START_VIOLATION_TRACKING}) @DependedUpon(DecoratorBarriers.END_OF_VIOLATION_TRACKING) public class ViolationTrackingDecorator implements Decorator { - private ReferenceAnalysis referenceAnalysis; + private LastSnapshots lastSnapshots; private Map<Violation, RuleFailureModel> referenceViolationsMap = Maps.newIdentityHashMap(); private SonarIndex index; private Project project; @@ -64,10 +67,10 @@ public class ViolationTrackingDecorator implements Decorator { /** * Live collection of unmapped past violations. */ - private Set<RuleFailureModel> unmappedPastViolations = Sets.newHashSet(); + private Set<RuleFailureModel> unmappedLastViolations = Sets.newHashSet(); - public ViolationTrackingDecorator(Project project, ReferenceAnalysis referenceAnalysis, SonarIndex index) { - this.referenceAnalysis = referenceAnalysis; + public ViolationTrackingDecorator(Project project, LastSnapshots lastSnapshots, SonarIndex index) { + this.lastSnapshots = lastSnapshots; this.index = index; this.project = project; } @@ -89,8 +92,8 @@ public class ViolationTrackingDecorator implements Decorator { // Load new violations List<Violation> newViolations = prepareNewViolations(context, source); - // Load reference violations - List<RuleFailureModel> referenceViolations = referenceAnalysis.getViolations(resource); + // Load the violations of the last available analysis + List<RuleFailureModel> referenceViolations = lastSnapshots.getViolations(resource); // Map new violations with old ones mapViolations(newViolations, referenceViolations, source, resource); @@ -111,39 +114,45 @@ public class ViolationTrackingDecorator implements Decorator { } @VisibleForTesting - Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, List<RuleFailureModel> pastViolations) { - return mapViolations(newViolations, pastViolations, null, null); + Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, @Nullable List<RuleFailureModel> lastViolations) { + return mapViolations(newViolations, lastViolations, null, null); } @VisibleForTesting - Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, List<RuleFailureModel> pastViolations, String source, Resource resource) { - unmappedPastViolations.addAll(pastViolations); - - Multimap<Integer, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create(); - for (RuleFailureModel pastViolation : pastViolations) { - pastViolationsByRule.put(pastViolation.getRuleId(), pastViolation); - } - - // Match the permanent id of the violation. This id is for example set explicitly when injecting manual violations - for (Violation newViolation : newViolations) { - mapViolation(newViolation, - findPastViolationWithSamePermanentId(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())), - pastViolationsByRule, referenceViolationsMap); - } + Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, @Nullable List<RuleFailureModel> lastViolations, + @Nullable String source, @Nullable Resource resource) { + boolean hasLastScan = false; + Multimap<Integer, RuleFailureModel> lastViolationsByRule = LinkedHashMultimap.create(); + + if (lastViolations != null) { + hasLastScan = true; + unmappedLastViolations.addAll(lastViolations); + + for (RuleFailureModel lastViolation : lastViolations) { + lastViolationsByRule.put(lastViolation.getRuleId(), lastViolation); + } - // Try first to match violations on same rule with same line and with same checksum (but not necessarily with same message) - for (Violation newViolation : newViolations) { - if (isNotAlreadyMapped(newViolation)) { + // Match the permanent id of the violation. This id is for example set explicitly when injecting manual violations + for (Violation newViolation : newViolations) { mapViolation(newViolation, - findPastViolationWithSameLineAndChecksum(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())), - pastViolationsByRule, referenceViolationsMap); + findLastViolationWithSamePermanentId(newViolation, lastViolationsByRule.get(newViolation.getRule().getId())), + lastViolationsByRule, referenceViolationsMap); + } + + // Try first to match violations on same rule with same line and with same checksum (but not necessarily with same message) + for (Violation newViolation : newViolations) { + if (isNotAlreadyMapped(newViolation)) { + mapViolation(newViolation, + findLastViolationWithSameLineAndChecksum(newViolation, lastViolationsByRule.get(newViolation.getRule().getId())), + lastViolationsByRule, referenceViolationsMap); + } } } // If each new violation matches an old one we can stop the matching mechanism if (referenceViolationsMap.size() != newViolations.size()) { - if (source != null && resource != null) { - String referenceSource = referenceAnalysis.getSource(resource); + if (source != null && resource != null && hasLastScan) { + String referenceSource = lastSnapshots.getSource(resource); if (referenceSource != null) { HashedSequence<StringText> hashedReference = HashedSequence.wrap(new StringText(referenceSource), StringTextComparator.IGNORE_WHITESPACE); HashedSequence<StringText> hashedSource = HashedSequence.wrap(new StringText(source), StringTextComparator.IGNORE_WHITESPACE); @@ -152,7 +161,7 @@ public class ViolationTrackingDecorator implements Decorator { ViolationTrackingBlocksRecognizer rec = new ViolationTrackingBlocksRecognizer(hashedReference, hashedSource, hashedComparator); Multimap<Integer, Violation> newViolationsByLines = newViolationsByLines(newViolations, rec); - Multimap<Integer, RuleFailureModel> pastViolationsByLines = pastViolationsByLines(unmappedPastViolations, rec); + Multimap<Integer, RuleFailureModel> lastViolationsByLines = lastViolationsByLines(unmappedLastViolations, rec); RollingHashSequence<HashedSequence<StringText>> a = RollingHashSequence.wrap(hashedReference, hashedComparator, 5); RollingHashSequence<HashedSequence<StringText>> b = RollingHashSequence.wrap(hashedSource, hashedComparator, 5); @@ -160,7 +169,7 @@ public class ViolationTrackingDecorator implements Decorator { Map<Integer, HashOccurrence> map = Maps.newHashMap(); - for (Integer line : pastViolationsByLines.keySet()) { + for (Integer line : lastViolationsByLines.keySet()) { int hash = cmp.hash(a, line - 1); HashOccurrence hashOccurrence = map.get(hash); if (hashOccurrence == null) { @@ -186,16 +195,16 @@ public class ViolationTrackingDecorator implements Decorator { for (HashOccurrence hashOccurrence : map.values()) { if (hashOccurrence.countA == 1 && hashOccurrence.countB == 1) { // Guaranteed that lineA has been moved to lineB, so we can map all violations on lineA to all violations on lineB - map(newViolationsByLines.get(hashOccurrence.lineB), pastViolationsByLines.get(hashOccurrence.lineA), pastViolationsByRule); - pastViolationsByLines.removeAll(hashOccurrence.lineA); + map(newViolationsByLines.get(hashOccurrence.lineB), lastViolationsByLines.get(hashOccurrence.lineA), lastViolationsByRule); + lastViolationsByLines.removeAll(hashOccurrence.lineA); newViolationsByLines.removeAll(hashOccurrence.lineB); } } // Check if remaining number of lines exceeds threshold - if (pastViolationsByLines.keySet().size() * newViolationsByLines.keySet().size() < 250000) { + if (lastViolationsByLines.keySet().size() * newViolationsByLines.keySet().size() < 250000) { List<LinePair> possibleLinePairs = Lists.newArrayList(); - for (Integer oldLine : pastViolationsByLines.keySet()) { + for (Integer oldLine : lastViolationsByLines.keySet()) { for (Integer newLine : newViolationsByLines.keySet()) { int weight = rec.computeLengthOfMaximalBlock(oldLine - 1, newLine - 1); possibleLinePairs.add(new LinePair(oldLine, newLine, weight)); @@ -204,7 +213,7 @@ public class ViolationTrackingDecorator implements Decorator { Collections.sort(possibleLinePairs, LINE_PAIR_COMPARATOR); for (LinePair linePair : possibleLinePairs) { // High probability that lineA has been moved to lineB, so we can map all violations on lineA to all violations on lineB - map(newViolationsByLines.get(linePair.lineB), pastViolationsByLines.get(linePair.lineA), pastViolationsByRule); + map(newViolationsByLines.get(linePair.lineB), lastViolationsByLines.get(linePair.lineA), lastViolationsByRule); } } } @@ -214,8 +223,8 @@ public class ViolationTrackingDecorator implements Decorator { for (Violation newViolation : newViolations) { if (isNotAlreadyMapped(newViolation)) { mapViolation(newViolation, - findPastViolationWithSameChecksumAndMessage(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())), - pastViolationsByRule, referenceViolationsMap); + findLastViolationWithSameChecksumAndMessage(newViolation, lastViolationsByRule.get(newViolation.getRule().getId())), + lastViolationsByRule, referenceViolationsMap); } } @@ -223,8 +232,8 @@ public class ViolationTrackingDecorator implements Decorator { for (Violation newViolation : newViolations) { if (isNotAlreadyMapped(newViolation)) { mapViolation(newViolation, - findPastViolationWithSameLineAndMessage(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())), - pastViolationsByRule, referenceViolationsMap); + findLastViolationWithSameLineAndMessage(newViolation, lastViolationsByRule.get(newViolation.getRule().getId())), + lastViolationsByRule, referenceViolationsMap); } } @@ -233,23 +242,22 @@ public class ViolationTrackingDecorator implements Decorator { for (Violation newViolation : newViolations) { if (isNotAlreadyMapped(newViolation)) { mapViolation(newViolation, - findPastViolationWithSameChecksum(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())), - pastViolationsByRule, referenceViolationsMap); + findLastViolationWithSameChecksum(newViolation, lastViolationsByRule.get(newViolation.getRule().getId())), + lastViolationsByRule, referenceViolationsMap); } } } - unmappedPastViolations.clear(); - + unmappedLastViolations.clear(); return referenceViolationsMap; } - private void map(Collection<Violation> newViolations, Collection<RuleFailureModel> pastViolations, Multimap<Integer, RuleFailureModel> pastViolationsByRule) { + private void map(Collection<Violation> newViolations, Collection<RuleFailureModel> lastViolations, Multimap<Integer, RuleFailureModel> lastViolationsByRule) { for (Violation newViolation : newViolations) { if (isNotAlreadyMapped(newViolation)) { - for (RuleFailureModel pastViolation : pastViolations) { + for (RuleFailureModel pastViolation : lastViolations) { if (isNotAlreadyMapped(pastViolation) && Objects.equal(newViolation.getRule().getId(), pastViolation.getRuleId())) { - mapViolation(newViolation, pastViolation, pastViolationsByRule, referenceViolationsMap); + mapViolation(newViolation, pastViolation, lastViolationsByRule, referenceViolationsMap); break; } } @@ -269,14 +277,14 @@ public class ViolationTrackingDecorator implements Decorator { return newViolationsByLines; } - private Multimap<Integer, RuleFailureModel> pastViolationsByLines(Collection<RuleFailureModel> pastViolations, ViolationTrackingBlocksRecognizer rec) { - Multimap<Integer, RuleFailureModel> pastViolationsByLines = LinkedHashMultimap.create(); - for (RuleFailureModel pastViolation : pastViolations) { + private Multimap<Integer, RuleFailureModel> lastViolationsByLines(Collection<RuleFailureModel> lastViolations, ViolationTrackingBlocksRecognizer rec) { + Multimap<Integer, RuleFailureModel> lastViolationsByLines = LinkedHashMultimap.create(); + for (RuleFailureModel pastViolation : lastViolations) { if (rec.isValidLineInSource(pastViolation.getLine())) { - pastViolationsByLines.put(pastViolation.getLine(), pastViolation); + lastViolationsByLines.put(pastViolation.getLine(), pastViolation); } } - return pastViolationsByLines; + return lastViolationsByLines; } private static final Comparator<LinePair> LINE_PAIR_COMPARATOR = new Comparator<LinePair>() { @@ -305,15 +313,15 @@ public class ViolationTrackingDecorator implements Decorator { } private boolean isNotAlreadyMapped(RuleFailureModel pastViolation) { - return unmappedPastViolations.contains(pastViolation); + return unmappedLastViolations.contains(pastViolation); } private boolean isNotAlreadyMapped(Violation newViolation) { return !referenceViolationsMap.containsKey(newViolation); } - private RuleFailureModel findPastViolationWithSameChecksum(Violation newViolation, Collection<RuleFailureModel> pastViolations) { - for (RuleFailureModel pastViolation : pastViolations) { + private RuleFailureModel findLastViolationWithSameChecksum(Violation newViolation, Collection<RuleFailureModel> lastViolations) { + for (RuleFailureModel pastViolation : lastViolations) { if (isSameChecksum(newViolation, pastViolation)) { return pastViolation; } @@ -321,8 +329,8 @@ public class ViolationTrackingDecorator implements Decorator { return null; } - private RuleFailureModel findPastViolationWithSameLineAndMessage(Violation newViolation, Collection<RuleFailureModel> pastViolations) { - for (RuleFailureModel pastViolation : pastViolations) { + private RuleFailureModel findLastViolationWithSameLineAndMessage(Violation newViolation, Collection<RuleFailureModel> lastViolations) { + for (RuleFailureModel pastViolation : lastViolations) { if (isSameLine(newViolation, pastViolation) && isSameMessage(newViolation, pastViolation)) { return pastViolation; } @@ -330,8 +338,8 @@ public class ViolationTrackingDecorator implements Decorator { return null; } - private RuleFailureModel findPastViolationWithSameChecksumAndMessage(Violation newViolation, Collection<RuleFailureModel> pastViolations) { - for (RuleFailureModel pastViolation : pastViolations) { + private RuleFailureModel findLastViolationWithSameChecksumAndMessage(Violation newViolation, Collection<RuleFailureModel> lastViolations) { + for (RuleFailureModel pastViolation : lastViolations) { if (isSameChecksum(newViolation, pastViolation) && isSameMessage(newViolation, pastViolation)) { return pastViolation; } @@ -339,8 +347,8 @@ public class ViolationTrackingDecorator implements Decorator { return null; } - private RuleFailureModel findPastViolationWithSameLineAndChecksum(Violation newViolation, Collection<RuleFailureModel> pastViolations) { - for (RuleFailureModel pastViolation : pastViolations) { + private RuleFailureModel findLastViolationWithSameLineAndChecksum(Violation newViolation, Collection<RuleFailureModel> lastViolations) { + for (RuleFailureModel pastViolation : lastViolations) { if (isSameLine(newViolation, pastViolation) && isSameChecksum(newViolation, pastViolation)) { return pastViolation; } @@ -348,8 +356,8 @@ public class ViolationTrackingDecorator implements Decorator { return null; } - private RuleFailureModel findPastViolationWithSamePermanentId(Violation newViolation, Collection<RuleFailureModel> pastViolations) { - for (RuleFailureModel pastViolation : pastViolations) { + private RuleFailureModel findLastViolationWithSamePermanentId(Violation newViolation, Collection<RuleFailureModel> lastViolations) { + for (RuleFailureModel pastViolation : lastViolations) { if (isSamePermanentId(newViolation, pastViolation)) { return pastViolation; } @@ -374,16 +382,16 @@ public class ViolationTrackingDecorator implements Decorator { } private void mapViolation(Violation newViolation, RuleFailureModel pastViolation, - Multimap<Integer, RuleFailureModel> pastViolationsByRule, Map<Violation, RuleFailureModel> violationMap) { + Multimap<Integer, RuleFailureModel> lastViolationsByRule, Map<Violation, RuleFailureModel> violationMap) { if (pastViolation != null) { newViolation.setCreatedAt(pastViolation.getCreatedAt()); newViolation.setPermanentId(pastViolation.getPermanentId()); newViolation.setSwitchedOff(pastViolation.isSwitchedOff()); newViolation.setPersonId(pastViolation.getPersonId()); newViolation.setNew(false); - pastViolationsByRule.remove(newViolation.getRule().getId(), pastViolation); + lastViolationsByRule.remove(newViolation.getRule().getId(), pastViolation); violationMap.put(newViolation, pastViolation); - unmappedPastViolations.remove(pastViolation); + unmappedLastViolations.remove(pastViolation); } else { newViolation.setNew(true); newViolation.setCreatedAt(project.getAnalysisDate()); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ReferenceAnalysisTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ReferenceAnalysisTest.java deleted file mode 100644 index 14b6922ca4b..00000000000 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ReferenceAnalysisTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.plugins.core.timemachine; - -import org.junit.Test; -import org.sonar.api.resources.JavaFile; -import org.sonar.api.resources.Resource; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -public class ReferenceAnalysisTest extends AbstractDbUnitTestCase { - - @Test - public void test() { - setupData("shared"); - - ReferenceAnalysis referenceAnalysis = new ReferenceAnalysis(getSession()); - - Resource resource = new JavaFile(""); - - resource.setEffectiveKey("project:org.foo.Bar"); - assertThat(referenceAnalysis.getViolations(resource).size(), is(1)); - assertThat(referenceAnalysis.getSource(resource), is("this is the file content")); - - resource.setEffectiveKey("project:no-such-resource"); - assertThat(referenceAnalysis.getViolations(resource).size(), is(0)); - assertThat(referenceAnalysis.getSource(resource), is("")); - } - -} 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 a84f402595b..617287da75e 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 @@ -28,6 +28,7 @@ import org.sonar.api.resources.Project; import org.sonar.api.rules.Rule; import org.sonar.api.rules.Violation; import org.sonar.api.utils.DateUtils; +import org.sonar.batch.scan.LastSnapshots; import java.io.IOException; import java.util.Arrays; @@ -47,19 +48,19 @@ public class ViolationTrackingTest { private ViolationTrackingDecorator decorator; private Project project; - private ReferenceAnalysis referenceAnalysis; + private LastSnapshots lastSnapshots; @Before public void setUp() { project = mock(Project.class); when(project.getAnalysisDate()).thenReturn(analysisDate); - referenceAnalysis = mock(ReferenceAnalysis.class); - decorator = new ViolationTrackingDecorator(project, referenceAnalysis, null); + lastSnapshots = mock(LastSnapshots.class); + decorator = new ViolationTrackingDecorator(project, lastSnapshots, null); } @Test public void pastViolationNotAssiciatedWithLineShouldNotCauseNPE() throws Exception { - when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + when(lastSnapshots.getSource(project)).thenReturn(load("example2-v1")); String source = load("example2-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("2 branches need to be covered", null, 50); @@ -78,7 +79,7 @@ public class ViolationTrackingTest { @Test public void newViolationNotAssiciatedWithLineShouldNotCauseNPE() throws Exception { - when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + when(lastSnapshots.getSource(project)).thenReturn(load("example2-v1")); String source = load("example2-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("Indentation", 7, 50); @@ -100,7 +101,7 @@ public class ViolationTrackingTest { */ @Test public void violationNotAssociatedWithLine() throws Exception { - when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + when(lastSnapshots.getSource(project)).thenReturn(load("example2-v1")); String source = load("example2-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("2 branches need to be covered", null, 50); @@ -121,7 +122,7 @@ public class ViolationTrackingTest { */ @Test public void example1() throws Exception { - when(referenceAnalysis.getSource(project)).thenReturn(load("example1-v1")); + when(lastSnapshots.getSource(project)).thenReturn(load("example1-v1")); String source = load("example1-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("Indentation", 7, 50); @@ -150,7 +151,7 @@ public class ViolationTrackingTest { */ @Test public void example2() throws Exception { - when(referenceAnalysis.getSource(project)).thenReturn(load("example2-v1")); + when(lastSnapshots.getSource(project)).thenReturn(load("example2-v1")); String source = load("example2-v2"); RuleFailureModel referenceViolation1 = newReferenceViolation("SystemPrintln", 5, 50); diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/ReferenceAnalysisTest/shared.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/ReferenceAnalysisTest/shared.xml deleted file mode 100644 index cca1757cb78..00000000000 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/ReferenceAnalysisTest/shared.xml +++ /dev/null @@ -1,16 +0,0 @@ -<dataset> - - <projects id="200" scope="FIL" qualifier="CLA" kee="project:org.foo.Bar" root_id="[null]" - name="Bar" long_name="org.foo.Bar" description="[null]" - enabled="true" language="java" copy_resource_id="[null]" person_id="[null]" /> - - <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="1000" project_id="200" parent_snapshot_id="[null]" root_project_id="100" root_snapshot_id="[null]" - scope="FIL" qualifier="CLA" created_at="2008-11-01 13:58:00.00" build_date="2008-11-01 13:58:00.00" version="[null]" path="" - status="P" islast="true" depth="3" /> - - <rule_failures switched_off="false" permanent_id="1" ID="1" SNAPSHOT_ID="1000" RULE_ID="30" FAILURE_LEVEL="3" MESSAGE="old message" LINE="10" COST="[null]" - created_at="2008-11-01 13:58:00.00" checksum="[null]" person_id="[null]"/> - - <snapshot_sources ID="1" SNAPSHOT_ID="1000" DATA="this is the file content"/> - -</dataset> |