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.

DuplicationMeasuresTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.duplication;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.ce.task.projectanalysis.component.FileAttributes;
  25. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  26. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  27. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  28. import static com.google.common.base.Preconditions.checkArgument;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.when;
  32. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS;
  33. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_BLOCKS_KEY;
  34. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES;
  35. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_FILES_KEY;
  36. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES;
  37. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY;
  38. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY;
  39. import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_KEY;
  40. import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
  41. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  42. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  43. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  44. public class DuplicationMeasuresTest {
  45. private static final int ROOT_REF = 1;
  46. private static final int DIRECTORY_REF = 1234;
  47. private static final int FILE_1_REF = 12341;
  48. private static final int FILE_2_REF = 12342;
  49. private static final int FILE_3_REF = 1261;
  50. private static final int FILE_4_REF = 1262;
  51. private static final int FILE_5_REF = 1263;
  52. private static final FileAttributes FILE_1_ATTRS = mock(FileAttributes.class);
  53. private static final FileAttributes FILE_2_ATTRS = mock(FileAttributes.class);
  54. private static final FileAttributes FILE_3_ATTRS = mock(FileAttributes.class);
  55. private static final FileAttributes FILE_4_ATTRS = mock(FileAttributes.class);
  56. private static final FileAttributes FILE_5_ATTRS = mock(FileAttributes.class);
  57. private static final String SOME_FILE_KEY = "some file key";
  58. @Rule
  59. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
  60. .setRoot(
  61. builder(PROJECT, ROOT_REF)
  62. .addChildren(
  63. builder(DIRECTORY, DIRECTORY_REF)
  64. .addChildren(
  65. builder(FILE, FILE_1_REF).setFileAttributes(FILE_1_ATTRS).build(),
  66. builder(FILE, FILE_2_REF).setFileAttributes(FILE_2_ATTRS).build())
  67. .build(),
  68. builder(FILE, FILE_3_REF).setFileAttributes(FILE_3_ATTRS).build(),
  69. builder(FILE, FILE_4_REF).setFileAttributes(FILE_4_ATTRS).build(),
  70. builder(FILE, FILE_5_REF).setFileAttributes(FILE_5_ATTRS).build())
  71. .build());
  72. @Rule
  73. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  74. .add(DUPLICATED_BLOCKS)
  75. .add(DUPLICATED_FILES)
  76. .add(DUPLICATED_LINES)
  77. .add(DUPLICATED_LINES_DENSITY);
  78. @Rule
  79. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  80. @Rule
  81. public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
  82. private DuplicationMeasures underTest = new DuplicationMeasures(treeRootHolder, metricRepository, measureRepository, duplicationRepository);
  83. @Before
  84. public void before() {
  85. when(FILE_5_ATTRS.isUnitTest()).thenReturn(true);
  86. }
  87. @Test
  88. public void compute_duplicated_blocks_one_for_original_one_for_each_InnerDuplicate() {
  89. TextBlock original = new TextBlock(1, 1);
  90. duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(2, 2), new TextBlock(3, 3), new TextBlock(2, 3));
  91. underTest.execute();
  92. assertRawMeasureValue(FILE_1_REF, DUPLICATED_BLOCKS_KEY, 4);
  93. }
  94. @Test
  95. public void dont_compute_duplicated_blocks_for_test_files() {
  96. duplicationRepository.addDuplication(FILE_5_REF, new TextBlock(1, 1), new TextBlock(3, 3));
  97. duplicationRepository.addDuplication(FILE_5_REF, new TextBlock(2, 2), new TextBlock(3, 3));
  98. underTest.execute();
  99. assertRawMeasureValue(FILE_5_REF, DUPLICATED_BLOCKS_KEY, 0);
  100. assertRawMeasureValue(FILE_5_REF, DUPLICATED_FILES_KEY, 0);
  101. }
  102. @Test
  103. public void compute_duplicated_blocks_does_not_count_blocks_only_once_it_assumes_consistency_from_duplication_data() {
  104. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), new TextBlock(3, 3));
  105. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(2, 2), new TextBlock(3, 3));
  106. underTest.execute();
  107. assertRawMeasureValue(FILE_1_REF, DUPLICATED_BLOCKS_KEY, 4);
  108. }
  109. @Test
  110. public void compute_duplicated_blocks_one_for_original_and_ignores_InProjectDuplicate() {
  111. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), FILE_2_REF, new TextBlock(2, 2));
  112. underTest.execute();
  113. assertRawMeasureValue(FILE_1_REF, DUPLICATED_BLOCKS_KEY, 1);
  114. }
  115. @Test
  116. public void compute_duplicated_blocks_one_for_original_and_ignores_CrossProjectDuplicate() {
  117. duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 1), SOME_FILE_KEY, new TextBlock(2, 2));
  118. underTest.execute();
  119. assertRawMeasureValue(FILE_1_REF, DUPLICATED_BLOCKS_KEY, 1);
  120. }
  121. @Test
  122. public void compute_and_aggregate_duplicated_blocks_from_single_duplication() {
  123. addDuplicatedBlock(FILE_1_REF, 10);
  124. addDuplicatedBlock(FILE_2_REF, 40);
  125. addDuplicatedBlock(FILE_4_REF, 5);
  126. underTest.execute();
  127. assertRawMeasureValue(FILE_1_REF, DUPLICATED_BLOCKS_KEY, 10);
  128. assertRawMeasureValue(FILE_2_REF, DUPLICATED_BLOCKS_KEY, 40);
  129. assertRawMeasureValue(FILE_3_REF, DUPLICATED_BLOCKS_KEY, 0);
  130. assertRawMeasureValue(FILE_4_REF, DUPLICATED_BLOCKS_KEY, 5);
  131. assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_BLOCKS_KEY, 50);
  132. assertRawMeasureValue(ROOT_REF, DUPLICATED_BLOCKS_KEY, 55);
  133. }
  134. @Test
  135. public void compute_and_aggregate_duplicated_blocks_to_zero_when_no_duplication() {
  136. underTest.execute();
  137. assertComputedAndAggregatedToZeroInt(DUPLICATED_BLOCKS_KEY);
  138. }
  139. @Test
  140. public void compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate_of_a_single_line() {
  141. TextBlock original = new TextBlock(1, 1);
  142. duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(2, 2));
  143. underTest.execute();
  144. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_KEY, 2);
  145. }
  146. @Test
  147. public void compute_duplicated_lines_counts_lines_from_original_and_ignores_InProjectDuplicate() {
  148. TextBlock original = new TextBlock(1, 1);
  149. duplicationRepository.addDuplication(FILE_1_REF, original, FILE_2_REF, new TextBlock(2, 2));
  150. underTest.execute();
  151. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_KEY, 1);
  152. }
  153. @Test
  154. public void compute_duplicated_lines_counts_lines_from_original_and_ignores_CrossProjectDuplicate() {
  155. TextBlock original = new TextBlock(1, 1);
  156. duplicationRepository.addCrossProjectDuplication(FILE_1_REF, original, SOME_FILE_KEY, new TextBlock(2, 2));
  157. underTest.execute();
  158. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_KEY, 1);
  159. }
  160. @Test
  161. public void compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate() {
  162. TextBlock original = new TextBlock(1, 5);
  163. duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(10, 11));
  164. underTest.execute();
  165. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_KEY, 7);
  166. }
  167. @Test
  168. public void compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate_only_once() {
  169. TextBlock original = new TextBlock(1, 12);
  170. duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(10, 11), new TextBlock(11, 15));
  171. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(2, 2), new TextBlock(96, 96));
  172. underTest.execute();
  173. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_KEY, 16);
  174. }
  175. @Test
  176. public void compute_and_aggregate_duplicated_files() {
  177. addDuplicatedBlock(FILE_1_REF, 2);
  178. addDuplicatedBlock(FILE_3_REF, 10);
  179. addDuplicatedBlock(FILE_4_REF, 50);
  180. underTest.execute();
  181. assertRawMeasureValue(FILE_1_REF, DUPLICATED_FILES_KEY, 1);
  182. assertRawMeasureValue(FILE_2_REF, DUPLICATED_FILES_KEY, 0);
  183. assertRawMeasureValue(FILE_3_REF, DUPLICATED_FILES_KEY, 1);
  184. assertRawMeasureValue(FILE_4_REF, DUPLICATED_FILES_KEY, 1);
  185. assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_FILES_KEY, 1);
  186. assertRawMeasureValue(ROOT_REF, DUPLICATED_FILES_KEY, 3);
  187. }
  188. @Test
  189. public void compute_and_aggregate_zero_duplicated_files_when_no_duplication_data() {
  190. underTest.execute();
  191. assertComputedAndAggregatedToZeroInt(DUPLICATED_FILES_KEY);
  192. }
  193. @Test
  194. public void compute_and_aggregate_duplicated_lines() {
  195. addDuplicatedBlock(FILE_1_REF, 10);
  196. addDuplicatedBlock(FILE_2_REF, 9);
  197. addDuplicatedBlock(FILE_4_REF, 7);
  198. underTest.execute();
  199. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_KEY, 10);
  200. assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_KEY, 9);
  201. assertRawMeasureValue(FILE_3_REF, DUPLICATED_LINES_KEY, 0);
  202. assertRawMeasureValue(FILE_4_REF, DUPLICATED_LINES_KEY, 7);
  203. assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_KEY, 19);
  204. assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_KEY, 26);
  205. }
  206. @Test
  207. public void compute_and_aggregate_zero_duplicated_line_when_no_duplication() {
  208. underTest.execute();
  209. assertComputedAndAggregatedToZeroInt(DUPLICATED_LINES_KEY);
  210. }
  211. @Test
  212. public void compute_and_aggregate_duplicated_lines_density_using_lines() {
  213. addDuplicatedBlock(FILE_1_REF, 2);
  214. addDuplicatedBlock(FILE_2_REF, 3);
  215. when(FILE_1_ATTRS.getLines()).thenReturn(10);
  216. when(FILE_2_ATTRS.getLines()).thenReturn(40);
  217. // this should have no effect as it's a test file
  218. when(FILE_5_ATTRS.getLines()).thenReturn(1_000_000);
  219. underTest.execute();
  220. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 20d);
  221. assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 7.5d);
  222. assertNoRawMeasure(FILE_3_REF, DUPLICATED_LINES_DENSITY_KEY);
  223. assertNoRawMeasure(FILE_4_REF, DUPLICATED_LINES_DENSITY_KEY);
  224. assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 10d);
  225. assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 10d);
  226. }
  227. @Test
  228. public void compute_zero_percent_duplicated_lines_density_when_there_is_no_duplication() {
  229. when(FILE_1_ATTRS.getLines()).thenReturn(10);
  230. when(FILE_2_ATTRS.getLines()).thenReturn(40);
  231. underTest.execute();
  232. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 0d);
  233. assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 0d);
  234. assertNoRawMeasure(FILE_3_REF, DUPLICATED_LINES_DENSITY_KEY);
  235. assertNoRawMeasure(FILE_4_REF, DUPLICATED_LINES_DENSITY_KEY);
  236. assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 0d);
  237. assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 0d);
  238. }
  239. @Test
  240. public void dont_compute_duplicated_lines_density_when_lines_is_zero() {
  241. when(FILE_1_ATTRS.getLines()).thenReturn(0);
  242. when(FILE_2_ATTRS.getLines()).thenReturn(0);
  243. underTest.execute();
  244. assertNoRawMeasures(DUPLICATED_LINES_DENSITY_KEY);
  245. }
  246. @Test
  247. public void compute_100_percent_duplicated_lines_density() {
  248. addDuplicatedBlock(FILE_1_REF, 2);
  249. addDuplicatedBlock(FILE_2_REF, 3);
  250. when(FILE_1_ATTRS.getLines()).thenReturn(2);
  251. when(FILE_2_ATTRS.getLines()).thenReturn(3);
  252. underTest.execute();
  253. assertRawMeasureValue(FILE_1_REF, DUPLICATED_LINES_DENSITY_KEY, 100d);
  254. assertRawMeasureValue(FILE_2_REF, DUPLICATED_LINES_DENSITY_KEY, 100d);
  255. assertNoRawMeasure(FILE_3_REF, DUPLICATED_LINES_DENSITY_KEY);
  256. assertNoRawMeasure(FILE_4_REF, DUPLICATED_LINES_DENSITY_KEY);
  257. assertRawMeasureValue(DIRECTORY_REF, DUPLICATED_LINES_DENSITY_KEY, 100d);
  258. assertRawMeasureValue(ROOT_REF, DUPLICATED_LINES_DENSITY_KEY, 100d);
  259. }
  260. /**
  261. * Adds duplication blocks of a single line (each line is specific to its block).
  262. * This is a very simple use case, convenient for unit tests but more realistic and complex use cases must be tested separately.
  263. */
  264. private void addDuplicatedBlock(int fileRef, int blockCount) {
  265. checkArgument(blockCount > 1, "BlockCount can not be less than 2");
  266. TextBlock original = new TextBlock(1, 1);
  267. TextBlock[] duplicates = new TextBlock[blockCount - 1];
  268. for (int i = 10; i < blockCount + 9; i++) {
  269. duplicates[i - 10] = new TextBlock(i, i);
  270. }
  271. duplicationRepository.addDuplication(fileRef, original, duplicates);
  272. }
  273. private void assertNoRawMeasures(String metricKey) {
  274. assertThat(measureRepository.getAddedRawMeasures(FILE_1_REF).get(metricKey)).isNull();
  275. assertThat(measureRepository.getAddedRawMeasures(FILE_2_REF).get(metricKey)).isNull();
  276. assertThat(measureRepository.getAddedRawMeasures(DIRECTORY_REF).get(metricKey)).isNull();
  277. assertThat(measureRepository.getAddedRawMeasures(ROOT_REF).get(metricKey)).isNull();
  278. }
  279. private void assertNoRawMeasure(int componentRef, String metricKey) {
  280. assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey)).isNotPresent();
  281. }
  282. private void assertRawMeasureValue(int componentRef, String metricKey, int value) {
  283. assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey).get().getIntValue()).isEqualTo(value);
  284. }
  285. private void assertRawMeasureValue(int componentRef, String metricKey, double value) {
  286. assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey).get().getDoubleValue()).isEqualTo(value);
  287. }
  288. private void assertComputedAndAggregatedToZeroInt(String metricKey) {
  289. assertRawMeasureValue(FILE_1_REF, metricKey, 0);
  290. assertRawMeasureValue(FILE_2_REF, metricKey, 0);
  291. assertRawMeasureValue(FILE_3_REF, metricKey, 0);
  292. assertRawMeasureValue(FILE_4_REF, metricKey, 0);
  293. assertRawMeasureValue(DIRECTORY_REF, metricKey, 0);
  294. assertRawMeasureValue(ROOT_REF, metricKey, 0);
  295. }
  296. }