]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5765 Restore access to duplication_data measure
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 20 Oct 2014 14:36:46 +0000 (16:36 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 20 Oct 2014 14:57:35 +0000 (16:57 +0200)
sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java
sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationUtils.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/index/DuplicationPersister.java
sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java
sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java

index 4152792943126e3a4c10a33b6a84577e1f5f34a9..ba4b4d3ad2553a631372f997dcdeb3d0f81bebde 100644 (file)
@@ -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 (file)
index 0000000..f99aa02
--- /dev/null
@@ -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("\"/>");
+  }
+}
index 9461d46dfd7783209305b695c21907c8589c2e18..4e5ebce1ef10c938af3966fc82d9f003f7ec7a1b 100644 (file)
@@ -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("\"/>");
-  }
-
 }
index c6aa4ac8a79d02e62f6dfcc756d24f4ca9ed9a69..b867e0fc0104c084f09f65c899277346078fbdd6 100644 (file)
@@ -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) {
index 12d35f08b4d62f9d645c475e24fc7c2877284a54..3516ab363fba9664d4c211661560acb97d06a714 100644 (file)
@@ -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");