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.

ViewsComplexityMeasuresStepTest.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.step;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  24. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  25. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  26. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  27. import org.sonar.ce.task.step.ComputationStep;
  28. import org.sonar.ce.task.step.TestComputationStepContext;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.sonar.api.measures.CoreMetrics.CLASSES;
  31. import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
  32. import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY;
  33. import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_KEY;
  34. import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY;
  35. import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY_KEY;
  36. import static org.sonar.api.measures.CoreMetrics.COMPLEXITY;
  37. import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES;
  38. import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES_KEY;
  39. import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS;
  40. import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
  41. import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_KEY;
  42. import static org.sonar.api.measures.CoreMetrics.FILES;
  43. import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
  44. import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY;
  45. import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION;
  46. import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY;
  47. import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_KEY;
  48. import static org.sonar.api.measures.CoreMetrics.FUNCTIONS;
  49. import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
  50. import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY;
  51. import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION;
  52. import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
  53. import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
  54. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
  55. import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
  56. import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
  57. import static org.sonar.ce.task.projectanalysis.component.ViewsComponent.builder;
  58. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  59. import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
  60. import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
  61. public class ViewsComplexityMeasuresStepTest {
  62. private static final int ROOT_REF = 1;
  63. private static final int SUBVIEW_REF = 11;
  64. private static final int SUB_SUBVIEW_1_REF = 111;
  65. private static final int PROJECT_VIEW_1_REF = 11111;
  66. private static final int PROJECT_VIEW_2_REF = 11121;
  67. private static final int SUB_SUBVIEW_2_REF = 112;
  68. private static final int PROJECT_VIEW_3_REF = 12;
  69. @Rule
  70. public BatchReportReaderRule reportReader = new BatchReportReaderRule();
  71. @Rule
  72. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
  73. .setRoot(builder(VIEW, ROOT_REF)
  74. .addChildren(
  75. builder(SUBVIEW, SUBVIEW_REF)
  76. .addChildren(
  77. builder(SUBVIEW, SUB_SUBVIEW_1_REF)
  78. .addChildren(
  79. builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(),
  80. builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build())
  81. .build(),
  82. builder(SUBVIEW, SUB_SUBVIEW_2_REF).build())
  83. .build(),
  84. builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build())
  85. .build());
  86. @Rule
  87. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  88. .add(COMPLEXITY)
  89. .add(COMPLEXITY_IN_CLASSES)
  90. .add(COMPLEXITY_IN_FUNCTIONS)
  91. .add(FUNCTION_COMPLEXITY_DISTRIBUTION)
  92. .add(FILE_COMPLEXITY_DISTRIBUTION)
  93. .add(FILE_COMPLEXITY)
  94. .add(FILES)
  95. .add(CLASS_COMPLEXITY)
  96. .add(CLASSES)
  97. .add(FUNCTION_COMPLEXITY)
  98. .add(FUNCTIONS)
  99. .add(COGNITIVE_COMPLEXITY);
  100. @Rule
  101. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  102. private ComputationStep underTest = new ComplexityMeasuresStep(treeRootHolder, metricRepository, measureRepository);
  103. @Test
  104. public void aggregate_complexity() {
  105. verify_sum_aggregation(COMPLEXITY_KEY);
  106. }
  107. @Test
  108. public void aggregate_complexity_in_classes() {
  109. verify_sum_aggregation(COMPLEXITY_IN_CLASSES_KEY);
  110. }
  111. @Test
  112. public void aggregate_complexity_in_functions() {
  113. verify_sum_aggregation(COMPLEXITY_IN_FUNCTIONS_KEY);
  114. }
  115. @Test
  116. public void aggregate_cognitive_complexity_in_functions() {
  117. verify_sum_aggregation(COGNITIVE_COMPLEXITY_KEY);
  118. }
  119. private void verify_sum_aggregation(String metricKey) {
  120. addRawMeasureValue(PROJECT_VIEW_1_REF, metricKey, 10);
  121. addRawMeasureValue(PROJECT_VIEW_2_REF, metricKey, 40);
  122. addRawMeasureValue(PROJECT_VIEW_3_REF, metricKey, 20);
  123. underTest.execute(new TestComputationStepContext());
  124. assertNoAddedRawMeasureOnProjectViews();
  125. assertAddedRawMeasures(SUB_SUBVIEW_1_REF, metricKey, 50);
  126. assertNoAddedRawMeasure(SUB_SUBVIEW_2_REF);
  127. assertAddedRawMeasures(SUBVIEW_REF, metricKey, 50);
  128. assertAddedRawMeasures(ROOT_REF, metricKey, 70);
  129. }
  130. @Test
  131. public void aggregate_function_complexity_distribution() {
  132. verify_distribution_aggregation(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
  133. }
  134. @Test
  135. public void aggregate_file_complexity_distribution() {
  136. verify_distribution_aggregation(FILE_COMPLEXITY_DISTRIBUTION_KEY);
  137. }
  138. private void verify_distribution_aggregation(String metricKey) {
  139. addRawMeasure(PROJECT_VIEW_1_REF, metricKey, "0.5=3;3.5=5;6.5=9");
  140. addRawMeasure(PROJECT_VIEW_2_REF, metricKey, "0.5=0;3.5=2;6.5=1");
  141. addRawMeasure(PROJECT_VIEW_3_REF, metricKey, "0.5=1;3.5=1;6.5=0");
  142. underTest.execute(new TestComputationStepContext());
  143. assertNoAddedRawMeasureOnProjectViews();
  144. assertAddedRawMeasures(SUB_SUBVIEW_1_REF, metricKey, "0.5=3;3.5=7;6.5=10");
  145. assertNoAddedRawMeasure(SUB_SUBVIEW_2_REF);
  146. assertAddedRawMeasures(SUBVIEW_REF, metricKey, "0.5=3;3.5=7;6.5=10");
  147. assertAddedRawMeasures(ROOT_REF, metricKey, "0.5=4;3.5=8;6.5=10");
  148. }
  149. @Test
  150. public void compute_and_aggregate_file_complexity() {
  151. verify_average_compute_and_aggregation(FILE_COMPLEXITY_KEY, COMPLEXITY_KEY, FILES_KEY);
  152. }
  153. @Test
  154. public void compute_and_aggregate_class_complexity() {
  155. verify_average_compute_and_aggregation(CLASS_COMPLEXITY_KEY, COMPLEXITY_IN_CLASSES_KEY, CLASSES_KEY);
  156. }
  157. @Test
  158. public void compute_and_aggregate_function_complexity() {
  159. verify_average_compute_and_aggregation(FUNCTION_COMPLEXITY_KEY, COMPLEXITY_IN_FUNCTIONS_KEY, FUNCTIONS_KEY);
  160. }
  161. private void verify_average_compute_and_aggregation(String metricKey, String mainMetric, String byMetric) {
  162. addRawMeasureValue(PROJECT_VIEW_1_REF, mainMetric, 5);
  163. addRawMeasureValue(PROJECT_VIEW_1_REF, byMetric, 2);
  164. addRawMeasureValue(PROJECT_VIEW_2_REF, mainMetric, 1);
  165. addRawMeasureValue(PROJECT_VIEW_2_REF, byMetric, 1);
  166. addRawMeasureValue(PROJECT_VIEW_3_REF, mainMetric, 6);
  167. addRawMeasureValue(PROJECT_VIEW_3_REF, byMetric, 8);
  168. underTest.execute(new TestComputationStepContext());
  169. assertNoAddedRawMeasureOnProjectViews();
  170. assertAddedRawMeasures(SUB_SUBVIEW_1_REF, metricKey, 2d);
  171. assertNoAddedRawMeasure(SUB_SUBVIEW_2_REF);
  172. assertAddedRawMeasures(SUBVIEW_REF, metricKey, 2d);
  173. assertAddedRawMeasures(ROOT_REF, metricKey, 1.1d);
  174. }
  175. private void addRawMeasure(int componentRef, String metricKey, String value) {
  176. measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value));
  177. }
  178. private void assertNoAddedRawMeasureOnProjectViews() {
  179. assertNoAddedRawMeasure(PROJECT_VIEW_1_REF);
  180. assertNoAddedRawMeasure(PROJECT_VIEW_2_REF);
  181. assertNoAddedRawMeasure(PROJECT_VIEW_3_REF);
  182. }
  183. private void assertNoAddedRawMeasure(int componentRef) {
  184. assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty();
  185. }
  186. private void assertAddedRawMeasures(int componentRef, String metricKey, String expected) {
  187. assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(expected)));
  188. }
  189. private void assertAddedRawMeasures(int componentRef, String metricKey, int expected) {
  190. assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(expected)));
  191. }
  192. private void assertAddedRawMeasures(int componentRef, String metricKey, double expected) {
  193. assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(expected, 1)));
  194. }
  195. private void addRawMeasureValue(int componentRef, String metricKey, int value) {
  196. measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value));
  197. }
  198. }