3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.analysis;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.db.component.BranchType;
26 import org.sonar.db.organization.OrganizationDto;
27 import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 public class AnalysisMetadataHolderImplTest {
35 private static Analysis baseProjectAnalysis = new Analysis.Builder()
38 .setCreatedAt(123456789L)
40 private static long SOME_DATE = 10000000L;
43 public ExpectedException expectedException = ExpectedException.none();
45 private AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
48 public void getOrganization_throws_ISE_if_organization_is_not_set() {
49 expectedException.expect(IllegalStateException.class);
50 expectedException.expectMessage("Organization has not been set");
52 underTest.getOrganization();
56 public void setOrganization_throws_NPE_is_parameter_is_null() {
57 expectedException.expect(NullPointerException.class);
58 expectedException.expectMessage("Organization can't be null");
60 underTest.setOrganization(null);
64 public void setOrganization_throws_ISE_if_called_twice() {
65 Organization organization = Organization.from(new OrganizationDto().setUuid("uuid").setKey("key").setName("name"));
66 underTest.setOrganization(organization);
68 expectedException.expect(IllegalStateException.class);
69 expectedException.expectMessage("Organization has already been set");
71 underTest.setOrganization(organization);
75 public void getUuid_throws_ISE_if_organization_uuid_is_not_set() {
76 expectedException.expect(IllegalStateException.class);
77 expectedException.expectMessage("Analysis uuid has not been set");
83 public void setUuid_throws_NPE_is_parameter_is_null() {
84 expectedException.expect(NullPointerException.class);
85 expectedException.expectMessage("Analysis uuid can't be null");
87 underTest.setUuid(null);
91 public void setUuid_throws_ISE_if_called_twice() {
92 underTest.setUuid("org1");
94 expectedException.expect(IllegalStateException.class);
95 expectedException.expectMessage("Analysis uuid has already been set");
97 underTest.setUuid("org1");
101 public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() throws InterruptedException {
103 underTest.setAnalysisDate(SOME_DATE);
105 assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
109 public void getAnalysisDate_throws_ISE_when_holder_is_not_initialized() {
110 expectedException.expect(IllegalStateException.class);
111 expectedException.expectMessage("Analysis date has not been set");
113 new AnalysisMetadataHolderImpl().getAnalysisDate();
117 public void setAnalysisDate_throws_ISE_when_called_twice() {
118 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
119 underTest.setAnalysisDate(SOME_DATE);
121 expectedException.expect(IllegalStateException.class);
122 expectedException.expectMessage("Analysis date has already been set");
124 underTest.setAnalysisDate(SOME_DATE);
128 public void hasAnalysisDateBeenSet_returns_false_when_holder_is_not_initialized() {
129 assertThat(new AnalysisMetadataHolderImpl().hasAnalysisDateBeenSet()).isFalse();
133 public void hasAnalysisDateBeenSet_returns_true_when_holder_date_is_set() {
134 AnalysisMetadataHolderImpl holder = new AnalysisMetadataHolderImpl();
135 holder.setAnalysisDate(46532);
136 assertThat(holder.hasAnalysisDateBeenSet()).isTrue();
140 public void isFirstAnalysis_return_true() throws Exception {
141 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
143 underTest.setBaseAnalysis(null);
144 assertThat(underTest.isFirstAnalysis()).isTrue();
148 public void isFirstAnalysis_return_false() throws Exception {
149 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
151 underTest.setBaseAnalysis(baseProjectAnalysis);
152 assertThat(underTest.isFirstAnalysis()).isFalse();
156 public void isFirstAnalysis_throws_ISE_when_base_project_snapshot_is_not_set() {
157 expectedException.expect(IllegalStateException.class);
158 expectedException.expectMessage("Base project snapshot has not been set");
160 new AnalysisMetadataHolderImpl().isFirstAnalysis();
164 public void baseProjectSnapshot_throws_ISE_when_base_project_snapshot_is_not_set() {
165 expectedException.expect(IllegalStateException.class);
166 expectedException.expectMessage("Base project snapshot has not been set");
168 new AnalysisMetadataHolderImpl().getBaseAnalysis();
172 public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
173 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
174 underTest.setBaseAnalysis(baseProjectAnalysis);
176 expectedException.expect(IllegalStateException.class);
177 expectedException.expectMessage("Base project snapshot has already been set");
178 underTest.setBaseAnalysis(baseProjectAnalysis);
182 public void isCrossProjectDuplicationEnabled_return_true() {
183 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
185 underTest.setCrossProjectDuplicationEnabled(true);
187 assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true);
191 public void isCrossProjectDuplicationEnabled_return_false() {
192 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
194 underTest.setCrossProjectDuplicationEnabled(false);
196 assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false);
200 public void isCrossProjectDuplicationEnabled_throws_ISE_when_holder_is_not_initialized() {
201 expectedException.expect(IllegalStateException.class);
202 expectedException.expectMessage("Cross project duplication flag has not been set");
204 new AnalysisMetadataHolderImpl().isCrossProjectDuplicationEnabled();
208 public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
209 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
210 underTest.setCrossProjectDuplicationEnabled(true);
212 expectedException.expect(IllegalStateException.class);
213 expectedException.expectMessage("Cross project duplication flag has already been set");
214 underTest.setCrossProjectDuplicationEnabled(false);
218 public void setIsIncrementalAnalysis_throws_ISE_when_called_twice() {
219 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
220 underTest.setIncrementalAnalysis(true);
222 expectedException.expect(IllegalStateException.class);
223 expectedException.expectMessage("Incremental analysis flag has already been set");
224 underTest.setIncrementalAnalysis(false);
228 public void isIncrementalAnalysis_return_true() {
229 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
231 underTest.setIncrementalAnalysis(true);
233 assertThat(underTest.isIncrementalAnalysis()).isEqualTo(true);
237 public void isIncrementalAnalysis_return_false() {
238 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
240 underTest.setIncrementalAnalysis(false);
242 assertThat(underTest.isIncrementalAnalysis()).isEqualTo(false);
246 public void isIncrementalAnalysisEnabled_throws_ISE_when_holder_is_not_initialized() {
247 expectedException.expect(IllegalStateException.class);
248 expectedException.expectMessage("Incremental analysis flag has not been set");
250 new AnalysisMetadataHolderImpl().isIncrementalAnalysis();
254 public void setIsIncrementalAnalys_throws_ISE_when_called_twice() {
255 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
256 underTest.setIncrementalAnalysis(true);
258 expectedException.expect(IllegalStateException.class);
259 expectedException.expectMessage("Incremental analysis flag has already been set");
260 underTest.setIncrementalAnalysis(false);
264 public void set_branch() {
265 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
267 underTest.setBranch(new DefaultBranchImpl("master"));
269 assertThat(underTest.getBranch().get().getName()).hasValue("master");
273 public void getBranch_throws_ISE_when_holder_is_not_initialized() {
274 expectedException.expect(IllegalStateException.class);
275 expectedException.expectMessage("Branch has not been set");
277 new AnalysisMetadataHolderImpl().getBranch();
281 public void setBranch_throws_ISE_when_called_twice() {
282 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
283 underTest.setBranch(new DefaultBranchImpl("master"));
285 expectedException.expect(IllegalStateException.class);
286 expectedException.expectMessage("Branch has already been set");
287 underTest.setBranch(new DefaultBranchImpl("master"));
291 public void set_and_get_project() {
292 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
294 Project project = new Project("U", "K", "N");
295 underTest.setProject(project);
297 assertThat(underTest.getProject()).isSameAs(project);
301 public void getProject_throws_ISE_when_holder_is_not_initialized() {
302 expectedException.expect(IllegalStateException.class);
303 expectedException.expectMessage("Project has not been set");
305 new AnalysisMetadataHolderImpl().getProject();
309 public void setProject_throws_ISE_when_called_twice() {
310 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
311 underTest.setProject(new Project("U", "K", "N"));
313 expectedException.expect(IllegalStateException.class);
314 expectedException.expectMessage("Project has already been set");
315 underTest.setProject(new Project("U", "K", "N"));
319 public void getRootComponentRef() throws InterruptedException {
320 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
322 underTest.setRootComponentRef(10);
324 assertThat(underTest.getRootComponentRef()).isEqualTo(10);
328 public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() {
329 expectedException.expect(IllegalStateException.class);
330 expectedException.expectMessage("Root component ref has not been set");
332 new AnalysisMetadataHolderImpl().getRootComponentRef();
336 public void setRootComponentRef_throws_ISE_when_called_twice() {
337 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
338 underTest.setRootComponentRef(10);
340 expectedException.expect(IllegalStateException.class);
341 expectedException.expectMessage("Root component ref has already been set");
342 underTest.setRootComponentRef(9);
346 public void getIsShortLivingBranch_throws_ISE_when_holder_is_not_initialized() {
347 expectedException.expect(IllegalStateException.class);
348 expectedException.expectMessage("Branch has not been set");
350 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
351 underTest.isShortLivingBranch();
355 public void getIsShortLivingBranch_returns_true() {
356 Branch branch = mock(Branch.class);
357 when(branch.getType()).thenReturn(BranchType.SHORT);
359 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
360 underTest.setBranch(branch);
362 assertThat(underTest.isShortLivingBranch()).isTrue();