diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2024-12-02 16:12:09 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-12-06 20:03:27 +0000 |
commit | 00aef6b15a70f1a1edee729ce69dbc8799165779 (patch) | |
tree | 4d25bb3b6c01d9d7655adb5b45e56c83a182814a /server/sonar-ce-task-projectanalysis/src | |
parent | 48b3f6a992f1ddfc53c103dd83bbb6019896aa8c (diff) | |
download | sonarqube-00aef6b15a70f1a1edee729ce69dbc8799165779.tar.gz sonarqube-00aef6b15a70f1a1edee729ce69dbc8799165779.zip |
SONAR-12647 Remove deprecated complexity metrics
* Remove from complexity measure step
* Remove from PersistMeasuresStep
* Remove from ScannerMetrics
* Add the DB migration
* Don't register complexity metrics
* Fix tests
Diffstat (limited to 'server/sonar-ce-task-projectanalysis/src')
5 files changed, 27 insertions, 313 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java index 875cdb3768b..4ff9babee3c 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java @@ -48,14 +48,11 @@ import org.sonar.db.metric.MetricDto; import org.sonar.server.project.Project; import static java.util.Collections.emptyList; -import static java.util.Map.entry; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY; -import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY; import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; @@ -95,8 +92,7 @@ class PersistMeasuresStepIT { @BeforeEach public void setUp() { - MetricDto stringMetricDto = - db.measures().insertMetric(m -> m.setKey(STRING_METRIC.getKey()).setValueType(Metric.ValueType.STRING.name())); + MetricDto stringMetricDto = db.measures().insertMetric(m -> m.setKey(STRING_METRIC.getKey()).setValueType(Metric.ValueType.STRING.name())); MetricDto intMetricDto = db.measures().insertMetric(m -> m.setKey(INT_METRIC.getKey()).setValueType(Metric.ValueType.INT.name())); MetricDto bestValueMetricDto = db.measures() .insertMetric(m -> m.setKey(METRIC_WITH_BEST_VALUE.getKey()).setValueType(Metric.ValueType.INT.name()).setOptimizedBestValue(true).setBestValue(0.0)); @@ -187,50 +183,6 @@ class PersistMeasuresStepIT { } @Test - void do_not_persist_excluded_file_metrics() { - MetricDto fileComplexityMetric = - db.measures().insertMetric(m -> m.setKey(FILE_COMPLEXITY_DISTRIBUTION_KEY).setValueType(Metric.ValueType.STRING.name())); - MetricDto functionComplexityMetric = - db.measures().insertMetric(m -> m.setKey(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY).setValueType(Metric.ValueType.INT.name())); - metricRepository.add(fileComplexityMetric.getUuid(), new Metric.Builder(FILE_COMPLEXITY_DISTRIBUTION_KEY, "File Distribution / " + - "Complexity", - Metric.ValueType.DISTRIB).create()); - metricRepository.add(functionComplexityMetric.getUuid(), new Metric.Builder(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "Function " + - "Distribution / Complexity", - Metric.ValueType.DISTRIB).create()); - - prepareProject(); - - // the computed measures - measureRepository.addRawMeasure(REF_1, FILE_COMPLEXITY_DISTRIBUTION_KEY, newMeasureBuilder().create("project-value")); - measureRepository.addRawMeasure(REF_3, FILE_COMPLEXITY_DISTRIBUTION_KEY, newMeasureBuilder().create("dir-value")); - measureRepository.addRawMeasure(REF_4, FILE_COMPLEXITY_DISTRIBUTION_KEY, newMeasureBuilder().create("file-value")); - - measureRepository.addRawMeasure(REF_1, FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, newMeasureBuilder().create("project-value")); - measureRepository.addRawMeasure(REF_3, FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, newMeasureBuilder().create("dir-value")); - measureRepository.addRawMeasure(REF_4, FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, newMeasureBuilder().create("file-value")); - - step().execute(context); - - // all measures are persisted, from project to file - assertThat(db.countRowsOfTable("measures")).isEqualTo(3); - - assertThat(selectMeasure("project-uuid")) - .hasValueSatisfying(measure -> assertThat(measure.getMetricValues()).contains( - entry(FILE_COMPLEXITY_DISTRIBUTION_KEY, "project-value"), - entry(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "project-value"))); - assertThat(selectMeasure("dir-uuid")) - .hasValueSatisfying(measure -> assertThat(measure.getMetricValues()).contains( - entry(FILE_COMPLEXITY_DISTRIBUTION_KEY, "dir-value"), - entry(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "dir-value"))); - assertThat(selectMeasure("file-uuid")) - .hasValueSatisfying(measure -> assertThat(measure.getMetricValues()) - .doesNotContainKeys(FILE_COMPLEXITY_DISTRIBUTION_KEY, FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)); - - verifyInsertsOrUpdates(4); - } - - @Test void measures_without_value_are_not_persisted() { prepareProject(); measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().createNoValue()); @@ -366,7 +318,8 @@ class PersistMeasuresStepIT { } private void insertBranch() { - dbClient.branchDao().insert(db.getSession(), new BranchDto().setUuid("project-uuid").setProjectUuid("project-uuid").setKey("branch").setBranchType(BranchType.BRANCH).setIsMain(true)); + dbClient.branchDao().insert(db.getSession(), + new BranchDto().setUuid("project-uuid").setProjectUuid("project-uuid").setKey("branch").setBranchType(BranchType.BRANCH).setIsMain(true)); db.commit(); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ComplexityMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ComplexityMeasuresStep.java index 0edb32a7a68..d97cb3a5bd6 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ComplexityMeasuresStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ComplexityMeasuresStep.java @@ -22,26 +22,14 @@ package org.sonar.ce.task.projectanalysis.step; import java.util.List; import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler; import org.sonar.ce.task.projectanalysis.component.TreeRootHolder; -import org.sonar.ce.task.projectanalysis.formula.AverageFormula; -import org.sonar.ce.task.projectanalysis.formula.DistributionFormula; import org.sonar.ce.task.projectanalysis.formula.Formula; import org.sonar.ce.task.projectanalysis.formula.FormulaExecutorComponentVisitor; import org.sonar.ce.task.projectanalysis.measure.MeasureRepository; import org.sonar.ce.task.projectanalysis.metric.MetricRepository; import org.sonar.ce.task.step.ComputationStep; -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.COGNITIVE_COMPLEXITY_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.ce.task.projectanalysis.formula.SumFormula.createIntSumFormula; /** @@ -50,23 +38,7 @@ import static org.sonar.ce.task.projectanalysis.formula.SumFormula.createIntSumF public class ComplexityMeasuresStep implements ComputationStep { private static final List<Formula<?>> FORMULAS = List.of( createIntSumFormula(COMPLEXITY_KEY), - createIntSumFormula(COMPLEXITY_IN_CLASSES_KEY), - createIntSumFormula(COMPLEXITY_IN_FUNCTIONS_KEY), - createIntSumFormula(COGNITIVE_COMPLEXITY_KEY), - new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY), - new DistributionFormula(FILE_COMPLEXITY_DISTRIBUTION_KEY), - 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) - .build(), - AverageFormula.Builder.newBuilder().setOutputMetricKey(FUNCTION_COMPLEXITY_KEY) - .setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY) - .setByMetricKey(FUNCTIONS_KEY) - .build() + createIntSumFormula(COGNITIVE_COMPLEXITY_KEY) ); private final TreeRootHolder treeRootHolder; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java index 7f12d96a6d5..46094e08fd0 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java @@ -51,19 +51,10 @@ import org.sonar.db.measure.MeasureHash; import org.springframework.beans.factory.annotation.Autowired; import static org.sonar.api.measures.CoreMetrics.DUPLICATIONS_DATA_KEY; -import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY; -import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY; import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; public class PersistMeasuresStep implements ComputationStep { - /** - * List of metrics that should not be persisted on file measure. - */ - private static final Set<String> NOT_TO_PERSIST_ON_FILE_METRIC_KEYS = Set.of( - FILE_COMPLEXITY_DISTRIBUTION_KEY, - FUNCTION_COMPLEXITY_DISTRIBUTION_KEY); - // 50 mb private static final int MAX_TRANSACTION_SIZE = 50_000_000; private static final Predicate<Measure> NON_EMPTY_MEASURE = measure -> @@ -155,9 +146,6 @@ public class PersistMeasuresStep implements ComputationStep { Map<String, Measure> measures = measureRepository.getRawMeasures(component); for (Map.Entry<String, Measure> measuresByMetricKey : measures.entrySet()) { String metricKey = measuresByMetricKey.getKey(); - if (NOT_TO_PERSIST_ON_FILE_METRIC_KEYS.contains(metricKey) && component.getType() == Type.FILE) { - continue; - } Metric metric = metricRepository.getByKey(metricKey); Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate(); Measure measure = measuresByMetricKey.getValue(); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComplexityMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComplexityMeasuresStepTest.java index 43820610c11..5103d59cc32 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComplexityMeasuresStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComplexityMeasuresStepTest.java @@ -19,9 +19,8 @@ */ package org.sonar.ce.task.projectanalysis.step; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule; import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule; import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule; @@ -30,29 +29,12 @@ import org.sonar.ce.task.step.TestComputationStepContext; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.api.measures.CoreMetrics.CLASSES; -import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY; -import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_KEY; import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY; import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY_KEY; import static org.sonar.api.measures.CoreMetrics.COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES; -import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES_KEY; -import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS; -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; -import static org.sonar.api.measures.CoreMetrics.FILES_KEY; -import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION; -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; -import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY; -import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION; -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.ce.task.projectanalysis.component.Component.Type.DIRECTORY; import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; @@ -61,17 +43,15 @@ import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilde import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf; import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries; -public class ReportComplexityMeasuresStepTest { +class ReportComplexityMeasuresStepTest { private static final int ROOT_REF = 1; private static final int DIRECTORY_REF = 1111; private static final int FILE_1_REF = 11111; private static final int FILE_2_REF = 11121; - @Rule - public BatchReportReaderRule reportReader = new BatchReportReaderRule(); - @Rule - public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule() + @RegisterExtension + private final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule() .setRoot(builder(PROJECT, ROOT_REF) .addChildren( builder(DIRECTORY, DIRECTORY_REF) @@ -80,43 +60,26 @@ public class ReportComplexityMeasuresStepTest { builder(FILE, FILE_2_REF).build()) .build()) .build()); - @Rule - public MetricRepositoryRule metricRepository = new MetricRepositoryRule() + @RegisterExtension + private final MetricRepositoryRule metricRepository = new MetricRepositoryRule() .add(COMPLEXITY) - .add(COMPLEXITY_IN_CLASSES) - .add(COMPLEXITY_IN_FUNCTIONS) - .add(FUNCTION_COMPLEXITY_DISTRIBUTION) - .add(FILE_COMPLEXITY_DISTRIBUTION) - .add(FILE_COMPLEXITY) .add(FILES) - .add(CLASS_COMPLEXITY) .add(CLASSES) - .add(FUNCTION_COMPLEXITY) .add(FUNCTIONS) .add(COGNITIVE_COMPLEXITY); - @Rule - public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); + @RegisterExtension + private final MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); - private ComputationStep underTest = new ComplexityMeasuresStep(treeRootHolder, metricRepository, measureRepository); + private final ComputationStep underTest = new ComplexityMeasuresStep(treeRootHolder, metricRepository, measureRepository); @Test - public void aggregate_complexity() { + void aggregate_complexity() { verify_sum_aggregation(COMPLEXITY_KEY); } @Test - public void aggregate_complexity_in_classes() { - verify_sum_aggregation(COMPLEXITY_IN_CLASSES_KEY); - } - - @Test - public void aggregate_complexity_in_functions() { - verify_sum_aggregation(COMPLEXITY_IN_FUNCTIONS_KEY); - } - - @Test - public void aggregate_cognitive_complexity() { + void aggregate_cognitive_complexity() { verify_sum_aggregation(COGNITIVE_COMPLEXITY_KEY); } @@ -134,60 +97,4 @@ public class ReportComplexityMeasuresStepTest { assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(entryOf(metricKey, newMeasureBuilder().create(expectedNonFileValue))); } - @Test - public void aggregate_function_complexity_distribution() { - verify_distribution_aggregation(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY); - } - - @Test - public void aggregate_file_complexity_distribution() { - verify_distribution_aggregation(FILE_COMPLEXITY_DISTRIBUTION_KEY); - } - - private void verify_distribution_aggregation(String metricKey) { - measureRepository.addRawMeasure(FILE_1_REF, metricKey, newMeasureBuilder().create("0.5=3;3.5=5;6.5=9")); - measureRepository.addRawMeasure(FILE_2_REF, metricKey, newMeasureBuilder().create("0.5=0;3.5=2;6.5=1")); - - underTest.execute(new TestComputationStepContext()); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, metricKey)).isNotPresent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, metricKey)).isNotPresent(); - - String expectedNonFileValue = "0.5=3;3.5=7;6.5=10"; - assertThat(toEntries(measureRepository.getAddedRawMeasures(DIRECTORY_REF))).contains(entryOf(metricKey, newMeasureBuilder().create(expectedNonFileValue))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(entryOf(metricKey, newMeasureBuilder().create(expectedNonFileValue))); - } - - @Test - public void compute_and_aggregate_file_complexity() { - verify_average_compute_and_aggregation(FILE_COMPLEXITY_KEY, COMPLEXITY_KEY, FILES_KEY); - } - - @Test - public void compute_and_aggregate_class_complexity() { - verify_average_compute_and_aggregation(CLASS_COMPLEXITY_KEY, COMPLEXITY_IN_CLASSES_KEY, CLASSES_KEY); - } - - @Test - public void compute_and_aggregate_function_complexity() { - verify_average_compute_and_aggregation(FUNCTION_COMPLEXITY_KEY, COMPLEXITY_IN_FUNCTIONS_KEY, FUNCTIONS_KEY); - } - - private void verify_average_compute_and_aggregation(String metricKey, String mainMetric, String byMetric) { - measureRepository.addRawMeasure(FILE_1_REF, mainMetric, newMeasureBuilder().create(5)); - measureRepository.addRawMeasure(FILE_1_REF, byMetric, newMeasureBuilder().create(2)); - - measureRepository.addRawMeasure(FILE_2_REF, mainMetric, newMeasureBuilder().create(1)); - measureRepository.addRawMeasure(FILE_2_REF, byMetric, newMeasureBuilder().create(1)); - - underTest.execute(new TestComputationStepContext()); - - assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_1_REF))).contains(entryOf(metricKey, newMeasureBuilder().create(2.5, 1))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_2_REF))).contains(entryOf(metricKey, newMeasureBuilder().create(1d, 1))); - - double expectedNonFileValue = 2d; - assertThat(toEntries(measureRepository.getAddedRawMeasures(DIRECTORY_REF))).contains(entryOf(metricKey, newMeasureBuilder().create(expectedNonFileValue, 1))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(entryOf(metricKey, newMeasureBuilder().create(expectedNonFileValue, 1))); - } - } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsComplexityMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsComplexityMeasuresStepTest.java index ab3d4567171..283890f7d2c 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsComplexityMeasuresStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsComplexityMeasuresStepTest.java @@ -19,8 +19,8 @@ */ package org.sonar.ce.task.projectanalysis.step; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule; import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule; import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule; @@ -30,29 +30,12 @@ import org.sonar.ce.task.step.TestComputationStepContext; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.api.measures.CoreMetrics.CLASSES; -import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY; -import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_KEY; import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY; import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY_KEY; import static org.sonar.api.measures.CoreMetrics.COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES; -import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES_KEY; -import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS; -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; -import static org.sonar.api.measures.CoreMetrics.FILES_KEY; -import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION; -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; -import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY; -import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY; -import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION; -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.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW; import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW; import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW; @@ -61,7 +44,7 @@ import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilde import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf; import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries; -public class ViewsComplexityMeasuresStepTest { +class ViewsComplexityMeasuresStepTest { private static final int ROOT_REF = 1; private static final int SUBVIEW_REF = 11; @@ -71,9 +54,7 @@ public class ViewsComplexityMeasuresStepTest { private static final int SUB_SUBVIEW_2_REF = 112; private static final int PROJECT_VIEW_3_REF = 12; - @Rule - public BatchReportReaderRule reportReader = new BatchReportReaderRule(); - @Rule + @RegisterExtension public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule() .setRoot(builder(VIEW, ROOT_REF) .addChildren( @@ -88,43 +69,26 @@ public class ViewsComplexityMeasuresStepTest { .build(), builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build()) .build()); - @Rule - public MetricRepositoryRule metricRepository = new MetricRepositoryRule() + @RegisterExtension + private final MetricRepositoryRule metricRepository = new MetricRepositoryRule() .add(COMPLEXITY) - .add(COMPLEXITY_IN_CLASSES) - .add(COMPLEXITY_IN_FUNCTIONS) - .add(FUNCTION_COMPLEXITY_DISTRIBUTION) - .add(FILE_COMPLEXITY_DISTRIBUTION) - .add(FILE_COMPLEXITY) .add(FILES) - .add(CLASS_COMPLEXITY) .add(CLASSES) - .add(FUNCTION_COMPLEXITY) .add(FUNCTIONS) .add(COGNITIVE_COMPLEXITY); - @Rule - public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); + @RegisterExtension + private final MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); - private ComputationStep underTest = new ComplexityMeasuresStep(treeRootHolder, metricRepository, measureRepository); + private final ComputationStep underTest = new ComplexityMeasuresStep(treeRootHolder, metricRepository, measureRepository); @Test - public void aggregate_complexity() { + void aggregate_complexity() { verify_sum_aggregation(COMPLEXITY_KEY); } @Test - public void aggregate_complexity_in_classes() { - verify_sum_aggregation(COMPLEXITY_IN_CLASSES_KEY); - } - - @Test - public void aggregate_complexity_in_functions() { - verify_sum_aggregation(COMPLEXITY_IN_FUNCTIONS_KEY); - } - - @Test - public void aggregate_cognitive_complexity_in_functions() { + void aggregate_cognitive_complexity_in_functions() { verify_sum_aggregation(COGNITIVE_COMPLEXITY_KEY); } @@ -142,68 +106,6 @@ public class ViewsComplexityMeasuresStepTest { assertAddedRawMeasures(ROOT_REF, metricKey, 70); } - @Test - public void aggregate_function_complexity_distribution() { - verify_distribution_aggregation(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY); - } - - @Test - public void aggregate_file_complexity_distribution() { - verify_distribution_aggregation(FILE_COMPLEXITY_DISTRIBUTION_KEY); - } - - private void verify_distribution_aggregation(String metricKey) { - addRawMeasure(PROJECT_VIEW_1_REF, metricKey, "0.5=3;3.5=5;6.5=9"); - addRawMeasure(PROJECT_VIEW_2_REF, metricKey, "0.5=0;3.5=2;6.5=1"); - addRawMeasure(PROJECT_VIEW_3_REF, metricKey, "0.5=1;3.5=1;6.5=0"); - - underTest.execute(new TestComputationStepContext()); - - assertNoAddedRawMeasureOnProjectViews(); - assertAddedRawMeasures(SUB_SUBVIEW_1_REF, metricKey, "0.5=3;3.5=7;6.5=10"); - assertNoAddedRawMeasure(SUB_SUBVIEW_2_REF); - assertAddedRawMeasures(SUBVIEW_REF, metricKey, "0.5=3;3.5=7;6.5=10"); - assertAddedRawMeasures(ROOT_REF, metricKey, "0.5=4;3.5=8;6.5=10"); - } - - @Test - public void compute_and_aggregate_file_complexity() { - verify_average_compute_and_aggregation(FILE_COMPLEXITY_KEY, COMPLEXITY_KEY, FILES_KEY); - } - - @Test - public void compute_and_aggregate_class_complexity() { - verify_average_compute_and_aggregation(CLASS_COMPLEXITY_KEY, COMPLEXITY_IN_CLASSES_KEY, CLASSES_KEY); - } - - @Test - public void compute_and_aggregate_function_complexity() { - verify_average_compute_and_aggregation(FUNCTION_COMPLEXITY_KEY, COMPLEXITY_IN_FUNCTIONS_KEY, FUNCTIONS_KEY); - } - - private void verify_average_compute_and_aggregation(String metricKey, String mainMetric, String byMetric) { - addRawMeasureValue(PROJECT_VIEW_1_REF, mainMetric, 5); - addRawMeasureValue(PROJECT_VIEW_1_REF, byMetric, 2); - - addRawMeasureValue(PROJECT_VIEW_2_REF, mainMetric, 1); - addRawMeasureValue(PROJECT_VIEW_2_REF, byMetric, 1); - - addRawMeasureValue(PROJECT_VIEW_3_REF, mainMetric, 6); - addRawMeasureValue(PROJECT_VIEW_3_REF, byMetric, 8); - - underTest.execute(new TestComputationStepContext()); - - assertNoAddedRawMeasureOnProjectViews(); - assertAddedRawMeasures(SUB_SUBVIEW_1_REF, metricKey, 2d); - assertNoAddedRawMeasure(SUB_SUBVIEW_2_REF); - assertAddedRawMeasures(SUBVIEW_REF, metricKey, 2d); - assertAddedRawMeasures(ROOT_REF, metricKey, 1.1d); - } - - private void addRawMeasure(int componentRef, String metricKey, String value) { - measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value)); - } - private void assertNoAddedRawMeasureOnProjectViews() { assertNoAddedRawMeasure(PROJECT_VIEW_1_REF); assertNoAddedRawMeasure(PROJECT_VIEW_2_REF); @@ -214,18 +116,10 @@ public class ViewsComplexityMeasuresStepTest { assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty(); } - private void assertAddedRawMeasures(int componentRef, String metricKey, String expected) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(expected))); - } - private void assertAddedRawMeasures(int componentRef, String metricKey, int expected) { assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(expected))); } - private void assertAddedRawMeasures(int componentRef, String metricKey, double expected) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(expected, 1))); - } - private void addRawMeasureValue(int componentRef, String metricKey, int value) { measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value)); } |