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.

PopulateFileSourceLineCountTest.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.dbmigration;
  21. import java.sql.SQLException;
  22. import java.util.Optional;
  23. import java.util.Random;
  24. import java.util.stream.Collectors;
  25. import java.util.stream.IntStream;
  26. import javax.annotation.Nullable;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.ce.task.CeTask;
  32. import org.sonar.db.DbTester;
  33. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.when;
  37. import static org.sonar.db.source.FileSourceDto.LINE_COUNT_NOT_POPULATED;
  38. public class PopulateFileSourceLineCountTest {
  39. @Rule
  40. public ExpectedException expectedException = ExpectedException.none();
  41. @Rule
  42. public DbTester db = DbTester.createForSchema(System2.INSTANCE, PopulateFileSourceLineCountTest.class, "file_sources.sql");
  43. private Random random = new Random();
  44. private CeTask ceTask = mock(CeTask.class);
  45. private PopulateFileSourceLineCount underTest = new PopulateFileSourceLineCount(db.database(), ceTask);
  46. @Test
  47. public void execute_has_no_effect_on_empty_table() throws SQLException {
  48. String projectUuid = randomAlphanumeric(4);
  49. when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
  50. underTest.execute();
  51. }
  52. @Test
  53. public void execute_populates_line_count_of_any_type() throws SQLException {
  54. String projectUuid = randomAlphanumeric(4);
  55. String fileUuid = randomAlphanumeric(5);
  56. when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
  57. int lineCount = 1 + random.nextInt(15);
  58. insertUnpopulatedFileSource(projectUuid, fileUuid, lineCount);
  59. assertThat(getLineCountByFileUuid(fileUuid)).isEqualTo(LINE_COUNT_NOT_POPULATED);
  60. underTest.execute();
  61. assertThat(getLineCountByFileUuid(fileUuid)).isEqualTo(lineCount);
  62. }
  63. @Test
  64. public void execute_changes_only_file_source_with_LINE_COUNT_NOT_POPULATED_value() throws SQLException {
  65. String projectUuid = randomAlphanumeric(4);
  66. String fileUuid1 = randomAlphanumeric(5);
  67. String fileUuid2 = randomAlphanumeric(6);
  68. String fileUuid3 = randomAlphanumeric(7);
  69. int lineCountFile1 = 100 + random.nextInt(15);
  70. int lineCountFile2 = 50 + random.nextInt(15);
  71. int lineCountFile3 = 150 + random.nextInt(15);
  72. when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
  73. insertPopulatedFileSource(projectUuid, fileUuid1, lineCountFile1);
  74. int badLineCountFile2 = insertInconsistentPopulatedFileSource(projectUuid, fileUuid2, lineCountFile2);
  75. insertUnpopulatedFileSource(projectUuid, fileUuid3, lineCountFile3);
  76. assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
  77. assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(badLineCountFile2);
  78. assertThat(getLineCountByFileUuid(fileUuid3)).isEqualTo(LINE_COUNT_NOT_POPULATED);
  79. underTest.execute();
  80. assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
  81. assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(badLineCountFile2);
  82. assertThat(getLineCountByFileUuid(fileUuid3)).isEqualTo(lineCountFile3);
  83. }
  84. @Test
  85. public void execute_changes_only_file_source_of_CeTask_component_uuid() throws SQLException {
  86. String projectUuid1 = randomAlphanumeric(4);
  87. String projectUuid2 = randomAlphanumeric(5);
  88. String fileUuid1 = randomAlphanumeric(6);
  89. String fileUuid2 = randomAlphanumeric(7);
  90. int lineCountFile1 = 100 + random.nextInt(15);
  91. int lineCountFile2 = 30 + random.nextInt(15);
  92. when(ceTask.getComponent()).thenReturn(newComponent(projectUuid1));
  93. insertUnpopulatedFileSource(projectUuid1, fileUuid1, lineCountFile1);
  94. insertUnpopulatedFileSource(projectUuid2, fileUuid2, lineCountFile2);
  95. underTest.execute();
  96. assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
  97. assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(LINE_COUNT_NOT_POPULATED);
  98. }
  99. @Test
  100. public void execute_set_line_count_to_zero_when_file_source_has_no_line_hashes() throws SQLException {
  101. String projectUuid = randomAlphanumeric(4);
  102. String fileUuid1 = randomAlphanumeric(5);
  103. when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
  104. insertFileSource(projectUuid, fileUuid1, null, LINE_COUNT_NOT_POPULATED);
  105. underTest.execute();
  106. assertThat(getLineCountByFileUuid(fileUuid1)).isZero();
  107. }
  108. @Test
  109. public void execute_set_line_count_to_1_when_file_source_has_empty_line_hashes() throws SQLException {
  110. String projectUuid = randomAlphanumeric(4);
  111. String fileUuid1 = randomAlphanumeric(5);
  112. when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
  113. insertFileSource(projectUuid, fileUuid1, "", LINE_COUNT_NOT_POPULATED);
  114. underTest.execute();
  115. assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(1);
  116. }
  117. private int getLineCountByFileUuid(String fileUuid) {
  118. Long res = (Long) db.selectFirst("select line_count as \"LINE_COUNT\" from file_sources where file_uuid = '" + fileUuid + "'")
  119. .get("LINE_COUNT");
  120. return res.intValue();
  121. }
  122. private void insertUnpopulatedFileSource(String projectUuid, String fileUuid, int numberOfHashes) {
  123. String lineHashes = generateLineHashes(numberOfHashes);
  124. insertFileSource(projectUuid, fileUuid, lineHashes, LINE_COUNT_NOT_POPULATED);
  125. }
  126. private void insertPopulatedFileSource(String projectUuid, String fileUuid, int lineCount) {
  127. String lineHashes = generateLineHashes(lineCount);
  128. insertFileSource(projectUuid, fileUuid, lineHashes, lineCount);
  129. }
  130. private int insertInconsistentPopulatedFileSource(String projectUuid, String fileUuid, int lineCount) {
  131. String lineHashes = generateLineHashes(lineCount);
  132. int badLineCount = lineCount + random.nextInt(6);
  133. insertFileSource(projectUuid, fileUuid, lineHashes, badLineCount);
  134. return badLineCount;
  135. }
  136. private static String generateLineHashes(int numberOfHashes) {
  137. return IntStream.range(0, numberOfHashes)
  138. .mapToObj(String::valueOf)
  139. .collect(Collectors.joining("\n"));
  140. }
  141. private void insertFileSource(String projectUuid, String fileUuid, @Nullable String lineHashes, int lineCount) {
  142. db.executeInsert(
  143. "FILE_SOURCES",
  144. "PROJECT_UUID", projectUuid,
  145. "FILE_UUID", fileUuid,
  146. "LINE_HASHES", lineHashes,
  147. "LINE_COUNT", lineCount,
  148. "CREATED_AT", 1_222_333L,
  149. "UPDATED_AT", 1_222_333L);
  150. db.commit();
  151. }
  152. private static Optional<CeTask.Component> newComponent(String projectUuid) {
  153. return Optional.of(new CeTask.Component(projectUuid, "key_" + projectUuid, "name_" + projectUuid));
  154. }
  155. }