Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SourceLinesDiffImplTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.source;
  21. import java.util.Arrays;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  25. import org.sonar.ce.task.projectanalysis.component.Component;
  26. import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.ComponentDao;
  30. import org.sonar.db.source.FileSourceDao;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.mockito.Mockito.mock;
  33. import static org.mockito.Mockito.when;
  34. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  35. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  36. public class SourceLinesDiffImplTest {
  37. private DbClient dbClient = mock(DbClient.class);
  38. private DbSession dbSession = mock(DbSession.class);
  39. private ComponentDao componentDao = mock(ComponentDao.class);
  40. private FileSourceDao fileSourceDao = mock(FileSourceDao.class);
  41. private SourceLinesHashRepository sourceLinesHash = mock(SourceLinesHashRepository.class);
  42. private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
  43. private ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
  44. private SourceLinesDiffImpl underTest = new SourceLinesDiffImpl(dbClient, fileSourceDao, sourceLinesHash,
  45. referenceBranchComponentUuids, analysisMetadataHolder);
  46. private static final int FILE_REF = 1;
  47. private static final String[] CONTENT = {
  48. "package org.sonar.ce.task.projectanalysis.source_diff;",
  49. "",
  50. "public class Foo {",
  51. " public String bar() {",
  52. " return \"Doh!\";",
  53. " }",
  54. "}"
  55. };
  56. @Before
  57. public void setUp() {
  58. when(dbClient.openSession(false)).thenReturn(dbSession);
  59. when(dbClient.componentDao()).thenReturn(componentDao);
  60. when(dbClient.fileSourceDao()).thenReturn(fileSourceDao);
  61. }
  62. @Test
  63. public void should_find_diff_with_reference_branch_for_prs() {
  64. Component component = fileComponent(FILE_REF);
  65. mockLineHashesInDb(2, CONTENT);
  66. setLineHashesInReport(component, CONTENT);
  67. when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
  68. when(referenceBranchComponentUuids.getComponentUuid(component.getKey())).thenReturn("uuid_2");
  69. assertThat(underTest.computeMatchingLines(component)).containsExactly(1, 2, 3, 4, 5, 6, 7);
  70. }
  71. @Test
  72. public void all_file_is_modified_if_no_source_in_db() {
  73. Component component = fileComponent(FILE_REF);
  74. setLineHashesInReport(component, CONTENT);
  75. assertThat(underTest.computeMatchingLines(component)).containsExactly(0, 0, 0, 0, 0, 0, 0);
  76. }
  77. @Test
  78. public void should_find_no_diff_when_report_and_db_content_are_identical() {
  79. Component component = fileComponent(FILE_REF);
  80. mockLineHashesInDb(FILE_REF, CONTENT);
  81. setLineHashesInReport(component, CONTENT);
  82. assertThat(underTest.computeMatchingLines(component)).containsExactly(1, 2, 3, 4, 5, 6, 7);
  83. }
  84. private void mockLineHashesInDb(int ref, String[] lineHashes) {
  85. when(fileSourceDao.selectLineHashes(dbSession, componentUuidOf(String.valueOf(ref))))
  86. .thenReturn(Arrays.asList(lineHashes));
  87. }
  88. private static String componentUuidOf(String key) {
  89. return "uuid_" + key;
  90. }
  91. private static Component fileComponent(int ref) {
  92. return builder(FILE, ref)
  93. .setName("report_path" + ref)
  94. .setUuid(componentUuidOf("" + ref))
  95. .build();
  96. }
  97. private void setLineHashesInReport(Component component, String[] content) {
  98. when(sourceLinesHash.getLineHashesMatchingDBVersion(component)).thenReturn(Arrays.asList(content));
  99. }
  100. }