]> source.dussan.org Git - sonarqube.git/commitdiff
Revert "SONAR-6605 Replace some batch formulas by compute formulas"
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 2 Jul 2015 15:30:50 +0000 (17:30 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 2 Jul 2015 15:43:27 +0000 (17:43 +0200)
This reverts commit 2f948758eebec934beb54701792cf2d558319251.

server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainerImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/formula/CoreFormulaRepositoryImpl.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/formula/CoreFormulaRepositoryImplTest.java [deleted file]
sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorStorageTest.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java

index b22ec16e2ffb1df7c092fd680ab1a846dc9a777f..dad331ca5ff2f5028d4a9a77967f34186fabc17b 100644 (file)
@@ -42,7 +42,6 @@ import org.sonar.server.computation.component.ProjectSettingsRepository;
 import org.sonar.server.computation.component.TreeRootHolderImpl;
 import org.sonar.server.computation.debt.DebtModelHolderImpl;
 import org.sonar.server.computation.event.EventRepositoryImpl;
-import org.sonar.server.computation.formula.CoreFormulaRepositoryImpl;
 import org.sonar.server.computation.issue.IssueCache;
 import org.sonar.server.computation.issue.IssueComputation;
 import org.sonar.server.computation.issue.RuleCache;
@@ -147,7 +146,6 @@ public class ComputeEngineContainerImpl extends ComponentContainer implements Co
       EventRepositoryImpl.class,
       ProjectSettingsRepository.class,
       DbIdsRepository.class,
-      CoreFormulaRepositoryImpl.class,
 
       QualityGateServiceImpl.class,
       EvaluationResultTextConverterImpl.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/CoreFormulaRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/CoreFormulaRepositoryImpl.java
deleted file mode 100644 (file)
index 627d99b..0000000
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * 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.server.computation.formula;
-
-import com.google.common.collect.ImmutableList;
-import java.util.List;
-
-import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
-import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_KEY;
-import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY;
-import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES_KEY;
-import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
-import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_KEY;
-import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
-import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY;
-import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_KEY;
-import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
-import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
-import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
-import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES_KEY;
-import static org.sonar.api.measures.CoreMetrics.GENERATED_NCLOC_KEY;
-import static org.sonar.api.measures.CoreMetrics.PUBLIC_API_KEY;
-import static org.sonar.api.measures.CoreMetrics.PUBLIC_UNDOCUMENTED_API_KEY;
-import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY;
-
-public class CoreFormulaRepositoryImpl implements FormulaRepository {
-
-  private static final List<Formula> FORMULAS;
-
-  static {
-    ImmutableList.Builder<Formula> formulaList = ImmutableList.builder();
-    formulaList.add(
-      // Sum formulas
-      // new SumFormula(LINES_KEY),
-      new SumFormula(GENERATED_LINES_KEY),
-      // new SumFormula(NCLOC_KEY),
-      new SumFormula(GENERATED_NCLOC_KEY),
-      new SumFormula(CLASSES_KEY),
-      new SumFormula(FUNCTIONS_KEY),
-      new SumFormula(STATEMENTS_KEY),
-      new SumFormula(PUBLIC_API_KEY),
-      new SumFormula(COMMENT_LINES_KEY),
-      new SumFormula(PUBLIC_UNDOCUMENTED_API_KEY),
-      new SumFormula(COMPLEXITY_KEY),
-      new SumFormula(COMPLEXITY_IN_CLASSES_KEY),
-      new SumFormula(COMPLEXITY_IN_FUNCTIONS_KEY),
-      // new SumFormula(LINES_TO_COVER_KEY),
-      // new SumFormula(NEW_LINES_TO_COVER_KEY),
-      // new SumFormula(UNCOVERED_LINES_KEY),
-      // new SumFormula(NEW_UNCOVERED_LINES_KEY),
-      // new SumFormula(CONDITIONS_TO_COVER_KEY),
-      // new SumFormula(NEW_CONDITIONS_TO_COVER_KEY),
-      // new SumFormula(UNCOVERED_CONDITIONS_KEY),
-      // new SumFormula(NEW_UNCOVERED_CONDITIONS_KEY),
-      // new SumFormula(IT_LINES_TO_COVER_KEY),
-      // new SumFormula(NEW_IT_LINES_TO_COVER_KEY),
-      // new SumFormula(IT_UNCOVERED_LINES_KEY),
-      // new SumFormula(NEW_IT_UNCOVERED_LINES_KEY),
-      // new SumFormula(IT_CONDITIONS_TO_COVER_KEY),
-      // new SumFormula(NEW_IT_CONDITIONS_TO_COVER_KEY),
-      // new SumFormula(IT_UNCOVERED_CONDITIONS_KEY),
-      // new SumFormula(NEW_IT_UNCOVERED_CONDITIONS_KEY),
-      // new SumFormula(OVERALL_LINES_TO_COVER_KEY),
-      // new SumFormula(NEW_OVERALL_LINES_TO_COVER_KEY),
-      // new SumFormula(OVERALL_UNCOVERED_LINES_KEY),
-      // new SumFormula(NEW_OVERALL_UNCOVERED_LINES_KEY),
-      // new SumFormula(OVERALL_CONDITIONS_TO_COVER_KEY),
-      // new SumFormula(NEW_OVERALL_CONDITIONS_TO_COVER_KEY),
-      // new SumFormula(OVERALL_UNCOVERED_CONDITIONS_KEY),
-      // new SumFormula(NEW_OVERALL_UNCOVERED_CONDITIONS_KEY),
-
-      // Distribution formulas
-      new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY),
-      new DistributionFormula(FILE_COMPLEXITY_DISTRIBUTION_KEY),
-
-      // Average formulas, must be executed after all sum formulas as they depend on their measures
-      AverageFormula.Builder.newBuilder().setOutputMetricKey(FILE_COMPLEXITY_KEY)
-        .setMainMetricKey(COMPLEXITY_KEY)
-        .setByMetricKey(FILES_KEY)
-        .build(),
-      AverageFormula.Builder.newBuilder().setOutputMetricKey(CLASS_COMPLEXITY_KEY)
-        .setMainMetricKey(COMPLEXITY_IN_CLASSES_KEY)
-        .setByMetricKey(CLASSES_KEY)
-        .setFallbackMetricKey(COMPLEXITY_KEY)
-        .build(),
-      AverageFormula.Builder.newBuilder().setOutputMetricKey(FUNCTION_COMPLEXITY_KEY)
-        .setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY)
-        .setByMetricKey(FUNCTIONS_KEY)
-        .setFallbackMetricKey(COMPLEXITY_KEY)
-        .build()
-      );
-    FORMULAS = formulaList.build();
-  }
-
-  /**
-   * Return list of formulas that was previously provided by CoreMetrics
-   *
-   * TODO When all decorators will be moved to CE, uncomment commented lines to activate all formulas and remove formulas declaration in {@link org.sonar.api.measures.CoreMetrics}
-   */
-  @Override
-  public List<Formula> getFormulas() {
-    return FORMULAS;
-  }
-
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/CoreFormulaRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/CoreFormulaRepositoryImplTest.java
deleted file mode 100644 (file)
index ddba6cc..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.server.computation.formula;
-
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class CoreFormulaRepositoryImplTest {
-
-  @Test
-  public void check_formulas_are_not_empty() throws Exception {
-    assertThat(new CoreFormulaRepositoryImpl().getFormulas()).isNotEmpty();
-
-  }
-}
index 87d124989ed538b9543d50ea76dff6a9e5b7c017..b46febe1ab5df5c344f3f2f047422b336944eaae 100644 (file)
@@ -65,7 +65,7 @@ public class MeasuresMediumTest {
       .newScanTask(new File(projectDir, "sonar-project.properties"))
       .start();
 
-    assertThat(result.allMeasures()).hasSize(63);
+    assertThat(result.allMeasures()).hasSize(69);
   }
 
   @Test
index d62fd760616468447ce8d3c520624cc52d691a6d..b03d9baf3bda9994abb5f0a675f5ebf520b6443f 100644 (file)
@@ -39,13 +39,14 @@ import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
 import org.sonar.api.config.Settings;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.PersistenceMode;
 import org.sonar.api.resources.File;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.batch.duplication.DuplicationCache;
-import org.sonar.batch.index.BatchComponentCache;
 import org.sonar.batch.index.DefaultIndex;
+import org.sonar.batch.index.BatchComponentCache;
 import org.sonar.batch.issue.ModuleIssues;
 import org.sonar.batch.report.ReportPublisher;
 import org.sonar.batch.sensor.coverage.CoverageExclusions;
@@ -124,6 +125,30 @@ public class DefaultSensorStorageTest {
     assertThat(m.getMetric()).isEqualTo(CoreMetrics.NCLOC);
   }
 
+  @Test
+  public void shouldSetAppropriatePersistenceMode() {
+    // Metric FUNCTION_COMPLEXITY_DISTRIBUTION is only persisted on directories.
+
+    InputFile file = new DefaultInputFile("foo", "src/Foo.php");
+
+    ArgumentCaptor<org.sonar.api.measures.Measure> argumentCaptor = ArgumentCaptor.forClass(org.sonar.api.measures.Measure.class);
+    Resource sonarFile = File.create("src/Foo.php").setEffectiveKey("foo:src/Foo.php");
+    resourceCache.add(sonarFile, null).setInputPath(file);
+
+    when(sonarIndex.addMeasure(eq(sonarFile), argumentCaptor.capture())).thenReturn(null);
+
+    sensorStorage.store(new DefaultMeasure()
+      .onFile(file)
+      .forMetric(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION)
+      .withValue("foo"));
+
+    org.sonar.api.measures.Measure m = argumentCaptor.getValue();
+    assertThat(m.getData()).isEqualTo("foo");
+    assertThat(m.getMetric()).isEqualTo(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
+    assertThat(m.getPersistenceMode()).isEqualTo(PersistenceMode.MEMORY);
+
+  }
+
   @Test
   public void shouldSaveProjectMeasureToSensorContext() {
 
index a7f590c7e6493e9c68751db1cdee02e2b35aa912..874c873ae251ab84db89528f1b14c45666a54337 100644 (file)
@@ -22,14 +22,16 @@ package org.sonar.api.measures;
 import com.google.common.annotations.Beta;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
+import org.sonar.api.resources.Scopes;
+import org.sonar.api.test.MutableTestPlan;
+import org.sonar.api.utils.SonarException;
+
+import javax.annotation.Nullable;
+
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.LinkedList;
 import java.util.List;
-import javax.annotation.Nullable;
-import org.sonar.api.resources.Scopes;
-import org.sonar.api.test.MutableTestPlan;
-import org.sonar.api.utils.SonarException;
 
 /**
  * @since 1.10
@@ -75,6 +77,7 @@ public final class CoreMetrics {
     .setDomain(DOMAIN_SIZE)
     .setBestValue(0.0)
     .setOptimizedBestValue(true)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   public static final String NCLOC_KEY = "ncloc";
@@ -109,6 +112,7 @@ public final class CoreMetrics {
     .setDomain(DOMAIN_SIZE)
     .setBestValue(0.0)
     .setOptimizedBestValue(true)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   public static final String CLASSES_KEY = "classes";
@@ -117,6 +121,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(false)
     .setDomain(DOMAIN_SIZE)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   public static final String FILES_KEY = "files";
@@ -159,6 +164,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(false)
     .setDomain(DOMAIN_SIZE)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   /**
@@ -188,6 +194,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(false)
     .setDomain(DOMAIN_SIZE)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   public static final String PUBLIC_API_KEY = "public_api";
@@ -196,6 +203,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(false)
     .setDomain(DOMAIN_SIZE)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   /**
@@ -225,6 +233,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_BETTER)
     .setQualitative(false)
     .setDomain(DOMAIN_DOCUMENTATION)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   public static final String COMMENT_LINES_DENSITY_KEY = "comment_lines_density";
@@ -255,6 +264,7 @@ public final class CoreMetrics {
     .setBestValue(0.0)
     .setDirection(Metric.DIRECTION_WORST)
     .setOptimizedBestValue(true)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   /**
@@ -290,6 +300,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(false)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(new SumChildValuesFormula(false))
     .create();
 
   public static final String FILE_COMPLEXITY_KEY = "file_complexity";
@@ -298,6 +309,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(true)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FILES))
     .create();
 
   /**
@@ -314,6 +326,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(false)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(new SumChildValuesFormula(false))
     .setDeleteHistoricalData(true)
     .create();
 
@@ -328,6 +341,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(true)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(AverageFormula.create(CoreMetrics.COMPLEXITY_IN_CLASSES, CoreMetrics.CLASSES).setFallbackForMainMetric(CoreMetrics.COMPLEXITY))
     .create();
 
   /**
@@ -344,6 +358,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(false)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(new SumChildValuesFormula(false))
     .setDeleteHistoricalData(true)
     .create();
 
@@ -358,6 +373,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_WORST)
     .setQualitative(true)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS).setFallbackForMainMetric(CoreMetrics.COMPLEXITY))
     .create();
 
   /**
@@ -387,6 +403,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_NONE)
     .setQualitative(true)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(new SumChildDistributionFormula().setMinimumScopeToPersist(Scopes.DIRECTORY))
     .create();
 
   public static final String FILE_COMPLEXITY_DISTRIBUTION_KEY = "file_complexity_distribution";
@@ -395,6 +412,7 @@ public final class CoreMetrics {
     .setDirection(Metric.DIRECTION_NONE)
     .setQualitative(true)
     .setDomain(DOMAIN_COMPLEXITY)
+    .setFormula(new SumChildDistributionFormula().setMinimumScopeToPersist(Scopes.DIRECTORY))
     .create();
 
   // --------------------------------------------------------------------------------------------------------------------
index be259b66de9ca5ccd93363b0b154300c30506bda..57f3fec83a221ff0a4a53fcfb36dbf6e2af9c8fd 100644 (file)
@@ -699,11 +699,7 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
      *
      * @param f the formula
      * @return the builder
-     *
-     * @deprecated since 5.2, it's no more possible to define a formula on a metric
-     * TODO add a link to the new API to declare formulas
      */
-    @Deprecated
     public Builder setFormula(Formula f) {
       this.formula = f;
       return this;