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.

ComponentImplTest.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.component;
  21. import java.util.Arrays;
  22. import java.util.Collections;
  23. import org.junit.Test;
  24. import org.sonar.ce.task.projectanalysis.component.Component.Status;
  25. import static com.google.common.base.Strings.repeat;
  26. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. import static org.assertj.core.api.Assertions.fail;
  30. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  31. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  32. import static org.sonar.ce.task.projectanalysis.component.ComponentImpl.builder;
  33. public class ComponentImplTest {
  34. static final String KEY = "KEY";
  35. static final String UUID = "UUID";
  36. @Test
  37. public void verify_key_uuid_and_name() {
  38. ComponentImpl component = buildSimpleComponent(FILE, KEY).setUuid(UUID).setName("name").build();
  39. assertThat(component.getDbKey()).isEqualTo(KEY);
  40. assertThat(component.getUuid()).isEqualTo(UUID);
  41. assertThat(component.getName()).isEqualTo("name");
  42. }
  43. @Test
  44. public void builder_throws_NPE_if_component_arg_is_Null() {
  45. assertThatThrownBy(() -> builder(null))
  46. .isInstanceOf(NullPointerException.class);
  47. }
  48. @Test
  49. public void builder_throws_NPE_if_status_arg_is_Null() {
  50. assertThatThrownBy(() -> builder(FILE).setStatus(null))
  51. .isInstanceOf(NullPointerException.class);
  52. }
  53. @Test
  54. public void builder_throws_NPE_if_status_is_Null() {
  55. assertThatThrownBy(() -> {
  56. builder(Component.Type.DIRECTORY)
  57. .setName("DIR")
  58. .setDbKey(KEY)
  59. .setUuid(UUID)
  60. .setReportAttributes(ReportAttributes.newBuilder(1).build())
  61. .build();
  62. })
  63. .isInstanceOf(NullPointerException.class);
  64. }
  65. @Test
  66. public void set_key_throws_NPE_if_component_arg_is_Null() {
  67. assertThatThrownBy(() -> builder(FILE).setUuid(null))
  68. .isInstanceOf(NullPointerException.class);
  69. }
  70. @Test
  71. public void set_uuid_throws_NPE_if_component_arg_is_Null() {
  72. assertThatThrownBy(() -> builder(FILE).setDbKey(null))
  73. .isInstanceOf(NullPointerException.class);
  74. }
  75. @Test
  76. public void build_without_key_throws_NPE_if_component_arg_is_Null() {
  77. assertThatThrownBy(() -> builder(FILE).setUuid("ABCD").build())
  78. .isInstanceOf(NullPointerException.class);
  79. }
  80. @Test
  81. public void build_without_uuid_throws_NPE_if_component_arg_is_Null() {
  82. assertThatThrownBy(() -> builder(FILE).setDbKey(KEY).build())
  83. .isInstanceOf(NullPointerException.class);
  84. }
  85. @Test
  86. public void get_name_from_batch_component() {
  87. String name = "project";
  88. ComponentImpl component = buildSimpleComponent(FILE, "file").setName(name).build();
  89. assertThat(component.getName()).isEqualTo(name);
  90. }
  91. @Test
  92. public void getFileAttributes_throws_ISE_if_BatchComponent_does_not_have_type_FILE() {
  93. Arrays.stream(Component.Type.values())
  94. .filter(type -> type != FILE)
  95. .forEach((componentType) -> {
  96. ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
  97. try {
  98. component.getFileAttributes();
  99. fail("A IllegalStateException should have been raised");
  100. } catch (IllegalStateException e) {
  101. assertThat(e).hasMessage("Only component of type FILE have a FileAttributes object");
  102. }
  103. });
  104. }
  105. @Test
  106. public void getSubViewAttributes_throws_ISE_if_component_is_not_have_type_SUBVIEW() {
  107. Arrays.stream(Component.Type.values())
  108. .filter(type -> type != FILE)
  109. .forEach((componentType) -> {
  110. ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
  111. try {
  112. component.getSubViewAttributes();
  113. fail("A IllegalStateException should have been raised");
  114. } catch (IllegalStateException e) {
  115. assertThat(e).hasMessage("Only component of type SUBVIEW have a SubViewAttributes object");
  116. }
  117. });
  118. }
  119. @Test
  120. public void getViewAttributes_throws_ISE_if_component_is_not_have_type_VIEW() {
  121. Arrays.stream(Component.Type.values())
  122. .filter(type -> type != FILE)
  123. .forEach((componentType) -> {
  124. ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
  125. try {
  126. component.getViewAttributes();
  127. fail("A IllegalStateException should have been raised");
  128. } catch (IllegalStateException e) {
  129. assertThat(e).hasMessage("Only component of type VIEW have a ViewAttributes object");
  130. }
  131. });
  132. }
  133. @Test
  134. public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() {
  135. ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(true, null, 1)).build();
  136. assertThat(component.getFileAttributes().isUnitTest()).isTrue();
  137. }
  138. @Test
  139. public void isUnitTest_returns_value_of_language_of_BatchComponent() {
  140. String languageKey = "some language key";
  141. ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(false, languageKey, 1)).build();
  142. assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
  143. }
  144. @Test
  145. public void keep_500_first_characters_of_name() {
  146. String veryLongString = repeat("a", 3_000);
  147. ComponentImpl underTest = buildSimpleComponent(FILE, "file")
  148. .setName(veryLongString)
  149. .build();
  150. String expectedName = repeat("a", 500 - 3) + "...";
  151. assertThat(underTest.getName()).isEqualTo(expectedName);
  152. }
  153. @Test
  154. public void keep_2000_first_characters_of_description() {
  155. String veryLongString = repeat("a", 3_000);
  156. ComponentImpl underTest = buildSimpleComponent(FILE, "file")
  157. .setDescription(veryLongString)
  158. .build();
  159. String expectedDescription = repeat("a", 2_000 - 3) + "...";
  160. assertThat(underTest.getDescription()).isEqualTo(expectedDescription);
  161. }
  162. @Test
  163. public void build_with_child() {
  164. ComponentImpl child = builder(FILE)
  165. .setName("CHILD_NAME")
  166. .setDbKey("CHILD_KEY")
  167. .setUuid("CHILD_UUID")
  168. .setStatus(Status.UNAVAILABLE)
  169. .setReportAttributes(ReportAttributes.newBuilder(2).build())
  170. .build();
  171. ComponentImpl componentImpl = builder(Component.Type.DIRECTORY)
  172. .setName("DIR")
  173. .setDbKey(KEY)
  174. .setUuid(UUID)
  175. .setStatus(Status.UNAVAILABLE)
  176. .setReportAttributes(ReportAttributes.newBuilder(1).build())
  177. .addChildren(Collections.singletonList(child))
  178. .build();
  179. assertThat(componentImpl.getChildren()).hasSize(1);
  180. Component childReloaded = componentImpl.getChildren().iterator().next();
  181. assertThat(childReloaded.getDbKey()).isEqualTo("CHILD_KEY");
  182. assertThat(childReloaded.getUuid()).isEqualTo("CHILD_UUID");
  183. assertThat(childReloaded.getType()).isEqualTo(FILE);
  184. }
  185. @Test
  186. public void equals_compares_on_uuid_only() {
  187. ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
  188. assertThat(builder.build()).isEqualTo(builder.build());
  189. assertThat(builder.build()).isEqualTo(buildSimpleComponent(FILE, "2").setUuid(UUID).build());
  190. assertThat(builder.build()).isNotEqualTo(buildSimpleComponent(FILE, "1").setUuid("otherUUid").build());
  191. }
  192. @Test
  193. public void hashCode_is_hashcode_of_uuid() {
  194. ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
  195. assertThat(builder.build()).hasSameHashCodeAs(builder.build().hashCode());
  196. assertThat(builder.build()).hasSameHashCodeAs(buildSimpleComponent(FILE, "2").setUuid(UUID).build().hashCode());
  197. assertThat(builder.build()).hasSameHashCodeAs(UUID.hashCode());
  198. }
  199. private static ComponentImpl.Builder buildSimpleComponent(Component.Type type, String dbKey) {
  200. ComponentImpl.Builder builder = builder(type)
  201. .setName("name_" + dbKey)
  202. .setDbKey(dbKey)
  203. .setStatus(Status.UNAVAILABLE)
  204. .setUuid("uuid_" + dbKey)
  205. .setReportAttributes(ReportAttributes.newBuilder(dbKey.hashCode()).build());
  206. if (type == PROJECT) {
  207. String buildString = randomAlphabetic(15);
  208. builder.setProjectAttributes(new ProjectAttributes("version_1", buildString, "453def"));
  209. }
  210. return builder;
  211. }
  212. }