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 set_branch() {
219 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
221 underTest.setBranch(new DefaultBranchImpl("master"));
223 assertThat(underTest.getBranch().get().getName()).isEqualTo("master");
227 public void getBranch_throws_ISE_when_holder_is_not_initialized() {
228 expectedException.expect(IllegalStateException.class);
229 expectedException.expectMessage("Branch has not been set");
231 new AnalysisMetadataHolderImpl().getBranch();
235 public void setBranch_throws_ISE_when_called_twice() {
236 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
237 underTest.setBranch(new DefaultBranchImpl("master"));
239 expectedException.expect(IllegalStateException.class);
240 expectedException.expectMessage("Branch has already been set");
241 underTest.setBranch(new DefaultBranchImpl("master"));
245 public void set_and_get_project() {
246 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
248 Project project = new Project("U", "K", "N");
249 underTest.setProject(project);
251 assertThat(underTest.getProject()).isSameAs(project);
255 public void getProject_throws_ISE_when_holder_is_not_initialized() {
256 expectedException.expect(IllegalStateException.class);
257 expectedException.expectMessage("Project has not been set");
259 new AnalysisMetadataHolderImpl().getProject();
263 public void setProject_throws_ISE_when_called_twice() {
264 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
265 underTest.setProject(new Project("U", "K", "N"));
267 expectedException.expect(IllegalStateException.class);
268 expectedException.expectMessage("Project has already been set");
269 underTest.setProject(new Project("U", "K", "N"));
273 public void getRootComponentRef() throws InterruptedException {
274 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
276 underTest.setRootComponentRef(10);
278 assertThat(underTest.getRootComponentRef()).isEqualTo(10);
282 public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() {
283 expectedException.expect(IllegalStateException.class);
284 expectedException.expectMessage("Root component ref has not been set");
286 new AnalysisMetadataHolderImpl().getRootComponentRef();
290 public void setRootComponentRef_throws_ISE_when_called_twice() {
291 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
292 underTest.setRootComponentRef(10);
294 expectedException.expect(IllegalStateException.class);
295 expectedException.expectMessage("Root component ref has already been set");
296 underTest.setRootComponentRef(9);
300 public void getIsShortLivingBranch_throws_ISE_when_holder_is_not_initialized() {
301 expectedException.expect(IllegalStateException.class);
302 expectedException.expectMessage("Branch has not been set");
304 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
305 underTest.isShortLivingBranch();
309 public void getIsShortLivingBranch_returns_true() {
310 Branch branch = mock(Branch.class);
311 when(branch.getType()).thenReturn(BranchType.SHORT);
313 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
314 underTest.setBranch(branch);
316 assertThat(underTest.isShortLivingBranch()).isTrue();