]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5212 Fix tests metric in multi-language projects
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 11 Apr 2014 15:52:17 +0000 (17:52 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 11 Apr 2014 15:52:17 +0000 (17:52 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java
sonar-batch/src/main/java/org/sonar/batch/index/Bucket.java
sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java

index 6ee37e94669f551cbc472d5d84ee08c6dff7cfac..6fca0264d7db2f94b3e8466055e5ce569aaa9bd9 100644 (file)
@@ -46,7 +46,7 @@ public class UnitTestDecorator implements Decorator {
   }
 
   public boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
-    return context.getMeasure(CoreMetrics.TESTS) == null && (ResourceUtils.isUnitTestClass(resource) || !ResourceUtils.isEntity(resource));
+    return ResourceUtils.isUnitTestClass(resource) || !ResourceUtils.isEntity(resource);
   }
 
   public void decorate(Resource resource, DecoratorContext context) {
index d1b23f797f70e4453ffd76411fc83cab59f0f8f1..4f52830b5de009f0c68d11477e8d116b80fc226d 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.plugins.core.sensors;
 
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.api.batch.DecoratorContext;
 import org.sonar.api.measures.CoreMetrics;
@@ -33,7 +34,11 @@ import static org.fest.assertions.Assertions.assertThat;
 import static org.hamcrest.Matchers.closeTo;
 import static org.mockito.Matchers.doubleThat;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
 
 public class UnitTestDecoratorTest {
 
@@ -85,6 +90,7 @@ public class UnitTestDecoratorTest {
   }
 
   @Test
+  @Ignore("Hack for SONAR-5212")
   public void doNotDecorateIfTestsMeasureAlreadyExists() {
     Project project = mock(Project.class);
     when(context.getMeasure(CoreMetrics.TESTS)).thenReturn(new Measure());
index 551c25327f357f5c4f76a69aba230a1b474349b4..af6a87ae8a662401f4c642a8d9f998c9e3a20949 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.batch.index;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Lists;
+import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.MeasuresFilter;
 import org.sonar.api.measures.MeasuresFilters;
@@ -87,6 +88,9 @@ public final class Bucket {
       if (index > -1) {
         if (metricMeasures.get(index) == measure) {
           add = false;
+        } else if (measure.getMetric().equals(CoreMetrics.TESTS)) {
+          // Hack for SONAR-5212
+          measuresByMetric.remove(measure.getMetric().getKey(), metricMeasures.get(index));
         } else {
           throw new SonarException("Can not add twice the same measure on " + resource + ": " + measure);
         }
index 564ee82ec7284991bc9e781879d904db2c09d015..8e61a931bd0da86145e2aab288c86ba77a8c7fab 100644 (file)
@@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory;
 import org.sonar.api.database.model.MeasureMapper;
 import org.sonar.api.database.model.MeasureModel;
 import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.RuleMeasure;
 import org.sonar.api.resources.Resource;
@@ -73,6 +74,10 @@ public final class MeasurePersister {
 
   public void saveMeasure(Resource resource, Measure measure) {
     if (shouldSaveLater(measure)) {
+      if (measure.getMetric().equals(CoreMetrics.TESTS) && unsavedMeasuresByResource.get(resource).contains(measure)) {
+        // Hack for SONAR-5212
+        unsavedMeasuresByResource.remove(resource, measure);
+      }
       unsavedMeasuresByResource.put(resource, measure);
       return;
     }