From: Julien HENRY Date: Mon, 20 Oct 2014 14:36:46 +0000 (+0200) Subject: SONAR-5765 Restore access to duplication_data measure X-Git-Tag: 4.5.1-RC1~16 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=14bf33fa1c0f4ac920775ee2d2363337fa3769c0;p=sonarqube.git SONAR-5765 Restore access to duplication_data measure --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java index 41527929431..ba4b4d3ad25 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java +++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java @@ -25,6 +25,7 @@ import com.google.common.collect.Lists; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.Event; import org.sonar.api.batch.SonarIndex; +import org.sonar.api.batch.sensor.duplication.DuplicationGroup; import org.sonar.api.design.Dependency; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; @@ -37,10 +38,14 @@ import org.sonar.api.resources.Resource; import org.sonar.api.rules.Violation; import org.sonar.api.utils.SonarException; import org.sonar.api.violations.ViolationQuery; +import org.sonar.batch.duplication.DuplicationCache; +import org.sonar.batch.duplication.DuplicationUtils; import org.sonar.batch.scan.measure.MeasureCache; import org.sonar.core.measure.MeasurementFilters; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Set; @@ -58,17 +63,19 @@ public class DefaultDecoratorContext implements DecoratorContext { private ListMultimap measuresByMetric = ArrayListMultimap.create(); private MeasureCache measureCache; private MetricFinder metricFinder; + private final DuplicationCache duplicationCache; public DefaultDecoratorContext(Resource resource, SonarIndex index, List childrenContexts, - MeasurementFilters measurementFilters, MeasureCache measureCache, MetricFinder metricFinder) { + MeasurementFilters measurementFilters, MeasureCache measureCache, MetricFinder metricFinder, DuplicationCache duplicationCache) { this.sonarIndex = index; this.resource = resource; this.childrenContexts = childrenContexts; this.measurementFilters = measurementFilters; this.measureCache = measureCache; this.metricFinder = metricFinder; + this.duplicationCache = duplicationCache; } public void init() { @@ -105,14 +112,31 @@ public class DefaultDecoratorContext implements DecoratorContext { public M getMeasures(MeasuresFilter filter) { Collection unfiltered; if (filter instanceof MeasuresFilters.MetricFilter) { - // optimization - unfiltered = measuresByMetric.get(((MeasuresFilters.MetricFilter) filter).filterOnMetricKey()); + unfiltered = getMeasuresOfASingleMetric(filter); } else { unfiltered = measuresByMetric.values(); } return filter.filter(unfiltered); } + private Collection getMeasuresOfASingleMetric(MeasuresFilter filter) { + Collection unfiltered; + String metricKey = ((MeasuresFilters.MetricFilter) filter).filterOnMetricKey(); + if (CoreMetrics.DUPLICATIONS_DATA_KEY.equals(metricKey)) { + // Hack for SONAR-5765 + List group = duplicationCache.byComponent(resource.getEffectiveKey()); + if (group != null) { + unfiltered = Arrays.asList(new Measure(CoreMetrics.DUPLICATIONS_DATA, DuplicationUtils.toXml(group))); + } else { + unfiltered = Collections.emptyList(); + } + } else { + // optimization + unfiltered = measuresByMetric.get(metricKey); + } + return unfiltered; + } + public Measure getMeasure(Metric metric) { return getMeasures(MeasuresFilters.metric(metric)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationUtils.java b/sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationUtils.java new file mode 100644 index 00000000000..f99aa02a023 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationUtils.java @@ -0,0 +1,48 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.duplication; + +import org.apache.commons.lang.StringEscapeUtils; +import org.sonar.api.batch.sensor.duplication.DuplicationGroup; + +public class DuplicationUtils { + + public static String toXml(Iterable duplications) { + StringBuilder xml = new StringBuilder(); + xml.append(""); + for (DuplicationGroup duplication : duplications) { + xml.append(""); + toXml(xml, duplication.originBlock()); + for (DuplicationGroup.Block part : duplication.duplicates()) { + toXml(xml, part); + } + xml.append(""); + } + xml.append(""); + return xml.toString(); + } + + private static void toXml(StringBuilder xml, DuplicationGroup.Block part) { + xml.append(""); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DuplicationPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DuplicationPersister.java index 9461d46dfd7..4e5ebce1ef1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DuplicationPersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DuplicationPersister.java @@ -19,7 +19,6 @@ */ package org.sonar.batch.index; -import org.apache.commons.lang.StringEscapeUtils; import org.sonar.api.batch.sensor.duplication.DuplicationGroup; import org.sonar.api.database.model.MeasureMapper; import org.sonar.api.database.model.MeasureModel; @@ -30,6 +29,7 @@ import org.sonar.api.measures.PersistenceMode; import org.sonar.api.resources.Resource; import org.sonar.api.rules.RuleFinder; import org.sonar.batch.duplication.DuplicationCache; +import org.sonar.batch.duplication.DuplicationUtils; import org.sonar.batch.index.Cache.Entry; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; @@ -64,7 +64,7 @@ public final class DuplicationPersister implements ScanPersister { org.sonar.api.measures.Metric duplicationMetricWithId = metricFinder.findByKey(CoreMetrics.DUPLICATIONS_DATA_KEY); for (Entry> entry : duplicationCache.entries()) { String effectiveKey = entry.key()[0].toString(); - Measure measure = new Measure(duplicationMetricWithId, toXml(entry.value())).setPersistenceMode(PersistenceMode.DATABASE); + Measure measure = new Measure(duplicationMetricWithId, DuplicationUtils.toXml(entry.value())).setPersistenceMode(PersistenceMode.DATABASE); Resource resource = resourceCache.get(effectiveKey); if (MeasurePersister.shouldPersistMeasure(resource, measure)) { @@ -82,26 +82,4 @@ public final class DuplicationPersister implements ScanPersister { } } - private static String toXml(Iterable duplications) { - StringBuilder xml = new StringBuilder(); - xml.append(""); - for (DuplicationGroup duplication : duplications) { - xml.append(""); - toXml(xml, duplication.originBlock()); - for (DuplicationGroup.Block part : duplication.duplicates()) { - toXml(xml, part); - } - xml.append(""); - } - xml.append(""); - return xml.toString(); - } - - private static void toXml(StringBuilder xml, DuplicationGroup.Block part) { - xml.append(""); - } - } diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java index c6aa4ac8a79..b867e0fc010 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java @@ -32,6 +32,7 @@ import org.sonar.api.utils.MessageException; import org.sonar.api.utils.SonarException; import org.sonar.batch.DecoratorsSelector; import org.sonar.batch.DefaultDecoratorContext; +import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.events.EventBus; import org.sonar.batch.scan.measure.MeasureCache; import org.sonar.core.measure.MeasurementFilters; @@ -48,11 +49,14 @@ public class DecoratorsExecutor implements BatchComponent { private MeasurementFilters measurementFilters; private MeasureCache measureCache; private MetricFinder metricFinder; + private final DuplicationCache duplicationCache; public DecoratorsExecutor(BatchExtensionDictionnary batchExtDictionnary, - Project project, SonarIndex index, EventBus eventBus, MeasurementFilters measurementFilters, MeasureCache measureCache, MetricFinder metricFinder) { + Project project, SonarIndex index, EventBus eventBus, MeasurementFilters measurementFilters, MeasureCache measureCache, MetricFinder metricFinder, + DuplicationCache duplicationCache) { this.measureCache = measureCache; this.metricFinder = metricFinder; + this.duplicationCache = duplicationCache; this.decoratorsSelector = new DecoratorsSelector(batchExtDictionnary); this.index = index; this.eventBus = eventBus; @@ -75,7 +79,7 @@ public class DecoratorsExecutor implements BatchComponent { childrenContexts.add(childContext.end()); } - DefaultDecoratorContext context = new DefaultDecoratorContext(resource, index, childrenContexts, measurementFilters, measureCache, metricFinder); + DefaultDecoratorContext context = new DefaultDecoratorContext(resource, index, childrenContexts, measurementFilters, measureCache, metricFinder, duplicationCache); context.init(); if (executeDecorators) { for (Decorator decorator : decorators) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java index 12d35f08b4d..3516ab363fb 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java @@ -30,6 +30,7 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.utils.SonarException; import org.sonar.batch.DefaultDecoratorContext; +import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.events.EventBus; import org.sonar.batch.scan.measure.MeasureCache; import org.sonar.core.measure.MeasurementFilters; @@ -66,7 +67,7 @@ public class DecoratorsExecutorTest { doThrow(new SonarException()).when(decorator).decorate(any(Resource.class), any(DecoratorContext.class)); DecoratorsExecutor executor = new DecoratorsExecutor(mock(BatchExtensionDictionnary.class), new Project("key"), mock(SonarIndex.class), - mock(EventBus.class), mock(MeasurementFilters.class), mock(MeasureCache.class), mock(MetricFinder.class)); + mock(EventBus.class), mock(MeasurementFilters.class), mock(MeasureCache.class), mock(MetricFinder.class), mock(DuplicationCache.class)); try { executor.executeDecorator(decorator, mock(DefaultDecoratorContext.class), File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false)); fail("Exception has not been thrown");