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.

DistributionFormulaTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 java.util.Optional;
  22. import org.junit.Test;
  23. import org.sonar.ce.task.projectanalysis.component.Component;
  24. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  25. import org.sonar.ce.task.projectanalysis.measure.Measure;
  26. import org.sonar.ce.task.projectanalysis.metric.Metric;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
  32. public class DistributionFormulaTest {
  33. private static final DistributionFormula BASIC_DISTRIBUTION_FORMULA = new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
  34. CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
  35. CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext(
  36. ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class));
  37. CreateMeasureContext fileCreateMeasureContext = new DumbCreateMeasureContext(
  38. ReportComponent.builder(Component.Type.FILE, 1).build(), mock(Metric.class));
  39. @Test
  40. public void check_new_counter_class() {
  41. assertThat(BASIC_DISTRIBUTION_FORMULA.createNewCounter().getClass()).isEqualTo(DistributionFormula.DistributionCounter.class);
  42. }
  43. @Test
  44. public void fail_with_NPE_when_creating_counter_with_null_metric() {
  45. assertThatThrownBy(() -> new DistributionFormula(null))
  46. .isInstanceOf(NullPointerException.class)
  47. .hasMessage("Metric key cannot be null");
  48. }
  49. @Test
  50. public void check_output_metric_key_is_function_complexity_distribution() {
  51. assertThat(BASIC_DISTRIBUTION_FORMULA.getOutputMetricKeys()).containsOnly(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
  52. }
  53. @Test
  54. public void create_measure() {
  55. DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
  56. addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
  57. counter.initialize(counterInitializationContext);
  58. assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getData()).isEqualTo("0=3;3=7;6=10");
  59. }
  60. @Test
  61. public void create_measure_when_counter_is_aggregating_from_another_counter() {
  62. DistributionFormula.DistributionCounter anotherCounter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
  63. addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
  64. anotherCounter.initialize(counterInitializationContext);
  65. DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
  66. counter.aggregate(anotherCounter);
  67. assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getData()).isEqualTo("0=3;3=7;6=10");
  68. }
  69. @Test
  70. public void create_no_measure_when_no_value() {
  71. DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
  72. when(counterInitializationContext.getMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(Optional.empty());
  73. counter.initialize(counterInitializationContext);
  74. assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext)).isNotPresent();
  75. }
  76. @Test
  77. public void not_create_measure_when_on_file() {
  78. DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
  79. addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
  80. counter.initialize(counterInitializationContext);
  81. assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, fileCreateMeasureContext)).isNotPresent();
  82. }
  83. private void addMeasure(String metricKey, String value) {
  84. when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
  85. }
  86. }