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.

AverageFormulaExecutionTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
  35. import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
  36. import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
  37. import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
  38. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  39. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  40. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  41. import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
  42. import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
  43. public class AverageFormulaExecutionTest {
  44. @Rule
  45. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  46. @Rule
  47. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  48. .add(CoreMetrics.FUNCTION_COMPLEXITY)
  49. .add(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)
  50. .add(CoreMetrics.FUNCTIONS);
  51. @Rule
  52. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  53. @Rule
  54. public PeriodHolderRule periodsHolder = new PeriodHolderRule();
  55. private FormulaExecutorComponentVisitor underTest;
  56. @Before
  57. public void setUp() {
  58. underTest = FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository)
  59. .buildFor(Lists.newArrayList(
  60. AverageFormula.Builder.newBuilder()
  61. .setOutputMetricKey(FUNCTION_COMPLEXITY_KEY)
  62. .setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY)
  63. .setByMetricKey(FUNCTIONS_KEY)
  64. .build()));
  65. }
  66. @Test
  67. public void add_measures() {
  68. ReportComponent project = builder(PROJECT, 1)
  69. .addChildren(
  70. builder(DIRECTORY, 111)
  71. .addChildren(
  72. builder(Component.Type.FILE, 1111).build(),
  73. builder(Component.Type.FILE, 1112).build())
  74. .build(),
  75. builder(DIRECTORY, 121)
  76. .addChildren(
  77. builder(Component.Type.FILE, 1211).build())
  78. .build())
  79. .build();
  80. treeRootHolder.setRoot(project);
  81. measureRepository.addRawMeasure(1111, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(5));
  82. measureRepository.addRawMeasure(1111, FUNCTIONS_KEY, newMeasureBuilder().create(2));
  83. measureRepository.addRawMeasure(1112, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(1));
  84. measureRepository.addRawMeasure(1112, FUNCTIONS_KEY, newMeasureBuilder().create(1));
  85. measureRepository.addRawMeasure(1211, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(9));
  86. measureRepository.addRawMeasure(1211, FUNCTIONS_KEY, newMeasureBuilder().create(2));
  87. new PathAwareCrawler<>(underTest).visit(project);
  88. assertThat(toEntries(measureRepository.getAddedRawMeasures(1))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(3d, 1)));
  89. assertThat(toEntries(measureRepository.getAddedRawMeasures(111))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(2d, 1)));
  90. assertThat(toEntries(measureRepository.getAddedRawMeasures(1111))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(2.5d, 1)));
  91. assertThat(toEntries(measureRepository.getAddedRawMeasures(1112))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(1d, 1)));
  92. assertThat(toEntries(measureRepository.getAddedRawMeasures(121))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(4.5d, 1)));
  93. assertThat(toEntries(measureRepository.getAddedRawMeasures(1211))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(4.5d, 1)));
  94. }
  95. @Test
  96. public void not_add_measures_when_no_data_on_file() {
  97. ReportComponent project = builder(PROJECT, 1)
  98. .addChildren(
  99. builder(DIRECTORY, 111)
  100. .addChildren(
  101. builder(Component.Type.FILE, 1111).build())
  102. .build())
  103. .build();
  104. treeRootHolder.setRoot(project);
  105. new PathAwareCrawler<>(underTest).visit(project);
  106. assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty();
  107. assertThat(measureRepository.getAddedRawMeasures(111)).isEmpty();
  108. assertThat(measureRepository.getAddedRawMeasures(1111)).isEmpty();
  109. }
  110. }