3 * Copyright (C) 2009-2019 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.apache.commons.lang.RandomStringUtils;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.ce.task.projectanalysis.component.Component.Status;
30 import static com.google.common.base.Strings.repeat;
31 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.fail;
34 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
35 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
36 import static org.sonar.ce.task.projectanalysis.component.ComponentImpl.builder;
38 public class ComponentImplTest {
40 static final String KEY = "KEY";
41 static final String UUID = "UUID";
44 public ExpectedException expectedException = ExpectedException.none();
47 public void verify_key_uuid_and_name() {
48 ComponentImpl component = buildSimpleComponent(FILE, KEY).setUuid(UUID).setName("name").build();
50 assertThat(component.getDbKey()).isEqualTo(KEY);
51 assertThat(component.getUuid()).isEqualTo(UUID);
52 assertThat(component.getName()).isEqualTo("name");
56 public void builder_throws_NPE_if_component_arg_is_Null() {
57 expectedException.expect(NullPointerException.class);
63 public void builder_throws_NPE_if_status_arg_is_Null() {
64 expectedException.expect(NullPointerException.class);
66 builder(FILE).setStatus(null);
70 public void builder_throws_NPE_if_status_is_Null() {
71 expectedException.expect(NullPointerException.class);
73 builder(Component.Type.DIRECTORY)
77 .setReportAttributes(ReportAttributes.newBuilder(1).build())
82 public void set_key_throws_NPE_if_component_arg_is_Null() {
83 expectedException.expect(NullPointerException.class);
85 builder(FILE).setUuid(null);
89 public void set_uuid_throws_NPE_if_component_arg_is_Null() {
90 expectedException.expect(NullPointerException.class);
92 builder(FILE).setDbKey(null);
96 public void build_without_key_throws_NPE_if_component_arg_is_Null() {
97 expectedException.expect(NullPointerException.class);
99 builder(FILE).setUuid("ABCD").build();
103 public void build_without_uuid_throws_NPE_if_component_arg_is_Null() {
104 expectedException.expect(NullPointerException.class);
106 builder(FILE).setDbKey(KEY).build();
110 public void get_name_from_batch_component() {
111 String name = "project";
112 ComponentImpl component = buildSimpleComponent(FILE, "file").setName(name).build();
113 assertThat(component.getName()).isEqualTo(name);
117 public void getFileAttributes_throws_ISE_if_BatchComponent_does_not_have_type_FILE() {
118 Arrays.stream(Component.Type.values())
119 .filter(type -> type != FILE)
120 .forEach((componentType) -> {
121 ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
123 component.getFileAttributes();
124 fail("A IllegalStateException should have been raised");
125 } catch (IllegalStateException e) {
126 assertThat(e).hasMessage("Only component of type FILE have a FileAttributes object");
132 public void getSubViewAttributes_throws_ISE_if_component_is_not_have_type_SUBVIEW() {
133 Arrays.stream(Component.Type.values())
134 .filter(type -> type != FILE)
135 .forEach((componentType) -> {
136 ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
138 component.getSubViewAttributes();
139 fail("A IllegalStateException should have been raised");
140 } catch (IllegalStateException e) {
141 assertThat(e).hasMessage("Only component of type SUBVIEW have a SubViewAttributes object");
147 public void getViewAttributes_throws_ISE_if_component_is_not_have_type_VIEW() {
148 Arrays.stream(Component.Type.values())
149 .filter(type -> type != FILE)
150 .forEach((componentType) -> {
151 ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build();
153 component.getViewAttributes();
154 fail("A IllegalStateException should have been raised");
155 } catch (IllegalStateException e) {
156 assertThat(e).hasMessage("Only component of type VIEW have a ViewAttributes object");
162 public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() {
163 ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(true, null, 1)).build();
165 assertThat(component.getFileAttributes().isUnitTest()).isTrue();
169 public void isUnitTest_returns_value_of_language_of_BatchComponent() {
170 String languageKey = "some language key";
171 ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(false, languageKey, 1)).build();
173 assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
177 public void keep_500_first_characters_of_name() {
178 String veryLongString = repeat("a", 3_000);
180 ComponentImpl underTest = buildSimpleComponent(FILE, "file")
181 .setName(veryLongString)
184 String expectedName = repeat("a", 500 - 3) + "...";
185 assertThat(underTest.getName()).isEqualTo(expectedName);
189 public void keep_2000_first_characters_of_description() {
190 String veryLongString = repeat("a", 3_000);
192 ComponentImpl underTest = buildSimpleComponent(FILE, "file")
193 .setDescription(veryLongString)
196 String expectedDescription = repeat("a", 2_000 - 3) + "...";
197 assertThat(underTest.getDescription()).isEqualTo(expectedDescription);
201 public void build_with_child() {
202 ComponentImpl child = builder(FILE)
203 .setName("CHILD_NAME")
204 .setDbKey("CHILD_KEY")
205 .setUuid("CHILD_UUID")
206 .setStatus(Status.UNAVAILABLE)
207 .setReportAttributes(ReportAttributes.newBuilder(2).build())
209 ComponentImpl componentImpl = builder(Component.Type.DIRECTORY)
213 .setStatus(Status.UNAVAILABLE)
214 .setReportAttributes(ReportAttributes.newBuilder(1).build())
215 .addChildren(Collections.singletonList(child))
218 assertThat(componentImpl.getChildren()).hasSize(1);
219 Component childReloaded = componentImpl.getChildren().iterator().next();
220 assertThat(childReloaded.getDbKey()).isEqualTo("CHILD_KEY");
221 assertThat(childReloaded.getUuid()).isEqualTo("CHILD_UUID");
222 assertThat(childReloaded.getType()).isEqualTo(FILE);
226 public void equals_compares_on_uuid_only() {
227 ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
229 assertThat(builder.build()).isEqualTo(builder.build());
230 assertThat(builder.build()).isEqualTo(buildSimpleComponent(FILE, "2").setUuid(UUID).build());
231 assertThat(builder.build()).isNotEqualTo(buildSimpleComponent(FILE, "1").setUuid("otherUUid").build());
235 public void hashCode_is_hashcode_of_uuid() {
236 ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
238 assertThat(builder.build().hashCode()).isEqualTo(builder.build().hashCode());
239 assertThat(builder.build().hashCode()).isEqualTo(buildSimpleComponent(FILE, "2").setUuid(UUID).build().hashCode());
240 assertThat(builder.build().hashCode()).isEqualTo(UUID.hashCode());
243 private static ComponentImpl.Builder buildSimpleComponent(Component.Type type, String dbKey) {
244 ComponentImpl.Builder builder = builder(type)
245 .setName("name_" + dbKey)
247 .setStatus(Status.UNAVAILABLE)
248 .setUuid("uuid_" + dbKey)
249 .setReportAttributes(ReportAttributes.newBuilder(dbKey.hashCode()).build());
250 if (type == PROJECT) {
251 String buildString = randomAlphabetic(15);
252 builder.setProjectAttributes(new ProjectAttributes("version_1", buildString));