diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-08-20 16:52:04 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-08-24 14:00:32 +0200 |
commit | 687bc3d92e9bdf270c930662d36f47ce23464561 (patch) | |
tree | 87790d7d956347a8c56b17f350169cb539c0c26c /server | |
parent | 6395d7c975ae1a7704d21d6fb3b2baade1bd7acf (diff) | |
download | sonarqube-687bc3d92e9bdf270c930662d36f47ce23464561.tar.gz sonarqube-687bc3d92e9bdf270c930662d36f47ce23464561.zip |
make DuplicationMeasuresStep support Views Component tree
Diffstat (limited to 'server')
5 files changed, 787 insertions, 399 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/DuplicationMeasuresStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/DuplicationMeasuresStep.java index aa058b199c4..f95579432cb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/DuplicationMeasuresStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/DuplicationMeasuresStep.java @@ -23,14 +23,14 @@ package org.sonar.server.computation.step; import com.google.common.base.Optional; 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.CrawlerDepthLimit; import org.sonar.server.computation.component.PathAwareCrawler; import org.sonar.server.computation.component.TreeRootHolder; import org.sonar.server.computation.formula.Counter; import org.sonar.server.computation.formula.CreateMeasureContext; -import org.sonar.server.computation.formula.LeafAggregateContext; import org.sonar.server.computation.formula.Formula; import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor; +import org.sonar.server.computation.formula.LeafAggregateContext; import org.sonar.server.computation.formula.SumCounter; import org.sonar.server.computation.measure.Measure; import org.sonar.server.computation.measure.MeasureRepository; @@ -45,7 +45,6 @@ import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY; import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_KEY; import static org.sonar.api.measures.CoreMetrics.LINES_KEY; import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; -import static org.sonar.server.computation.component.Component.Type.FILE; /** * Computes duplication measures on files and then aggregates them on higher components. @@ -102,7 +101,8 @@ public class DuplicationMeasuresStep implements ComputationStep { private Optional<Measure> createDuplicatedLinesMeasure(SumDuplicationCounter counter, CreateMeasureContext context) { int duplicatedLines = counter.value; - if (context.getMetric().getKey().equals(DUPLICATED_LINES_KEY) && context.getComponent().getType().isHigherThan(FILE)) { + if (context.getMetric().getKey().equals(DUPLICATED_LINES_KEY) + && CrawlerDepthLimit.LEAVES.isDeeperThan(context.getComponent().getType())) { return Optional.of(Measure.newMeasureBuilder().create(duplicatedLines)); } return Optional.absent(); @@ -156,7 +156,7 @@ public class DuplicationMeasuresStep implements ComputationStep { @Override public Optional<Measure> createMeasure(SumDuplicationCounter counter, CreateMeasureContext context) { int value = counter.value; - if (context.getComponent().getType().isHigherThan(Component.Type.FILE)) { + if (CrawlerDepthLimit.LEAVES.isDeeperThan(context.getComponent().getType())) { return Optional.of(Measure.newMeasureBuilder().create(value)); } return Optional.absent(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ViewsComponent.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ViewsComponent.java index c8a54109faa..326b13ff279 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ViewsComponent.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ViewsComponent.java @@ -48,7 +48,7 @@ public class ViewsComponent implements Component { private ViewsComponent(Type type, String key, @Nullable String uuid, @Nullable String name, List<Component> children, @Nullable ProjectViewAttributes projectViewAttributes) { - checkArgument(type.isViewsType()); + checkArgument(type.isViewsType(), "Component type must be a Views type"); this.type = type; this.key = requireNonNull(key); this.uuid = uuid; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/DuplicationMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/DuplicationMeasuresStepTest.java deleted file mode 100644 index 17f08370daf..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/DuplicationMeasuresStepTest.java +++ /dev/null @@ -1,393 +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.Before; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.server.computation.batch.TreeRootHolderRule; -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.assertj.guava.api.Assertions.assertThat; -import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES; -import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS_KEY; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES_KEY; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY; -import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_KEY; -import static org.sonar.api.measures.CoreMetrics.LINES; -import static org.sonar.api.measures.CoreMetrics.LINES_KEY; -import static org.sonar.api.measures.CoreMetrics.NCLOC; -import static org.sonar.api.measures.CoreMetrics.NCLOC_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.ReportComponent.builder; -import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; - -public class DuplicationMeasuresStepTest { - - 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_REF = 1234; - private static final int FILE_1_REF = 12341; - private static final int FILE_2_REF = 12342; - - @Rule - public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); - - @Rule - public MetricRepositoryRule metricRepository = new MetricRepositoryRule() - .add(LINES) - .add(NCLOC) - .add(COMMENT_LINES) - .add(DUPLICATED_BLOCKS) - .add(DUPLICATED_FILES) - .add(DUPLICATED_LINES) - .add(DUPLICATED_LINES_DENSITY); - - @Rule - public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); - - ComputationStep underTest = new DuplicationMeasuresStep(treeRootHolder, metricRepository, measureRepository); - - @Before - public void setUp() throws Exception { - treeRootHolder.setRoot( - builder(PROJECT, ROOT_REF) - .addChildren( - builder(MODULE, MODULE_REF) - .addChildren( - builder(MODULE, SUB_MODULE_REF) - .addChildren( - builder(DIRECTORY, DIRECTORY_REF) - .addChildren( - builder(FILE, FILE_1_REF).build(), - builder(FILE, FILE_2_REF).build() - ).build() - ).build() - ).build() - ).build()); - } - - @Test - public void aggregate_duplicated_blocks() { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY, newMeasureBuilder().create(40)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(50); - } - - @Test - public void aggregate_zero_duplicated_blocks() { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY, newMeasureBuilder().create(0)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - } - - @Test - public void aggregate_zero_duplicated_blocks_when_no_data() { - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_BLOCKS_KEY).get().getIntValue()).isEqualTo(0); - } - - @Test - public void aggregate_duplicated_files() { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY, newMeasureBuilder().create(40)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(50); - } - - @Test - public void aggregate_zero_duplicated_files() { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY, newMeasureBuilder().create(0)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - } - - @Test - public void aggregate_zero_duplicated_files_when_no_data() { - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_FILES_KEY).get().getIntValue()).isEqualTo(0); - } - - @Test - public void aggregate_duplicated_lines() { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(40)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(50); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(50); - } - - @Test - public void aggregate_zero_duplicated_line() { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(0)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - } - - @Test - public void aggregate_zero_duplicated_line_when_no_data() { - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY)).isAbsent(); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_KEY).get().getIntValue()).isEqualTo(0); - } - - @Test - public void compute_and_aggregate_duplicated_lines_density_using_lines() throws Exception { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(2)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(3)); - - measureRepository.addRawMeasure(FILE_1_REF, LINES_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(FILE_2_REF, LINES_KEY, newMeasureBuilder().create(40)); - measureRepository.addRawMeasure(DIRECTORY_REF, LINES_KEY, newMeasureBuilder().create(50)); - measureRepository.addRawMeasure(SUB_MODULE_REF, LINES_KEY, newMeasureBuilder().create(50)); - measureRepository.addRawMeasure(MODULE_REF, LINES_KEY, newMeasureBuilder().create(50)); - measureRepository.addRawMeasure(ROOT_REF, LINES_KEY, newMeasureBuilder().create(50)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(20d); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(7.5d); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - } - - @Test - public void compute_and_aggregate_duplicated_lines_density_using_nclocs_and_comment_lines() throws Exception { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(2)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(3)); - - measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(2)); - measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - measureRepository.addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - measureRepository.addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - measureRepository.addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - - measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(8)); - measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(30)); - measureRepository.addRawMeasure(DIRECTORY_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - measureRepository.addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - measureRepository.addRawMeasure(MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(20d); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(7.5d); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - } - - @Test - public void compute_duplicated_lines_density_using_only_nclocs() throws Exception { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(2)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(3)); - - measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(40)); - measureRepository.addRawMeasure(DIRECTORY_REF, NCLOC_KEY, newMeasureBuilder().create(50)); - measureRepository.addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(50)); - measureRepository.addRawMeasure(MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(50)); - measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(50)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(20d); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(7.5d); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(10d); - } - - @Test - public void compute_zero_percent_duplicated_lines_density_when_duplicated_lines_are_zero() throws Exception { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(0)); - - measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(2)); - measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - measureRepository.addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - measureRepository.addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - measureRepository.addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(12)); - - measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(8)); - measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(30)); - measureRepository.addRawMeasure(DIRECTORY_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - measureRepository.addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - measureRepository.addRawMeasure(MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(38)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); - } - - @Test - public void not_compute_duplicated_lines_density_when_lines_is_zero() throws Exception { - measureRepository.addRawMeasure(FILE_1_REF, LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(FILE_2_REF, LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(DIRECTORY_REF, LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(SUB_MODULE_REF, LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(MODULE_REF, LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(ROOT_REF, LINES_KEY, newMeasureBuilder().create(0)); - - underTest.execute(); - - assertNoNewMeasures(DUPLICATED_LINES_DENSITY_KEY); - } - - @Test - public void not_compute_duplicated_lines_density_when_ncloc_and_comment_are_zero() throws Exception { - measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); - - measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(DIRECTORY_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - - underTest.execute(); - - assertNoNewMeasures(DUPLICATED_LINES_DENSITY_KEY); - } - - @Test - public void compute_100_percent_duplicated_lines_density() throws Exception { - measureRepository.addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(2)); - measureRepository.addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, newMeasureBuilder().create(3)); - - measureRepository.addRawMeasure(FILE_1_REF, LINES_KEY, newMeasureBuilder().create(2)); - measureRepository.addRawMeasure(FILE_2_REF, LINES_KEY, newMeasureBuilder().create(3)); - measureRepository.addRawMeasure(DIRECTORY_REF, LINES_KEY, newMeasureBuilder().create(5)); - measureRepository.addRawMeasure(SUB_MODULE_REF, LINES_KEY, newMeasureBuilder().create(5)); - measureRepository.addRawMeasure(MODULE_REF, LINES_KEY, newMeasureBuilder().create(5)); - measureRepository.addRawMeasure(ROOT_REF, LINES_KEY, newMeasureBuilder().create(5)); - - underTest.execute(); - - assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); - assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); - assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); - assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); - assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); - assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); - } - - private void assertNoNewMeasures(String metricKey){ - assertThat(measureRepository.getAddedRawMeasures(FILE_1_REF).get(metricKey)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(FILE_2_REF).get(metricKey)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(DIRECTORY_REF).get(metricKey)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(SUB_MODULE_REF).get(metricKey)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(MODULE_REF).get(metricKey)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(ROOT_REF).get(metricKey)).isEmpty(); - } - -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportDuplicationMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportDuplicationMeasuresStepTest.java new file mode 100644 index 00000000000..650255cea78 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportDuplicationMeasuresStepTest.java @@ -0,0 +1,402 @@ +/* + * 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.server.computation.batch.TreeRootHolderRule; +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.assertj.guava.api.Assertions.assertThat; +import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES; +import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.LINES; +import static org.sonar.api.measures.CoreMetrics.LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.NCLOC; +import static org.sonar.api.measures.CoreMetrics.NCLOC_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.ReportComponent.builder; +import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; + +public class ReportDuplicationMeasuresStepTest { + + 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_REF = 1234; + private static final int FILE_1_REF = 12341; + private static final int FILE_2_REF = 12342; + + @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_REF) + .addChildren( + builder(FILE, FILE_1_REF).build(), + builder(FILE, FILE_2_REF).build()) + .build()) + .build()) + .build()) + .build()); + @Rule + public MetricRepositoryRule metricRepository = new MetricRepositoryRule() + .add(LINES) + .add(NCLOC) + .add(COMMENT_LINES) + .add(DUPLICATED_BLOCKS) + .add(DUPLICATED_FILES) + .add(DUPLICATED_LINES) + .add(DUPLICATED_LINES_DENSITY); + @Rule + public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); + + ComputationStep underTest = new DuplicationMeasuresStep(treeRootHolder, metricRepository, measureRepository); + + @Test + public void aggregate_duplicated_blocks() { + addRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY, 10); + addRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY, 40); + + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_BLOCKS_KEY, 50); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_BLOCKS_KEY, 50); + assertRawMeasureValue(MODULE_REF, DUPLICATED_BLOCKS_KEY, 50); + assertRawMeasureValue(ROOT_REF, DUPLICATED_BLOCKS_KEY, 50); + } + + @Test + public void aggregate_zero_duplicated_blocks() { + addRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY, 0); + addRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY, 0); + + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(MODULE_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_BLOCKS_KEY, 0); + } + + @Test + public void aggregate_zero_duplicated_blocks_when_no_data() { + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_BLOCKS_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_BLOCKS_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(MODULE_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_BLOCKS_KEY, 0); + } + + @Test + public void aggregate_duplicated_files() { + addRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY, 10); + addRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY, 40); + + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_FILES_KEY, 50); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_FILES_KEY, 50); + assertRawMeasureValue(MODULE_REF, DUPLICATED_FILES_KEY, 50); + assertRawMeasureValue(ROOT_REF, DUPLICATED_FILES_KEY, 50); + } + + @Test + public void aggregate_zero_duplicated_files() { + addRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY, 0); + addRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY, 0); + + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(MODULE_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_FILES_KEY, 0); + } + + @Test + public void aggregate_zero_duplicated_files_when_no_data() { + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_FILES_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_FILES_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(MODULE_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_FILES_KEY, 0); + } + + @Test + public void aggregate_duplicated_lines() { + addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, 10); + addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, 40); + + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_KEY, 50); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_KEY, 50); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_KEY, 50); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_KEY, 50); + } + + @Test + public void aggregate_zero_duplicated_line() { + addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, 0); + addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, 0); + + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_KEY, 0); + } + + @Test + public void aggregate_zero_duplicated_line_when_no_data() { + underTest.execute(); + + assertNoRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY); + assertNoRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_KEY, 0); + } + + @Test + public void compute_and_aggregate_duplicated_lines_density_using_lines() throws Exception { + addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, 3); + + addRawMeasure(FILE_1_REF, LINES_KEY, 10); + addRawMeasure(FILE_2_REF, LINES_KEY, 40); + addRawMeasure(DIRECTORY_REF, LINES_KEY, 50); + addRawMeasure(SUB_MODULE_REF, LINES_KEY, 50); + addRawMeasure(MODULE_REF, LINES_KEY, 50); + addRawMeasure(ROOT_REF, LINES_KEY, 50); + + underTest.execute(); + + assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 20d); + assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 7.5d); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + } + + @Test + public void compute_and_aggregate_duplicated_lines_density_using_nclocs_and_comment_lines() throws Exception { + addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, 3); + + addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, 2); + addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, 10); + addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, 12); + + addRawMeasure(FILE_1_REF, NCLOC_KEY, 8); + addRawMeasure(FILE_2_REF, NCLOC_KEY, 30); + addRawMeasure(DIRECTORY_REF, NCLOC_KEY, 38); + addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, 38); + addRawMeasure(MODULE_REF, NCLOC_KEY, 38); + addRawMeasure(ROOT_REF, NCLOC_KEY, 38); + + underTest.execute(); + + assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 20d); + assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 7.5d); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + } + + @Test + public void compute_duplicated_lines_density_using_only_nclocs() throws Exception { + addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, 3); + + addRawMeasure(FILE_1_REF, NCLOC_KEY, 10); + addRawMeasure(FILE_2_REF, NCLOC_KEY, 40); + addRawMeasure(DIRECTORY_REF, NCLOC_KEY, 50); + addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, 50); + addRawMeasure(MODULE_REF, NCLOC_KEY, 50); + addRawMeasure(ROOT_REF, NCLOC_KEY, 50); + + underTest.execute(); + + assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 20d); + assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 7.5d); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + } + + @Test + public void compute_zero_percent_duplicated_lines_density_when_duplicated_lines_are_zero() throws Exception { + addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, 0); + addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, 0); + + addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, 2); + addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, 10); + addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, 12); + + addRawMeasure(FILE_1_REF, NCLOC_KEY, 8); + addRawMeasure(FILE_2_REF, NCLOC_KEY, 30); + addRawMeasure(DIRECTORY_REF, NCLOC_KEY, 38); + addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, 38); + addRawMeasure(MODULE_REF, NCLOC_KEY, 38); + addRawMeasure(ROOT_REF, NCLOC_KEY, 38); + + underTest.execute(); + + assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + } + + @Test + public void not_compute_duplicated_lines_density_when_lines_is_zero() throws Exception { + addRawMeasure(FILE_1_REF, LINES_KEY, 0); + addRawMeasure(FILE_2_REF, LINES_KEY, 0); + addRawMeasure(DIRECTORY_REF, LINES_KEY, 0); + addRawMeasure(SUB_MODULE_REF, LINES_KEY, 0); + addRawMeasure(MODULE_REF, LINES_KEY, 0); + addRawMeasure(ROOT_REF, LINES_KEY, 0); + + underTest.execute(); + + assertNoRawMeasures(DUPLICATED_LINES_DENSITY_KEY); + } + + @Test + public void not_compute_duplicated_lines_density_when_ncloc_and_comment_are_zero() throws Exception { + addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, 0); + + addRawMeasure(FILE_1_REF, NCLOC_KEY, 0); + addRawMeasure(FILE_2_REF, NCLOC_KEY, 0); + addRawMeasure(DIRECTORY_REF, NCLOC_KEY, 0); + addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, 0); + addRawMeasure(MODULE_REF, NCLOC_KEY, 0); + addRawMeasure(ROOT_REF, NCLOC_KEY, 0); + + underTest.execute(); + + assertNoRawMeasures(DUPLICATED_LINES_DENSITY_KEY); + } + + @Test + public void compute_100_percent_duplicated_lines_density() throws Exception { + addRawMeasure(FILE_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(FILE_2_REF, DUPLICATED_LINES_KEY, 3); + + addRawMeasure(FILE_1_REF, LINES_KEY, 2); + addRawMeasure(FILE_2_REF, LINES_KEY, 3); + addRawMeasure(DIRECTORY_REF, LINES_KEY, 5); + addRawMeasure(SUB_MODULE_REF, LINES_KEY, 5); + addRawMeasure(MODULE_REF, LINES_KEY, 5); + addRawMeasure(ROOT_REF, LINES_KEY, 5); + + underTest.execute(); + + assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + assertRawMeasureValue(SUB_MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + assertRawMeasureValue(MODULE_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + } + + private void addRawMeasure(int componentRef, String metricKey, int value) { + measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value)); + } + + private void assertNoRawMeasures(String metricKey) { + assertThat(measureRepository.getAddedRawMeasures(FILE_1_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(FILE_2_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(DIRECTORY_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(SUB_MODULE_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(MODULE_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(ROOT_REF).get(metricKey)).isEmpty(); + } + + private void assertNoRawMeasure(int componentRef, String metricKey) { + assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey)).isAbsent(); + } + + private void assertRawMeasureValue(int componentRef, String metricKey, int value) { + assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey).get().getIntValue()).isEqualTo(value); + } + + private void assertRawMeasureValue(int componentRef, String metricKey, double value) { + assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey).get().getDoubleValue()).isEqualTo(value); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsDuplicationMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsDuplicationMeasuresStepTest.java new file mode 100644 index 00000000000..64dd85ba8f3 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsDuplicationMeasuresStepTest.java @@ -0,0 +1,379 @@ +/* + * 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.server.computation.batch.TreeRootHolderRule; +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.assertj.guava.api.Assertions.assertThat; +import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES; +import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.LINES; +import static org.sonar.api.measures.CoreMetrics.LINES_KEY; +import static org.sonar.api.measures.CoreMetrics.NCLOC; +import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; +import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW; +import static org.sonar.server.computation.component.Component.Type.SUBVIEW; +import static org.sonar.server.computation.component.Component.Type.VIEW; +import static org.sonar.server.computation.component.ViewsComponent.builder; +import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; + +public class ViewsDuplicationMeasuresStepTest { + + private static final int ROOT_REF = 1; + private static final int SUBVIEW_REF = 12; + private static final int SUB_SUBVIEW_REF = 123; + private static final int PROJECT_VIEW_1_REF = 1231; + private static final int PROJECT_VIEW_2_REF = 1232; + private static final int PROJECT_VIEW_3_REF = 13; + + @Rule + public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule() + .setRoot(builder(VIEW, ROOT_REF) + .addChildren( + builder(SUBVIEW, SUBVIEW_REF) + .addChildren( + builder(SUBVIEW, SUB_SUBVIEW_REF) + .addChildren( + builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(), + builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build()) + .build()) + .build(), + builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build()) + .build()); + @Rule + public MetricRepositoryRule metricRepository = new MetricRepositoryRule() + .add(LINES) + .add(NCLOC) + .add(COMMENT_LINES) + .add(DUPLICATED_BLOCKS) + .add(DUPLICATED_FILES) + .add(DUPLICATED_LINES) + .add(DUPLICATED_LINES_DENSITY); + @Rule + public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); + + ComputationStep underTest = new DuplicationMeasuresStep(treeRootHolder, metricRepository, measureRepository); + + @Test + public void aggregate_duplicated_blocks() { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_BLOCKS_KEY, 10); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_BLOCKS_KEY, 40); + addRawMeasure(PROJECT_VIEW_3_REF, DUPLICATED_BLOCKS_KEY, 60); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_BLOCKS_KEY, 50); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_BLOCKS_KEY, 50); + assertRawMeasureValue(ROOT_REF, DUPLICATED_BLOCKS_KEY, 110); + } + + @Test + public void aggregate_zero_duplicated_blocks() { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_BLOCKS_KEY, 0); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_BLOCKS_KEY, 0); + // no raw measure for PROJECT_VIEW_3_REF + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_BLOCKS_KEY, 0); + } + + @Test + public void aggregate_zero_duplicated_blocks_when_no_data() { + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_BLOCKS_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_BLOCKS_KEY, 0); + } + + @Test + public void aggregate_duplicated_files() { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_FILES_KEY, 10); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_FILES_KEY, 40); + addRawMeasure(PROJECT_VIEW_3_REF, DUPLICATED_FILES_KEY, 70); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_FILES_KEY, 50); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_FILES_KEY, 50); + assertRawMeasureValue(ROOT_REF, DUPLICATED_FILES_KEY, 120); + } + + @Test + public void aggregate_zero_duplicated_files() { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_FILES_KEY, 0); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_FILES_KEY, 0); + // no raw measure for PROJECT_VIEW_3_REF + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_FILES_KEY, 0); + } + + @Test + public void aggregate_zero_duplicated_files_when_no_data() { + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_FILES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_FILES_KEY, 0); + } + + @Test + public void aggregate_duplicated_lines() { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_LINES_KEY, 10); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_LINES_KEY, 40); + addRawMeasure(PROJECT_VIEW_3_REF, DUPLICATED_LINES_KEY, 50); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_KEY, 50); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_KEY, 50); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_KEY, 100); + } + + @Test + public void aggregate_zero_duplicated_line() { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_LINES_KEY, 0); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_LINES_KEY, 0); + // no raw measure for PROJECT_VIEW_3_REF + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_KEY, 0); + } + + @Test + public void aggregate_zero_duplicated_line_when_no_data() { + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_KEY, 0); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_KEY, 0); + } + + @Test + public void compute_and_aggregate_duplicated_lines_density_using_lines() throws Exception { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_LINES_KEY, 3); + addRawMeasure(PROJECT_VIEW_3_REF, DUPLICATED_LINES_KEY, 4); + + addRawMeasure(PROJECT_VIEW_1_REF, LINES_KEY, 10); + addRawMeasure(PROJECT_VIEW_2_REF, LINES_KEY, 40); + addRawMeasure(PROJECT_VIEW_3_REF, LINES_KEY, 70); + addRawMeasure(SUB_SUBVIEW_REF, LINES_KEY, 50); + addRawMeasure(SUBVIEW_REF, LINES_KEY, 50); + addRawMeasure(ROOT_REF, LINES_KEY, 50); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 18d); + } + + @Test + public void compute_and_aggregate_duplicated_lines_density_using_nclocs_and_comment_lines() throws Exception { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_LINES_KEY, 3); + addRawMeasure(PROJECT_VIEW_3_REF, DUPLICATED_LINES_KEY, 4); + + addRawMeasure(PROJECT_VIEW_1_REF, COMMENT_LINES_KEY, 2); + addRawMeasure(PROJECT_VIEW_2_REF, COMMENT_LINES_KEY, 10); + addRawMeasure(PROJECT_VIEW_3_REF, COMMENT_LINES_KEY, 20); + addRawMeasure(SUB_SUBVIEW_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(SUBVIEW_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, 12); + + addRawMeasure(PROJECT_VIEW_1_REF, NCLOC_KEY, 8); + addRawMeasure(PROJECT_VIEW_2_REF, NCLOC_KEY, 30); + addRawMeasure(PROJECT_VIEW_3_REF, NCLOC_KEY, 50); + addRawMeasure(SUB_SUBVIEW_REF, NCLOC_KEY, 38); + addRawMeasure(SUBVIEW_REF, NCLOC_KEY, 38); + addRawMeasure(ROOT_REF, NCLOC_KEY, 38); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 18d); + } + + @Test + public void compute_duplicated_lines_density_using_only_nclocs() throws Exception { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_LINES_KEY, 3); + + addRawMeasure(PROJECT_VIEW_1_REF, NCLOC_KEY, 10); + addRawMeasure(PROJECT_VIEW_2_REF, NCLOC_KEY, 40); + addRawMeasure(SUB_SUBVIEW_REF, NCLOC_KEY, 50); + addRawMeasure(SUBVIEW_REF, NCLOC_KEY, 50); + addRawMeasure(ROOT_REF, NCLOC_KEY, 50); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 10d); + } + + @Test + public void compute_zero_percent_duplicated_lines_density_when_duplicated_lines_are_zero() throws Exception { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_LINES_KEY, 0); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_LINES_KEY, 0); + // no raw measure for PROJECT_VIEW_3_REF + + addRawMeasure(PROJECT_VIEW_1_REF, COMMENT_LINES_KEY, 2); + addRawMeasure(PROJECT_VIEW_2_REF, COMMENT_LINES_KEY, 10); + addRawMeasure(SUB_SUBVIEW_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(SUBVIEW_REF, COMMENT_LINES_KEY, 12); + addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, 12); + + addRawMeasure(PROJECT_VIEW_1_REF, NCLOC_KEY, 8); + addRawMeasure(PROJECT_VIEW_2_REF, NCLOC_KEY, 30); + addRawMeasure(SUB_SUBVIEW_REF, NCLOC_KEY, 38); + addRawMeasure(SUBVIEW_REF, NCLOC_KEY, 38); + addRawMeasure(ROOT_REF, NCLOC_KEY, 38); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 0d); + } + + @Test + public void not_compute_duplicated_lines_density_when_lines_is_zero() throws Exception { + addRawMeasure(PROJECT_VIEW_1_REF, LINES_KEY, 0); + addRawMeasure(PROJECT_VIEW_2_REF, LINES_KEY, 0); + // no raw measure for PROJECT_VIEW_3_REF + addRawMeasure(SUB_SUBVIEW_REF, LINES_KEY, 0); + addRawMeasure(SUBVIEW_REF, LINES_KEY, 0); + addRawMeasure(ROOT_REF, LINES_KEY, 0); + + underTest.execute(); + + assertNoRawMeasures(DUPLICATED_LINES_DENSITY_KEY); + } + + @Test + public void not_compute_duplicated_lines_density_when_ncloc_and_comment_are_zero() throws Exception { + addRawMeasure(PROJECT_VIEW_1_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(PROJECT_VIEW_2_REF, COMMENT_LINES_KEY, 0); + // no raw measure for PROJECT_VIEW_3_REF + addRawMeasure(SUB_SUBVIEW_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(SUBVIEW_REF, COMMENT_LINES_KEY, 0); + addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, 0); + + addRawMeasure(PROJECT_VIEW_1_REF, NCLOC_KEY, 0); + addRawMeasure(PROJECT_VIEW_2_REF, NCLOC_KEY, 0); + addRawMeasure(SUB_SUBVIEW_REF, NCLOC_KEY, 0); + addRawMeasure(SUBVIEW_REF, NCLOC_KEY, 0); + addRawMeasure(ROOT_REF, NCLOC_KEY, 0); + + underTest.execute(); + + assertNoRawMeasures(DUPLICATED_LINES_DENSITY_KEY); + } + + @Test + public void compute_100_percent_duplicated_lines_density() throws Exception { + addRawMeasure(PROJECT_VIEW_1_REF, DUPLICATED_LINES_KEY, 2); + addRawMeasure(PROJECT_VIEW_2_REF, DUPLICATED_LINES_KEY, 3); + // no raw measure for PROJECT_VIEW_3_REF + + addRawMeasure(PROJECT_VIEW_1_REF, LINES_KEY, 2); + addRawMeasure(PROJECT_VIEW_2_REF, LINES_KEY, 3); + addRawMeasure(SUB_SUBVIEW_REF, LINES_KEY, 5); + addRawMeasure(SUBVIEW_REF, LINES_KEY, 5); + addRawMeasure(ROOT_REF, LINES_KEY, 5); + + underTest.execute(); + + assertNoNewRawMeasuresOnProjectViews(); + assertRawMeasureValue(SUB_SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + assertRawMeasureValue(SUBVIEW_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 100d); + } + + private void addRawMeasure(int componentRef, String metricKey, int value) { + measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value)); + } + + private void assertNoRawMeasures(String metricKey) { + assertThat(measureRepository.getAddedRawMeasures(PROJECT_VIEW_1_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(PROJECT_VIEW_2_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(SUBVIEW_REF).get(metricKey)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(ROOT_REF).get(metricKey)).isEmpty(); + } + + private void assertNoNewRawMeasuresOnProjectViews() { + assertThat(measureRepository.getAddedRawMeasures(PROJECT_VIEW_1_REF)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(PROJECT_VIEW_2_REF)).isEmpty(); + assertThat(measureRepository.getAddedRawMeasures(PROJECT_VIEW_3_REF)).isEmpty(); + } + + private void assertNoRawMeasure(int componentRef, String metricKey) { + assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey)).isAbsent(); + } + + private void assertRawMeasureValue(int componentRef, String metricKey, int value) { + assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey).get().getIntValue()).isEqualTo(value); + } + + private void assertRawMeasureValue(int componentRef, String metricKey, double value) { + assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey).get().getDoubleValue()).isEqualTo(value); + } + +} |