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;
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;
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() {
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));
}
--- /dev/null
+/*
+ * 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("\"/>");
+ }
+}
*/
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;
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;
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)) {
}
}
- 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("\"/>");
- }
-
}
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;
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;
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) {
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;
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");