]> source.dussan.org Git - sonarqube.git/blob
31d72a3f1f84bc16d73690d0f71ff3e48c95d8b3
[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.component;
21
22 import javax.annotation.Nullable;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.utils.MessageException;
27 import org.sonar.db.component.BranchDto;
28 import org.sonar.db.component.BranchType;
29 import org.sonar.scanner.protocol.output.ScannerReport;
30 import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33
34 public class DefaultBranchImplTest {
35
36   private static final ScannerReport.Component PROJECT = ScannerReport.Component.newBuilder().setType(ComponentType.PROJECT).setKey("P").build();
37   private static final ScannerReport.Component MODULE = ScannerReport.Component.newBuilder().setType(ComponentType.MODULE).setKey("M").build();
38   private static final ScannerReport.Component FILE = ScannerReport.Component.newBuilder().setType(ComponentType.FILE).setPath("src/Foo.js").build();
39
40   @Rule
41   public ExpectedException expectedException = ExpectedException.none();
42
43   @Test
44   public void throw_ME_if_name_contains_invalid_characters() {
45     assertThatNameIsCorrect("master");
46     assertThatNameIsCorrect("feature/foo");
47     assertThatNameIsCorrect("feature_foo");
48
49     assertThatNameIsNotCorrect("feature foo");
50     assertThatNameIsNotCorrect("feature#foo");
51   }
52
53   @Test
54   public void default_branch_represents_the_project() {
55     DefaultBranchImpl branch = new DefaultBranchImpl();
56
57     assertThat(branch.isMain()).isTrue();
58     assertThat(branch.getType()).isEqualTo(BranchType.LONG);
59     assertThat(branch.getName().get()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
60     assertThat(branch.supportsCrossProjectCpd()).isTrue();
61
62     assertThat(branch.generateKey(PROJECT, null)).isEqualTo("P");
63     assertThat(branch.generateKey(MODULE, null)).isEqualTo("M");
64     assertThat(branch.generateKey(MODULE, FILE)).isEqualTo("M:src/Foo.js");
65   }
66
67   @Test
68   public void branch_represents_a_forked_project_with_different_key() {
69     DefaultBranchImpl branch = new DefaultBranchImpl("bar");
70
71     // not a real branch. Parameter sonar.branch forks project.
72     assertThat(branch.isMain()).isTrue();
73     assertThat(branch.getType()).isEqualTo(BranchType.LONG);
74     assertThat(branch.getName()).hasValue("bar");
75     assertThat(branch.supportsCrossProjectCpd()).isFalse();
76
77     assertThat(branch.generateKey(PROJECT, null)).isEqualTo("P:bar");
78     assertThat(branch.generateKey(MODULE, null)).isEqualTo("M:bar");
79     assertThat(branch.generateKey(MODULE, FILE)).isEqualTo("M:bar:src/Foo.js");
80   }
81
82   private void assertThatNameIsCorrect(@Nullable String name) {
83     DefaultBranchImpl branch = new DefaultBranchImpl(name);
84     assertThat(branch.getName()).hasValue(name);
85   }
86
87   private void assertThatNameIsNotCorrect(String name) {
88     expectedException.expect(MessageException.class);
89     expectedException.expectMessage("\"" + name + "\" is not a valid branch name. Allowed characters are alphanumeric, '-', '_', '.' and '/'.");
90
91     new DefaultBranchImpl(name);
92   }
93 }