diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-08-03 17:27:33 +0200 |
---|---|---|
committer | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-09-12 10:59:55 +0200 |
commit | d6e6f92aefc44df2dd0af10ff3abda6aabb10b39 (patch) | |
tree | d58eb49a31782962fab9ef8b9d477e212fd7863d /sonar-scanner-engine/src/test | |
parent | 47ef3bd0dc6610d0034f1753e06e8ac467eb906b (diff) | |
download | sonarqube-d6e6f92aefc44df2dd0af10ff3abda6aabb10b39.tar.gz sonarqube-d6e6f92aefc44df2dd0af10ff3abda6aabb10b39.zip |
SONAR-9616 Handle branch in scanner
Diffstat (limited to 'sonar-scanner-engine/src/test')
5 files changed, 77 insertions, 36 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java index e572f184aa2..e23ba7b4b49 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java @@ -51,6 +51,7 @@ import org.sonar.api.utils.DateUtils; import org.sonar.batch.bootstrapper.Batch; import org.sonar.batch.bootstrapper.EnvironmentInformation; import org.sonar.batch.bootstrapper.LogOutput; +import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.bootstrap.GlobalMode; import org.sonar.scanner.issue.tracking.ServerLineHashesLoader; import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue; @@ -66,9 +67,13 @@ import org.sonar.scanner.repository.settings.SettingsLoader; import org.sonar.scanner.rule.ActiveRulesLoader; import org.sonar.scanner.rule.LoadedActiveRule; import org.sonar.scanner.rule.RulesLoader; +import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.BranchConfigurationLoader; import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; import org.sonarqube.ws.Rules.ListResponse.Rule; +import static org.mockito.Mockito.mock; + /** * Main utility class for writing scanner medium tests. * @@ -78,6 +83,7 @@ public class ScannerMediumTester extends ExternalResource { private static Path userHome = null; private Map<String, String> globalProperties = new HashMap<>(); private final FakeMetricsRepositoryLoader globalRefProvider = new FakeMetricsRepositoryLoader(); + private final FakeBranchConfigurationLoader projectBranchesProvider = new FakeBranchConfigurationLoader(); private final FakeProjectRepositoriesLoader projectRefProvider = new FakeProjectRepositoriesLoader(); private final FakePluginInstaller pluginInstaller = new FakePluginInstaller(); private final FakeServerIssuesLoader serverIssues = new FakeServerIssuesLoader(); @@ -288,6 +294,7 @@ public class ScannerMediumTester extends ExternalResource { tester.globalRefProvider, tester.qualityProfiles, tester.rulesLoader, + tester.projectBranchesProvider, tester.projectRefProvider, tester.activeRules, tester.serverIssues, @@ -365,7 +372,7 @@ public class ScannerMediumTester extends ExternalResource { private Date lastAnalysisDate; @Override - public ProjectRepositories load(String projectKey, boolean isIssuesMode) { + public ProjectRepositories load(String projectKey, boolean isIssuesMode, @Nullable String branchTarget) { Table<String, String, String> settings = HashBasedTable.create(); return new ProjectRepositories(settings, fileDataTable, lastAnalysisDate); } @@ -382,6 +389,13 @@ public class ScannerMediumTester extends ExternalResource { } + private static class FakeBranchConfigurationLoader implements BranchConfigurationLoader { + @Override + public BranchConfiguration load(String projectKey, GlobalConfiguration settings) { + return mock(BranchConfiguration.class); + } + } + private static class FakeQualityProfileLoader implements QualityProfileLoader { private List<QualityProfile> qualityProfiles = new LinkedList<>(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index 3a4051ece54..456221d91cd 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -44,6 +44,7 @@ import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.rule.ModuleQProfiles; import org.sonar.scanner.rule.QProfile; +import org.sonar.scanner.scan.BranchConfiguration; import static java.util.Arrays.asList; import static java.util.Collections.emptyMap; @@ -66,6 +67,7 @@ public class MetadataPublisherTest { private InputModuleHierarchy inputModuleHierarchy; private AnalysisMode analysisMode; private ScannerPluginRepository pluginRepository; + private BranchConfiguration branches; @Before public void prepare() throws IOException { @@ -84,7 +86,9 @@ public class MetadataPublisherTest { inputModuleHierarchy = mock(InputModuleHierarchy.class); when(inputModuleHierarchy.root()).thenReturn(rootModule); analysisMode = mock(AnalysisMode.class); - underTest = new MetadataPublisher(projectAnalysisInfo, inputModuleHierarchy, settings.asConfig(), qProfiles, cpdSettings, analysisMode, pluginRepository); + branches = mock(BranchConfiguration.class); + underTest = new MetadataPublisher(projectAnalysisInfo, inputModuleHierarchy, settings.asConfig(), qProfiles, cpdSettings, analysisMode, + pluginRepository, branches); } @Test @@ -160,4 +164,39 @@ public class MetadataPublisherTest { assertThat(metadata.getOrganizationKey()).isEqualTo("SonarSource"); } + @Test + public void write_long_lived_branch_info() throws Exception { + String branchName = "long-lived"; + settings.setProperty(ScannerProperties.BRANCH_NAME, branchName); + + when(branches.branchType()).thenReturn(BranchConfiguration.BranchType.LONG); + + File outputDir = temp.newFolder(); + underTest.publish(new ScannerReportWriter(outputDir)); + + ScannerReportReader reader = new ScannerReportReader(outputDir); + ScannerReport.Metadata metadata = reader.readMetadata(); + assertThat(metadata.getBranchName()).isEqualTo(branchName); + assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.LONG); + } + + @Test + public void write_short_lived_branch_info() throws Exception { + String branchName = "feature"; + String branchTarget = "short-lived"; + settings.setProperty(ScannerProperties.BRANCH_NAME, branchName); + settings.setProperty(ScannerProperties.BRANCH_TARGET, branchTarget); + + when(branches.branchTarget()).thenReturn(branchTarget); + + File outputDir = temp.newFolder(); + underTest.publish(new ScannerReportWriter(outputDir)); + + ScannerReportReader reader = new ScannerReportReader(outputDir); + ScannerReport.Metadata metadata = reader.readMetadata(); + assertThat(metadata.getBranchName()).isEqualTo(branchName); + assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.SHORT); + assertThat(metadata.getMergeBranchName()).isEqualTo(branchTarget); + } + } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java index 3050b55efcb..abb528b202d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java @@ -59,7 +59,7 @@ public class DefaultProjectRepositoriesLoaderTest { @Test public void continueOnError() { when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class); - ProjectRepositories proj = loader.load(PROJECT_KEY, false); + ProjectRepositories proj = loader.load(PROJECT_KEY, false, null); assertThat(proj.exists()).isEqualTo(false); } @@ -68,7 +68,7 @@ public class DefaultProjectRepositoriesLoaderTest { InputStream is = mock(InputStream.class); when(is.read()).thenThrow(IOException.class); WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is); - loader.load(PROJECT_KEY, false); + loader.load(PROJECT_KEY, false, null); } @Test(expected = IllegalStateException.class) @@ -76,7 +76,7 @@ public class DefaultProjectRepositoriesLoaderTest { HttpException http = new HttpException("url", 403, null); IllegalStateException e = new IllegalStateException("http error", http); WsTestUtil.mockException(wsClient, e); - loader.load(PROJECT_KEY, false); + loader.load(PROJECT_KEY, false, null); } @Test @@ -87,26 +87,26 @@ public class DefaultProjectRepositoriesLoaderTest { HttpException http = new HttpException("uri", 403, null); MessageException e = MessageException.of("http error", http); WsTestUtil.mockException(wsClient, e); - loader.load(PROJECT_KEY, false); + loader.load(PROJECT_KEY, false, null); } @Test public void passIssuesModeParameter() { - loader.load(PROJECT_KEY, false); + loader.load(PROJECT_KEY, false, null); WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F"); - loader.load(PROJECT_KEY, true); + loader.load(PROJECT_KEY, true, null); WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F&issues_mode=true"); } @Test public void deserializeResponse() throws IOException { - loader.load(PROJECT_KEY, false); + loader.load(PROJECT_KEY, false, null); } @Test public void passAndEncodeProjectKeyParameter() { - loader.load(PROJECT_KEY, false); + loader.load(PROJECT_KEY, false, null); WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F"); } @@ -124,7 +124,7 @@ public class DefaultProjectRepositoriesLoaderTest { InputStream is = getTestResource("project.protobuf"); WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=org.sonarsource.github%3Asonar-github-plugin&issues_mode=true", is); - ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true); + ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true, null); FileData fd = proj.fileData("org.sonarsource.github:sonar-github-plugin", "src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java"); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java index 5dddeab9a29..e7c5b810a10 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java @@ -28,8 +28,10 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.sonar.api.batch.bootstrap.ProjectKey; import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.scan.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -46,6 +48,8 @@ public class ProjectRepositoriesProviderTest { private ProjectKey projectKey; @Mock private DefaultAnalysisMode mode; + @Mock + private BranchConfiguration branchConfiguration; @Before public void setUp() { @@ -63,24 +67,24 @@ public class ProjectRepositoriesProviderTest { @Test public void testValidation() { when(mode.isIssues()).thenReturn(true); - when(loader.load(eq("key"), eq(true))).thenReturn(project); + when(loader.load(eq("key"), eq(true), any())).thenReturn(project); - provider.provide(loader, projectKey, mode); + provider.provide(loader, projectKey, mode, branchConfiguration); } @Test public void testAssociated() { when(mode.isIssues()).thenReturn(false); - when(loader.load(eq("key"), eq(false))).thenReturn(project); + when(loader.load(eq("key"), eq(false), any())).thenReturn(project); - ProjectRepositories repo = provider.provide(loader, projectKey, mode); + ProjectRepositories repo = provider.provide(loader, projectKey, mode, branchConfiguration); assertThat(repo.exists()).isEqualTo(true); assertThat(repo.lastAnalysisDate()).isNotNull(); verify(mode, times(1)).isIssues(); verify(projectKey).get(); - verify(loader).load(eq("key"), eq(false)); + verify(loader).load(eq("key"), eq(false), eq(null)); verifyNoMoreInteractions(loader, projectKey, mode); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java index 740bdd24818..896b3a9fd06 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java @@ -19,11 +19,6 @@ */ package org.sonar.scanner.scan; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.Optional; - import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -32,26 +27,23 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.utils.MessageException; -import org.sonar.core.config.ScannerProperties; import org.sonar.scanner.analysis.DefaultAnalysisMode; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class ProjectReactorValidatorTest { @Rule public ExpectedException thrown = ExpectedException.none(); private DefaultAnalysisMode mode; - private ProjectSettings settings; private ProjectReactorValidator validator; @Before public void prepare() { mode = mock(DefaultAnalysisMode.class); - - settings = mock(ProjectSettings.class); - when(settings.get(ScannerProperties.BRANCH_NAME)).thenReturn(Optional.empty()); - - validator = new ProjectReactorValidator(mode, settings); + validator = new ProjectReactorValidator(mode); } @Test @@ -161,14 +153,6 @@ public class ProjectReactorValidatorTest { validator.validate(reactor); } - @Test - public void should_fail_when_both_old_and_new_branch_property_present() { - thrown.expect(MessageException.class); - thrown.expectMessage("The sonar.branch.name parameter must not be used together with the deprecated sonar.branch parameter"); - when(settings.get(ScannerProperties.BRANCH_NAME)).thenReturn(Optional.of("feature")); - validator.validate(createProjectReactor("foo", "branch1")); - } - private ProjectReactor createProjectReactor(String projectKey) { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, projectKey); ProjectReactor reactor = new ProjectReactor(def); |