3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.component;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.component.Component.Status;
27 import static com.google.common.base.Strings.repeat;
28 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.assertThatThrownBy;
31 import static org.assertj.core.api.Assertions.fail;
32 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
33 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
34 import static org.sonar.ce.task.projectanalysis.component.ComponentImpl.builder;
36 public class ComponentImplTest {
38 static final String KEY = "KEY";
39 static final String UUID = "UUID";
43 public void verify_key_uuid_and_name() {
44 ComponentImpl component = buildSimpleComponent(FILE, KEY).setUuid(UUID).setName("name").build();
46 assertThat(component.getKey()).isEqualTo(KEY);
47 assertThat(component.getUuid()).isEqualTo(UUID);
48 assertThat(component.getName()).isEqualTo("name");
52 public void builder_throws_NPE_if_component_arg_is_Null() {
53 assertThatThrownBy(() -> builder(null))
54 .isInstanceOf(NullPointerException.class);
58 public void builder_throws_NPE_if_status_arg_is_Null() {
59 assertThatThrownBy(() -> builder(FILE).setStatus(null))
60 .isInstanceOf(NullPointerException.class);
64 public void builder_throws_NPE_if_status_is_Null() {
65 assertThatThrownBy(() -> {
66 builder(Component.Type.DIRECTORY)
70 .setReportAttributes(ReportAttributes.newBuilder(1).build())
73 .isInstanceOf(NullPointerException.class);
77 public void set_key_throws_NPE_if_component_arg_is_Null() {
78 assertThatThrownBy(() -> builder(FILE).setUuid(null))
79 .isInstanceOf(NullPointerException.class);
83 public void set_uuid_throws_NPE_if_component_arg_is_Null() {
84 assertThatThrownBy(() -> builder(FILE).setKey(null))
85 .isInstanceOf(NullPointerException.class);
89 public void build_without_key_throws_NPE_if_component_arg_is_Null() {
90 assertThatThrownBy(() -> builder(FILE).setUuid("ABCD").build())
91 .isInstanceOf(NullPointerException.class);
95 public void build_without_uuid_throws_NPE_if_component_arg_is_Null() {
96 assertThatThrownBy(() -> builder(FILE).setKey(KEY).build())
97 .isInstanceOf(NullPointerException.class);
101 public void get_name_from_batch_component() {
102 String name = "project";
103 ComponentImpl component = buildSimpleComponent(FILE, "file").setName(name).build();
104 assertThat(component.getName()).isEqualTo(name);
108 public void getFileAttributes_throws_ISE_if_BatchComponent_does_not_have_type_FILE() {
109 Arrays.stream(Component.Type.values())
110 .filter(type -> type != FILE)
111 .forEach((componentType) -> {
112 ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
114 component.getFileAttributes();
115 fail("A IllegalStateException should have been raised");
116 } catch (IllegalStateException e) {
117 assertThat(e).hasMessage("Only component of type FILE have a FileAttributes object");
123 public void getSubViewAttributes_throws_ISE_if_component_is_not_have_type_SUBVIEW() {
124 Arrays.stream(Component.Type.values())
125 .filter(type -> type != FILE)
126 .forEach((componentType) -> {
127 ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
129 component.getSubViewAttributes();
130 fail("A IllegalStateException should have been raised");
131 } catch (IllegalStateException e) {
132 assertThat(e).hasMessage("Only component of type SUBVIEW have a SubViewAttributes object");
138 public void getViewAttributes_throws_ISE_if_component_is_not_have_type_VIEW() {
139 Arrays.stream(Component.Type.values())
140 .filter(type -> type != FILE)
141 .forEach((componentType) -> {
142 ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
144 component.getViewAttributes();
145 fail("A IllegalStateException should have been raised");
146 } catch (IllegalStateException e) {
147 assertThat(e).hasMessage("Only component of type VIEW have a ViewAttributes object");
153 public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() {
154 ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(true, null, 1)).build();
156 assertThat(component.getFileAttributes().isUnitTest()).isTrue();
160 public void isUnitTest_returns_value_of_language_of_BatchComponent() {
161 String languageKey = "some language key";
162 ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(false, languageKey, 1)).build();
164 assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
168 public void keep_500_first_characters_of_name() {
169 String veryLongString = repeat("a", 3_000);
171 ComponentImpl underTest = buildSimpleComponent(FILE, "file")
172 .setName(veryLongString)
175 String expectedName = repeat("a", 500 - 3) + "...";
176 assertThat(underTest.getName()).isEqualTo(expectedName);
180 public void keep_2000_first_characters_of_description() {
181 String veryLongString = repeat("a", 3_000);
183 ComponentImpl underTest = buildSimpleComponent(FILE, "file")
184 .setDescription(veryLongString)
187 String expectedDescription = repeat("a", 2_000 - 3) + "...";
188 assertThat(underTest.getDescription()).isEqualTo(expectedDescription);
192 public void build_with_child() {
193 ComponentImpl child = builder(FILE)
194 .setName("CHILD_NAME")
196 .setUuid("CHILD_UUID")
197 .setStatus(Status.UNAVAILABLE)
198 .setReportAttributes(ReportAttributes.newBuilder(2).build())
200 ComponentImpl componentImpl = builder(Component.Type.DIRECTORY)
204 .setStatus(Status.UNAVAILABLE)
205 .setReportAttributes(ReportAttributes.newBuilder(1).build())
206 .addChildren(Collections.singletonList(child))
209 assertThat(componentImpl.getChildren()).hasSize(1);
210 Component childReloaded = componentImpl.getChildren().iterator().next();
211 assertThat(childReloaded.getKey()).isEqualTo("CHILD_KEY");
212 assertThat(childReloaded.getUuid()).isEqualTo("CHILD_UUID");
213 assertThat(childReloaded.getType()).isEqualTo(FILE);
217 public void equals_compares_on_uuid_only() {
218 ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
220 assertThat(builder.build()).isEqualTo(builder.build());
221 assertThat(builder.build()).isEqualTo(buildSimpleComponent(FILE, "2").setUuid(UUID).build());
222 assertThat(builder.build()).isNotEqualTo(buildSimpleComponent(FILE, "1").setUuid("otherUUid").build());
226 public void hashCode_is_hashcode_of_uuid() {
227 ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
229 assertThat(builder.build()).hasSameHashCodeAs(builder.build().hashCode());
230 assertThat(builder.build()).hasSameHashCodeAs(buildSimpleComponent(FILE, "2").setUuid(UUID).build().hashCode());
231 assertThat(builder.build()).hasSameHashCodeAs(UUID.hashCode());
234 private static ComponentImpl.Builder buildSimpleComponent(Component.Type type, String dbKey) {
235 ComponentImpl.Builder builder = builder(type)
236 .setName("name_" + dbKey)
238 .setStatus(Status.UNAVAILABLE)
239 .setUuid("uuid_" + dbKey)
240 .setReportAttributes(ReportAttributes.newBuilder(dbKey.hashCode()).build());
241 if (type == PROJECT) {
242 String buildString = randomAlphabetic(15);
243 builder.setProjectAttributes(new ProjectAttributes("version_1", buildString, "453def"));