]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-3009 Keep backward compatibility
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 24 Jun 2013 14:23:36 +0000 (16:23 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 24 Jun 2013 14:23:36 +0000 (16:23 +0200)
ProjectReactor is still provided by SonarRunner for SonarQube older than 3.7

sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java
sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java
sonar-runner-batch/src/main/java/org/sonar/runner/batch/Utils.java
sonar-runner-batch/src/main/java/org/sonar/runner/batch/VersionUtils.java [new file with mode: 0644]
sonar-runner-batch/src/test/java/org/sonar/runner/batch/IsolatedLauncherTest.java
sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java
sonar-runner-batch/src/test/java/org/sonar/runner/batch/UtilsTest.java
sonar-runner-batch/src/test/java/org/sonar/runner/batch/VersionUtilsTest.java [new file with mode: 0644]
sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java
sonar-runner-impl/src/test/java/org/sonar/runner/impl/BatchLauncherTest.java

index 5a4184d7e40fd336237554bf125f8ea1ce8f8e75..f61fafe22eb6fa973e989d54b0bf70e623d2af7e 100644 (file)
@@ -41,11 +41,11 @@ import java.util.Properties;
  */
 public class IsolatedLauncher {
 
-  public void execute(Properties properties, List<Object> extensions) {
-    createBatch(properties, extensions).execute();
+  public void execute(String sonarVersion, Properties properties, List<Object> extensions) {
+    createBatch(sonarVersion, properties, extensions).execute();
   }
 
-  Batch createBatch(Properties properties, List<Object> extensions) {
+  Batch createBatch(String sonarVersion, Properties properties, List<Object> 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();
   }
 
index bdc6d795115a3c24b2a4179d3520f7f274045cf4..2c5e3f0d5a5814742c731a003f83d10d7d3f5ce6 100644 (file)
@@ -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);
index 8bf39f9b3312a4dc20a205085c81195a4b42121e..7037f00d93ed975ab33a082ef9384375e1e7a58d 100644 (file)
@@ -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 (file)
index 0000000..e26e918
--- /dev/null
@@ -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);
+  }
+}
index e3cc081609d7706848a46c2dcfa0dea5c3a10f63..fff4b14dcb2356cdf06809d86428d6faab47a700 100644 (file)
@@ -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();
   }
index a8db88bebb62a37dc3a65d7f434456ae73a89909..a8fe7b385907127e44bab18921f7c010e3cff372 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sonar Runner - Batch
+ * SonarQube Runner - Batch
  * Copyright (C) 2011 SonarSource
  * dev@sonar.codehaus.org
  *
index 21959d5d8c54354dd003a4d7608d70d6a0030194..2cf5b3af46643c2df886e70c707885753a0c9c12 100644 (file)
@@ -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 (file)
index 0000000..3838311
--- /dev/null
@@ -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();
+  }
+
+}
index b8ad0cf903590423a608e4e631949ea917ed84f6..e2abd3f4bf7c7c2d29ca18b4c3beb8acb19f0312 100644 (file)
@@ -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<Object> extensions) {
+  Object doExecute(final JarDownloader jarDownloader, final ServerVersion serverVersion, final Properties props, final List<Object> extensions) {
     Object launcher = AccessController.doPrivileged(new PrivilegedAction<Object>() {
       public Object run() {
         List<File> 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<Object> extensions) {
+      private Object delegateExecution(IsolatedClassloader classloader, String sonarVersion, Properties properties, List<Object> 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
index 1b32344f264c1dc50c777946affdf3964d8037fe..4a2e8c84c62322d60e1eee15d1fe0a6c851f82ad 100644 (file)
@@ -51,7 +51,7 @@ public class BatchLauncherTest {
     props.put(InternalProperties.RUNNER_MASK_RULES, "UNMASK|org.sonar.runner.impl.");
     List<Object> extensions = new ArrayList<Object>();
 
-    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<Object> 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<Object> extensions = null;
+    private String sonarVersion;
 
-    public void execute(Properties props, List<Object> extensions) {
+    public void execute(String sonarVersion, Properties props, List<Object> extensions) {
+      this.sonarVersion = sonarVersion;
       this.props = props;
       this.extensions = extensions;
     }