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 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;
32 import static org.assertj.core.api.Assertions.assertThat;
34 public class DefaultBranchImplTest {
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();
41 public ExpectedException expectedException = ExpectedException.none();
44 public void throw_ME_if_name_contains_invalid_characters() {
45 assertThatNameIsCorrect("master");
46 assertThatNameIsCorrect("feature/foo");
47 assertThatNameIsCorrect("feature_foo");
49 assertThatNameIsNotCorrect("feature foo");
50 assertThatNameIsNotCorrect("feature#foo");
54 public void default_branch_represents_the_project() {
55 DefaultBranchImpl branch = new DefaultBranchImpl();
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();
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");
68 public void branch_represents_a_forked_project_with_different_key() {
69 DefaultBranchImpl branch = new DefaultBranchImpl("bar");
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();
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");
82 private void assertThatNameIsCorrect(@Nullable String name) {
83 DefaultBranchImpl branch = new DefaultBranchImpl(name);
84 assertThat(branch.getName()).hasValue(name);
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 '/'.");
91 new DefaultBranchImpl(name);