diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-09-15 15:37:38 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-09-16 15:14:49 +0200 |
commit | 991c4b28ff79817a292f7a05845dd0f5cfae0842 (patch) | |
tree | 5ab4700c31f6a18d178279d823ba96ecea4b4b71 /sonar-batch/src/test/java | |
parent | b48e0f8bc3787c8e73edb47503b345ace29d224c (diff) | |
download | sonarqube-991c4b28ff79817a292f7a05845dd0f5cfae0842.tar.gz sonarqube-991c4b28ff79817a292f7a05845dd0f5cfae0842.zip |
SONAR-6835 Include batch analysis context in the report sent by the batch
Diffstat (limited to 'sonar-batch/src/test/java')
3 files changed, 108 insertions, 18 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/AnalysisContextReportPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/AnalysisContextReportPublisherTest.java new file mode 100644 index 00000000000..e7bdc1b66e4 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/report/AnalysisContextReportPublisherTest.java @@ -0,0 +1,91 @@ +/* + * 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.report; + +import java.util.Arrays; +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.AnalysisMode; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.config.Settings; +import org.sonar.batch.bootstrap.BatchPluginRepository; +import org.sonar.batch.protocol.output.BatchReportWriter; +import org.sonar.core.platform.PluginInfo; +import org.sonar.updatecenter.common.Version; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AnalysisContextReportPublisherTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private BatchPluginRepository pluginRepo = mock(BatchPluginRepository.class); + private AnalysisContextReportPublisher publisher; + private AnalysisMode analysisMode = mock(AnalysisMode.class); + + @Before + public void prepare() throws Exception { + publisher = new AnalysisContextReportPublisher(analysisMode, pluginRepo); + } + + @Test + public void shouldDumpPlugins() throws Exception { + when(pluginRepo.getPluginInfos()).thenReturn(Arrays.asList(new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0")))); + + BatchReportWriter writer = new BatchReportWriter(temp.newFolder()); + publisher.init(writer); + + assertThat(writer.getFileStructure().analysisLog()).exists(); + assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog())).contains("Xoo 1.0 (xoo)"); + } + + @Test + public void shouldNotDumpInIssuesMode() throws Exception { + when(analysisMode.isIssues()).thenReturn(true); + + BatchReportWriter writer = new BatchReportWriter(temp.newFolder()); + publisher.init(writer); + publisher.dumpSettings(ProjectDefinition.create().setProperty("sonar.projectKey", "foo"), new Settings()); + + assertThat(writer.getFileStructure().analysisLog()).doesNotExist(); + } + + @Test + public void shouldNotDumpSensitiveProperties() throws Exception { + BatchReportWriter writer = new BatchReportWriter(temp.newFolder()); + publisher.init(writer); + + assertThat(writer.getFileStructure().analysisLog()).exists(); + + Settings settings = new Settings(); + settings.setProperty("sonar.projectKey", "foo"); + settings.setProperty("sonar.password", "azerty"); + settings.setProperty("sonar.cpp.license.secured", "AZERTY"); + publisher.dumpSettings(ProjectDefinition.create().setProperty("sonar.projectKey", "foo"), settings); + + assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog())).contains("sonar.projectKey=foo", "sonar.password=******", "sonar.cpp.license.secured=******"); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java index de11e64af45..7d1c47d058d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java @@ -19,8 +19,6 @@ */ package org.sonar.batch.report; -import org.sonar.batch.analysis.DefaultAnalysisMode; - import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; @@ -29,8 +27,10 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; import org.sonar.api.utils.TempFolder; +import org.sonar.batch.analysis.DefaultAnalysisMode; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.scan.ImmutableProjectReactor; + import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -52,7 +52,8 @@ public class ReportPublisherTest { public void should_log_successful_analysis() { Settings settings = new Settings(); settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); + ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, + mock(TempFolder.class), new ReportPublisherStep[0]); Logger logger = mock(Logger.class); job.logSuccess(logger); @@ -65,7 +66,8 @@ public class ReportPublisherTest { public void should_log_successful_issues_analysis() { Settings settings = new Settings(); when(mode.isIssues()).thenReturn(true); - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); + ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, + mock(TempFolder.class), new ReportPublisherStep[0]); Logger logger = mock(Logger.class); job.logSuccess(logger); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java index b68eee07f32..336cf7a2a85 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java @@ -20,10 +20,11 @@ package org.sonar.batch.scan; import com.google.common.collect.HashBasedTable; - import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableTable; import com.google.common.collect.Table; +import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -34,11 +35,9 @@ import org.sonar.api.utils.MessageException; import org.sonar.batch.analysis.DefaultAnalysisMode; import org.sonar.batch.bootstrap.GlobalSettings; import org.sonar.batch.protocol.input.FileData; +import org.sonar.batch.report.AnalysisContextReportPublisher; import org.sonar.batch.repository.ProjectSettingsRepo; -import java.util.List; -import java.util.Map; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -85,14 +84,13 @@ public class ModuleSettingsTest { when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); when(batchSettings.getProperties()).thenReturn(ImmutableMap.of( "overridding", "batch", - "on-batch", "true" - )); + "on-batch", "true")); ProjectSettingsRepo projSettingsRepo = createSettings("struts-core", ImmutableMap.of("on-module", "true", "overridding", "module")); ProjectDefinition module = ProjectDefinition.create().setKey("struts-core"); - ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, projSettingsRepo, mode); + ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, projSettingsRepo, mode, mock(AnalysisContextReportPublisher.class)); assertThat(moduleSettings.getString("overridding")).isEqualTo("module"); assertThat(moduleSettings.getString("on-batch")).isEqualTo("true"); @@ -105,14 +103,13 @@ public class ModuleSettingsTest { GlobalSettings batchSettings = mock(GlobalSettings.class); when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); when(batchSettings.getProperties()).thenReturn(ImmutableMap.of( - "sonar.foo.secured", "bar" - )); + "sonar.foo.secured", "bar")); ProjectSettingsRepo projSettingsRepo = createSettings("struts-core", ImmutableMap.of("sonar.foo.license.secured", "bar2")); ProjectDefinition module = ProjectDefinition.create().setKey("struts-core"); - ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, projSettingsRepo, mode); + ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, projSettingsRepo, mode, mock(AnalysisContextReportPublisher.class)); assertThat(moduleSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2"); assertThat(moduleSettings.getString("sonar.foo.secured")).isEqualTo("bar"); @@ -123,8 +120,7 @@ public class ModuleSettingsTest { GlobalSettings batchSettings = mock(GlobalSettings.class); when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); when(batchSettings.getProperties()).thenReturn(ImmutableMap.of( - "sonar.foo.secured", "bar" - )); + "sonar.foo.secured", "bar")); ProjectSettingsRepo projSettingsRepo = createSettings("struts-core", ImmutableMap.of("sonar.foo.license.secured", "bar2")); @@ -132,13 +128,14 @@ public class ModuleSettingsTest { ProjectDefinition module = ProjectDefinition.create().setKey("struts-core"); - ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, projSettingsRepo, mode); + ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, projSettingsRepo, mode, mock(AnalysisContextReportPublisher.class)); assertThat(moduleSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2"); thrown.expect(MessageException.class); thrown - .expectMessage("Access to the secured property 'sonar.foo.secured' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); + .expectMessage( + "Access to the secured property 'sonar.foo.secured' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); moduleSettings.getString("sonar.foo.secured"); } } |