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.

NewMaintainabilityMeasuresVisitorTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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.qualitymodel;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.google.common.collect.ImmutableSet;
  23. import com.google.common.collect.Ordering;
  24. import java.util.Arrays;
  25. import java.util.HashSet;
  26. import java.util.Optional;
  27. import java.util.Set;
  28. import org.assertj.core.data.Offset;
  29. import org.junit.Before;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.sonar.api.utils.KeyValueFormat;
  33. import org.sonar.ce.task.projectanalysis.component.Component;
  34. import org.sonar.ce.task.projectanalysis.component.FileAttributes;
  35. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  36. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  37. import org.sonar.ce.task.projectanalysis.component.VisitorsCrawler;
  38. import org.sonar.ce.task.projectanalysis.measure.Measure;
  39. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  40. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  41. import org.sonar.ce.task.projectanalysis.source.NewLinesRepository;
  42. import org.sonar.server.measure.DebtRatingGrid;
  43. import org.sonar.server.measure.Rating;
  44. import static com.google.common.base.Preconditions.checkArgument;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.when;
  47. import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA;
  48. import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA_KEY;
  49. import static org.sonar.api.measures.CoreMetrics.NEW_DEVELOPMENT_COST;
  50. import static org.sonar.api.measures.CoreMetrics.NEW_DEVELOPMENT_COST_KEY;
  51. import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
  52. import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY;
  53. import static org.sonar.api.measures.CoreMetrics.NEW_SQALE_DEBT_RATIO;
  54. import static org.sonar.api.measures.CoreMetrics.NEW_SQALE_DEBT_RATIO_KEY;
  55. import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT;
  56. import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY;
  57. import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
  58. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  59. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  60. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  61. import static org.sonar.ce.task.projectanalysis.measure.MeasureAssert.assertThat;
  62. import static org.sonar.server.measure.Rating.A;
  63. import static org.sonar.server.measure.Rating.D;
  64. public class NewMaintainabilityMeasuresVisitorTest {
  65. private static final double[] RATING_GRID = new double[] {0.1, 0.2, 0.5, 1};
  66. private static final String LANGUAGE_1_KEY = "language 1 key";
  67. private static final long LANGUAGE_1_DEV_COST = 30L;
  68. private static final int ROOT_REF = 1;
  69. private static final int LANGUAGE_1_FILE_REF = 11111;
  70. private static final Offset<Double> VARIATION_COMPARISON_OFFSET = Offset.offset(0.01);
  71. @Rule
  72. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  73. @Rule
  74. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  75. .add(NEW_TECHNICAL_DEBT)
  76. .add(NCLOC_DATA)
  77. .add(NEW_SQALE_DEBT_RATIO)
  78. .add(NEW_MAINTAINABILITY_RATING)
  79. .add(NEW_DEVELOPMENT_COST);
  80. @Rule
  81. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  82. private NewLinesRepository newLinesRepository = mock(NewLinesRepository.class);
  83. private RatingSettings ratingSettings = mock(RatingSettings.class);
  84. private VisitorsCrawler underTest;
  85. @Before
  86. public void setUp() {
  87. when(ratingSettings.getDebtRatingGrid()).thenReturn(new DebtRatingGrid(RATING_GRID));
  88. underTest = new VisitorsCrawler(Arrays.asList(new NewMaintainabilityMeasuresVisitor(metricRepository, measureRepository, newLinesRepository, ratingSettings)));
  89. }
  90. @Test
  91. public void project_has_new_measures() {
  92. when(newLinesRepository.newLinesAvailable()).thenReturn(true);
  93. treeRootHolder.setRoot(builder(PROJECT, ROOT_REF).build());
  94. underTest.visit(treeRootHolder.getRoot());
  95. assertNewDebtRatioValues(ROOT_REF, 0);
  96. assertNewMaintainability(ROOT_REF, A);
  97. }
  98. @Test
  99. public void project_has_no_measure_if_new_lines_not_available() {
  100. when(newLinesRepository.newLinesAvailable()).thenReturn(false);
  101. treeRootHolder.setRoot(builder(PROJECT, ROOT_REF).build());
  102. underTest.visit(treeRootHolder.getRoot());
  103. assertNoNewDebtRatioMeasure(ROOT_REF);
  104. assertNoNewMaintainability(ROOT_REF);
  105. }
  106. @Test
  107. public void file_has_no_new_debt_ratio_variation_if_new_lines_not_available() {
  108. when(newLinesRepository.newLinesAvailable()).thenReturn(false);
  109. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  110. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_NEW_LINES);
  111. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
  112. underTest.visit(treeRootHolder.getRoot());
  113. assertNoNewDebtRatioMeasure(LANGUAGE_1_FILE_REF);
  114. assertNoNewDebtRatioMeasure(ROOT_REF);
  115. }
  116. @Test
  117. public void file_has_0_new_debt_ratio_if_no_line_is_new() {
  118. ReportComponent file = builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 1)).build();
  119. treeRootHolder.setRoot(
  120. builder(PROJECT, ROOT_REF)
  121. .addChildren(file)
  122. .build());
  123. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
  124. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
  125. setNewLines(file);
  126. underTest.visit(treeRootHolder.getRoot());
  127. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
  128. assertNewDebtRatioValues(ROOT_REF, 0);
  129. }
  130. @Test
  131. public void file_has_new_debt_ratio_if_some_lines_are_new() {
  132. when(newLinesRepository.newLinesAvailable()).thenReturn(true);
  133. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  134. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_NEW_LINES);
  135. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
  136. underTest.visit(treeRootHolder.getRoot());
  137. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 83.33);
  138. assertNewDebtRatioValues(ROOT_REF, 83.33);
  139. }
  140. @Test
  141. public void new_debt_ratio_changes_with_language_cost() {
  142. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST * 10);
  143. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_NEW_LINES);
  144. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
  145. underTest.visit(treeRootHolder.getRoot());
  146. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 8.33);
  147. assertNewDebtRatioValues(ROOT_REF, 8.33);
  148. }
  149. @Test
  150. public void new_debt_ratio_changes_with_new_technical_debt() {
  151. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  152. setupOneFileAloneInAProject(500, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_NEW_LINES);
  153. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(500));
  154. underTest.visit(treeRootHolder.getRoot());
  155. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33);
  156. assertNewDebtRatioValues(ROOT_REF, 833.33);
  157. }
  158. @Test
  159. public void new_debt_ratio_on_non_file_level_is_based_on_new_technical_debt_of_that_level() {
  160. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  161. setupOneFileAloneInAProject(500, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.WITH_NEW_LINES);
  162. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(1200));
  163. underTest.visit(treeRootHolder.getRoot());
  164. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33);
  165. assertNewDebtRatioValues(ROOT_REF, 833.33);
  166. }
  167. @Test
  168. public void new_debt_ratio_when_file_is_unit_test() {
  169. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  170. setupOneFileAloneInAProject(500, Flag.UT_FILE, Flag.WITH_NCLOC, Flag.WITH_NEW_LINES);
  171. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(1200));
  172. underTest.visit(treeRootHolder.getRoot());
  173. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33);
  174. assertNewDebtRatioValues(ROOT_REF, 833.33);
  175. }
  176. @Test
  177. public void new_debt_ratio_is_0_when_file_has_no_new_lines() {
  178. when(newLinesRepository.newLinesAvailable()).thenReturn(true);
  179. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  180. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_NEW_LINES);
  181. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
  182. underTest.visit(treeRootHolder.getRoot());
  183. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
  184. assertNewDebtRatioValues(ROOT_REF, 0);
  185. }
  186. @Test
  187. public void new_debt_ratio_is_0_on_non_file_level_when_no_file_has_new_lines() {
  188. when(newLinesRepository.newLinesAvailable()).thenReturn(true);
  189. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  190. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_NEW_LINES);
  191. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200));
  192. underTest.visit(treeRootHolder.getRoot());
  193. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
  194. assertNewDebtRatioValues(ROOT_REF, 0);
  195. }
  196. @Test
  197. public void new_debt_ratio_is_0_when_there_is_no_ncloc_in_file() {
  198. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  199. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.NO_NCLOC, Flag.WITH_NEW_LINES);
  200. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
  201. underTest.visit(treeRootHolder.getRoot());
  202. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
  203. assertNewDebtRatioValues(ROOT_REF, 0);
  204. }
  205. @Test
  206. public void new_debt_ratio_is_0_on_non_file_level_when_one_file_has_zero_new_debt_because_of_no_changeset() {
  207. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  208. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.NO_NCLOC, Flag.WITH_NEW_LINES);
  209. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200));
  210. underTest.visit(treeRootHolder.getRoot());
  211. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
  212. assertNewDebtRatioValues(ROOT_REF, 0);
  213. }
  214. @Test
  215. public void new_debt_ratio_is_0_when_ncloc_measure_is_missing() {
  216. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  217. setupOneFileAloneInAProject(50, Flag.SRC_FILE, Flag.MISSING_MEASURE_NCLOC, Flag.WITH_NEW_LINES);
  218. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50));
  219. underTest.visit(treeRootHolder.getRoot());
  220. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0);
  221. assertNewDebtRatioValues(ROOT_REF, 0);
  222. }
  223. @Test
  224. public void leaf_components_always_have_a_measure_when_at_least_one_period_exist_and_ratio_is_computed_from_current_level_new_debt() {
  225. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  226. Component file = builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 1)).build();
  227. treeRootHolder.setRoot(
  228. builder(PROJECT, ROOT_REF)
  229. .addChildren(
  230. builder(DIRECTORY, 111)
  231. .addChildren(file)
  232. .build())
  233. .build());
  234. Measure newDebtMeasure = createNewDebtMeasure(50);
  235. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
  236. measureRepository.addRawMeasure(111, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(150));
  237. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(250));
  238. // 4 lines file, only first one is not ncloc
  239. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
  240. // first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
  241. setNewLines(file, 3, 4);
  242. underTest.visit(treeRootHolder.getRoot());
  243. assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 83.33);
  244. assertNewDebtRatioValues(111, 83.33);
  245. assertNewDebtRatioValues(ROOT_REF, 83.33);
  246. }
  247. @Test
  248. public void compute_new_maintainability_rating() {
  249. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  250. ReportComponent file = builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 1)).build();
  251. treeRootHolder.setRoot(
  252. builder(PROJECT, ROOT_REF)
  253. .addChildren(
  254. builder(DIRECTORY, 111)
  255. .addChildren(file)
  256. .build())
  257. .build());
  258. Measure newDebtMeasure = createNewDebtMeasure(50);
  259. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
  260. measureRepository.addRawMeasure(111, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(150));
  261. measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(250));
  262. // 4 lines file, only first one is not ncloc
  263. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
  264. // first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
  265. setNewLines(file, 3, 4);
  266. underTest.visit(treeRootHolder.getRoot());
  267. assertNewMaintainability(LANGUAGE_1_FILE_REF, D);
  268. assertNewMaintainability(111, D);
  269. assertNewMaintainability(ROOT_REF, D);
  270. }
  271. @Test
  272. public void compute_new_development_cost() {
  273. ReportComponent file1 = builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 4)).build();
  274. ReportComponent file2 = builder(FILE, 22_222).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 6)).build();
  275. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  276. treeRootHolder.setRoot(
  277. builder(PROJECT, ROOT_REF)
  278. .addChildren(
  279. builder(DIRECTORY, 111)
  280. .addChildren(file1, file2)
  281. .build())
  282. .build());
  283. // 4 lines file, only first one is not ncloc
  284. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
  285. // first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
  286. setNewLines(file1, 3, 4);
  287. // 6 lines file, only last one is not ncloc
  288. measureRepository.addRawMeasure(22_222, NCLOC_DATA_KEY, createNclocDataMeasure(1, 2, 3, 4, 5));
  289. // first 2 lines are before all snapshots, 4 last lines are after PERIOD 2's snapshot date
  290. setNewLines(file2, 3, 4, 5, 6);
  291. underTest.visit(treeRootHolder.getRoot());
  292. assertNewDevelopmentCostValues(ROOT_REF, 5 * LANGUAGE_1_DEV_COST);
  293. assertNewDevelopmentCostValues(LANGUAGE_1_FILE_REF, 2 * LANGUAGE_1_DEV_COST);
  294. assertNewDevelopmentCostValues(22_222, 3 * LANGUAGE_1_DEV_COST);
  295. }
  296. @Test
  297. public void compute_new_maintainability_rating_to_A_when_no_debt() {
  298. when(newLinesRepository.newLinesAvailable()).thenReturn(true);
  299. when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
  300. treeRootHolder.setRoot(
  301. builder(PROJECT, ROOT_REF)
  302. .addChildren(
  303. builder(DIRECTORY, 111)
  304. .addChildren(
  305. builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 1)).build())
  306. .build())
  307. .build());
  308. underTest.visit(treeRootHolder.getRoot());
  309. assertNewMaintainability(LANGUAGE_1_FILE_REF, A);
  310. assertNewMaintainability(111, A);
  311. assertNewMaintainability(ROOT_REF, A);
  312. }
  313. private void setupOneFileAloneInAProject(int newDebt, Flag isUnitTest, Flag withNclocLines, Flag withNewLines) {
  314. checkArgument(isUnitTest == Flag.UT_FILE || isUnitTest == Flag.SRC_FILE);
  315. checkArgument(withNclocLines == Flag.WITH_NCLOC || withNclocLines == Flag.NO_NCLOC || withNclocLines == Flag.MISSING_MEASURE_NCLOC);
  316. checkArgument(withNewLines == Flag.WITH_NEW_LINES || withNewLines == Flag.NO_NEW_LINES);
  317. Component file = builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(isUnitTest == Flag.UT_FILE, LANGUAGE_1_KEY, 1)).build();
  318. treeRootHolder.setRoot(
  319. builder(PROJECT, ROOT_REF)
  320. .addChildren(file)
  321. .build());
  322. Measure newDebtMeasure = createNewDebtMeasure(newDebt);
  323. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
  324. if (withNclocLines == Flag.WITH_NCLOC) {
  325. // 4 lines file, only first one is not ncloc
  326. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
  327. } else if (withNclocLines == Flag.NO_NCLOC) {
  328. // 4 lines file, none of which is ncloc
  329. measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNoNclocDataMeasure(4));
  330. }
  331. if (withNewLines == Flag.WITH_NEW_LINES) {
  332. // 2 last lines are new
  333. setNewLines(file, 3, 4);
  334. }
  335. }
  336. private void setNewLines(Component component, int... lineNumbers) {
  337. HashSet<Integer> newLines = new HashSet<>();
  338. for (int i : lineNumbers) {
  339. newLines.add(i);
  340. }
  341. when(newLinesRepository.newLinesAvailable()).thenReturn(true);
  342. when(newLinesRepository.getNewLines(component)).thenReturn(Optional.of(newLines));
  343. }
  344. private enum Flag {
  345. UT_FILE, SRC_FILE, NO_NEW_LINES, WITH_NEW_LINES, WITH_NCLOC, NO_NCLOC, MISSING_MEASURE_NCLOC
  346. }
  347. public static ReportComponent.Builder builder(Component.Type type, int ref) {
  348. return ReportComponent.builder(type, ref).setKey(String.valueOf(ref));
  349. }
  350. private Measure createNewDebtMeasure(double variation) {
  351. return newMeasureBuilder().setVariation(variation).createNoValue();
  352. }
  353. private static Measure createNclocDataMeasure(Integer... nclocLines) {
  354. Set<Integer> nclocLinesSet = ImmutableSet.copyOf(nclocLines);
  355. int max = Ordering.<Integer>natural().max(nclocLinesSet);
  356. ImmutableMap.Builder<Integer, Integer> builder = ImmutableMap.builder();
  357. for (int i = 1; i <= max; i++) {
  358. builder.put(i, nclocLinesSet.contains(i) ? 1 : 0);
  359. }
  360. return newMeasureBuilder().create(KeyValueFormat.format(builder.build(), KeyValueFormat.newIntegerConverter(), KeyValueFormat.newIntegerConverter()));
  361. }
  362. private static Measure createNoNclocDataMeasure(int lineCount) {
  363. ImmutableMap.Builder<Integer, Integer> builder = ImmutableMap.builder();
  364. for (int i = 1; i <= lineCount; i++) {
  365. builder.put(i, 0);
  366. }
  367. return newMeasureBuilder().create(KeyValueFormat.format(builder.build(), KeyValueFormat.newIntegerConverter(), KeyValueFormat.newIntegerConverter()));
  368. }
  369. private void assertNoNewDebtRatioMeasure(int componentRef) {
  370. assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_SQALE_DEBT_RATIO_KEY))
  371. .isAbsent();
  372. }
  373. private void assertNewDebtRatioValues(int componentRef, double expectedVariation) {
  374. assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_SQALE_DEBT_RATIO_KEY)).hasVariation(expectedVariation, VARIATION_COMPARISON_OFFSET);
  375. }
  376. private void assertNewDevelopmentCostValues(int componentRef, long expectedVariation) {
  377. assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_DEVELOPMENT_COST_KEY)).hasVariation(expectedVariation, VARIATION_COMPARISON_OFFSET);
  378. }
  379. private void assertNewMaintainability(int componentRef, Rating expectedVariation) {
  380. assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_MAINTAINABILITY_RATING_KEY)).hasVariation(expectedVariation.getIndex());
  381. }
  382. private void assertNoNewMaintainability(int componentRef) {
  383. assertThat(measureRepository.getAddedRawMeasure(componentRef, NEW_MAINTAINABILITY_RATING_KEY))
  384. .isAbsent();
  385. }
  386. }