From aaeea8c321fc0088535d32f8f67f589a8801dcb8 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 24 Jun 2013 16:23:36 +0200 Subject: [PATCH] SONARPLUGINS-3009 Keep backward compatibility ProjectReactor is still provided by SonarRunner for SonarQube older than 3.7 --- .../sonar/runner/batch/IsolatedLauncher.java | 12 +++--- .../runner/batch/ProjectReactorBuilder.java | 4 +- .../java/org/sonar/runner/batch/Utils.java | 2 +- .../org/sonar/runner/batch/VersionUtils.java | 42 +++++++++++++++++++ .../runner/batch/IsolatedLauncherTest.java | 2 +- .../batch/ProjectReactorBuilderTest.java | 2 +- .../org/sonar/runner/batch/UtilsTest.java | 2 +- .../sonar/runner/batch/VersionUtilsTest.java | 38 +++++++++++++++++ .../org/sonar/runner/impl/BatchLauncher.java | 12 +++--- .../sonar/runner/impl/BatchLauncherTest.java | 10 +++-- 10 files changed, 104 insertions(+), 22 deletions(-) create mode 100644 sonar-runner-batch/src/main/java/org/sonar/runner/batch/VersionUtils.java create mode 100644 sonar-runner-batch/src/test/java/org/sonar/runner/batch/VersionUtilsTest.java diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java index 5a4184d..f61fafe 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java @@ -41,11 +41,11 @@ import java.util.Properties; */ public class IsolatedLauncher { - public void execute(Properties properties, List extensions) { - createBatch(properties, extensions).execute(); + public void execute(String sonarVersion, Properties properties, List extensions) { + createBatch(sonarVersion, properties, extensions).execute(); } - Batch createBatch(Properties properties, List extensions) { + Batch createBatch(String sonarVersion, Properties properties, List extensions) { ProjectReactor projectReactor = null; initLogging(properties); EnvironmentInformation env = new EnvironmentInformation(properties.getProperty("sonarRunner.app"), properties.getProperty("sonarRunner.appVersion")); @@ -54,18 +54,16 @@ public class IsolatedLauncher { .addComponents(extensions); String task = properties.getProperty("sonar.task", "scan"); - if ("scan".equals(task)) { + if (VersionUtils.isLessThan37(sonarVersion) && "scan".equals(task)) { Properties propsClone = new Properties(); propsClone.putAll(properties); projectReactor = new ProjectReactorBuilder(propsClone).build(); + builder.setProjectReactor(projectReactor); } else { // only on sonar 3.5+... in theory builder.setGlobalProperties((Map) properties); } - if (projectReactor != null) { - builder.setProjectReactor(projectReactor); - } return builder.build(); } diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java index bdc6d79..2c5e3f0 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java @@ -1,5 +1,5 @@ /* - * Sonar Runner - Batch + * SonarQube Runner - Batch * Copyright (C) 2011 SonarSource * dev@sonar.codehaus.org * @@ -47,7 +47,9 @@ import java.util.Properties; * Class that creates a Sonar project definition based on a set of properties. * * @since 1.5 + * @deprecated since 2.3. Moved to SonarQube batch 3.7. Will be deleted when SonarQube reunner will require SonarQube 3.7+. */ +@Deprecated class ProjectReactorBuilder { private static final Logger LOG = LoggerFactory.getLogger(ProjectReactorBuilder.class); diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java index 8bf39f9..7037f00 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java @@ -1,5 +1,5 @@ /* - * Sonar Runner - Batch + * SonarQube Runner - Batch * Copyright (C) 2011 SonarSource * dev@sonar.codehaus.org * diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/VersionUtils.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/VersionUtils.java new file mode 100644 index 0000000..e26e918 --- /dev/null +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/VersionUtils.java @@ -0,0 +1,42 @@ +/* + * SonarQube Runner - Batch + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * 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 02 + */ +package org.sonar.runner.batch; + +class VersionUtils { + + private static final String[] LESS_THAN_3_7 = {"0", "1", "2", "3.0", "3.1", "3.2", "3.3", "3.4", "3.5", "3.6"}; + + static boolean isLessThan37(String version) { + return inVersions(version, LESS_THAN_3_7); + } + + private static boolean inVersions(String version, String[] versions) { + for (String s : versions) { + if (isVersion(version, s)) { + return true; + } + } + return false; + } + + private static boolean isVersion(String version, String prefix) { + return version.startsWith(prefix + ".") || version.equals(prefix); + } +} diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java index e3cc081..fff4b14 100644 --- a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java @@ -39,7 +39,7 @@ public class IsolatedLauncherTest { props.setProperty("sonar.projectName", "Sample"); props.setProperty("sonar.projectVersion", "1.0"); props.setProperty("sonar.sources", "src"); - Batch batch = launcher.createBatch(props, Collections.emptyList()); + Batch batch = launcher.createBatch("3.5", props, Collections.emptyList()); assertThat(batch).isNotNull(); } diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java index a8db88b..a8fe7b3 100644 --- a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java @@ -1,5 +1,5 @@ /* - * Sonar Runner - Batch + * SonarQube Runner - Batch * Copyright (C) 2011 SonarSource * dev@sonar.codehaus.org * diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/UtilsTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/UtilsTest.java index 21959d5..2cf5b3a 100644 --- a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/UtilsTest.java +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/UtilsTest.java @@ -1,5 +1,5 @@ /* - * Sonar Runner - Batch + * SonarQube Runner - Batch * Copyright (C) 2011 SonarSource * dev@sonar.codehaus.org * diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/VersionUtilsTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/VersionUtilsTest.java new file mode 100644 index 0000000..3838311 --- /dev/null +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/VersionUtilsTest.java @@ -0,0 +1,38 @@ +/* + * SonarQube Runner - Batch + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * 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 02 + */ +package org.sonar.runner.batch; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class VersionUtilsTest { + + @Test + public void testIsLessThan3_7() { + assertThat(VersionUtils.isLessThan37("2.5")).isTrue(); + assertThat(VersionUtils.isLessThan37("3.0")).isTrue(); + assertThat(VersionUtils.isLessThan37("3.0.1")).isTrue(); + assertThat(VersionUtils.isLessThan37("3.6")).isTrue(); + assertThat(VersionUtils.isLessThan37("3.7")).isFalse(); + assertThat(VersionUtils.isLessThan37("4.0")).isFalse(); + } + +} diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java index b8ad0cf..e2abd3f 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java @@ -47,7 +47,7 @@ public class BatchLauncher { ServerConnection serverConnection = ServerConnection.create(props); ServerVersion serverVersion = new ServerVersion(serverConnection); JarDownloader jarDownloader = new JarDownloader(props, serverConnection, serverVersion); - doExecute(jarDownloader, props, extensions); + doExecute(jarDownloader, serverVersion, props, extensions); } private static String[][] getMaskRules(final Properties props) { @@ -65,26 +65,26 @@ public class BatchLauncher { /** * @return the {@link org.sonar.runner.batch.IsolatedLauncher} instance for unit tests */ - Object doExecute(final JarDownloader jarDownloader, final Properties props, final List extensions) { + Object doExecute(final JarDownloader jarDownloader, final ServerVersion serverVersion, final Properties props, final List extensions) { Object launcher = AccessController.doPrivileged(new PrivilegedAction() { public Object run() { List jarFiles = jarDownloader.download(); String[][] maskRules = getMaskRules(props); IsolatedClassloader classloader = new IsolatedClassloader(getClass().getClassLoader(), maskRules); classloader.addFiles(jarFiles); - Object launcher = delegateExecution(classloader, props, extensions); + Object launcher = delegateExecution(classloader, serverVersion.version(), props, extensions); tempCleaning.clean(); return launcher; } - private Object delegateExecution(IsolatedClassloader classloader, Properties properties, List extensions) { + private Object delegateExecution(IsolatedClassloader classloader, String sonarVersion, Properties properties, List extensions) { ClassLoader initialContextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(classloader); Class launcherClass = classloader.loadClass(isolatedLauncherClass); - Method executeMethod = launcherClass.getMethod("execute", Properties.class, List.class); + Method executeMethod = launcherClass.getMethod("execute", String.class, Properties.class, List.class); Object launcher = launcherClass.newInstance(); - executeMethod.invoke(launcher, properties, extensions); + executeMethod.invoke(launcher, sonarVersion, properties, extensions); return launcher; } catch (InvocationTargetException e) { // Unwrap original exception diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java index 1b32344..4a2e8c8 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java @@ -51,7 +51,7 @@ public class BatchLauncherTest { props.put(InternalProperties.RUNNER_MASK_RULES, "UNMASK|org.sonar.runner.impl."); List extensions = new ArrayList(); - FakeIsolatedLauncher isolatedLauncher = (FakeIsolatedLauncher) launcher.doExecute(jarDownloader, props, extensions); + FakeIsolatedLauncher isolatedLauncher = (FakeIsolatedLauncher) launcher.doExecute(jarDownloader, mock(ServerVersion.class), props, extensions); assertThat(isolatedLauncher.props.get("foo")).isEqualTo("bar"); assertThat(isolatedLauncher.extensions).isSameAs(extensions); verify(jarDownloader).download(); @@ -66,7 +66,7 @@ public class BatchLauncherTest { // The current classloader in not available -> fail to load FakeIsolatedLauncher props.put(InternalProperties.RUNNER_MASK_RULES, ""); try { - launcher.doExecute(jarDownloader, props, Collections.emptyList()); + launcher.doExecute(jarDownloader, mock(ServerVersion.class), props, Collections.emptyList()); fail(); } catch (RunnerException e) { // success @@ -86,7 +86,7 @@ public class BatchLauncherTest { Properties props = new Properties(); List extensions = Collections.emptyList(); BatchLauncher launcher = spy(new BatchLauncher()); - doReturn(new Object()).when(launcher).doExecute(any(JarDownloader.class), eq(props), eq(extensions)); + doReturn(new Object()).when(launcher).doExecute(any(JarDownloader.class), any(ServerVersion.class), eq(props), eq(extensions)); launcher.execute(props, extensions); @@ -96,8 +96,10 @@ public class BatchLauncherTest { public static class FakeIsolatedLauncher { public Properties props = null; public List extensions = null; + private String sonarVersion; - public void execute(Properties props, List extensions) { + public void execute(String sonarVersion, Properties props, List extensions) { + this.sonarVersion = sonarVersion; this.props = props; this.extensions = extensions; } -- 2.39.5