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