Browse Source

SONAR-5417 Initial version of batch protocol + refactoring of settings

tags/4.5-RC1
Julien HENRY 10 years ago
parent
commit
51cc491410

+ 1
- 7
pom.xml View File

@@ -1043,7 +1043,7 @@
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.2.2</version>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
@@ -1054,12 +1054,6 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>xmlunit</groupId>

+ 42
- 0
sonar-batch-protocol/pom.xml View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar</artifactId>
<version>4.5-SNAPSHOT</version>
</parent>

<artifactId>sonar-batch-protocol</artifactId>
<name>SonarQube :: Batch :: Protocol</name>
<description>Classes used for communication between batch and server</description>

<dependencies>
<dependency>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo</artifactId>
<version>2.24.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<!-- unit tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettingsReady.java → sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java View File

@@ -17,31 +17,26 @@
* 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.batch.scan;
package org.sonar.batch.protocol.input;

import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.batch.bootstrap.BatchSettings;
public class Language {

/**
* Barrier to control the project settings are loaded from Sonar DB before applying project exclusions
* </ul>
*/
public class ProjectSettingsReady {
private String key;

private String name;

private final ProjectReactor reactor;
private final BatchSettings settings;
private String[] suffixes;

public ProjectSettingsReady(ProjectReactor reactor, BatchSettings settings) {
this.reactor = reactor;
this.settings = settings;
public String key() {
return key;
}

public void start() {
settings.init(reactor);
public String name() {
return name;
}

public void stop() {
// Remove project specific settings
settings.restore();
public String[] suffixes() {
return suffixes;
}

}

+ 64
- 0
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java View File

@@ -0,0 +1,64 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.batch.protocol.input;

import com.google.gson.Gson;

import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class ProjectReferentials {

private long timestamp;
private Collection<Language> languages = new ArrayList<Language>();
private Map<String, Map<String, String>> projectSettings = new HashMap<String, Map<String, String>>();

public Map<String, String> projectSettings(String projectOrSubProjectKey) {
return projectSettings.get(projectOrSubProjectKey);
}

public void setProjectSettings(String projectOrSubProjectKey, Map<String, String> projectSettings) {
this.projectSettings.put(projectOrSubProjectKey, projectSettings);
}

public Collection<Language> languages() {
return languages;
}

public long timestamp() {
return timestamp;
}

public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

public String toJson() {
return new Gson().toJson(this);
}

public static ProjectReferentials fromJson(Reader input) {
return new Gson().fromJson(input, ProjectReferentials.class);
}

}

+ 33
- 0
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfiles.java View File

@@ -0,0 +1,33 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.batch.protocol.input;

import java.util.Map;

public class QProfiles {

public static class QProfile {

private String key, name, language;
}

private Map<String, QProfile> byLanguage;

}

+ 49
- 0
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java View File

@@ -0,0 +1,49 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.batch.protocol.input;

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.io.StringReader;
import java.util.HashMap;

import static org.fest.assertions.Assertions.assertThat;

public class ProjectReferentialsTest {

@Test
public void testToJson() throws JSONException {
ProjectReferentials ref = new ProjectReferentials();
HashMap<String, String> projectSettings = new HashMap<String, String>();
projectSettings.put("sonar.foo", "bar");
ref.setProjectSettings("foo", projectSettings);

JSONAssert.assertEquals("{languages: [], projectSettings: {foo: {'sonar.foo': 'bar'}}, timestamp: 0}", ref.toJson(), true);
}

@Test
public void testFromJson() throws JSONException {
ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{languages: [], projectSettings: {foo: {'sonar.foo': 'bar'}}, timestamp: 1}"));

assertThat(ref.timestamp()).isEqualTo(1);
}
}

+ 1
- 1
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java View File

@@ -98,7 +98,7 @@ public class BootstrapContainer extends ComponentContainer {
AnalysisMode.class,
BatchPluginRepository.class,
BatchPluginJarInstaller.class,
BatchSettings.class,
GlobalSettings.class,
ServerClient.class,
ExtensionInstaller.class,
Logback.class,

sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java → sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java View File

@@ -20,11 +20,9 @@
package org.sonar.batch.bootstrap;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
@@ -32,20 +30,17 @@ import org.sonar.batch.settings.SettingsReferential;

import javax.annotation.Nullable;

import java.util.Map;
public class GlobalSettings extends Settings {

public class BatchSettings extends Settings {

private static final Logger LOG = LoggerFactory.getLogger(BatchSettings.class);
private static final Logger LOG = LoggerFactory.getLogger(GlobalSettings.class);

private Configuration deprecatedConfiguration;

private final BootstrapProperties bootstrapProps;
private final SettingsReferential settingsReferential;
private final AnalysisMode mode;
private Map<String, String> savedProperties;

public BatchSettings(BootstrapProperties bootstrapProps, PropertyDefinitions propertyDefinitions,
public GlobalSettings(BootstrapProperties bootstrapProps, PropertyDefinitions propertyDefinitions,
SettingsReferential settingsReferential, Configuration deprecatedConfiguration, AnalysisMode mode) {

super(propertyDefinitions);
@@ -54,45 +49,13 @@ public class BatchSettings extends Settings {
this.bootstrapProps = bootstrapProps;
this.settingsReferential = settingsReferential;
this.deprecatedConfiguration = deprecatedConfiguration;
init(null);
init();
}

public void init(@Nullable ProjectReactor reactor) {
savedProperties = this.getProperties();

if (reactor != null) {
LOG.info("Load project settings");

String branch = reactor.getRoot().getProperties().getProperty(CoreProperties.PROJECT_BRANCH_PROPERTY);
String projectKey = reactor.getRoot().getKey();
if (StringUtils.isNotBlank(branch)) {
projectKey = String.format("%s:%s", projectKey, branch);
}
downloadSettings(projectKey);
} else {
LOG.info("Load global settings");
downloadSettings(null);
}

private void init() {
LOG.info("Load global settings");
addProperties(settingsReferential.globalSettings());
addProperties(bootstrapProps.properties());
if (reactor != null) {
addProperties(reactor.getRoot().getProperties());
}
}

/**
* Restore properties like they were before call of the {@link #init(org.sonar.api.batch.bootstrap.ProjectReactor)} method
*/
public void restore() {
this.setProperties(savedProperties);
}

private void downloadSettings(@Nullable String projectKey) {
if (StringUtils.isNotBlank(projectKey)) {
addProperties(settingsReferential.projectSettings(projectKey));
} else {
addProperties(settingsReferential.globalSettings());
}
}

@Override

+ 4
- 4
sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java View File

@@ -28,7 +28,7 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.AnalysisMode;
import org.sonar.batch.bootstrap.BatchSettings;
import org.sonar.batch.bootstrap.GlobalSettings;
import org.sonar.batch.settings.SettingsReferential;

import javax.annotation.Nullable;
@@ -44,7 +44,7 @@ public class ModuleSettings extends Settings {
private final SettingsReferential settingsReferential;
private AnalysisMode analysisMode;

public ModuleSettings(BatchSettings batchSettings, ProjectDefinition project, Configuration deprecatedCommonsConf, SettingsReferential settingsReferential,
public ModuleSettings(GlobalSettings batchSettings, ProjectDefinition project, Configuration deprecatedCommonsConf, SettingsReferential settingsReferential,
AnalysisMode analysisMode) {
super(batchSettings.getDefinitions());
this.settingsReferential = settingsReferential;
@@ -56,13 +56,13 @@ public class ModuleSettings extends Settings {
init(project, batchSettings);
}

private ModuleSettings init(ProjectDefinition project, BatchSettings batchSettings) {
private ModuleSettings init(ProjectDefinition project, GlobalSettings batchSettings) {
addProjectProperties(project, batchSettings);
addBuildProperties(project);
return this;
}

private void addProjectProperties(ProjectDefinition project, BatchSettings batchSettings) {
private void addProjectProperties(ProjectDefinition project, GlobalSettings batchSettings) {
String branch = batchSettings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
String projectKey = project.getKey();
if (StringUtils.isNotBlank(branch)) {

+ 4
- 6
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectExclusions.java View File

@@ -45,16 +45,14 @@ public class ProjectExclusions implements TaskComponent {
private ProjectReactor reactor;

public ProjectExclusions(Settings settings, ProjectReactor reactor,
// exclusions are applied when settings are loaded from Sonar DB
ProjectSettingsReady settingsReady,
// exclusions are applied when the project is completely defined by extensions
@Nullable ProjectBuilder[] projectBuilders) {
// exclusions are applied when the project is completely defined by extensions
@Nullable ProjectBuilder[] projectBuilders) {
this.settings = settings;
this.reactor = reactor;
}

public ProjectExclusions(Settings settings, ProjectReactor reactor, ProjectSettingsReady settingsReady) {
this(settings, reactor, settingsReady, new ProjectBuilder[0]);
public ProjectExclusions(Settings settings, ProjectReactor reactor) {
this(settings, reactor, new ProjectBuilder[0]);
}

public void apply() {

+ 1
- 1
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java View File

@@ -186,7 +186,7 @@ public class ProjectScanContainer extends ComponentContainer {
// Measures
MeasureCache.class,

ProjectSettingsReady.class);
ProjectSettings.class);
}

private void fixMavenExecutor() {

+ 95
- 0
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java View File

@@ -0,0 +1,95 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.batch.scan;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.AnalysisMode;
import org.sonar.batch.bootstrap.GlobalSettings;
import org.sonar.batch.settings.SettingsReferential;

import javax.annotation.Nullable;

public class ProjectSettings extends Settings {

private static final Logger LOG = LoggerFactory.getLogger(ProjectSettings.class);

private Configuration deprecatedConfiguration;

private final GlobalSettings globalSettings;
private final SettingsReferential settingsReferential;
private final AnalysisMode mode;

public ProjectSettings(ProjectReactor reactor, GlobalSettings globalSettings, PropertyDefinitions propertyDefinitions,
SettingsReferential settingsReferential, Configuration deprecatedConfiguration, AnalysisMode mode) {
super(propertyDefinitions);
this.mode = mode;
getEncryption().setPathToSecretKey(globalSettings.getString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH));
this.globalSettings = globalSettings;
this.settingsReferential = settingsReferential;
this.deprecatedConfiguration = deprecatedConfiguration;
init(reactor);
}

private void init(ProjectReactor reactor) {
LOG.info("Load project settings");

addProperties(globalSettings.getProperties());

String branch = reactor.getRoot().getProperties().getProperty(CoreProperties.PROJECT_BRANCH_PROPERTY);
String projectKey = reactor.getRoot().getKey();
if (StringUtils.isNotBlank(branch)) {
projectKey = String.format("%s:%s", projectKey, branch);
}
addProperties(settingsReferential.projectSettings(projectKey));

addProperties(reactor.getRoot().getProperties());
}

@Override
protected void doOnSetProperty(String key, @Nullable String value) {
deprecatedConfiguration.setProperty(key, value);
}

@Override
protected void doOnRemoveProperty(String key) {
deprecatedConfiguration.clearProperty(key);
}

@Override
protected void doOnClearProperties() {
deprecatedConfiguration.clear();
}

@Override
protected void doOnGetProperties(String key) {
if (mode.isPreview() && key.endsWith(".secured") && !key.contains(".license")) {
throw MessageException.of("Access to the secured property '" + key
+ "' is not possible in preview mode. The SonarQube plugin which requires this property must be deactivated in preview mode.");
}
}
}

+ 2
- 0
sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java View File

@@ -35,6 +35,7 @@ import org.sonar.batch.bootstrap.ExtensionUtils;
import org.sonar.batch.index.Caches;
import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
import org.sonar.batch.scan.ProjectReactorBuilder;
import org.sonar.batch.scan.ProjectSettings;
import org.sonar.batch.scan.filesystem.InputFileCache;
import org.sonar.batch.scan.maven.FakeMavenPluginExecutor;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
@@ -77,6 +78,7 @@ public class ProjectScanContainer extends ComponentContainer {

private void addBatchComponents() {
add(
ProjectSettings.class,
Caches.class,

// Measures

+ 65
- 0
sonar-batch/src/test/java/org/sonar/batch/bootstrap/GlobalSettingsTest.java View File

@@ -0,0 +1,65 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.batch.bootstrap;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.batch.settings.SettingsReferential;

import java.util.Collections;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GlobalSettingsTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

SettingsReferential settingsRef = mock(SettingsReferential.class);
ProjectDefinition project = ProjectDefinition.create().setKey("struts");
Configuration deprecatedConf = new BaseConfiguration();
BootstrapProperties bootstrapProps;

private AnalysisMode mode;

@Before
public void prepare() {
bootstrapProps = new BootstrapProperties(Collections.<String, String>emptyMap());
mode = mock(AnalysisMode.class);
}

@Test
public void should_load_global_settings() {
when(settingsRef.globalSettings()).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true"));

GlobalSettings batchSettings = new GlobalSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(batchSettings.getBoolean("sonar.cpd.cross")).isTrue();
}
}

+ 4
- 4
sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java View File

@@ -30,7 +30,7 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.AnalysisMode;
import org.sonar.batch.bootstrap.BatchSettings;
import org.sonar.batch.bootstrap.GlobalSettings;
import org.sonar.batch.settings.SettingsReferential;

import java.util.List;
@@ -68,7 +68,7 @@ public class ModuleSettingsTest {

@Test
public void test_loading_of_module_settings() {
BatchSettings batchSettings = mock(BatchSettings.class);
GlobalSettings batchSettings = mock(GlobalSettings.class);
when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions());
when(batchSettings.getProperties()).thenReturn(ImmutableMap.of(
"overridding", "batch",
@@ -92,7 +92,7 @@ public class ModuleSettingsTest {

@Test
public void should_not_fail_when_accessing_secured_properties() {
BatchSettings batchSettings = mock(BatchSettings.class);
GlobalSettings batchSettings = mock(GlobalSettings.class);
when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions());
when(batchSettings.getProperties()).thenReturn(ImmutableMap.of(
"sonar.foo.secured", "bar"
@@ -110,7 +110,7 @@ public class ModuleSettingsTest {

@Test
public void should_fail_when_accessing_secured_properties_in_preview() {
BatchSettings batchSettings = mock(BatchSettings.class);
GlobalSettings batchSettings = mock(GlobalSettings.class);
when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions());
when(batchSettings.getProperties()).thenReturn(ImmutableMap.of(
"sonar.foo.secured", "bar"

+ 13
- 1
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java View File

@@ -19,6 +19,7 @@
*/
package org.sonar.batch.scan;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.BatchExtension;
@@ -32,9 +33,14 @@ import org.sonar.api.config.Settings;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.task.TaskExtension;
import org.sonar.api.utils.System2;
import org.sonar.batch.bootstrap.AnalysisMode;
import org.sonar.batch.bootstrap.BootstrapProperties;
import org.sonar.batch.bootstrap.ExtensionInstaller;
import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
import org.sonar.batch.settings.SettingsReferential;

import java.util.Collections;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@@ -46,6 +52,7 @@ public class ProjectScanContainerTest {
private ProjectScanContainer container;
private Settings settings;
private ComponentContainer parentContainer;
private BootstrapProperties bootstrapProperties;

@Before
public void prepare() {
@@ -55,6 +62,11 @@ public class ProjectScanContainerTest {
parentContainer = new ComponentContainer();
parentContainer.add(settings);
parentContainer.add(System2.INSTANCE);
bootstrapProperties = new BootstrapProperties(Collections.<String, String>emptyMap());
parentContainer.add(bootstrapProperties);
parentContainer.add(new AnalysisMode(bootstrapProperties));
parentContainer.add(new PropertiesConfiguration());
parentContainer.add(mock(SettingsReferential.class));
container = new ProjectScanContainer(parentContainer);
}

@@ -84,7 +96,7 @@ public class ProjectScanContainerTest {

assertThat(container.getComponentsByType(PhasesSumUpTimeProfiler.class)).hasSize(0);

settings.setProperty(CoreProperties.PROFILING_LOG_PROPERTY, "true");
bootstrapProperties.properties().put(CoreProperties.PROFILING_LOG_PROPERTY, "true");

container = new ProjectScanContainer(parentContainer);
container.add(mock(ExtensionInstaller.class), projectBootstrapper);

sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java → sonar-batch/src/test/java/org/sonar/batch/scan/ProjectSettingsTest.java View File

@@ -17,7 +17,7 @@
* 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.batch.bootstrap;
package org.sonar.batch.scan;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.configuration.BaseConfiguration;
@@ -31,6 +31,9 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.AnalysisMode;
import org.sonar.batch.bootstrap.BootstrapProperties;
import org.sonar.batch.bootstrap.GlobalSettings;
import org.sonar.batch.settings.SettingsReferential;

import java.util.Collections;
@@ -39,58 +42,38 @@ import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BatchSettingsTest {
public class ProjectSettingsTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

private static final String JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}]";
private static final String JSON_RESPONSE_WITH_SECURED = "[{\"k\":\"sonar.foo.secured\",\"v\":\"bar\"},{\"k\":\"sonar.foo.license.secured\",\"v\":\"bar2\"}]";

private static final String REACTOR_JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}," +
"{\"k\":\"sonar.java.coveragePlugin\",\"v\":\"jacoco\"}]";

private static final String BRANCH_REACTOR_JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}," +
"{\"k\":\"sonar.java.coveragePlugin\",\"v\":\"jacoco\"}]";

SettingsReferential settingsRef = mock(SettingsReferential.class);
ProjectDefinition project = ProjectDefinition.create().setKey("struts");
Configuration deprecatedConf = new BaseConfiguration();
BootstrapProperties bootstrapProps;
GlobalSettings bootstrapProps;

private AnalysisMode mode;

@Before
public void prepare() {
bootstrapProps = new BootstrapProperties(Collections.<String, String>emptyMap());
mode = mock(AnalysisMode.class);
bootstrapProps = new GlobalSettings(new BootstrapProperties(Collections.<String, String>emptyMap()), new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
}

@Test
public void should_load_project_props() {
project.setProperty("project.prop", "project");

BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
batchSettings.init(new ProjectReactor(project));
ProjectSettings batchSettings = new ProjectSettings(new ProjectReactor(project), bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(batchSettings.getString("project.prop")).isEqualTo("project");
}

@Test
public void should_load_global_settings() {
when(settingsRef.globalSettings()).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true"));

BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(batchSettings.getBoolean("sonar.cpd.cross")).isTrue();
}

@Test
public void should_load_project_root_settings() {
when(settingsRef.projectSettings("struts")).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true", "sonar.java.coveragePlugin", "jacoco"));

BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
batchSettings.init(new ProjectReactor(project));
ProjectSettings batchSettings = new ProjectSettings(new ProjectReactor(project), bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(batchSettings.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco");
}
@@ -101,8 +84,7 @@ public class BatchSettingsTest {

when(settingsRef.projectSettings("struts:mybranch")).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true", "sonar.java.coveragePlugin", "jacoco"));

BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
batchSettings.init(new ProjectReactor(project));
ProjectSettings batchSettings = new ProjectSettings(new ProjectReactor(project), bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(batchSettings.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco");
}
@@ -111,8 +93,7 @@ public class BatchSettingsTest {
public void should_not_fail_when_accessing_secured_properties() {
when(settingsRef.projectSettings("struts")).thenReturn(ImmutableMap.of("sonar.foo.secured", "bar", "sonar.foo.license.secured", "bar2"));

BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
batchSettings.init(new ProjectReactor(project));
ProjectSettings batchSettings = new ProjectSettings(new ProjectReactor(project), bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(batchSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2");
assertThat(batchSettings.getString("sonar.foo.secured")).isEqualTo("bar");
@@ -124,8 +105,7 @@ public class BatchSettingsTest {

when(mode.isPreview()).thenReturn(true);

BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
batchSettings.init(new ProjectReactor(project));
ProjectSettings batchSettings = new ProjectSettings(new ProjectReactor(project), bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(batchSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2");
thrown.expect(MessageException.class);
@@ -138,8 +118,7 @@ public class BatchSettingsTest {
public void should_forward_to_deprecated_commons_configuration() {
when(settingsRef.projectSettings("struts")).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true", "sonar.java.coveragePlugin", "jacoco"));

BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
batchSettings.init(new ProjectReactor(project));
ProjectSettings batchSettings = new ProjectSettings(new ProjectReactor(project), bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);

assertThat(deprecatedConf.getString("sonar.cpd.cross")).isEqualTo("true");
assertThat(deprecatedConf.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco");
@@ -153,10 +132,4 @@ public class BatchSettingsTest {
assertThat(deprecatedConf.getString("sonar.java.coveragePlugin")).isNull();
}

@Test
public void project_should_be_optional() {
when(settingsRef.globalSettings()).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true"));
BatchSettings batchSettings = new BatchSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
assertThat(batchSettings.getProperties()).isNotEmpty();
}
}

+ 1
- 1
sonar-batch/src/test/java/org/sonar/batch/scan/ScanTaskTest.java View File

@@ -45,7 +45,7 @@ public class ScanTaskTest {
ScanTask task = new ScanTask(mock(TaskContainer.class));
ComponentContainer projectScanContainer = new ComponentContainer();
projectScanContainer.add(mock(ProjectConfigurator.class), new ProjectReactor(ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo")),
mock(Settings.class), mock(ProjectSettingsReady.class),
mock(Settings.class),
mock(ResourceDao.class));
task.scan(projectScanContainer);


Loading…
Cancel
Save