diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-07-22 15:08:50 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-07-23 11:06:03 +0200 |
commit | 7af633d72d95ce49b37a8e3ced03fb19c91a710f (patch) | |
tree | df0532cceb09e0e99107caa141cf9dccc80e6bd7 /server | |
parent | 44a68c43498da870ebc4c7b4f638620f14e50b3b (diff) | |
download | sonarqube-7af633d72d95ce49b37a8e3ced03fb19c91a710f.tar.gz sonarqube-7af633d72d95ce49b37a8e3ced03fb19c91a710f.zip |
SONAR-6605 move some core Formulas to FileAndDirectoryMeasuresStep
rename FileAndDirectoryMeasuresStep to SizeMeasuresStep in the process
Diffstat (limited to 'server')
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/formula/CoreFormulaRepositoryImpl.java | 114 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java | 2 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/step/SizeMeasuresStep.java (renamed from server/sonar-server/src/main/java/org/sonar/server/computation/step/FileAndDirectoryMeasuresStep.java) | 35 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/computation/step/FileAndDirectoryMeasuresStepTest.java | 111 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/computation/step/SizeMeasuresStepTest.java | 208 |
5 files changed, 292 insertions, 178 deletions
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 index 752fc5edead..0f4ebfa24fc 100644 --- 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 @@ -23,7 +23,6 @@ package org.sonar.server.computation.formula; import com.google.common.collect.ImmutableList; import java.util.List; -import static org.sonar.api.measures.CoreMetrics.ACCESSORS_KEY; 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.COMMENTED_OUT_CODE_LINES_KEY; @@ -36,77 +35,66 @@ 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.STATEMENTS_KEY; public class CoreFormulaRepositoryImpl implements CoreFormulaRepository { private static final List<Formula> FORMULAS = ImmutableList.<Formula>of( - // 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} + // 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} - // 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(COMMENTED_OUT_CODE_LINES_KEY), - new SumFormula(COMPLEXITY_KEY), - new SumFormula(COMPLEXITY_IN_CLASSES_KEY), - // TODO this formula seems to be useless as this measure seems only required on files - new SumFormula(COMPLEXITY_IN_FUNCTIONS_KEY), + // Sum formulas + new SumFormula(COMMENTED_OUT_CODE_LINES_KEY), + new SumFormula(COMPLEXITY_KEY), + new SumFormula(COMPLEXITY_IN_CLASSES_KEY), + // TODO this formula seems to be useless as this measure seems only required on files + new SumFormula(COMPLEXITY_IN_FUNCTIONS_KEY), - new SumFormula(ACCESSORS_KEY), -// new SumFormula(CoreMetrics.COMMENT_LINES_KEY), + // new SumFormula(CoreMetrics.COMMENT_LINES_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), + // 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), + // 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() + // 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() ); /** diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java index fe213aaabe6..f1feca9a249 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java @@ -52,7 +52,7 @@ public class ComputationSteps { FeedPeriodsStep.class, // data computation - FileAndDirectoryMeasuresStep.class, + SizeMeasuresStep.class, NewCoverageMeasuresStep.class, CoverageMeasuresStep.class, CommentMeasuresStep.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/FileAndDirectoryMeasuresStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/SizeMeasuresStep.java index 563bc3e5495..b4b32888314 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/FileAndDirectoryMeasuresStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/SizeMeasuresStep.java @@ -19,26 +19,51 @@ */ package org.sonar.server.computation.step; +import com.google.common.collect.ImmutableList; import org.sonar.api.measures.CoreMetrics; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.PathAwareVisitor; import org.sonar.server.computation.component.TreeRootHolder; +import org.sonar.server.computation.formula.Formula; +import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor; +import org.sonar.server.computation.formula.SumFormula; import org.sonar.server.computation.measure.MeasureRepository; import org.sonar.server.computation.metric.Metric; import org.sonar.server.computation.metric.MetricRepository; +import static org.sonar.api.measures.CoreMetrics.ACCESSORS_KEY; +import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY; +import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_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.LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; +import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY; import static org.sonar.server.computation.component.Component.Type.FILE; import static org.sonar.server.computation.component.ComponentVisitor.Order.POST_ORDER; import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; -public class FileAndDirectoryMeasuresStep implements ComputationStep { +/** + * Compute size measures + */ +public class SizeMeasuresStep implements ComputationStep { private static final CounterStackElementFactory COUNTER_STACK_ELEMENT_FACTORY = new CounterStackElementFactory(); + private static final ImmutableList<Formula> AGGREGATED_SIZE_MEASURE_FORMULAS = ImmutableList.<Formula>of( + new SumFormula(LINES_KEY), + new SumFormula(GENERATED_LINES_KEY), + new SumFormula(NCLOC_KEY), + new SumFormula(GENERATED_NCLOC_KEY), + new SumFormula(FUNCTIONS_KEY), + new SumFormula(STATEMENTS_KEY), + new SumFormula(CLASSES_KEY), + new SumFormula(ACCESSORS_KEY) + ); private final TreeRootHolder treeRootHolder; private final MetricRepository metricRepository; private final MeasureRepository measureRepository; - public FileAndDirectoryMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) { + public SizeMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) { this.treeRootHolder = treeRootHolder; this.metricRepository = metricRepository; this.measureRepository = measureRepository; @@ -49,7 +74,11 @@ public class FileAndDirectoryMeasuresStep implements ComputationStep { Metric fileMetric = metricRepository.getByKey(CoreMetrics.FILES_KEY); Metric directoryMetric = metricRepository.getByKey(CoreMetrics.DIRECTORIES_KEY); - new FileAndDirectoryMeasureVisitor(directoryMetric, fileMetric).visit(treeRootHolder.getRoot()); + new FileAndDirectoryMeasureVisitor(directoryMetric, fileMetric) + .visit(treeRootHolder.getRoot()); + FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository) + .buildFor(AGGREGATED_SIZE_MEASURE_FORMULAS) + .visit(treeRootHolder.getRoot()); } @Override diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/FileAndDirectoryMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/FileAndDirectoryMeasuresStepTest.java deleted file mode 100644 index 3989a86268a..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/FileAndDirectoryMeasuresStepTest.java +++ /dev/null @@ -1,111 +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.step; - -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.server.computation.batch.TreeRootHolderRule; -import org.sonar.server.computation.component.FileAttributes; -import org.sonar.server.computation.measure.MeasureRepositoryRule; -import org.sonar.server.computation.metric.MetricRepositoryRule; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.api.measures.CoreMetrics.DIRECTORIES_KEY; -import static org.sonar.api.measures.CoreMetrics.FILES_KEY; -import static org.sonar.server.computation.component.Component.Type.DIRECTORY; -import static org.sonar.server.computation.component.Component.Type.FILE; -import static org.sonar.server.computation.component.Component.Type.MODULE; -import static org.sonar.server.computation.component.Component.Type.PROJECT; -import static org.sonar.server.computation.component.DumbComponent.builder; -import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; -import static org.sonar.server.computation.measure.MeasureRepoEntry.entryOf; -import static org.sonar.server.computation.measure.MeasureRepoEntry.toEntries; - -public class FileAndDirectoryMeasuresStepTest { - - private static final int ROOT_REF = 1; - private static final int MODULE_REF = 12; - private static final int SUB_MODULE_REF = 123; - private static final int DIRECTORY_1_REF = 1234; - private static final int DIRECTORY_2_REF = 1235; - private static final int FILE_1_REF = 12341; - private static final int FILE_2_REF = 12343; - private static final int FILE_3_REF = 12351; - private static final int UNIT_TEST_REF = 12352; - public static final String LANGUAGE_KEY_DOES_NOT_MATTER_HERE = null; - - @Rule - public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); - @Rule - public MetricRepositoryRule metricRepository = new MetricRepositoryRule() - .add(CoreMetrics.FILES) - .add(CoreMetrics.DIRECTORIES); - @Rule - public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); - - private FileAndDirectoryMeasuresStep underTest = new FileAndDirectoryMeasuresStep(treeRootHolder, metricRepository, measureRepository); - - @Test - public void verify_FILE_and_DIRECTORY_computation_and_aggregation() { - treeRootHolder.setRoot( - builder(PROJECT, ROOT_REF) - .addChildren( - builder(MODULE, MODULE_REF) - .addChildren( - builder(MODULE, SUB_MODULE_REF) - .addChildren( - builder(DIRECTORY, DIRECTORY_1_REF) - .addChildren( - builder(FILE, FILE_1_REF).build(), - builder(FILE, FILE_2_REF).build()) - .build(), - builder(DIRECTORY, DIRECTORY_2_REF) - .addChildren( - builder(FILE, FILE_3_REF).build(), - builder(FILE, UNIT_TEST_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_KEY_DOES_NOT_MATTER_HERE)).build()) - .build()) - .build()) - .build()) - .build()); - - underTest.execute(); - - verifyMeasuresOnFile(FILE_1_REF, 1); - verifyMeasuresOnFile(FILE_2_REF, 1); - verifyMeasuresOnFile(FILE_3_REF, 1); - assertThat(toEntries(measureRepository.getAddedRawMeasures(UNIT_TEST_REF))).isEmpty(); - verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1); - verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1); - verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 2); - verifyMeasuresOnOtherComponent(MODULE_REF, 3, 2); - verifyMeasuresOnOtherComponent(ROOT_REF, 3, 2); - } - - private void verifyMeasuresOnFile(int componentRef, int fileCount) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))) - .containsOnly(entryOf(FILES_KEY, newMeasureBuilder().create(fileCount))); - } - - private void verifyMeasuresOnOtherComponent(int componentRef, int fileCount, int directoryCount) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))) - .containsOnly(entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)), entryOf(DIRECTORIES_KEY, newMeasureBuilder().create(directoryCount))); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/SizeMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/SizeMeasuresStepTest.java new file mode 100644 index 00000000000..c54fef566f5 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/SizeMeasuresStepTest.java @@ -0,0 +1,208 @@ +/* + * 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.step; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.server.computation.batch.TreeRootHolderRule; +import org.sonar.server.computation.component.FileAttributes; +import org.sonar.server.computation.measure.MeasureRepoEntry; +import org.sonar.server.computation.measure.MeasureRepositoryRule; +import org.sonar.server.computation.metric.MetricRepositoryRule; + +import static com.google.common.collect.FluentIterable.from; +import static com.google.common.collect.Iterables.concat; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY; +import static org.sonar.api.measures.CoreMetrics.DIRECTORIES_KEY; +import static org.sonar.api.measures.CoreMetrics.FILES_KEY; +import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY; +import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; +import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY; +import static org.sonar.server.computation.component.Component.Type.DIRECTORY; +import static org.sonar.server.computation.component.Component.Type.FILE; +import static org.sonar.server.computation.component.Component.Type.MODULE; +import static org.sonar.server.computation.component.Component.Type.PROJECT; +import static org.sonar.server.computation.component.DumbComponent.builder; +import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; +import static org.sonar.server.computation.measure.MeasureRepoEntry.entryOf; +import static org.sonar.server.computation.measure.MeasureRepoEntry.toEntries; + +public class SizeMeasuresStepTest { + + private static final String LANGUAGE_DOES_NOT_MATTER_HERE = null; + private static final int ROOT_REF = 1; + private static final int MODULE_REF = 12; + private static final int SUB_MODULE_REF = 123; + private static final int DIRECTORY_1_REF = 1234; + private static final int DIRECTORY_2_REF = 1235; + private static final int FILE_1_REF = 12341; + private static final int FILE_2_REF = 12343; + private static final int FILE_3_REF = 12351; + private static final int UNIT_TEST_REF = 12352; + + @Rule + public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot( + builder(PROJECT, ROOT_REF) + .addChildren( + builder(MODULE, MODULE_REF) + .addChildren( + builder(MODULE, SUB_MODULE_REF) + .addChildren( + builder(DIRECTORY, DIRECTORY_1_REF) + .addChildren( + builder(FILE, FILE_1_REF).build(), + builder(FILE, FILE_2_REF).build()) + .build(), + builder(DIRECTORY, DIRECTORY_2_REF) + .addChildren( + builder(FILE, FILE_3_REF).build(), + builder(FILE, UNIT_TEST_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE)).build()) + .build()) + .build()) + .build()) + .build()); + @Rule + public MetricRepositoryRule metricRepository = new MetricRepositoryRule() + .add(CoreMetrics.FILES) + .add(CoreMetrics.DIRECTORIES) + .add(CoreMetrics.LINES) + .add(CoreMetrics.GENERATED_LINES) + .add(CoreMetrics.NCLOC) + .add(CoreMetrics.GENERATED_NCLOC) + .add(CoreMetrics.FUNCTIONS) + .add(CoreMetrics.STATEMENTS) + .add(CoreMetrics.CLASSES) + .add(CoreMetrics.ACCESSORS); + @Rule + public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); + + private SizeMeasuresStep underTest = new SizeMeasuresStep(treeRootHolder, metricRepository, measureRepository); + + @Test + public void verify_FILE_and_DIRECTORY_computation_and_aggregation() { + underTest.execute(); + + verifyMeasuresOnFile(FILE_1_REF, 1); + verifyMeasuresOnFile(FILE_2_REF, 1); + verifyMeasuresOnFile(FILE_3_REF, 1); + verifyNoMeasure(UNIT_TEST_REF); + verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1); + verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1); + verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 2); + verifyMeasuresOnOtherComponent(MODULE_REF, 3, 2); + verifyMeasuresOnOtherComponent(ROOT_REF, 3, 2); + } + + @Test + public void verify_LINE_related_measures_aggregation() { + verifyTwoMeasureAggregation(LINES_KEY, GENERATED_LINES_KEY); + } + + private void verifyTwoMeasureAggregation(String metric1Key, String metric2Key) { + measureRepository.addRawMeasure(FILE_1_REF, metric1Key, newMeasureBuilder().create(1)); + measureRepository.addRawMeasure(FILE_1_REF, metric2Key, newMeasureBuilder().create(10)); + // FILE_2_REF has no metric2 measure + measureRepository.addRawMeasure(FILE_2_REF, metric1Key, newMeasureBuilder().create(6)); + // FILE_3_REF has no measure at all + // UNIT_TEST_REF has no metric1 + measureRepository.addRawMeasure(UNIT_TEST_REF, metric2Key, newMeasureBuilder().create(90)); + + underTest.execute(); + + verifyMeasuresOnFile(FILE_1_REF, 1); + verifyMeasuresOnFile(FILE_2_REF, 1); + verifyMeasuresOnFile(FILE_3_REF, 1); + verifyNoMeasure(UNIT_TEST_REF); + verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1, + entryOf(metric1Key, newMeasureBuilder().create(7)), entryOf(metric2Key, newMeasureBuilder().create(10))); + verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1, + entryOf(metric2Key, newMeasureBuilder().create(90))); + MeasureRepoEntry[] subModuleAndAboveEntries = { + entryOf(metric1Key, newMeasureBuilder().create(7)), + entryOf(metric2Key, newMeasureBuilder().create(100)) + }; + verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 2, subModuleAndAboveEntries); + verifyMeasuresOnOtherComponent(MODULE_REF, 3, 2, subModuleAndAboveEntries); + verifyMeasuresOnOtherComponent(ROOT_REF, 3, 2, subModuleAndAboveEntries); + } + + @Test + public void verify_NCLOC_measure_aggregation() { + verifyMetricAggregation(NCLOC_KEY); + } + + private void verifyMetricAggregation(String metricKey) { + measureRepository.addRawMeasure(FILE_1_REF, metricKey, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, metricKey, newMeasureBuilder().create(6)); + measureRepository.addRawMeasure(UNIT_TEST_REF, metricKey, newMeasureBuilder().create(3)); + + underTest.execute(); + + verifyMeasuresOnFile(FILE_1_REF, 1); + verifyMeasuresOnFile(FILE_2_REF, 1); + verifyMeasuresOnFile(FILE_3_REF, 1); + verifyNoMeasure(UNIT_TEST_REF); + verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1, entryOf(metricKey, newMeasureBuilder().create(16))); + verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1, entryOf(metricKey, newMeasureBuilder().create(3))); + verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 2, entryOf(metricKey, newMeasureBuilder().create(19))); + verifyMeasuresOnOtherComponent(MODULE_REF, 3, 2, entryOf(metricKey, newMeasureBuilder().create(19))); + verifyMeasuresOnOtherComponent(ROOT_REF, 3, 2, entryOf(metricKey, newMeasureBuilder().create(19))); + } + + @Test + public void verify_FUNCTIONS_and_STATEMENT_measure_aggregation() { + verifyTwoMeasureAggregation(FUNCTIONS_KEY, STATEMENTS_KEY); + } + + @Test + public void verify_CLASSES_measure_aggregation() { + verifyMetricAggregation(CLASSES_KEY); + } + + private void verifyMeasuresOnFile(int componentRef, int fileCount) { + assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))) + .containsOnly(entryOf(FILES_KEY, newMeasureBuilder().create(fileCount))); + } + + private void verifyMeasuresOnOtherComponent(int componentRef, int fileCount, int directoryCount, MeasureRepoEntry... otherMeasures) { + MeasureRepoEntry[] measureRepoEntries = concatIntoArray( + otherMeasures, + entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)), entryOf(DIRECTORIES_KEY, newMeasureBuilder().create(directoryCount))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))) + .containsOnly( + measureRepoEntries); + } + + private static MeasureRepoEntry[] concatIntoArray(MeasureRepoEntry[] otherMeasures, MeasureRepoEntry... measureRepoEntries) { + return from( + concat( + asList(otherMeasures), + asList(measureRepoEntries))).toArray(MeasureRepoEntry.class); + } + + private void verifyNoMeasure(int componentRef) { + assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).isEmpty(); + } +} |