]> source.dussan.org Git - sonarqube.git/blob
513fa54b3a5dfd7796ea4b3d8a388e3f865d89c4
[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").setDefaultQualityGateUuid("anyuuidr"));
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 set_branch() {
219     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
220
221     underTest.setBranch(new DefaultBranchImpl("master"));
222
223     assertThat(underTest.getBranch().getName()).isEqualTo("master");
224   }
225
226   @Test
227   public void getBranch_throws_ISE_when_holder_is_not_initialized() {
228     expectedException.expect(IllegalStateException.class);
229     expectedException.expectMessage("Branch has not been set");
230
231     new AnalysisMetadataHolderImpl().getBranch();
232   }
233
234   @Test
235   public void setBranch_throws_ISE_when_called_twice() {
236     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
237     underTest.setBranch(new DefaultBranchImpl("master"));
238
239     expectedException.expect(IllegalStateException.class);
240     expectedException.expectMessage("Branch has already been set");
241     underTest.setBranch(new DefaultBranchImpl("master"));
242   }
243
244   @Test
245   public void set_and_get_project() {
246     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
247
248     Project project = new Project("U", "K", "N");
249     underTest.setProject(project);
250
251     assertThat(underTest.getProject()).isSameAs(project);
252   }
253
254   @Test
255   public void getProject_throws_ISE_when_holder_is_not_initialized() {
256     expectedException.expect(IllegalStateException.class);
257     expectedException.expectMessage("Project has not been set");
258
259     new AnalysisMetadataHolderImpl().getProject();
260   }
261
262   @Test
263   public void setProject_throws_ISE_when_called_twice() {
264     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
265     underTest.setProject(new Project("U", "K", "N"));
266
267     expectedException.expect(IllegalStateException.class);
268     expectedException.expectMessage("Project has already been set");
269     underTest.setProject(new Project("U", "K", "N"));
270   }
271
272   @Test
273   public void getRootComponentRef() throws InterruptedException {
274     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
275
276     underTest.setRootComponentRef(10);
277
278     assertThat(underTest.getRootComponentRef()).isEqualTo(10);
279   }
280
281   @Test
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");
285
286     new AnalysisMetadataHolderImpl().getRootComponentRef();
287   }
288
289   @Test
290   public void setRootComponentRef_throws_ISE_when_called_twice() {
291     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
292     underTest.setRootComponentRef(10);
293
294     expectedException.expect(IllegalStateException.class);
295     expectedException.expectMessage("Root component ref has already been set");
296     underTest.setRootComponentRef(9);
297   }
298
299   @Test
300   public void getIsShortLivingBranch_throws_ISE_when_holder_is_not_initialized() {
301     expectedException.expect(IllegalStateException.class);
302     expectedException.expectMessage("Branch has not been set");
303
304     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
305     underTest.isShortLivingBranch();
306   }
307
308   @Test
309   public void getIsShortLivingBranch_returns_true() {
310     Branch branch = mock(Branch.class);
311     when(branch.getType()).thenReturn(BranchType.SHORT);
312
313     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
314     underTest.setBranch(branch);
315
316     assertThat(underTest.isShortLivingBranch()).isTrue();
317   }
318 }