3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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.component;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.utils.MessageException;
26 import org.sonar.db.component.BranchDto;
27 import org.sonar.scanner.protocol.output.ScannerReport;
28 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.mock;
34 public class BranchLoaderTest {
36 public ExpectedException expectedException = ExpectedException.none();
39 public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule();
42 public void throw_ME_if_both_branch_properties_are_set() {
43 ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
44 .setDeprecatedBranch("foo")
48 expectedException.expect(MessageException.class);
49 expectedException.expectMessage("Properties sonar.branch and sonar.branch.name can't be set together");
51 new BranchLoader(metadataHolder).load(metadata);
55 public void regular_analysis_of_project_is_enabled_if_delegate_is_absent() {
56 ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
59 new BranchLoader(metadataHolder).load(metadata);
61 assertThat(metadataHolder.getBranch()).isNotNull();
63 Branch branch = metadataHolder.getBranch();
64 assertThat(branch.isMain()).isTrue();
65 assertThat(branch.getName()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
69 public void default_support_of_branches_is_enabled_if_delegate_is_absent() {
70 ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
71 .setDeprecatedBranch("foo")
74 new BranchLoader(metadataHolder).load(metadata);
76 assertThat(metadataHolder.getBranch()).isNotNull();
78 Branch branch = metadataHolder.getBranch();
79 assertThat(branch.isMain()).isTrue();
80 assertThat(branch.getName()).isEqualTo("foo");
84 public void default_support_of_branches_is_enabled_if_delegate_is_present() {
85 ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
86 .setDeprecatedBranch("foo")
89 FakeDelegate delegate = new FakeDelegate();
90 new BranchLoader(metadataHolder, delegate).load(metadata);
92 assertThat(metadataHolder.getBranch()).isNotNull();
94 Branch branch = metadataHolder.getBranch();
95 assertThat(branch.isMain()).isTrue();
96 assertThat(branch.getName()).isEqualTo("foo");
99 private class FakeDelegate implements BranchLoaderDelegate {
100 Branch branch = mock(Branch.class);
103 public void load(ScannerReport.Metadata metadata) {
104 metadataHolder.setBranch(branch);