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.

MeasureComputerDefinitionImplTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.api.measurecomputer;
  21. import org.junit.Test;
  22. import org.sonar.api.ce.measure.MeasureComputer;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  25. public class MeasureComputerDefinitionImplTest {
  26. @Test
  27. public void build_measure_computer_definition() {
  28. String inputMetric = "ncloc";
  29. String outputMetric = "comment_density";
  30. MeasureComputer.MeasureComputerDefinition measureComputer = new MeasureComputerDefinitionImpl.BuilderImpl()
  31. .setInputMetrics(inputMetric)
  32. .setOutputMetrics(outputMetric)
  33. .build();
  34. assertThat(measureComputer.getInputMetrics()).containsOnly(inputMetric);
  35. assertThat(measureComputer.getOutputMetrics()).containsOnly(outputMetric);
  36. }
  37. @Test
  38. public void build_measure_computer_with_multiple_metrics() {
  39. String[] inputMetrics = {"ncloc", "comment"};
  40. String[] outputMetrics = {"comment_density_1", "comment_density_2"};
  41. MeasureComputer.MeasureComputerDefinition measureComputer = new MeasureComputerDefinitionImpl.BuilderImpl()
  42. .setInputMetrics(inputMetrics)
  43. .setOutputMetrics(outputMetrics)
  44. .build();
  45. assertThat(measureComputer.getInputMetrics()).containsOnly(inputMetrics);
  46. assertThat(measureComputer.getOutputMetrics()).containsOnly(outputMetrics);
  47. }
  48. @Test
  49. public void input_metrics_can_be_empty() {
  50. MeasureComputer.MeasureComputerDefinition measureComputer = new MeasureComputerDefinitionImpl.BuilderImpl()
  51. .setInputMetrics()
  52. .setOutputMetrics("comment_density_1", "comment_density_2")
  53. .build();
  54. assertThat(measureComputer.getInputMetrics()).isEmpty();
  55. }
  56. @Test
  57. public void input_metrics_is_empty_when_not_set() {
  58. MeasureComputer.MeasureComputerDefinition measureComputer = new MeasureComputerDefinitionImpl.BuilderImpl()
  59. .setOutputMetrics("comment_density_1", "comment_density_2")
  60. .build();
  61. assertThat(measureComputer.getInputMetrics()).isEmpty();
  62. }
  63. @Test
  64. public void fail_with_NPE_when_null_input_metrics() {
  65. assertThatThrownBy(() -> {
  66. new MeasureComputerDefinitionImpl.BuilderImpl()
  67. .setInputMetrics((String[]) null)
  68. .setOutputMetrics("comment_density_1", "comment_density_2");
  69. })
  70. .isInstanceOf(NullPointerException.class)
  71. .hasMessage("Input metrics cannot be null");
  72. }
  73. @Test
  74. public void fail_with_NPE_when_one_input_metric_is_null() {
  75. assertThatThrownBy(() -> {
  76. new MeasureComputerDefinitionImpl.BuilderImpl()
  77. .setInputMetrics("ncloc", null)
  78. .setOutputMetrics("comment_density_1", "comment_density_2");
  79. })
  80. .isInstanceOf(NullPointerException.class)
  81. .hasMessage("Null metric is not allowed");
  82. }
  83. @Test
  84. public void fail_with_NPE_when_no_output_metrics() {
  85. assertThatThrownBy(() -> {
  86. new MeasureComputerDefinitionImpl.BuilderImpl()
  87. .setInputMetrics("ncloc", "comment")
  88. .build();
  89. })
  90. .isInstanceOf(NullPointerException.class)
  91. .hasMessage("Output metrics cannot be null");
  92. }
  93. @Test
  94. public void fail_with_NPE_when_null_output_metrics() {
  95. assertThatThrownBy(() -> {
  96. new MeasureComputerDefinitionImpl.BuilderImpl()
  97. .setInputMetrics("ncloc", "comment")
  98. .setOutputMetrics((String[]) null);
  99. })
  100. .isInstanceOf(NullPointerException.class)
  101. .hasMessage("Output metrics cannot be null");
  102. }
  103. @Test
  104. public void fail_with_NPE_when_one_output_metric_is_null() {
  105. assertThatThrownBy(() -> {
  106. new MeasureComputerDefinitionImpl.BuilderImpl()
  107. .setInputMetrics("ncloc", "comment")
  108. .setOutputMetrics("comment_density_1", null);
  109. })
  110. .isInstanceOf(NullPointerException.class)
  111. .hasMessage("Null metric is not allowed");
  112. }
  113. @Test
  114. public void fail_with_IAE_with_empty_output_metrics() {
  115. assertThatThrownBy(() -> {
  116. new MeasureComputerDefinitionImpl.BuilderImpl()
  117. .setInputMetrics("ncloc", "comment")
  118. .setOutputMetrics();
  119. })
  120. .isInstanceOf(IllegalArgumentException.class)
  121. .hasMessage("At least one output metric must be defined");
  122. }
  123. @Test
  124. public void test_equals_and_hashcode() {
  125. MeasureComputer.MeasureComputerDefinition computer = new MeasureComputerDefinitionImpl.BuilderImpl()
  126. .setInputMetrics("ncloc", "comment")
  127. .setOutputMetrics("comment_density_1", "comment_density_2")
  128. .build();
  129. MeasureComputer.MeasureComputerDefinition sameComputer = new MeasureComputerDefinitionImpl.BuilderImpl()
  130. .setInputMetrics("comment", "ncloc")
  131. .setOutputMetrics("comment_density_2", "comment_density_1")
  132. .build();
  133. MeasureComputer.MeasureComputerDefinition anotherComputer = new MeasureComputerDefinitionImpl.BuilderImpl()
  134. .setInputMetrics("comment")
  135. .setOutputMetrics("debt")
  136. .build();
  137. assertThat(computer).isEqualTo(computer)
  138. .isEqualTo(sameComputer)
  139. .isNotEqualTo(anotherComputer)
  140. .isNotEqualTo(null);
  141. assertThat(computer)
  142. .hasSameHashCodeAs(computer)
  143. .hasSameHashCodeAs(sameComputer);
  144. assertThat(computer.hashCode()).isNotEqualTo(anotherComputer.hashCode());
  145. }
  146. @Test
  147. public void test_to_string() {
  148. assertThat(new MeasureComputerDefinitionImpl.BuilderImpl()
  149. .setInputMetrics("ncloc", "comment")
  150. .setOutputMetrics("comment_density_1", "comment_density_2")
  151. .build().toString())
  152. .isEqualTo("MeasureComputerDefinitionImpl{inputMetricKeys=[ncloc, comment], outputMetrics=[comment_density_1, comment_density_2]}");
  153. }
  154. }