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.

ScrollForFileMoveComponentDaoTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.db.component;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Random;
  27. import java.util.stream.Collectors;
  28. import java.util.stream.IntStream;
  29. import org.apache.ibatis.session.ResultContext;
  30. import org.apache.ibatis.session.ResultHandler;
  31. import org.junit.Rule;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.DbTester;
  37. import org.sonar.db.organization.OrganizationDto;
  38. import org.sonar.db.source.FileSourceDto;
  39. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.junit.Assert.fail;
  42. import static org.sonar.api.resources.Qualifiers.FILE;
  43. import static org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE;
  44. @RunWith(DataProviderRunner.class)
  45. public class ScrollForFileMoveComponentDaoTest {
  46. @Rule
  47. public DbTester db = DbTester.create(System2.INSTANCE);
  48. private Random random = new Random();
  49. private DbSession dbSession = db.getSession();
  50. private ComponentDao underTest = new ComponentDao();
  51. @Test
  52. public void scrollAllFilesForFileMove_has_no_effect_if_project_does_not_exist() {
  53. String nonExistingProjectUuid = randomAlphabetic(10);
  54. underTest.scrollAllFilesForFileMove(dbSession, nonExistingProjectUuid, resultContext -> fail("handler should not be called"));
  55. }
  56. @Test
  57. public void scrollAllFilesForFileMove_has_no_effect_if_project_has_no_file() {
  58. OrganizationDto organization = db.organizations().insert();
  59. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  60. underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultContext -> fail("handler should not be called"));
  61. }
  62. @Test
  63. public void scrollAllFilesForFileMove_ignores_files_with_null_path() {
  64. OrganizationDto organization = db.organizations().insert();
  65. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  66. ComponentAndSource file = insertFileAndSource(project, FILE);
  67. ComponentAndSource ut = insertFileAndSource(project, UNIT_TEST_FILE);
  68. ComponentDto fileNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project).setPath(null).setQualifier(FILE));
  69. db.fileSources().insertFileSource(fileNoPath);
  70. ComponentDto utNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project).setPath(null).setQualifier(UNIT_TEST_FILE));
  71. db.fileSources().insertFileSource(utNoPath);
  72. RecordingResultHandler resultHandler = new RecordingResultHandler();
  73. underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler);
  74. assertThat(resultHandler.dtos).hasSize(2);
  75. verifyFileMoveRowDto(resultHandler, file);
  76. verifyFileMoveRowDto(resultHandler, ut);
  77. }
  78. @Test
  79. public void scrollAllFilesForFileMove_ignores_files_without_source() {
  80. OrganizationDto organization = db.organizations().insert();
  81. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  82. ComponentAndSource file = insertFileAndSource(project, FILE);
  83. ComponentAndSource ut = insertFileAndSource(project, UNIT_TEST_FILE);
  84. ComponentDto fileNoSource = db.components().insertComponent(ComponentTesting.newFileDto(project).setPath(null).setQualifier(FILE));
  85. ComponentDto utNoSource = db.components().insertComponent(ComponentTesting.newFileDto(project).setPath(null).setQualifier(UNIT_TEST_FILE));
  86. RecordingResultHandler resultHandler = new RecordingResultHandler();
  87. underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler);
  88. assertThat(resultHandler.dtos).hasSize(2);
  89. verifyFileMoveRowDto(resultHandler, file);
  90. verifyFileMoveRowDto(resultHandler, ut);
  91. }
  92. @Test
  93. public void scrollAllFilesForFileMove_scrolls_files_of_project() {
  94. OrganizationDto organization = db.organizations().insert();
  95. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  96. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project));
  97. ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(module1));
  98. ComponentAndSource file1 = insertFileAndSource(project, FILE);
  99. ComponentAndSource file2 = insertFileAndSource(module1, FILE);
  100. ComponentAndSource file3 = insertFileAndSource(module2, FILE);
  101. RecordingResultHandler resultHandler = new RecordingResultHandler();
  102. underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler);
  103. assertThat(resultHandler.dtos).hasSize(3);
  104. verifyFileMoveRowDto(resultHandler, file1);
  105. verifyFileMoveRowDto(resultHandler, file2);
  106. verifyFileMoveRowDto(resultHandler, file3);
  107. }
  108. @Test
  109. public void scrollAllFilesForFileMove_scrolls_large_number_of_files_and_uts() {
  110. OrganizationDto organization = db.organizations().insert();
  111. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  112. List<ComponentAndSource> files = IntStream.range(0, 300 + random.nextInt(500))
  113. .mapToObj(i -> {
  114. String qualifier = random.nextBoolean() ? FILE : UNIT_TEST_FILE;
  115. ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project).setDbKey("f_" + i).setQualifier(qualifier));
  116. FileSourceDto fileSource = db.fileSources().insertFileSource(file);
  117. return new ComponentAndSource(file, fileSource);
  118. })
  119. .collect(Collectors.toList());
  120. RecordingResultHandler resultHandler = new RecordingResultHandler();
  121. underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler);
  122. assertThat(resultHandler.dtos).hasSize(files.size());
  123. files.forEach(f -> verifyFileMoveRowDto(resultHandler, f));
  124. }
  125. @Test
  126. public void scrollAllFilesForFileMove_scrolls_unit_tests_of_project() {
  127. OrganizationDto organization = db.organizations().insert();
  128. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  129. ComponentAndSource ut = insertFileAndSource(project, UNIT_TEST_FILE);
  130. RecordingResultHandler resultHandler = new RecordingResultHandler();
  131. underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler);
  132. assertThat(resultHandler.dtos).hasSize(1);
  133. verifyFileMoveRowDto(resultHandler, ut);
  134. }
  135. @Test
  136. @UseDataProvider("branchTypes")
  137. public void scrollAllFilesForFileMove_scrolls_files_and_unit_tests_of_branch(BranchType branchType) {
  138. OrganizationDto organization = db.organizations().insert();
  139. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  140. ComponentDto branch = db.components().insertProjectBranch(project, t -> t.setBranchType(branchType));
  141. ComponentAndSource file = insertFileAndSource(branch, FILE);
  142. ComponentAndSource ut = insertFileAndSource(branch, UNIT_TEST_FILE);
  143. RecordingResultHandler resultHandler = new RecordingResultHandler();
  144. underTest.scrollAllFilesForFileMove(dbSession, branch.uuid(), resultHandler);
  145. assertThat(resultHandler.dtos).hasSize(2);
  146. verifyFileMoveRowDto(resultHandler, file);
  147. verifyFileMoveRowDto(resultHandler, ut);
  148. }
  149. @DataProvider
  150. public static Object[][] branchTypes() {
  151. return new Object[][] {
  152. {BranchType.LONG},
  153. {BranchType.SHORT},
  154. {BranchType.PULL_REQUEST}
  155. };
  156. }
  157. @Test
  158. public void scrollAllFilesForFileMove_ignores_non_file_and_non_ut_component_with_source() {
  159. OrganizationDto organization = db.organizations().insert();
  160. ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  161. db.fileSources().insertFileSource(project);
  162. ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project));
  163. db.fileSources().insertFileSource(module);
  164. ComponentDto dir = db.components().insertComponent(ComponentTesting.newDirectory(module, "foo"));
  165. db.fileSources().insertFileSource(dir);
  166. ComponentAndSource file = insertFileAndSource(module, FILE);
  167. ComponentAndSource ut = insertFileAndSource(dir, UNIT_TEST_FILE);
  168. ComponentDto portfolio = random.nextBoolean() ? db.components().insertPublicPortfolio(organization) : db.components().insertPrivatePortfolio(organization);
  169. db.fileSources().insertFileSource(portfolio);
  170. ComponentDto subView = db.components().insertSubView(portfolio);
  171. db.fileSources().insertFileSource(subView);
  172. ComponentDto application = random.nextBoolean() ? db.components().insertPrivateApplication(organization) : db.components().insertPublicApplication(organization);
  173. db.fileSources().insertFileSource(application);
  174. RecordingResultHandler resultHandler = new RecordingResultHandler();
  175. underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler);
  176. underTest.scrollAllFilesForFileMove(dbSession, portfolio.uuid(), resultHandler);
  177. underTest.scrollAllFilesForFileMove(dbSession, application.uuid(), resultHandler);
  178. assertThat(resultHandler.dtos).hasSize(2);
  179. verifyFileMoveRowDto(resultHandler, file);
  180. verifyFileMoveRowDto(resultHandler, ut);
  181. }
  182. private static final class RecordingResultHandler implements ResultHandler<FileMoveRowDto> {
  183. List<FileMoveRowDto> dtos = new ArrayList<>();
  184. @Override
  185. public void handleResult(ResultContext<? extends FileMoveRowDto> resultContext) {
  186. dtos.add(resultContext.getResultObject());
  187. }
  188. private java.util.Optional<FileMoveRowDto> getById(long id) {
  189. return dtos.stream().filter(t -> t.getId() == id).findAny();
  190. }
  191. }
  192. private ComponentAndSource insertFileAndSource(ComponentDto parent, String qualifier) {
  193. ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(parent).setQualifier(qualifier));
  194. FileSourceDto fileSource = db.fileSources().insertFileSource(file);
  195. return new ComponentAndSource(file, fileSource);
  196. }
  197. private static final class ComponentAndSource {
  198. private final ComponentDto component;
  199. private final FileSourceDto source;
  200. private ComponentAndSource(ComponentDto component, FileSourceDto source) {
  201. this.component = component;
  202. this.source = source;
  203. }
  204. }
  205. private static void verifyFileMoveRowDto(RecordingResultHandler resultHander, ComponentAndSource componentAndSource) {
  206. FileMoveRowDto dto = resultHander.getById(componentAndSource.component.getId()).get();
  207. assertThat(dto.getKey()).isEqualTo(componentAndSource.component.getDbKey());
  208. assertThat(dto.getUuid()).isEqualTo(componentAndSource.component.uuid());
  209. assertThat(dto.getPath()).isEqualTo(componentAndSource.component.path());
  210. assertThat(dto.getLineCount()).isEqualTo(componentAndSource.source.getLineCount());
  211. }
  212. }