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.

MutableMovedFilesRepositoryImplTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.filemove;
  21. import java.util.Random;
  22. import org.junit.Test;
  23. import org.sonar.ce.task.projectanalysis.component.Component;
  24. import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  27. import static org.assertj.core.api.Assertions.fail;
  28. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  29. public class MutableMovedFilesRepositoryImplTest {
  30. private static final Component SOME_FILE = builder(Component.Type.FILE, 1).build();
  31. private static final Component[] COMPONENTS_EXCEPT_FILE = {
  32. builder(Component.Type.PROJECT, 1).build(),
  33. builder(Component.Type.DIRECTORY, 1).build(),
  34. ViewsComponent.builder(Component.Type.VIEW, 1).build(),
  35. ViewsComponent.builder(Component.Type.SUBVIEW, 1).build(),
  36. ViewsComponent.builder(Component.Type.PROJECT_VIEW, 1).build()
  37. };
  38. private static final MovedFilesRepository.OriginalFile SOME_ORIGINAL_FILE = new MovedFilesRepository.OriginalFile("uuid for 100", "key for 100");
  39. private MutableMovedFilesRepositoryImpl underTest = new MutableMovedFilesRepositoryImpl();
  40. @Test
  41. public void setOriginalFile_throws_NPE_when_file_is_null() {
  42. assertThatThrownBy(() -> underTest.setOriginalFile(null, SOME_ORIGINAL_FILE))
  43. .isInstanceOf(NullPointerException.class)
  44. .hasMessage("file can't be null");
  45. }
  46. @Test
  47. public void setOriginalFile_throws_NPE_when_originalFile_is_null() {
  48. assertThatThrownBy(() -> underTest.setOriginalFile(SOME_FILE, null))
  49. .isInstanceOf(NullPointerException.class)
  50. .hasMessage("originalFile can't be null");
  51. }
  52. @Test
  53. public void setOriginalFile_throws_IAE_when_type_is_no_FILE() {
  54. for (Component component : COMPONENTS_EXCEPT_FILE) {
  55. try {
  56. underTest.setOriginalFile(component, SOME_ORIGINAL_FILE);
  57. fail("should have raised a NPE");
  58. } catch (IllegalArgumentException e) {
  59. assertThat(e)
  60. .isInstanceOf(IllegalArgumentException.class)
  61. .hasMessage("file must be of type FILE");
  62. }
  63. }
  64. }
  65. @Test
  66. public void setOriginalFile_throws_ISE_if_settings_another_originalFile() {
  67. underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
  68. assertThatThrownBy(() -> underTest.setOriginalFile(SOME_FILE, new MovedFilesRepository.OriginalFile("uudi", "key")))
  69. .isInstanceOf(IllegalStateException.class)
  70. .hasMessageContaining("Original file OriginalFile{uuid='uuid for 100', key='key for 100'} " +
  71. "already registered for file ReportComponent{ref=1, key='key_1', type=FILE}");
  72. }
  73. @Test
  74. public void setOriginalFile_does_not_fail_if_same_original_file_is_added_multiple_times_for_the_same_component() {
  75. underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
  76. for (int i = 0; i < 1 + Math.abs(new Random().nextInt(10)); i++) {
  77. underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
  78. }
  79. }
  80. @Test
  81. public void setOriginalFile_does_not_fail_when_originalFile_is_added_twice_for_different_files() {
  82. underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
  83. underTest.setOriginalFile(builder(Component.Type.FILE, 2).build(), SOME_ORIGINAL_FILE);
  84. }
  85. @Test
  86. public void getOriginalFile_throws_NPE_when_file_is_null() {
  87. assertThatThrownBy(() -> underTest.getOriginalFile(null))
  88. .isInstanceOf(NullPointerException.class)
  89. .hasMessage("file can't be null");
  90. }
  91. @Test
  92. public void getOriginalFile_returns_absent_for_any_component_type_when_empty() {
  93. assertThat(underTest.getOriginalFile(SOME_FILE)).isEmpty();
  94. for (Component component : COMPONENTS_EXCEPT_FILE) {
  95. assertThat(underTest.getOriginalFile(component)).isEmpty();
  96. }
  97. }
  98. @Test
  99. public void getOriginalFile_returns_absent_for_any_type_of_Component_but_file_when_non_empty() {
  100. underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
  101. for (Component component : COMPONENTS_EXCEPT_FILE) {
  102. assertThat(underTest.getOriginalFile(component)).isEmpty();
  103. }
  104. assertThat(underTest.getOriginalFile(SOME_FILE)).contains(SOME_ORIGINAL_FILE);
  105. }
  106. @Test
  107. public void getOriginalFile_returns_originalFile_base_on_file_key() {
  108. underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
  109. assertThat(underTest.getOriginalFile(SOME_FILE)).contains(SOME_ORIGINAL_FILE);
  110. assertThat(underTest.getOriginalFile(builder(Component.Type.FILE, 1).setUuid("toto").build())).contains(SOME_ORIGINAL_FILE);
  111. }
  112. }