From e5818438a289fe522c87245d5bd18d71406f0e39 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 13 Jun 2016 17:12:39 +0200 Subject: SONAR-7755 deploy/plugins/index.txt should expose SonarLint compatibility --- .../src/main/java/org/sonar/api/Plugin.java | 20 +-- .../main/java/org/sonar/api/RuntimeApiVersion.java | 161 ----------------- .../src/main/java/org/sonar/api/SonarProduct.java | 31 ++++ .../src/main/java/org/sonar/api/SonarQubeSide.java | 32 ++++ .../main/java/org/sonar/api/SonarQubeVersion.java | 13 +- .../src/main/java/org/sonar/api/SonarRuntime.java | 190 +++++++++++++++++++++ .../org/sonar/api/batch/sensor/SensorContext.java | 5 +- .../batch/sensor/internal/SensorContextTester.java | 18 +- .../api/internal/RuntimeApiVersionFactory.java | 50 ------ .../sonar/api/internal/SonarRuntimeFactory.java | 53 ++++++ .../src/test/java/org/sonar/api/PluginTest.java | 4 +- .../java/org/sonar/api/RuntimeApiVersionTest.java | 45 ----- .../java/org/sonar/api/SonarQubeVersionTest.java | 3 +- .../test/java/org/sonar/api/SonarRuntimeTest.java | 73 ++++++++ .../api/internal/RuntimeApiVersionFactoryTest.java | 57 ------- .../api/internal/SonarRuntimeFactoryTest.java | 58 +++++++ 16 files changed, 471 insertions(+), 342 deletions(-) delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/RuntimeApiVersion.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/SonarProduct.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeSide.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/SonarRuntime.java delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/internal/RuntimeApiVersionFactory.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/internal/SonarRuntimeFactory.java delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/RuntimeApiVersionTest.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/SonarRuntimeTest.java delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/internal/RuntimeApiVersionFactoryTest.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/internal/SonarRuntimeFactoryTest.java (limited to 'sonar-plugin-api') diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java b/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java index aa1a6f9511a..8fb50a0b14c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java @@ -85,21 +85,19 @@ import static java.util.Objects.requireNonNull; public interface Plugin { class Context { - private final Version runtimeApiVersion; + private final SonarRuntime sonarRuntime; private final List extensions = new ArrayList(); - private final boolean sonarlintRuntime; - public Context(Version runtimeApiVersion, boolean sonarlintRuntime) { - this.runtimeApiVersion = runtimeApiVersion; - this.sonarlintRuntime = sonarlintRuntime; + public Context(SonarRuntime sonarRuntime) { + this.sonarRuntime = sonarRuntime; } /** - * @deprecated since 6.0 + * @deprecated since 6.0 use {@link #getRuntimeApiVersion()} */ @Deprecated public Version getSonarQubeVersion() { - return runtimeApiVersion; + return sonarRuntime.getApiVersion(); } /** @@ -107,7 +105,7 @@ public interface Plugin { * @since 6.0 */ public Version getRuntimeApiVersion() { - return runtimeApiVersion; + return sonarRuntime.getApiVersion(); } /** @@ -154,11 +152,11 @@ public interface Plugin { } /** - * Test if plugin is currently executed in SonarLint. Can be use to conditionnaly add some extensions. + * Test the product the plugin is currently executed in. This can allow to implement a different behavior. * @since 6.0 */ - public boolean isSonarlintRuntime() { - return sonarlintRuntime; + public SonarProduct getRuntimeProduct() { + return sonarRuntime.getProduct(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/RuntimeApiVersion.java b/sonar-plugin-api/src/main/java/org/sonar/api/RuntimeApiVersion.java deleted file mode 100644 index e221f6f9f8e..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/RuntimeApiVersion.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * 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 02110-1301, USA. - */ -package org.sonar.api; - -import javax.annotation.concurrent.Immutable; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.ce.ComputeEngineSide; -import org.sonar.api.server.ServerSide; -import org.sonar.api.utils.Version; - -import static java.util.Objects.requireNonNull; - -/** - * Version of SonarQube at runtime. This component can be injected as a dependency - * of plugin extensions. The main usage for a plugin is to benefit from new APIs - * while keeping backward-compatibility with previous versions of SonarQube. - *

- * - * Example 1: a {@link Sensor} wants to use an API introduced in version 5.5 and still requires to support older versions - * at runtime. - *
- * public class MySensor implements Sensor {
- *
- *   public void execute(SensorContext context) {
- *     if (context.getRuntimeApiVersion().isGreaterThanOrEqual(RuntimeApiVersion.V5_5)) {
- *       context.newMethodIntroducedIn5_5();
- *     }
- *   }
- * }
- * 
- * - * Example 2: a plugin needs to use an API introduced in version 5.6 ({@code AnApi} in the following - * snippet) and still requires to support version 5.5 at runtime. - *
- *
- * // Component provided by sonar-plugin-api
- * // @since 5.5
- * public interface AnApi {
- *   // implicitly since 5.5
- *   public void foo();
- *
- *   // @since 5.6
- *   public void bar();
- * }
- * 
- * // Component provided by plugin
- * public class MyExtension {
- *   private final RuntimeApiVersion runtimeApiVersion;
- *   private final AnApi api;
- *
- *   public MyExtension(RuntimeApiVersion runtimeApiVersion, AnApi api) {
- *     this.runtimeApiVersion = runtimeApiVersion;
- *     this.api = api;
- *   }
- *
- *   public void doSomething() {
- *     // assume that runtime is 5.5+
- *     api.foo();
- *
- *     if (runtimeApiVersion.isGreaterThanOrEqual(SonarQubeVersion.V5_6)) {
- *       api.bar();
- *     }
- *   }
- * }
- * 
- *

- * The minimal supported version of plugin API is verified at runtime. As plugin is built - * with sonar-plugin-api 5.6, we assume that the plugin requires v5.6 or greater at runtime. - * For this reason the plugin must default which is the minimal supported version - * in the configuration of sonar-packaging-maven-plugin 1.16+: - *

- *

- * <packaging>sonar-plugin</packaging>
- *
- * <dependencies>
- *   <dependency>
- *     <groupId>org.sonarsource.sonarqube</groupId>
- *     <artifactId>sonar-plugin-api</artifactId>
- *     <version>5.6</version>
- *     <scope>provided</scope>
- *   </dependency>
- * </dependencies>
- *
- * <build>
- *  <plugins>
- *    <plugin>
- *      <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
- *      <artifactId>sonar-packaging-maven-plugin</artifactId>
- *      <version>1.16</version>
- *      <extensions>true</extensions>
- *      <configuration>
- *        <!-- Override the default value 5.6 which is guessed from sonar-plugin-api dependency -->
- *        <sonarQubeMinVersion>5.5</sonarQubeMinVersion>
- *      </configuration>
- *    </plugin>
- *  </plugins>
- * </build>
- * 
- * - * - * @since 6.0 - */ -@BatchSide -@ServerSide -@ComputeEngineSide -@Immutable -public class RuntimeApiVersion { - - /** - * Constant for version 5.5 - */ - public static final Version V5_5 = Version.create(5, 5); - - /** - * Constant for version 5.6 - */ - public static final Version V5_6 = Version.create(5, 6); - - private final Version version; - private final boolean sonarlint; - - public RuntimeApiVersion(Version version, boolean sonarlint) { - requireNonNull(version); - this.version = version; - this.sonarlint = sonarlint; - } - - public Version get() { - return this.version; - } - - public boolean isGreaterThanOrEqual(Version than) { - return this.version.isGreaterThanOrEqual(than); - } - - /** - * @since 6.0 Test if current runtime is SonarLint. Can be used to implement a different behavior. - */ - public boolean isSonarlintRuntime() { - return sonarlint; - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/SonarProduct.java b/sonar-plugin-api/src/main/java/org/sonar/api/SonarProduct.java new file mode 100644 index 00000000000..97a5dba1e37 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/SonarProduct.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api; + +/** + * List of different products/runtimes. + * @since 6.0 + */ +public enum SonarProduct { + + SONARQUBE, + SONARLINT; + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeSide.java b/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeSide.java new file mode 100644 index 00000000000..5e02c2eae61 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeSide.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api; + +/** + * Differentiate runtime context in SonarQube product. + * @since 6.0 + */ +public enum SonarQubeSide { + + SCANNER, + SERVER, + COMPUTE_ENGINE; + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java b/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java index 6b8a1e5cdbd..044df52a450 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java @@ -19,6 +19,7 @@ */ package org.sonar.api; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import org.sonar.api.batch.ScannerSide; import org.sonar.api.batch.sensor.Sensor; @@ -115,17 +116,21 @@ import org.sonar.api.utils.Version; * * * @since 5.5 - * @deprecated since 6.0 replaced by {@link RuntimeApiVersion} + * @deprecated since 6.0 replaced by {@link SonarRuntime} */ @ScannerSide @ServerSide @ComputeEngineSide @Immutable @Deprecated -public class SonarQubeVersion extends RuntimeApiVersion { +public class SonarQubeVersion extends SonarRuntime { - public SonarQubeVersion(Version version, boolean sonarlint) { - super(version, sonarlint); + public SonarQubeVersion(Version version, SonarProduct product, @Nullable SonarQubeSide sonarQubeSide) { + super(version, product, sonarQubeSide); + } + + public Version get() { + return super.getApiVersion(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/SonarRuntime.java b/sonar-plugin-api/src/main/java/org/sonar/api/SonarRuntime.java new file mode 100644 index 00000000000..0760fb77244 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/SonarRuntime.java @@ -0,0 +1,190 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api; + +import com.google.common.base.Preconditions; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; +import org.sonar.api.batch.BatchSide; +import org.sonar.api.batch.sensor.Sensor; +import org.sonar.api.ce.ComputeEngineSide; +import org.sonar.api.server.ServerSide; +import org.sonar.api.utils.Version; +import org.sonarsource.api.sonarlint.SonarLintSide; + +import static java.util.Objects.requireNonNull; + +/** + * Version of SonarQube at runtime. This component can be injected as a dependency + * of plugin extensions. The main usage for a plugin is to benefit from new APIs + * while keeping backward-compatibility with previous versions of SonarQube. + *

+ * + * Example 1: a {@link Sensor} wants to use an API introduced in version 5.5 and still requires to support older versions + * at runtime. + *
+ * public class MySensor implements Sensor {
+ *
+ *   public void execute(SensorContext context) {
+ *     if (context.getRuntimeApiVersion().isGreaterThanOrEqual(RuntimeApiVersion.V5_5)) {
+ *       context.newMethodIntroducedIn5_5();
+ *     }
+ *   }
+ * }
+ * 
+ * + * Example 2: a plugin needs to use an API introduced in version 5.6 ({@code AnApi} in the following + * snippet) and still requires to support version 5.5 at runtime. + *
+ *
+ * // Component provided by sonar-plugin-api
+ * // @since 5.5
+ * public interface AnApi {
+ *   // implicitly since 5.5
+ *   public void foo();
+ *
+ *   // @since 5.6
+ *   public void bar();
+ * }
+ * 
+ * // Component provided by plugin
+ * public class MyExtension {
+ *   private final RuntimeApiVersion runtimeApiVersion;
+ *   private final AnApi api;
+ *
+ *   public MyExtension(RuntimeApiVersion runtimeApiVersion, AnApi api) {
+ *     this.runtimeApiVersion = runtimeApiVersion;
+ *     this.api = api;
+ *   }
+ *
+ *   public void doSomething() {
+ *     // assume that runtime is 5.5+
+ *     api.foo();
+ *
+ *     if (runtimeApiVersion.isGreaterThanOrEqual(SonarQubeVersion.V5_6)) {
+ *       api.bar();
+ *     }
+ *   }
+ * }
+ * 
+ *

+ * The minimal supported version of plugin API is verified at runtime. As plugin is built + * with sonar-plugin-api 5.6, we assume that the plugin requires v5.6 or greater at runtime. + * For this reason the plugin must default which is the minimal supported version + * in the configuration of sonar-packaging-maven-plugin 1.16+: + *

+ *

+ * <packaging>sonar-plugin</packaging>
+ *
+ * <dependencies>
+ *   <dependency>
+ *     <groupId>org.sonarsource.sonarqube</groupId>
+ *     <artifactId>sonar-plugin-api</artifactId>
+ *     <version>5.6</version>
+ *     <scope>provided</scope>
+ *   </dependency>
+ * </dependencies>
+ *
+ * <build>
+ *  <plugins>
+ *    <plugin>
+ *      <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
+ *      <artifactId>sonar-packaging-maven-plugin</artifactId>
+ *      <version>1.16</version>
+ *      <extensions>true</extensions>
+ *      <configuration>
+ *        <!-- Override the default value 5.6 which is guessed from sonar-plugin-api dependency -->
+ *        <sonarQubeMinVersion>5.5</sonarQubeMinVersion>
+ *      </configuration>
+ *    </plugin>
+ *  </plugins>
+ * </build>
+ * 
+ * + * + * @since 6.0 + */ +@BatchSide +@ServerSide +@ComputeEngineSide +@SonarLintSide +@Immutable +public class SonarRuntime { + + /** + * Constant for version 5.5 + */ + public static final Version V5_5 = Version.create(5, 5); + + /** + * Constant for version 5.6 + */ + public static final Version V5_6 = Version.create(5, 6); + + /** + * Constant for version 6.0 + */ + public static final Version V6_0 = Version.create(6, 0); + + private final Version version; + private final SonarProduct product; + private final SonarQubeSide sonarQubeSide; + + public SonarRuntime(Version version, SonarProduct product, @Nullable SonarQubeSide sonarQubeSide) { + requireNonNull(version); + requireNonNull(product); + Preconditions.checkArgument((product == SonarProduct.SONARQUBE) == (sonarQubeSide != null), "sonarQubeSide should be provided only for SonarQube product"); + this.version = version; + this.product = product; + this.sonarQubeSide = sonarQubeSide; + } + + /** + * Runtime version of sonar-plugin-api. This could be used to test if a new feature can be used or not without using reflection. + */ + public Version getApiVersion() { + return this.version; + } + + public boolean isGreaterThanOrEqual(Version than) { + return this.version.isGreaterThanOrEqual(than); + } + + /** + * Allow to know what is current runtime product. Can be used to implement different behavior depending on runtime (SonarQube, SonarLint, ...). + * @since 6.0 + */ + public SonarProduct getProduct() { + return product; + } + + /** + * Allow to know the precise runtime context in SonarQube product. Only valid when {@link #getProduct()} returns {@link SonarProduct#SONARQUBE} + * @since 6.0 + * @throws UnsupportedOperationException if called and {@link #getProduct()} is not equal to {@link SonarProduct#SONARQUBE} + */ + public SonarQubeSide getSonarQubeSide() { + if (sonarQubeSide == null) { + throw new UnsupportedOperationException("Can only be called in SonarQube"); + } + return sonarQubeSide; + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index 4970132e71a..22319fb4613 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -20,6 +20,7 @@ package org.sonar.api.batch.sensor; import java.io.Serializable; +import org.sonar.api.SonarProduct; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.rule.ActiveRules; @@ -75,10 +76,10 @@ public interface SensorContext { Version getRuntimeApiVersion(); /** - * Test if plugin is currently executed in SonarLint. This can allow to implement a different behavior. + * Test the product the plugin is currently executed in. This can allow to implement a different behavior. * @since 6.0 */ - boolean isSonarLintRuntime(); + SonarProduct getRuntimeProduct(); // ----------- MEASURES -------------- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 9c3c8476028..53a66952c17 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -30,6 +30,8 @@ import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.CheckForNull; +import org.sonar.api.SonarProduct; +import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarQubeVersion; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.TextRange; @@ -58,7 +60,7 @@ import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.batch.sensor.symbol.NewSymbolTable; import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; import org.sonar.api.config.Settings; -import org.sonar.api.internal.RuntimeApiVersionFactory; +import org.sonar.api.internal.SonarRuntimeFactory; import org.sonar.api.measures.Metric; import org.sonar.api.utils.System2; import org.sonar.api.utils.Version; @@ -95,7 +97,7 @@ public class SensorContextTester implements SensorContext { this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); this.module = new DefaultInputModule("projectKey"); - this.sqVersion = RuntimeApiVersionFactory.create(System2.INSTANCE, false); + this.sqVersion = SonarRuntimeFactory.create(System2.INSTANCE, SonarProduct.SONARQUBE, SonarQubeSide.SCANNER); } public static SensorContextTester create(File moduleBaseDir) { @@ -142,21 +144,21 @@ public class SensorContextTester implements SensorContext { */ @Override public Version getSonarQubeVersion() { - return sqVersion.get(); + return sqVersion.getApiVersion(); } @Override public Version getRuntimeApiVersion() { - return sqVersion.get(); + return sqVersion.getApiVersion(); } @Override - public boolean isSonarLintRuntime() { - return sqVersion.isSonarlintRuntime(); + public SonarProduct getRuntimeProduct() { + return sqVersion.getProduct(); } - public SensorContextTester setRuntime(Version version, boolean isSonarLint) { - this.sqVersion = new SonarQubeVersion(version, isSonarLint); + public SensorContextTester setRuntime(Version version, SonarProduct product, SonarQubeSide sonarQubeSide) { + this.sqVersion = new SonarQubeVersion(version, product, sonarQubeSide); return this; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/internal/RuntimeApiVersionFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/internal/RuntimeApiVersionFactory.java deleted file mode 100644 index fd098bd4a64..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/internal/RuntimeApiVersionFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * 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 02110-1301, USA. - */ -package org.sonar.api.internal; - -import com.google.common.io.Resources; -import java.io.IOException; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import org.sonar.api.SonarQubeVersion; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.Version; - -/** - * For internal use only. - */ -public class RuntimeApiVersionFactory { - - private static final String FILE_PATH = "/sq-version.txt"; - - private RuntimeApiVersionFactory() { - // prevents instantiation - } - - public static SonarQubeVersion create(System2 system, boolean isSonarLint) { - try { - URL url = system.getResource(FILE_PATH); - String versionInFile = Resources.toString(url, StandardCharsets.UTF_8); - return new SonarQubeVersion(Version.parse(versionInFile), isSonarLint); - } catch (IOException e) { - throw new IllegalStateException("Can not load " + FILE_PATH + " from classpath", e); - } - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/internal/SonarRuntimeFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/internal/SonarRuntimeFactory.java new file mode 100644 index 00000000000..b47189b58a8 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/internal/SonarRuntimeFactory.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api.internal; + +import com.google.common.io.Resources; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import javax.annotation.Nullable; +import org.sonar.api.SonarProduct; +import org.sonar.api.SonarQubeSide; +import org.sonar.api.SonarQubeVersion; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.Version; + +/** + * For internal use only. + */ +public class SonarRuntimeFactory { + + private static final String FILE_PATH = "/sq-version.txt"; + + private SonarRuntimeFactory() { + // prevents instantiation + } + + public static SonarQubeVersion create(System2 system, SonarProduct product, @Nullable SonarQubeSide sonarQubeSide) { + try { + URL url = system.getResource(FILE_PATH); + String versionInFile = Resources.toString(url, StandardCharsets.UTF_8); + return new SonarQubeVersion(Version.parse(versionInFile), product, sonarQubeSide); + } catch (IOException e) { + throw new IllegalStateException("Can not load " + FILE_PATH + " from classpath", e); + } + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java index 853a48bd59c..caa022c0290 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java @@ -23,13 +23,13 @@ import java.util.Arrays; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.api.RuntimeApiVersion.V5_5; +import static org.sonar.api.SonarRuntime.V5_5; public class PluginTest { @Test public void test_context() { - Plugin.Context context = new Plugin.Context(V5_5, true); + Plugin.Context context = new Plugin.Context(new SonarRuntime(V5_5, SonarProduct.SONARQUBE, SonarQubeSide.SERVER)); assertThat(context.getSonarQubeVersion()).isEqualTo(V5_5); assertThat(context.getExtensions()).isEmpty(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/RuntimeApiVersionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/RuntimeApiVersionTest.java deleted file mode 100644 index e7fb658e2eb..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/RuntimeApiVersionTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * 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 02110-1301, USA. - */ -package org.sonar.api; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.utils.Version; - -import static org.assertj.core.api.Assertions.assertThat; - -public class RuntimeApiVersionTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void isGte() { - Version version = Version.parse("1.2.3"); - RuntimeApiVersion apiVersion = new RuntimeApiVersion(version, false); - assertThat(apiVersion.get()).isEqualTo(version); - assertThat(apiVersion.isSonarlintRuntime()).isFalse(); - assertThat(apiVersion.isGreaterThanOrEqual(version)).isTrue(); - assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.1"))).isTrue(); - assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.3"))).isFalse(); - } - -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java index e5ef829db69..8ee63aa98fd 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java @@ -34,9 +34,8 @@ public class SonarQubeVersionTest { @Test public void isGte() { Version version = Version.parse("1.2.3"); - SonarQubeVersion qubeVersion = new SonarQubeVersion(version, true); + SonarQubeVersion qubeVersion = new SonarQubeVersion(version, SonarProduct.SONARLINT, null); assertThat(qubeVersion.get()).isEqualTo(version); - assertThat(qubeVersion.isSonarlintRuntime()).isTrue(); assertThat(qubeVersion.isGreaterThanOrEqual(version)).isTrue(); assertThat(qubeVersion.isGreaterThanOrEqual(Version.parse("1.1"))).isTrue(); assertThat(qubeVersion.isGreaterThanOrEqual(Version.parse("1.3"))).isFalse(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/SonarRuntimeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/SonarRuntimeTest.java new file mode 100644 index 00000000000..6d26a5678ce --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/SonarRuntimeTest.java @@ -0,0 +1,73 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api; + +import org.assertj.core.api.Assertions; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.Version; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SonarRuntimeTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void isGteInSQ() { + Version version = Version.parse("1.2.3"); + SonarRuntime apiVersion = new SonarRuntime(version, SonarProduct.SONARQUBE, SonarQubeSide.SCANNER); + assertThat(apiVersion.getApiVersion()).isEqualTo(version); + assertThat(apiVersion.getProduct()).isEqualTo(SonarProduct.SONARQUBE); + assertThat(apiVersion.getSonarQubeSide()).isEqualTo(SonarQubeSide.SCANNER); + assertThat(apiVersion.isGreaterThanOrEqual(version)).isTrue(); + assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.1"))).isTrue(); + assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.3"))).isFalse(); + } + + @Test + public void inSL() { + Version version = Version.parse("1.2.3"); + SonarRuntime apiVersion = new SonarRuntime(version, SonarProduct.SONARLINT, null); + assertThat(apiVersion.getApiVersion()).isEqualTo(version); + assertThat(apiVersion.getProduct()).isEqualTo(SonarProduct.SONARLINT); + assertThat(apiVersion.isGreaterThanOrEqual(version)).isTrue(); + assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.1"))).isTrue(); + assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.3"))).isFalse(); + try { + apiVersion.getSonarQubeSide(); + Assertions.fail("Expected exception"); + } catch (Exception e) { + assertThat(e).isInstanceOf(UnsupportedOperationException.class); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testConstructorMissSide() throws Exception { + new SonarRuntime(Version.parse("1.2.3"), SonarProduct.SONARQUBE, null); + } + + @Test(expected = IllegalArgumentException.class) + public void testConstructorNoSideOnSonarLint() throws Exception { + new SonarRuntime(Version.parse("1.2.3"), SonarProduct.SONARLINT, SonarQubeSide.COMPUTE_ENGINE); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/internal/RuntimeApiVersionFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/internal/RuntimeApiVersionFactoryTest.java deleted file mode 100644 index 69b3cca8c54..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/internal/RuntimeApiVersionFactoryTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * 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 02110-1301, USA. - */ -package org.sonar.api.internal; - -import java.io.File; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.SonarQubeVersion; -import org.sonar.api.utils.System2; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -public class RuntimeApiVersionFactoryTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void create() { - SonarQubeVersion version = RuntimeApiVersionFactory.create(System2.INSTANCE, true); - assertThat(version).isNotNull(); - assertThat(version.get().major()).isGreaterThanOrEqualTo(5); - assertThat(version.isSonarlintRuntime()).isTrue(); - } - - @Test - public void throw_ISE_if_fail_to_load_version() throws Exception { - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Can not load /sq-version.txt from classpath"); - - System2 system = spy(System2.class); - when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL()); - RuntimeApiVersionFactory.create(system, false); - } - -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/internal/SonarRuntimeFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/internal/SonarRuntimeFactoryTest.java new file mode 100644 index 00000000000..4db26447ca5 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/internal/SonarRuntimeFactoryTest.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api.internal; + +import java.io.File; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.SonarProduct; +import org.sonar.api.SonarQubeVersion; +import org.sonar.api.utils.System2; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class SonarRuntimeFactoryTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void create() { + SonarQubeVersion version = SonarRuntimeFactory.create(System2.INSTANCE, SonarProduct.SONARLINT, null); + assertThat(version).isNotNull(); + assertThat(version.getApiVersion().major()).isGreaterThanOrEqualTo(5); + assertThat(version.getProduct()).isEqualTo(SonarProduct.SONARLINT); + } + + @Test + public void throw_ISE_if_fail_to_load_version() throws Exception { + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can not load /sq-version.txt from classpath"); + + System2 system = spy(System2.class); + when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL()); + SonarRuntimeFactory.create(system, SonarProduct.SONARLINT, null); + } + +} -- cgit v1.2.3