]> source.dussan.org Git - sonarqube.git/blob
ac60f9964a95abdf635a7d2b26968f5727a27bcf
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.ce.task.projectanalysis.issue;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
27 import org.sonar.ce.task.projectanalysis.analysis.Branch;
28 import org.sonar.ce.task.projectanalysis.component.Component;
29 import org.sonar.core.issue.DefaultIssue;
30 import org.sonar.core.issue.tracking.Tracking;
31 import org.sonar.db.component.BranchType;
32
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.verifyZeroInteractions;
36 import static org.mockito.Mockito.when;
37
38 public class IssueTrackingDelegatorTest {
39   @Mock
40   private ShortBranchOrPullRequestTrackerExecution shortBranchTracker;
41   @Mock
42   private MergeBranchTrackerExecution mergeBranchTracker;
43   @Mock
44   private TrackerExecution tracker;
45   @Mock
46   private AnalysisMetadataHolder analysisMetadataHolder;
47   @Mock
48   private Component component;
49   @Mock
50   private Tracking<DefaultIssue, DefaultIssue> trackingResult;
51
52   private IssueTrackingDelegator underTest;
53
54   @Before
55   public void setUp() {
56     MockitoAnnotations.initMocks(this);
57     underTest = new IssueTrackingDelegator(shortBranchTracker, mergeBranchTracker, tracker, analysisMetadataHolder);
58     when(tracker.track(component)).thenReturn(trackingResult);
59     when(mergeBranchTracker.track(component)).thenReturn(trackingResult);
60     when(shortBranchTracker.track(component)).thenReturn(trackingResult);
61   }
62
63   @Test
64   public void delegate_regular_tracker() {
65     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
66     when(analysisMetadataHolder.getBranch()).thenReturn(mock(Branch.class));
67
68     underTest.track(component);
69
70     verify(tracker).track(component);
71     verifyZeroInteractions(shortBranchTracker);
72     verifyZeroInteractions(mergeBranchTracker);
73   }
74
75   @Test
76   public void delegate_merge_tracker() {
77     Branch branch = mock(Branch.class);
78     when(branch.getType()).thenReturn(BranchType.LONG);
79     when(branch.isMain()).thenReturn(false);
80     when(analysisMetadataHolder.getBranch()).thenReturn(branch);
81     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
82
83     underTest.track(component);
84
85     verify(mergeBranchTracker).track(component);
86     verifyZeroInteractions(tracker);
87     verifyZeroInteractions(shortBranchTracker);
88
89   }
90
91   @Test
92   public void delegate_short_branch_tracker() {
93     Branch branch = mock(Branch.class);
94     when(branch.getType()).thenReturn(BranchType.SHORT);
95     when(analysisMetadataHolder.getBranch()).thenReturn(mock(Branch.class));
96     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(true);
97
98     underTest.track(component);
99
100     verify(shortBranchTracker).track(component);
101     verifyZeroInteractions(tracker);
102     verifyZeroInteractions(mergeBranchTracker);
103   }
104
105   @Test
106   public void delegate_pull_request_tracker() {
107     Branch branch = mock(Branch.class);
108     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
109     when(analysisMetadataHolder.getBranch()).thenReturn(mock(Branch.class));
110     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(true);
111
112     underTest.track(component);
113
114     verify(shortBranchTracker).track(component);
115     verifyZeroInteractions(tracker);
116     verifyZeroInteractions(mergeBranchTracker);
117   }
118 }