3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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;
26 import static org.assertj.core.api.Assertions.assertThat;
28 public class AnalysisMetadataHolderImplTest {
31 public ExpectedException expectedException = ExpectedException.none();
33 static Analysis baseProjectAnalysis = new Analysis.Builder()
36 .setCreatedAt(123456789L)
39 static long SOME_DATE = 10000000L;
42 public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() throws InterruptedException {
43 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
45 underTest.setAnalysisDate(SOME_DATE);
47 assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
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");
55 new AnalysisMetadataHolderImpl().getAnalysisDate();
59 public void setAnalysisDate_throws_ISE_when_called_twice() {
60 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
61 underTest.setAnalysisDate(SOME_DATE);
63 expectedException.expect(IllegalStateException.class);
64 expectedException.expectMessage("Analysis date has already been set");
66 underTest.setAnalysisDate(SOME_DATE);
70 public void isFirstAnalysis_return_true() throws Exception {
71 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
73 underTest.setBaseAnalysis(null);
74 assertThat(underTest.isFirstAnalysis()).isTrue();
78 public void isFirstAnalysis_return_false() throws Exception {
79 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
81 underTest.setBaseAnalysis(baseProjectAnalysis);
82 assertThat(underTest.isFirstAnalysis()).isFalse();
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");
90 new AnalysisMetadataHolderImpl().isFirstAnalysis();
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");
98 new AnalysisMetadataHolderImpl().getBaseAnalysis();
102 public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
103 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
104 underTest.setBaseAnalysis(baseProjectAnalysis);
106 expectedException.expect(IllegalStateException.class);
107 expectedException.expectMessage("Base project snapshot has already been set");
108 underTest.setBaseAnalysis(baseProjectAnalysis);
112 public void isCrossProjectDuplicationEnabled_return_true() {
113 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
115 underTest.setCrossProjectDuplicationEnabled(true);
117 assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true);
121 public void isCrossProjectDuplicationEnabled_return_false() {
122 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
124 underTest.setCrossProjectDuplicationEnabled(false);
126 assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false);
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");
134 new AnalysisMetadataHolderImpl().isCrossProjectDuplicationEnabled();
138 public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
139 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
140 underTest.setCrossProjectDuplicationEnabled(true);
142 expectedException.expect(IllegalStateException.class);
143 expectedException.expectMessage("Cross project duplication flag has already been set");
144 underTest.setCrossProjectDuplicationEnabled(false);
148 public void set_branch() {
149 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
151 underTest.setBranch("origin/master");
153 assertThat(underTest.getBranch()).isEqualTo("origin/master");
157 public void set_no_branch() {
158 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
160 underTest.setBranch(null);
162 assertThat(underTest.getBranch()).isNull();
166 public void branch_throws_ISE_when_holder_is_not_initialized() {
167 expectedException.expect(IllegalStateException.class);
168 expectedException.expectMessage("Branch has not been set");
170 new AnalysisMetadataHolderImpl().getBranch();
174 public void setBranch_throws_ISE_when_called_twice() {
175 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
176 underTest.setBranch("origin/master");
178 expectedException.expect(IllegalStateException.class);
179 expectedException.expectMessage("Branch has already been set");
180 underTest.setBranch("origin/master");
184 public void getRootComponentRef() throws InterruptedException {
185 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
187 underTest.setRootComponentRef(10);
189 assertThat(underTest.getRootComponentRef()).isEqualTo(10);
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");
197 new AnalysisMetadataHolderImpl().getRootComponentRef();
201 public void setRootComponentRef_throws_ISE_when_called_twice() {
202 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
203 underTest.setRootComponentRef(10);
205 expectedException.expect(IllegalStateException.class);
206 expectedException.expectMessage("Root component ref has already been set");
207 underTest.setRootComponentRef(9);