]> source.dussan.org Git - sonarqube.git/blob
e1e7d216e73ad48a4e0cafe59d7eca5086c0065d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.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;
29
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;
37
38 public class ComponentImplTest {
39
40   static final String KEY = "KEY";
41   static final String UUID = "UUID";
42
43   @Rule
44   public ExpectedException expectedException = ExpectedException.none();
45
46   @Test
47   public void verify_key_uuid_and_name() {
48     ComponentImpl component = buildSimpleComponent(FILE, KEY).setUuid(UUID).setName("name").build();
49
50     assertThat(component.getDbKey()).isEqualTo(KEY);
51     assertThat(component.getUuid()).isEqualTo(UUID);
52     assertThat(component.getName()).isEqualTo("name");
53   }
54
55   @Test
56   public void builder_throws_NPE_if_component_arg_is_Null() {
57     expectedException.expect(NullPointerException.class);
58
59     builder(null);
60   }
61
62   @Test
63   public void builder_throws_NPE_if_status_arg_is_Null() {
64     expectedException.expect(NullPointerException.class);
65
66     builder(FILE).setStatus(null);
67   }
68
69   @Test
70   public void builder_throws_NPE_if_status_is_Null() {
71     expectedException.expect(NullPointerException.class);
72
73     builder(Component.Type.DIRECTORY)
74       .setName("DIR")
75       .setDbKey(KEY)
76       .setUuid(UUID)
77       .setReportAttributes(ReportAttributes.newBuilder(1).build())
78       .build();
79   }
80
81   @Test
82   public void set_key_throws_NPE_if_component_arg_is_Null() {
83     expectedException.expect(NullPointerException.class);
84
85     builder(FILE).setUuid(null);
86   }
87
88   @Test
89   public void set_uuid_throws_NPE_if_component_arg_is_Null() {
90     expectedException.expect(NullPointerException.class);
91
92     builder(FILE).setDbKey(null);
93   }
94
95   @Test
96   public void build_without_key_throws_NPE_if_component_arg_is_Null() {
97     expectedException.expect(NullPointerException.class);
98
99     builder(FILE).setUuid("ABCD").build();
100   }
101
102   @Test
103   public void build_without_uuid_throws_NPE_if_component_arg_is_Null() {
104     expectedException.expect(NullPointerException.class);
105
106     builder(FILE).setDbKey(KEY).build();
107   }
108
109   @Test
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);
114   }
115
116   @Test
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();
122         try {
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");
127         }
128       });
129   }
130
131   @Test
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();
137         try {
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");
142         }
143       });
144   }
145
146   @Test
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();
152         try {
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");
157         }
158       });
159   }
160
161   @Test
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();
164
165     assertThat(component.getFileAttributes().isUnitTest()).isTrue();
166   }
167
168   @Test
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();
172
173     assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
174   }
175
176   @Test
177   public void keep_500_first_characters_of_name() {
178     String veryLongString = repeat("a", 3_000);
179
180     ComponentImpl underTest = buildSimpleComponent(FILE, "file")
181       .setName(veryLongString)
182       .build();
183
184     String expectedName = repeat("a", 500 - 3) + "...";
185     assertThat(underTest.getName()).isEqualTo(expectedName);
186   }
187
188   @Test
189   public void keep_2000_first_characters_of_description() {
190     String veryLongString = repeat("a", 3_000);
191
192     ComponentImpl underTest = buildSimpleComponent(FILE, "file")
193       .setDescription(veryLongString)
194       .build();
195
196     String expectedDescription = repeat("a", 2_000 - 3) + "...";
197     assertThat(underTest.getDescription()).isEqualTo(expectedDescription);
198   }
199
200   @Test
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())
208       .build();
209     ComponentImpl componentImpl = builder(Component.Type.DIRECTORY)
210       .setName("DIR")
211       .setDbKey(KEY)
212       .setUuid(UUID)
213       .setStatus(Status.UNAVAILABLE)
214       .setReportAttributes(ReportAttributes.newBuilder(1).build())
215       .addChildren(Collections.singletonList(child))
216       .build();
217
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);
223   }
224
225   @Test
226   public void equals_compares_on_uuid_only() {
227     ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
228
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());
232   }
233
234   @Test
235   public void hashCode_is_hashcode_of_uuid() {
236     ComponentImpl.Builder builder = buildSimpleComponent(FILE, "1").setUuid(UUID);
237
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());
241   }
242
243   private static ComponentImpl.Builder buildSimpleComponent(Component.Type type, String dbKey) {
244     ComponentImpl.Builder builder = builder(type)
245       .setName("name_" + dbKey)
246       .setDbKey(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));
253     }
254     return builder;
255   }
256 }