]> source.dussan.org Git - sonarqube.git/blob
b3f2b9a023605fbbcceeadb018e18a8d203c372a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.analysis;
21
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;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 public class AnalysisMetadataHolderImplTest {
34
35   private static Analysis baseProjectAnalysis = new Analysis.Builder()
36     .setId(1)
37     .setUuid("uuid_1")
38     .setCreatedAt(123456789L)
39     .build();
40   private static long SOME_DATE = 10000000L;
41
42   @Rule
43   public ExpectedException expectedException = ExpectedException.none();
44
45   private AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
46
47   @Test
48   public void getOrganization_throws_ISE_if_organization_is_not_set() {
49     expectedException.expect(IllegalStateException.class);
50     expectedException.expectMessage("Organization has not been set");
51
52     underTest.getOrganization();
53   }
54
55   @Test
56   public void setOrganization_throws_NPE_is_parameter_is_null() {
57     expectedException.expect(NullPointerException.class);
58     expectedException.expectMessage("Organization can't be null");
59
60     underTest.setOrganization(null);
61   }
62
63   @Test
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);
67
68     expectedException.expect(IllegalStateException.class);
69     expectedException.expectMessage("Organization has already been set");
70
71     underTest.setOrganization(organization);
72   }
73
74   @Test
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");
78
79     underTest.getUuid();
80   }
81
82   @Test
83   public void setUuid_throws_NPE_is_parameter_is_null() {
84     expectedException.expect(NullPointerException.class);
85     expectedException.expectMessage("Analysis uuid can't be null");
86
87     underTest.setUuid(null);
88   }
89
90   @Test
91   public void setUuid_throws_ISE_if_called_twice() {
92     underTest.setUuid("org1");
93
94     expectedException.expect(IllegalStateException.class);
95     expectedException.expectMessage("Analysis uuid has already been set");
96
97     underTest.setUuid("org1");
98   }
99
100   @Test
101   public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() throws InterruptedException {
102
103     underTest.setAnalysisDate(SOME_DATE);
104
105     assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
106   }
107
108   @Test
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");
112
113     new AnalysisMetadataHolderImpl().getAnalysisDate();
114   }
115
116   @Test
117   public void setAnalysisDate_throws_ISE_when_called_twice() {
118     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
119     underTest.setAnalysisDate(SOME_DATE);
120
121     expectedException.expect(IllegalStateException.class);
122     expectedException.expectMessage("Analysis date has already been set");
123
124     underTest.setAnalysisDate(SOME_DATE);
125   }
126
127   @Test
128   public void hasAnalysisDateBeenSet_returns_false_when_holder_is_not_initialized() {
129     assertThat(new AnalysisMetadataHolderImpl().hasAnalysisDateBeenSet()).isFalse();
130   }
131
132   @Test
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();
137   }
138
139   @Test
140   public void isFirstAnalysis_return_true() throws Exception {
141     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
142
143     underTest.setBaseAnalysis(null);
144     assertThat(underTest.isFirstAnalysis()).isTrue();
145   }
146
147   @Test
148   public void isFirstAnalysis_return_false() throws Exception {
149     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
150
151     underTest.setBaseAnalysis(baseProjectAnalysis);
152     assertThat(underTest.isFirstAnalysis()).isFalse();
153   }
154
155   @Test
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");
159
160     new AnalysisMetadataHolderImpl().isFirstAnalysis();
161   }
162
163   @Test
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");
167
168     new AnalysisMetadataHolderImpl().getBaseAnalysis();
169   }
170
171   @Test
172   public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
173     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
174     underTest.setBaseAnalysis(baseProjectAnalysis);
175
176     expectedException.expect(IllegalStateException.class);
177     expectedException.expectMessage("Base project snapshot has already been set");
178     underTest.setBaseAnalysis(baseProjectAnalysis);
179   }
180
181   @Test
182   public void isCrossProjectDuplicationEnabled_return_true() {
183     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
184
185     underTest.setCrossProjectDuplicationEnabled(true);
186
187     assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true);
188   }
189
190   @Test
191   public void isCrossProjectDuplicationEnabled_return_false() {
192     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
193
194     underTest.setCrossProjectDuplicationEnabled(false);
195
196     assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false);
197   }
198
199   @Test
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");
203
204     new AnalysisMetadataHolderImpl().isCrossProjectDuplicationEnabled();
205   }
206
207   @Test
208   public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
209     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
210     underTest.setCrossProjectDuplicationEnabled(true);
211
212     expectedException.expect(IllegalStateException.class);
213     expectedException.expectMessage("Cross project duplication flag has already been set");
214     underTest.setCrossProjectDuplicationEnabled(false);
215   }
216
217   @Test
218   public void setIsIncrementalAnalysis_throws_ISE_when_called_twice() {
219     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
220     underTest.setIncrementalAnalysis(true);
221
222     expectedException.expect(IllegalStateException.class);
223     expectedException.expectMessage("Incremental analysis flag has already been set");
224     underTest.setIncrementalAnalysis(false);
225   }
226
227   @Test
228   public void isIncrementalAnalysis_return_true() {
229     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
230
231     underTest.setIncrementalAnalysis(true);
232
233     assertThat(underTest.isIncrementalAnalysis()).isEqualTo(true);
234   }
235
236   @Test
237   public void isIncrementalAnalysis_return_false() {
238     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
239
240     underTest.setIncrementalAnalysis(false);
241
242     assertThat(underTest.isIncrementalAnalysis()).isEqualTo(false);
243   }
244
245   @Test
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");
249
250     new AnalysisMetadataHolderImpl().isIncrementalAnalysis();
251   }
252
253   @Test
254   public void setIsIncrementalAnalys_throws_ISE_when_called_twice() {
255     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
256     underTest.setIncrementalAnalysis(true);
257
258     expectedException.expect(IllegalStateException.class);
259     expectedException.expectMessage("Incremental analysis flag has already been set");
260     underTest.setIncrementalAnalysis(false);
261   }
262
263   @Test
264   public void set_branch() {
265     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
266
267     underTest.setBranch(new DefaultBranchImpl("master"));
268
269     assertThat(underTest.getBranch().get().getName()).hasValue("master");
270   }
271
272   @Test
273   public void getBranch_throws_ISE_when_holder_is_not_initialized() {
274     expectedException.expect(IllegalStateException.class);
275     expectedException.expectMessage("Branch has not been set");
276
277     new AnalysisMetadataHolderImpl().getBranch();
278   }
279
280   @Test
281   public void setBranch_throws_ISE_when_called_twice() {
282     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
283     underTest.setBranch(new DefaultBranchImpl("master"));
284
285     expectedException.expect(IllegalStateException.class);
286     expectedException.expectMessage("Branch has already been set");
287     underTest.setBranch(new DefaultBranchImpl("master"));
288   }
289
290   @Test
291   public void set_and_get_project() {
292     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
293
294     Project project = new Project("U", "K", "N");
295     underTest.setProject(project);
296
297     assertThat(underTest.getProject()).isSameAs(project);
298   }
299
300   @Test
301   public void getProject_throws_ISE_when_holder_is_not_initialized() {
302     expectedException.expect(IllegalStateException.class);
303     expectedException.expectMessage("Project has not been set");
304
305     new AnalysisMetadataHolderImpl().getProject();
306   }
307
308   @Test
309   public void setProject_throws_ISE_when_called_twice() {
310     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
311     underTest.setProject(new Project("U", "K", "N"));
312
313     expectedException.expect(IllegalStateException.class);
314     expectedException.expectMessage("Project has already been set");
315     underTest.setProject(new Project("U", "K", "N"));
316   }
317
318   @Test
319   public void getRootComponentRef() throws InterruptedException {
320     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
321
322     underTest.setRootComponentRef(10);
323
324     assertThat(underTest.getRootComponentRef()).isEqualTo(10);
325   }
326
327   @Test
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");
331
332     new AnalysisMetadataHolderImpl().getRootComponentRef();
333   }
334
335   @Test
336   public void setRootComponentRef_throws_ISE_when_called_twice() {
337     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
338     underTest.setRootComponentRef(10);
339
340     expectedException.expect(IllegalStateException.class);
341     expectedException.expectMessage("Root component ref has already been set");
342     underTest.setRootComponentRef(9);
343   }
344
345   @Test
346   public void getIsShortLivingBranch_throws_ISE_when_holder_is_not_initialized() {
347     expectedException.expect(IllegalStateException.class);
348     expectedException.expectMessage("Branch has not been set");
349
350     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
351     underTest.isShortLivingBranch();
352   }
353
354   @Test
355   public void getIsShortLivingBranch_returns_true() {
356     Branch branch = mock(Branch.class);
357     when(branch.getType()).thenReturn(BranchType.SHORT);
358
359     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
360     underTest.setBranch(branch);
361
362     assertThat(underTest.isShortLivingBranch()).isTrue();
363   }
364 }