You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SumFormulaExecutionTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.formula;
  21. import com.google.common.collect.Lists;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.measures.CoreMetrics;
  26. import org.sonar.ce.task.projectanalysis.component.Component;
  27. import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler;
  28. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  29. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  30. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  31. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
  34. import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
  35. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  36. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  37. import static org.sonar.ce.task.projectanalysis.formula.SumFormula.createIntSumFormula;
  38. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  39. import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
  40. import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
  41. public class SumFormulaExecutionTest {
  42. @Rule
  43. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  44. @Rule
  45. public MetricRepositoryRule metricRepository = new MetricRepositoryRule().add(CoreMetrics.LINES);
  46. @Rule
  47. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  48. FormulaExecutorComponentVisitor underTest;
  49. @Before
  50. public void setUp() {
  51. underTest = FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository)
  52. .buildFor(Lists.newArrayList(createIntSumFormula(LINES_KEY)));
  53. }
  54. @Test
  55. public void add_measures() {
  56. ReportComponent project = builder(PROJECT, 1)
  57. .addChildren(
  58. builder(DIRECTORY, 111)
  59. .addChildren(
  60. builder(Component.Type.FILE, 1111).build(),
  61. builder(Component.Type.FILE, 1112).build())
  62. .build(),
  63. builder(DIRECTORY, 121)
  64. .addChildren(
  65. builder(Component.Type.FILE, 1211).build())
  66. .build())
  67. .build();
  68. treeRootHolder.setRoot(project);
  69. measureRepository.addRawMeasure(1111, LINES_KEY, newMeasureBuilder().create(10));
  70. measureRepository.addRawMeasure(1112, LINES_KEY, newMeasureBuilder().create(8));
  71. measureRepository.addRawMeasure(1211, LINES_KEY, newMeasureBuilder().create(2));
  72. new PathAwareCrawler<>(underTest).visit(project);
  73. assertThat(toEntries(measureRepository.getAddedRawMeasures(1))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(20)));
  74. assertThat(toEntries(measureRepository.getAddedRawMeasures(111))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(18)));
  75. assertThat(measureRepository.getAddedRawMeasures(1111)).isEmpty();
  76. assertThat(measureRepository.getAddedRawMeasures(1112)).isEmpty();
  77. assertThat(toEntries(measureRepository.getAddedRawMeasures(121))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(2)));
  78. assertThat(measureRepository.getAddedRawMeasures(1211)).isEmpty();
  79. }
  80. @Test
  81. public void not_add_measures_when_no_data_on_file() {
  82. ReportComponent project = builder(PROJECT, 1)
  83. .addChildren(
  84. builder(DIRECTORY, 111)
  85. .addChildren(
  86. builder(Component.Type.FILE, 1111).build())
  87. .build())
  88. .build();
  89. treeRootHolder.setRoot(project);
  90. new PathAwareCrawler<>(underTest).visit(project);
  91. assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty();
  92. assertThat(measureRepository.getAddedRawMeasures(111)).isEmpty();
  93. assertThat(measureRepository.getAddedRawMeasures(1111)).isEmpty();
  94. }
  95. }