]> source.dussan.org Git - sonarqube.git/blob
a4a4e363d7d73873fe33fe1b82bde39af8b3ab87
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 public class AnalysisMetadataHolderImplTest {
29
30   @Rule
31   public ExpectedException expectedException = ExpectedException.none();
32
33   static Analysis baseProjectAnalysis = new Analysis.Builder()
34     .setId(1)
35     .setUuid("uuid_1")
36     .setCreatedAt(123456789L)
37     .build();
38
39   static long SOME_DATE = 10000000L;
40
41   @Test
42   public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() throws InterruptedException {
43     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
44
45     underTest.setAnalysisDate(SOME_DATE);
46
47     assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
48   }
49
50   @Test
51   public void getAnalysisDate_throws_ISE_when_holder_is_not_initialized() {
52     expectedException.expect(IllegalStateException.class);
53     expectedException.expectMessage("Analysis date has not been set");
54
55     new AnalysisMetadataHolderImpl().getAnalysisDate();
56   }
57
58   @Test
59   public void setAnalysisDate_throws_ISE_when_called_twice() {
60     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
61     underTest.setAnalysisDate(SOME_DATE);
62
63     expectedException.expect(IllegalStateException.class);
64     expectedException.expectMessage("Analysis date has already been set");
65
66     underTest.setAnalysisDate(SOME_DATE);
67   }
68
69   @Test
70   public void isFirstAnalysis_return_true() throws Exception {
71     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
72
73     underTest.setBaseAnalysis(null);
74     assertThat(underTest.isFirstAnalysis()).isTrue();
75   }
76
77   @Test
78   public void isFirstAnalysis_return_false() throws Exception {
79     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
80
81     underTest.setBaseAnalysis(baseProjectAnalysis);
82     assertThat(underTest.isFirstAnalysis()).isFalse();
83   }
84
85   @Test
86   public void isFirstAnalysis_throws_ISE_when_base_project_snapshot_is_not_set() {
87     expectedException.expect(IllegalStateException.class);
88     expectedException.expectMessage("Base project snapshot has not been set");
89
90     new AnalysisMetadataHolderImpl().isFirstAnalysis();
91   }
92
93   @Test
94   public void baseProjectSnapshot_throws_ISE_when_base_project_snapshot_is_not_set() {
95     expectedException.expect(IllegalStateException.class);
96     expectedException.expectMessage("Base project snapshot has not been set");
97
98     new AnalysisMetadataHolderImpl().getBaseAnalysis();
99   }
100
101   @Test
102   public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
103     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
104     underTest.setBaseAnalysis(baseProjectAnalysis);
105
106     expectedException.expect(IllegalStateException.class);
107     expectedException.expectMessage("Base project snapshot has already been set");
108     underTest.setBaseAnalysis(baseProjectAnalysis);
109   }
110
111   @Test
112   public void isCrossProjectDuplicationEnabled_return_true() {
113     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
114
115     underTest.setCrossProjectDuplicationEnabled(true);
116
117     assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true);
118   }
119
120   @Test
121   public void isCrossProjectDuplicationEnabled_return_false() {
122     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
123
124     underTest.setCrossProjectDuplicationEnabled(false);
125
126     assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false);
127   }
128
129   @Test
130   public void isCrossProjectDuplicationEnabled_throws_ISE_when_holder_is_not_initialized() {
131     expectedException.expect(IllegalStateException.class);
132     expectedException.expectMessage("Cross project duplication flag has not been set");
133
134     new AnalysisMetadataHolderImpl().isCrossProjectDuplicationEnabled();
135   }
136
137   @Test
138   public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
139     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
140     underTest.setCrossProjectDuplicationEnabled(true);
141
142     expectedException.expect(IllegalStateException.class);
143     expectedException.expectMessage("Cross project duplication flag has already been set");
144     underTest.setCrossProjectDuplicationEnabled(false);
145   }
146
147   @Test
148   public void set_branch() {
149     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
150
151     underTest.setBranch("origin/master");
152
153     assertThat(underTest.getBranch()).isEqualTo("origin/master");
154   }
155
156   @Test
157   public void set_no_branch() {
158     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
159
160     underTest.setBranch(null);
161
162     assertThat(underTest.getBranch()).isNull();
163   }
164
165   @Test
166   public void branch_throws_ISE_when_holder_is_not_initialized() {
167     expectedException.expect(IllegalStateException.class);
168     expectedException.expectMessage("Branch has not been set");
169
170     new AnalysisMetadataHolderImpl().getBranch();
171   }
172
173   @Test
174   public void setBranch_throws_ISE_when_called_twice() {
175     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
176     underTest.setBranch("origin/master");
177
178     expectedException.expect(IllegalStateException.class);
179     expectedException.expectMessage("Branch has already been set");
180     underTest.setBranch("origin/master");
181   }
182
183   @Test
184   public void getRootComponentRef() throws InterruptedException {
185     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
186
187     underTest.setRootComponentRef(10);
188
189     assertThat(underTest.getRootComponentRef()).isEqualTo(10);
190   }
191
192   @Test
193   public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() {
194     expectedException.expect(IllegalStateException.class);
195     expectedException.expectMessage("Root component ref has not been set");
196
197     new AnalysisMetadataHolderImpl().getRootComponentRef();
198   }
199
200   @Test
201   public void setRootComponentRef_throws_ISE_when_called_twice() {
202     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
203     underTest.setRootComponentRef(10);
204
205     expectedException.expect(IllegalStateException.class);
206     expectedException.expectMessage("Root component ref has already been set");
207     underTest.setRootComponentRef(9);
208   }
209 }