]> source.dussan.org Git - sonarqube.git/blob
02efc7fc4dad44eef95b7582a4d1d89c6abde469
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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
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;
26
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;
35
36 public class ComponentImplTest {
37
38   static final String KEY = "KEY";
39   static final String UUID = "UUID";
40
41
42   @Test
43   public void verify_key_uuid_and_name() {
44     ComponentImpl component = buildSimpleComponent(FILE, KEY).setUuid(UUID).setName("name").build();
45
46     assertThat(component.getKey()).isEqualTo(KEY);
47     assertThat(component.getUuid()).isEqualTo(UUID);
48     assertThat(component.getName()).isEqualTo("name");
49   }
50
51   @Test
52   public void builder_throws_NPE_if_component_arg_is_Null() {
53     assertThatThrownBy(() -> builder(null))
54       .isInstanceOf(NullPointerException.class);
55   }
56
57   @Test
58   public void builder_throws_NPE_if_status_arg_is_Null() {
59     assertThatThrownBy(() -> builder(FILE).setStatus(null))
60       .isInstanceOf(NullPointerException.class);
61   }
62
63   @Test
64   public void builder_throws_NPE_if_status_is_Null() {
65     assertThatThrownBy(() -> {
66       builder(Component.Type.DIRECTORY)
67         .setName("DIR")
68         .setKey(KEY)
69         .setUuid(UUID)
70         .setReportAttributes(ReportAttributes.newBuilder(1).build())
71         .build();
72     })
73       .isInstanceOf(NullPointerException.class);
74   }
75
76   @Test
77   public void set_key_throws_NPE_if_component_arg_is_Null() {
78     assertThatThrownBy(() -> builder(FILE).setUuid(null))
79       .isInstanceOf(NullPointerException.class);
80   }
81
82   @Test
83   public void set_uuid_throws_NPE_if_component_arg_is_Null() {
84     assertThatThrownBy(() -> builder(FILE).setKey(null))
85       .isInstanceOf(NullPointerException.class);
86   }
87
88   @Test
89   public void build_without_key_throws_NPE_if_component_arg_is_Null() {
90     assertThatThrownBy(() -> builder(FILE).setUuid("ABCD").build())
91       .isInstanceOf(NullPointerException.class);
92   }
93
94   @Test
95   public void build_without_uuid_throws_NPE_if_component_arg_is_Null() {
96     assertThatThrownBy(() -> builder(FILE).setKey(KEY).build())
97       .isInstanceOf(NullPointerException.class);
98   }
99
100   @Test
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);
105   }
106
107   @Test
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();
113         try {
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");
118         }
119       });
120   }
121
122   @Test
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();
128         try {
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");
133         }
134       });
135   }
136
137   @Test
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();
143         try {
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");
148         }
149       });
150   }
151
152   @Test
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();
155
156     assertThat(component.getFileAttributes().isUnitTest()).isTrue();
157   }
158
159   @Test
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();
163
164     assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
165   }
166
167   @Test
168   public void keep_500_first_characters_of_name() {
169     String veryLongString = repeat("a", 3_000);
170
171     ComponentImpl underTest = buildSimpleComponent(FILE, "file")
172       .setName(veryLongString)
173       .build();
174
175     String expectedName = repeat("a", 500 - 3) + "...";
176     assertThat(underTest.getName()).isEqualTo(expectedName);
177   }
178
179   @Test
180   public void keep_2000_first_characters_of_description() {
181     String veryLongString = repeat("a", 3_000);
182
183     ComponentImpl underTest = buildSimpleComponent(FILE, "file")
184       .setDescription(veryLongString)
185       .build();
186
187     String expectedDescription = repeat("a", 2_000 - 3) + "...";
188     assertThat(underTest.getDescription()).isEqualTo(expectedDescription);
189   }
190
191   @Test
192   public void build_with_child() {
193     ComponentImpl child = builder(FILE)
194       .setName("CHILD_NAME")
195       .setKey("CHILD_KEY")
196       .setUuid("CHILD_UUID")
197       .setStatus(Status.UNAVAILABLE)
198       .setReportAttributes(ReportAttributes.newBuilder(2).build())
199       .build();
200     ComponentImpl componentImpl = builder(Component.Type.DIRECTORY)
201       .setName("DIR")
202       .setKey(KEY)
203       .setUuid(UUID)
204       .setStatus(Status.UNAVAILABLE)
205       .setReportAttributes(ReportAttributes.newBuilder(1).build())
206       .addChildren(Collections.singletonList(child))
207       .build();
208
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);
214   }
215
216   @Test
217   public void equals_compares_on_uuid_only() {
218     ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
219
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());
223   }
224
225   @Test
226   public void hashCode_is_hashcode_of_uuid() {
227     ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
228
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());
232   }
233
234   private static ComponentImpl.Builder buildSimpleComponent(Component.Type type, String dbKey) {
235     ComponentImpl.Builder builder = builder(type)
236       .setName("name_" + dbKey)
237       .setKey(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"));
244     }
245     return builder;
246   }
247 }