aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2017-08-11 10:58:25 +0200
committerJanos Gyerik <janos.gyerik@sonarsource.com>2017-09-12 10:59:55 +0200
commitf87bdd9d007c372556a34e76dca2ab3da4b4b620 (patch)
tree092cc030a1a11ecf1cacfc78ad974e8871bdf564
parentd6e6f92aefc44df2dd0af10ff3abda6aabb10b39 (diff)
downloadsonarqube-f87bdd9d007c372556a34e76dca2ab3da4b4b620.tar.gz
sonarqube-f87bdd9d007c372556a34e76dca2ab3da4b4b620.zip
Improve tests and quality
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfiguration.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationProvider.java8
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchConfiguration.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java3
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/BranchConfigurationProviderTest.java64
5 files changed, 74 insertions, 5 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfiguration.java
index b830a771748..786744fcf5d 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfiguration.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfiguration.java
@@ -20,7 +20,9 @@
package org.sonar.scanner.scan;
import javax.annotation.CheckForNull;
+import javax.annotation.concurrent.Immutable;
+@Immutable
public interface BranchConfiguration {
enum BranchType {
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationProvider.java
index c3cbd0f5c0d..46e182672a7 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationProvider.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationProvider.java
@@ -36,14 +36,14 @@ public class BranchConfigurationProvider extends ProviderAdapter {
public BranchConfiguration provide(@Nullable BranchConfigurationLoader loader, ProjectKey projectKey, GlobalConfiguration globalConfiguration) {
if (branchConfiguration == null) {
+ Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
if (loader == null) {
- return new DefaultBranchConfiguration();
+ branchConfiguration = new DefaultBranchConfiguration();
+ } else {
+ branchConfiguration = loader.load(projectKey.get(), globalConfiguration);
}
- Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- branchConfiguration = loader.load(projectKey.get(), globalConfiguration);
profiler.stopInfo();
}
return branchConfiguration;
}
-
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchConfiguration.java
index 83fdd3133e6..2bcd480d003 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchConfiguration.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchConfiguration.java
@@ -20,7 +20,9 @@
package org.sonar.scanner.scan;
import javax.annotation.CheckForNull;
+import javax.annotation.concurrent.Immutable;
+@Immutable
public class DefaultBranchConfiguration implements BranchConfiguration {
@Override
public BranchType branchType() {
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java
index 0aa2f3ed9ec..244dc9c445a 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java
@@ -239,7 +239,8 @@ public class ProjectScanContainer extends ComponentContainer {
String branchName = props.property(ScannerProperties.BRANCH_NAME);
if (branchName != null) {
- LOG.info("Branch name: {}", branchName);
+ BranchConfiguration branchConfig = getComponentByType(BranchConfiguration.class);
+ LOG.info("Branch name: {}, type: {}", branchName, branchConfig.branchType().toString().toLowerCase());
}
LOG.debug("Start recursive analysis of project modules");
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/BranchConfigurationProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/BranchConfigurationProviderTest.java
new file mode 100644
index 00000000000..41c3f9a17be
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/BranchConfigurationProviderTest.java
@@ -0,0 +1,64 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.scan;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.scanner.bootstrap.GlobalConfiguration;
+import org.sonar.scanner.scan.BranchConfiguration.BranchType;
+
+public class BranchConfigurationProviderTest {
+ private BranchConfigurationProvider provider = new BranchConfigurationProvider();
+ private GlobalConfiguration globalConfiguration;
+ private BranchConfigurationLoader loader;
+ private BranchConfiguration config;
+
+ @Before
+ public void setUp() {
+ globalConfiguration = mock(GlobalConfiguration.class);
+ loader = mock(BranchConfigurationLoader.class);
+ config = mock(BranchConfiguration.class);
+ }
+
+ @Test
+ public void should_cache_config() {
+ BranchConfiguration configuration = provider.provide(null, () -> "project", globalConfiguration);
+ assertThat(provider.provide(null, () -> "project", globalConfiguration)).isSameAs(configuration);
+ }
+
+ @Test
+ public void should_use_loader() {
+ when(loader.load("key", globalConfiguration)).thenReturn(config);
+ BranchConfiguration branchConfig = provider.provide(loader, () -> "key", globalConfiguration);
+
+ assertThat(branchConfig).isSameAs(config);
+ }
+
+ @Test
+ public void should_return_default_if_no_loader() {
+ BranchConfiguration configuration = provider.provide(null, () -> "project", globalConfiguration);
+ assertThat(configuration.branchTarget()).isNull();
+ assertThat(configuration.branchType()).isEqualTo(BranchType.LONG);
+ }
+}