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.

DuplicationRepositoryImplTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 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.Arrays;
  25. import org.assertj.core.api.Assertions;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  30. import org.sonar.ce.task.projectanalysis.util.WrapInSingleElementArray;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.when;
  35. @RunWith(DataProviderRunner.class)
  36. public class DuplicationRepositoryImplTest {
  37. private static final Component FILE_COMPONENT_1 = ReportComponent.builder(Component.Type.FILE, 1).build();
  38. private static final Component FILE_COMPONENT_2 = ReportComponent.builder(Component.Type.FILE, 2).build();
  39. private static final Duplication SOME_DUPLICATION = createDuplication(1, 2);
  40. private DuplicationRepository underTest = new DuplicationRepositoryImpl();
  41. @Test
  42. public void getDuplications_throws_NPE_if_Component_argument_is_null() {
  43. assertThatThrownBy(() -> underTest.getDuplications(null))
  44. .isInstanceOf(NullPointerException.class)
  45. .hasMessage("file can not be null");
  46. }
  47. @Test
  48. @UseDataProvider("allComponentTypesButFile")
  49. public void getDuplications_throws_IAE_if_Component_type_is_not_FILE(Component.Type type) {
  50. assertThatThrownBy(() -> {
  51. Component component = mockComponentGetType(type);
  52. underTest.getDuplications(component);
  53. })
  54. .isInstanceOf(IllegalArgumentException.class)
  55. .hasMessage("type of file must be FILE");
  56. }
  57. @Test
  58. public void getDuplications_returns_empty_set_when_repository_is_empty() {
  59. assertNoDuplication(FILE_COMPONENT_1);
  60. }
  61. @Test
  62. public void add_throws_NPE_if_file_argument_is_null() {
  63. assertThatThrownBy(() -> underTest.add(null, SOME_DUPLICATION))
  64. .isInstanceOf(NullPointerException.class)
  65. .hasMessage("file can not be null");
  66. }
  67. @Test
  68. public void addDuplication_inner_throws_NPE_if_duplication_argument_is_null() {
  69. assertThatThrownBy(() -> underTest.add(FILE_COMPONENT_1, null))
  70. .isInstanceOf(NullPointerException.class)
  71. .hasMessage("duplication can not be null");
  72. }
  73. @Test
  74. @UseDataProvider("allComponentTypesButFile")
  75. public void addDuplication_inner_throws_IAE_if_file_type_is_not_FILE(Component.Type type) {
  76. assertThatThrownBy(() -> {
  77. Component component = mockComponentGetType(type);
  78. underTest.add(component, SOME_DUPLICATION);
  79. })
  80. .isInstanceOf(IllegalArgumentException.class)
  81. .hasMessage("type of file must be FILE");
  82. }
  83. @Test
  84. public void added_duplication_is_returned_as_is_by_getDuplications() {
  85. underTest.add(FILE_COMPONENT_1, SOME_DUPLICATION);
  86. Iterable<Duplication> duplications = underTest.getDuplications(FILE_COMPONENT_1);
  87. Assertions.assertThat(duplications).hasSize(1);
  88. assertThat(duplications.iterator().next()).isSameAs(SOME_DUPLICATION);
  89. assertNoDuplication(FILE_COMPONENT_2);
  90. }
  91. @Test
  92. public void added_duplication_does_not_avoid_same_duplication_inserted_twice_but_only_one_is_returned() {
  93. underTest.add(FILE_COMPONENT_1, SOME_DUPLICATION);
  94. underTest.add(FILE_COMPONENT_1, SOME_DUPLICATION);
  95. Iterable<Duplication> duplications = underTest.getDuplications(FILE_COMPONENT_1);
  96. Assertions.assertThat(duplications).hasSize(1);
  97. assertThat(duplications.iterator().next()).isSameAs(SOME_DUPLICATION);
  98. assertNoDuplication(FILE_COMPONENT_2);
  99. }
  100. @Test
  101. public void added_duplications_are_returned_in_any_order_and_associated_to_the_right_file() {
  102. underTest.add(FILE_COMPONENT_1, SOME_DUPLICATION);
  103. underTest.add(FILE_COMPONENT_1, createDuplication(2, 4));
  104. underTest.add(FILE_COMPONENT_1, createDuplication(2, 3));
  105. underTest.add(FILE_COMPONENT_2, createDuplication(2, 3));
  106. underTest.add(FILE_COMPONENT_2, createDuplication(1, 2));
  107. assertThat(underTest.getDuplications(FILE_COMPONENT_1)).containsOnly(SOME_DUPLICATION, createDuplication(2, 3), createDuplication(2, 4));
  108. assertThat(underTest.getDuplications(FILE_COMPONENT_2)).containsOnly(createDuplication(1, 2), createDuplication(2, 3));
  109. }
  110. private static Duplication createDuplication(int originalLine, int duplicateLine) {
  111. return new Duplication(new TextBlock(originalLine, originalLine), Arrays.asList(new InnerDuplicate(new TextBlock(duplicateLine, duplicateLine))));
  112. }
  113. @DataProvider
  114. public static Object[][] allComponentTypesButFile() {
  115. return Arrays.stream(Component.Type.values())
  116. .filter(t -> t != Component.Type.FILE)
  117. .map(WrapInSingleElementArray.INSTANCE)
  118. .toArray(Object[][]::new);
  119. }
  120. private void assertNoDuplication(Component component) {
  121. assertThat(underTest.getDuplications(component)).isEmpty();
  122. }
  123. private Component mockComponentGetType(Component.Type type) {
  124. Component component = mock(Component.class);
  125. when(component.getType()).thenReturn(type);
  126. return component;
  127. }
  128. }