aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-10-20 16:36:46 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2014-10-20 16:57:35 +0200
commit14bf33fa1c0f4ac920775ee2d2363337fa3769c0 (patch)
treec2f05a1fae8a9b4df9c28f54597cbd5b8427c739 /sonar-batch
parent520415e5f77ed04d8441a2fe97f51badc5b0f8fd (diff)
downloadsonarqube-14bf33fa1c0f4ac920775ee2d2363337fa3769c0.tar.gz
sonarqube-14bf33fa1c0f4ac920775ee2d2363337fa3769c0.zip
SONAR-5765 Restore access to duplication_data measure
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java30
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationUtils.java48
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/DuplicationPersister.java26
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java8
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java3
5 files changed, 85 insertions, 30 deletions
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<String, Measure> measuresByMetric = ArrayListMultimap.create();
private MeasureCache measureCache;
private MetricFinder metricFinder;
+ private final DuplicationCache duplicationCache;
public DefaultDecoratorContext(Resource resource,
SonarIndex index,
List<DecoratorContext> 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> M getMeasures(MeasuresFilter<M> filter) {
Collection<Measure> unfiltered;
if (filter instanceof MeasuresFilters.MetricFilter) {
- // optimization
- unfiltered = measuresByMetric.get(((MeasuresFilters.MetricFilter<M>) filter).filterOnMetricKey());
+ unfiltered = getMeasuresOfASingleMetric(filter);
} else {
unfiltered = measuresByMetric.values();
}
return filter.filter(unfiltered);
}
+ private <M> Collection<Measure> getMeasuresOfASingleMetric(MeasuresFilter<M> filter) {
+ Collection<Measure> unfiltered;
+ String metricKey = ((MeasuresFilters.MetricFilter<M>) filter).filterOnMetricKey();
+ if (CoreMetrics.DUPLICATIONS_DATA_KEY.equals(metricKey)) {
+ // Hack for SONAR-5765
+ List<DuplicationGroup> group = duplicationCache.byComponent(resource.getEffectiveKey());
+ if (group != null) {
+ unfiltered = Arrays.asList(new Measure(CoreMetrics.DUPLICATIONS_DATA, DuplicationUtils.toXml(group)));
+ } else {
+ unfiltered = Collections.<Measure>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<DuplicationGroup> duplications) {
+ StringBuilder xml = new StringBuilder();
+ xml.append("<duplications>");
+ for (DuplicationGroup duplication : duplications) {
+ xml.append("<g>");
+ toXml(xml, duplication.originBlock());
+ for (DuplicationGroup.Block part : duplication.duplicates()) {
+ toXml(xml, part);
+ }
+ xml.append("</g>");
+ }
+ xml.append("</duplications>");
+ return xml.toString();
+ }
+
+ private static void toXml(StringBuilder xml, DuplicationGroup.Block part) {
+ xml.append("<b s=\"").append(part.startLine())
+ .append("\" l=\"").append(part.length())
+ .append("\" r=\"").append(StringEscapeUtils.escapeXml(part.resourceKey()))
+ .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<List<DuplicationGroup>> 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<DuplicationGroup> duplications) {
- StringBuilder xml = new StringBuilder();
- xml.append("<duplications>");
- for (DuplicationGroup duplication : duplications) {
- xml.append("<g>");
- toXml(xml, duplication.originBlock());
- for (DuplicationGroup.Block part : duplication.duplicates()) {
- toXml(xml, part);
- }
- xml.append("</g>");
- }
- xml.append("</duplications>");
- return xml.toString();
- }
-
- private static void toXml(StringBuilder xml, DuplicationGroup.Block part) {
- xml.append("<b s=\"").append(part.startLine())
- .append("\" l=\"").append(part.length())
- .append("\" r=\"").append(StringEscapeUtils.escapeXml(part.resourceKey()))
- .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");