From 887b32c45fda63696113755a1a0434dad3d5fa0c Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 13 Aug 2015 13:34:48 +0200 Subject: [PATCH] SONARUNNER-144 Expose sonarqube version in API --- .../org/sonar/runner/api/EmbeddedRunner.java | 15 +++++++++++++++ .../sonar/runner/api/EmbeddedRunnerTest.java | 18 ++++++++++++++++-- .../runner/batch/BatchIsolatedLauncher.java | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java index bebbf38..5c50fa9 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java @@ -110,6 +110,7 @@ public class EmbeddedRunner { } public void runAnalysis(Properties analysisProperties, @Nullable IssueListener issueListener) { + checkLauncherExists(); Properties copy = new Properties(); copy.putAll(analysisProperties); initAnalysisProperties(copy); @@ -125,6 +126,7 @@ public class EmbeddedRunner { } public void syncProject(String projectKey) { + checkLauncherExists(); if (!VersionUtils.isAtLeast52(launcher.getVersion())) { throw new IllegalStateException("not supported in current SonarQube version: " + launcher.getVersion()); } @@ -141,8 +143,15 @@ public class EmbeddedRunner { } public void stop() { + checkLauncherExists(); doStop(); } + + public String serverVersion() { + checkLauncherExists(); + return launcher.getVersion(); + } + /** * @deprecated since 2.5 use {@link #start()}, {@link #runAnalysis(Properties)} and then {@link #stop()} @@ -223,6 +232,12 @@ public class EmbeddedRunner { launcher.executeOldVersion(prop); } } + + private void checkLauncherExists() { + if(launcher == null) { + throw new IllegalStateException("not started"); + } + } static class IssueListenerAdapter implements org.sonar.runner.batch.IssueListener { private IssueListener apiIssueListener; diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java index a1ffdce..9a6d676 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java @@ -73,15 +73,29 @@ public class EmbeddedRunnerTest { } @Test - public void test_syncProject() { + public void test_sync_project() { String projectKey = "proj"; runner.start(); runner.syncProject(projectKey); verify(launcher).syncProject(projectKey); } + + @Test + public void test_server_version() { + runner.start(); + assertThat(runner.serverVersion()).isEqualTo("5.2"); + } + + @Test + public void test_run_before_start() { + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("started"); + + runner.runAnalysis(new Properties()); + } @Test - public void test_fail_projectSync_old_sq() { + public void test_fail_project_sync_old_sq() { when(launcher.getVersion()).thenReturn("5.0"); expectedException.expect(IllegalStateException.class); diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/BatchIsolatedLauncher.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/BatchIsolatedLauncher.java index 0d7be18..f941f32 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/BatchIsolatedLauncher.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/BatchIsolatedLauncher.java @@ -86,7 +86,7 @@ public class BatchIsolatedLauncher implements IsolatedLauncher { public void executeOldVersion(Properties properties) { createBatch(properties, null).execute(); } - + @Override public String getVersion() { InputStream is = this.getClass().getClassLoader().getResourceAsStream("sq-version.txt"); -- 2.39.5