]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16232 Use plugin api version for SonarRuntime impl
authorJacek <jacek.poreda@sonarsource.com>
Mon, 30 May 2022 16:50:42 +0000 (18:50 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 31 May 2022 20:02:50 +0000 (20:02 +0000)
- introduce internal SonarQubeVersion

27 files changed:
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/WriteMetadataStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/WriteMetadataStepTest.java
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/SQDatabase.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchema.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest.java
server/sonar-main/src/main/java/org/sonar/application/command/CommandFactoryImpl.java
server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerImpl.java
server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java
server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerImplTest.java
server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java
server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookCallerImplTest.java
server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginJarLoader.java
server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java
server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginJarLoaderTest.java
server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/UpdateCenterMatrixFactoryTest.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/LogServerVersion.java
server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersion.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionTest.java [new file with mode: 0644]
sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java
sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/SonarRuntimeImpl.java
sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringGlobalContainer.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/platform/DefaultServer.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/platform/DefaultServerTest.java

index 7c20196cc439583238293d9a04f654e8edee8c95..f7516b7de5fa068422904991d50811fab9fc2ad9 100644 (file)
 package org.sonar.ce.task.projectexport.steps;
 
 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.utils.System2;
 import org.sonar.ce.task.step.ComputationStep;
+import org.sonar.core.platform.SonarQubeVersion;
 
 public class WriteMetadataStep implements ComputationStep {
 
   private final System2 system2;
   private final DumpWriter dumpWriter;
   private final ProjectHolder projectHolder;
-  private final SonarRuntime sonarRuntime;
+  private final SonarQubeVersion sonarQubeVersion;
 
-  public WriteMetadataStep(System2 system2, DumpWriter dumpWriter, ProjectHolder projectHolder, SonarRuntime sonarRuntime) {
+  public WriteMetadataStep(System2 system2, DumpWriter dumpWriter, ProjectHolder projectHolder, SonarQubeVersion sonarQubeVersion) {
     this.system2 = system2;
     this.dumpWriter = dumpWriter;
     this.projectHolder = projectHolder;
-    this.sonarRuntime = sonarRuntime;
+    this.sonarQubeVersion = sonarQubeVersion;
   }
 
   @Override
@@ -43,7 +43,7 @@ public class WriteMetadataStep implements ComputationStep {
     dumpWriter.write(ProjectDump.Metadata.newBuilder()
       .setProjectKey(projectHolder.projectDto().getKey())
       .setProjectUuid(projectHolder.projectDto().getUuid())
-      .setSonarqubeVersion(sonarRuntime.getApiVersion().toString())
+      .setSonarqubeVersion(sonarQubeVersion.get().toString())
       .setDumpDate(system2.now())
       .build());
   }
index 20f09ee3c08c238fecd99f2feeec5fabd3027eb4..557ce07137fa6e4a56d7d5a561a798e7ccf51d81 100644 (file)
@@ -21,12 +21,10 @@ package org.sonar.ce.task.projectexport.steps;
 
 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
 import org.junit.Test;
-import org.sonar.api.SonarEdition;
-import org.sonar.api.SonarQubeSide;
-import org.sonar.api.internal.SonarRuntimeImpl;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.Version;
 import org.sonar.ce.task.step.TestComputationStepContext;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.db.project.ProjectDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -44,7 +42,7 @@ public class WriteMetadataStepTest {
   MutableProjectHolderImpl projectHolder = new MutableProjectHolderImpl();
   Version sqVersion = Version.create(6, 0);
   WriteMetadataStep underTest = new WriteMetadataStep(system2, dumpWriter, projectHolder,
-    SonarRuntimeImpl.forSonarQube(sqVersion, SonarQubeSide.SERVER, SonarEdition.COMMUNITY));
+      new SonarQubeVersion(sqVersion));
 
   @Test
   public void write_metadata() {
index 8193be28b6516bd7e758ef246a921112c2af0e71..3a30af5138a8422a2afe407fecec61eb03fa1f0c 100644 (file)
@@ -25,7 +25,6 @@ import java.util.List;
 import javax.annotation.CheckForNull;
 import org.sonar.api.SonarEdition;
 import org.sonar.api.SonarQubeSide;
-import org.sonar.api.SonarQubeVersion;
 import org.sonar.api.config.EmailSettings;
 import org.sonar.api.internal.MetadataLoader;
 import org.sonar.api.internal.SonarRuntimeImpl;
@@ -270,13 +269,15 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer {
   }
 
   private static void populateLevel1(Container container, Props props, ComputeEngineStatus computeEngineStatus) {
-    Version apiVersion = MetadataLoader.loadVersion(System2.INSTANCE);
+    Version apiVersion = MetadataLoader.loadApiVersion(System2.INSTANCE);
+    Version sqVersion = MetadataLoader.loadSQVersion(System2.INSTANCE);
     SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE);
     container.add(
       props.rawProperties(),
       ThreadLocalSettings.class,
       new ConfigurationProvider(),
-      new SonarQubeVersion(apiVersion),
+      new org.sonar.api.SonarQubeVersion(sqVersion),
+      new org.sonar.core.platform.SonarQubeVersion(sqVersion),
       SonarRuntimeImpl.forSonarQube(apiVersion, SonarQubeSide.COMPUTE_ENGINE, edition),
       CeProcessLogging.class,
       UuidFactoryImpl.INSTANCE,
index dc515bfcd4f6c4c3eb9c51b1f564bbe62cd80f61..9cee679c359168b342cd797aa51a5e4465362d9b 100644 (file)
@@ -27,15 +27,13 @@ import javax.sql.DataSource;
 import org.apache.commons.io.output.NullWriter;
 import org.apache.ibatis.io.Resources;
 import org.apache.ibatis.jdbc.ScriptRunner;
-import org.sonar.api.SonarEdition;
-import org.sonar.api.SonarQubeSide;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.config.internal.Settings;
-import org.sonar.api.internal.SonarRuntimeImpl;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.Version;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.core.platform.Container;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.core.platform.SpringComponentContainer;
 import org.sonar.core.util.UuidFactoryFast;
 import org.sonar.core.util.logs.Profiler;
@@ -147,7 +145,7 @@ public class SQDatabase extends DefaultDatabase {
     migrationConfigurationModule.configure(container);
 
     // dependencies required by DB migrations
-    container.add(SonarRuntimeImpl.forSonarQube(Version.create(8, 0), SonarQubeSide.SERVER, SonarEdition.COMMUNITY));
+    container.add(new SonarQubeVersion(Version.create(8, 0)));
     container.add(UuidFactoryFast.getInstance());
     container.add(System2.INSTANCE);
     container.add(MapSettings.class);
index 74cf229a2f780940bd99f8bc5b11c0dcff73015b..762a1c142136bea1753914def962b9cfeca48188 100644 (file)
@@ -23,8 +23,8 @@ import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.utils.System2;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
@@ -46,13 +46,13 @@ public class PopulateInitialSchema extends DataChange {
 
   private final System2 system2;
   private final UuidFactory uuidFactory;
-  private final SonarRuntime sonarRuntime;
+  private final SonarQubeVersion sonarQubeVersion;
 
-  public PopulateInitialSchema(Database db, System2 system2, UuidFactory uuidFactory, SonarRuntime sonarRuntime) {
+  public PopulateInitialSchema(Database db, System2 system2, UuidFactory uuidFactory, SonarQubeVersion sonarQubeVersion) {
     super(db);
     this.system2 = system2;
     this.uuidFactory = uuidFactory;
-    this.sonarRuntime = sonarRuntime;
+    this.sonarQubeVersion = sonarQubeVersion;
   }
 
   @Override
@@ -108,7 +108,7 @@ public class PopulateInitialSchema extends DataChange {
     upsert
       .setString(1, "installation.version")
       .setBoolean(2, false)
-      .setString(3, sonarRuntime.getApiVersion().toString())
+      .setString(3, sonarQubeVersion.get().toString())
       .setLong(4, now)
       .addBatch();
     upsert
index 81a8ff02d38e25b73242216056aa598824edb684..ad8bc02f549e48d9a32e679920be44d471a9c09b 100644 (file)
@@ -28,9 +28,9 @@ import java.util.stream.Stream;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.Version;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.core.util.UuidFactoryFast;
 import org.sonar.core.util.stream.MoreCollectors;
@@ -48,16 +48,16 @@ public class PopulateInitialSchemaTest {
   private final Version version = Version.create(1 + random.nextInt(10), 1 + random.nextInt(10), random.nextInt(10));
   private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
   private final System2 system2 = mock(System2.class);
-  private final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
+  private final SonarQubeVersion sonarQubeVersion = mock(SonarQubeVersion.class);
 
   @Rule
   public final CoreDbTester db = CoreDbTester.createForSchema(PopulateInitialSchemaTest.class, "v89.sql");
 
-  private final PopulateInitialSchema underTest = new PopulateInitialSchema(db.database(), system2, uuidFactory, sonarRuntime);
+  private final PopulateInitialSchema underTest = new PopulateInitialSchema(db.database(), system2, uuidFactory, sonarQubeVersion);
 
   @Before
   public void setUp() {
-    when(sonarRuntime.getApiVersion()).thenReturn(version);
+    when(sonarQubeVersion.get()).thenReturn(version);
   }
 
   @Test
index c28f501eac370e96ffee82cb929c2e874511eb13..a613120d69447afc1db3df42fa4d190e243d16aa 100644 (file)
@@ -71,7 +71,7 @@ public class CommandFactoryImpl implements CommandFactory {
     SOCKS_PROXY_HOST.getKey(),
     SOCKS_PROXY_PORT.getKey()};
 
-  private static final Version SQ_VERSION = MetadataLoader.loadVersion(org.sonar.api.utils.System2.INSTANCE);
+  private static final Version SQ_VERSION = MetadataLoader.loadSQVersion(org.sonar.api.utils.System2.INSTANCE);
   private final Props props;
   private final File tempDir;
   private final System2 system2;
index df10f62ee9504c701842787191a6b07c43924e60..3b493e0ae60a7c37f280bef459cbc9b0071c0cd8 100644 (file)
@@ -22,11 +22,11 @@ package org.sonar.server.platform;
 import java.util.Date;
 import javax.annotation.CheckForNull;
 import org.sonar.api.CoreProperties;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.ce.ComputeEngineSide;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.platform.Server;
 import org.sonar.api.server.ServerSide;
+import org.sonar.core.platform.SonarQubeVersion;
 
 @ComputeEngineSide
 @ServerSide
@@ -34,13 +34,13 @@ public class ServerImpl extends Server {
   private final Configuration config;
   private final StartupMetadata state;
   private final UrlSettings urlSettings;
-  private final SonarRuntime runtime;
+  private final SonarQubeVersion version;
 
-  public ServerImpl(Configuration config, StartupMetadata state, UrlSettings urlSettings, SonarRuntime runtime) {
+  public ServerImpl(Configuration config, StartupMetadata state, UrlSettings urlSettings, SonarQubeVersion version) {
     this.config = config;
     this.state = state;
     this.urlSettings = urlSettings;
-    this.runtime = runtime;
+    this.version = version;
   }
 
   /**
@@ -59,7 +59,7 @@ public class ServerImpl extends Server {
 
   @Override
   public String getVersion() {
-    return runtime.getApiVersion().toString();
+    return version.get().toString();
   }
 
   @Override
index 3a50f78a28e8a95574011117f7380108a9bf74a5..487e555d67753cfc26b85f26ac1f932840230797 100644 (file)
 package org.sonar.server.util;
 
 import okhttp3.OkHttpClient;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.ce.ComputeEngineSide;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.server.ServerSide;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonarqube.ws.client.OkHttpClientBuilder;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Primary;
@@ -54,7 +54,7 @@ public class OkHttpClientProvider {
    */
   @Primary
   @Bean("OkHttpClient")
-  public OkHttpClient provide(Configuration config, SonarRuntime runtime) {
+  public OkHttpClient provide(Configuration config, SonarQubeVersion version) {
     OkHttpClientBuilder builder = new OkHttpClientBuilder();
     builder.setConnectTimeoutMs(DEFAULT_CONNECT_TIMEOUT_IN_MS);
     builder.setReadTimeoutMs(DEFAULT_READ_TIMEOUT_IN_MS);
@@ -62,7 +62,7 @@ public class OkHttpClientProvider {
     // configured by bootstrap process.
     builder.setProxyLogin(config.get(HTTP_PROXY_USER.getKey()).orElse(null));
     builder.setProxyPassword(config.get(HTTP_PROXY_PASSWORD.getKey()).orElse(null));
-    builder.setUserAgent(format("SonarQube/%s", runtime.getApiVersion().toString()));
+    builder.setUserAgent(format("SonarQube/%s", version));
     return builder.build();
   }
 }
index 6c1650191d26d2795b12c199994fe83ee55efe6a..a13376863e5ed28f11c364ea2814cbc2101d7083 100644 (file)
@@ -21,20 +21,20 @@ package org.sonar.server.platform;
 
 import org.junit.Test;
 import org.sonar.api.CoreProperties;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.utils.Version;
+import org.sonar.core.platform.SonarQubeVersion;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public class ServerImplTest {
-  private MapSettings settings = new MapSettings();
-  private StartupMetadata state = mock(StartupMetadata.class);
-  private UrlSettings urlSettings = mock(UrlSettings.class);
-  private SonarRuntime runtime = mock(SonarRuntime.class);
-  private ServerImpl underTest = new ServerImpl(settings.asConfig(), state, urlSettings, runtime);
+  private final MapSettings settings = new MapSettings();
+  private final StartupMetadata state = mock(StartupMetadata.class);
+  private final UrlSettings urlSettings = mock(UrlSettings.class);
+  private final SonarQubeVersion sonarQubeVersion = mock(SonarQubeVersion.class);
+  private final ServerImpl underTest = new ServerImpl(settings.asConfig(), state, urlSettings, sonarQubeVersion);
 
   @Test
   public void test_url_information() {
@@ -66,7 +66,7 @@ public class ServerImplTest {
   @Test
   public void test_getVersion() {
     Version version = Version.create(6, 1);
-    when(runtime.getApiVersion()).thenReturn(version);
+    when(sonarQubeVersion.get()).thenReturn(version);
 
     assertThat(underTest.getVersion()).isEqualTo(version.toString());
   }
index 01879d1053a6d624a66810f2d41e5bdbf8bf9e54..98fffedd18cf44ad849f1d93e80efe41769c1c3d 100644 (file)
@@ -30,19 +30,16 @@ import okhttp3.mockwebserver.MockWebServer;
 import okhttp3.mockwebserver.RecordedRequest;
 import org.junit.Rule;
 import org.junit.Test;
-import org.sonar.api.SonarEdition;
-import org.sonar.api.SonarQubeSide;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.config.internal.MapSettings;
-import org.sonar.api.internal.SonarRuntimeImpl;
 import org.sonar.api.utils.Version;
+import org.sonar.core.platform.SonarQubeVersion;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class OkHttpClientProviderTest {
 
   private final MapSettings settings = new MapSettings();
-  private final SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("6.2"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY);
+  private final SonarQubeVersion sonarQubeVersion = new SonarQubeVersion(Version.parse("6.2"));
   private final OkHttpClientProvider underTest = new OkHttpClientProvider();
 
   @Rule
@@ -50,7 +47,7 @@ public class OkHttpClientProviderTest {
 
   @Test
   public void get_returns_a_OkHttpClient_with_default_configuration() throws Exception {
-    OkHttpClient client = underTest.provide(settings.asConfig(), runtime);
+    OkHttpClient client = underTest.provide(settings.asConfig(), sonarQubeVersion);
 
     assertThat(client.connectTimeoutMillis()).isEqualTo(10_000);
     assertThat(client.readTimeoutMillis()).isEqualTo(10_000);
@@ -66,7 +63,7 @@ public class OkHttpClientProviderTest {
     settings.setProperty("http.proxyUser", "the-login");
     settings.setProperty("http.proxyPassword", "the-password");
 
-    OkHttpClient client = underTest.provide(settings.asConfig(), runtime);
+    OkHttpClient client = underTest.provide(settings.asConfig(), sonarQubeVersion);
     Response response = new Response.Builder().protocol(Protocol.HTTP_1_1).request(new Request.Builder().url("http://foo").build()).code(407)
       .message("").build();
     Request request = client.proxyAuthenticator().authenticate(null, response);
index 4aa96fba64af7629cc1e7527bd79805a4f661c3a..a71d371cfc6aa477d2a1741ee492904bff86ec1a 100644 (file)
@@ -33,15 +33,12 @@ import org.junit.rules.DisableOnDebug;
 import org.junit.rules.TestRule;
 import org.junit.rules.Timeout;
 import org.mockito.Mockito;
-import org.sonar.api.SonarEdition;
-import org.sonar.api.SonarQubeSide;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.impl.utils.TestSystem2;
-import org.sonar.api.internal.SonarRuntimeImpl;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.Version;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.server.util.OkHttpClientProvider;
 
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
@@ -284,10 +281,10 @@ public class WebhookCallerImplTest {
   }
 
   private WebhookCaller newSender(boolean validateWebhook) {
-    SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("6.2"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY);
+    SonarQubeVersion version = new SonarQubeVersion(Version.parse("6.2"));
     when(configuration.getBoolean(SONAR_VALIDATE_WEBHOOKS_PROPERTY))
       .thenReturn(Optional.of(validateWebhook));
     WebhookCustomDns webhookCustomDns = new WebhookCustomDns(configuration, networkInterfaceProvider);
-    return new WebhookCallerImpl(system, new OkHttpClientProvider().provide(new MapSettings().asConfig(), runtime), webhookCustomDns);
+    return new WebhookCallerImpl(system, new OkHttpClientProvider().provide(new MapSettings().asConfig(), version), webhookCustomDns);
   }
 }
index a39998be1321b6cee10597927824274e3b6a560c..a7402600556395d5bda526ba230113e6984d9629 100644 (file)
@@ -36,11 +36,11 @@ import java.util.function.Function;
 import java.util.stream.Collectors;
 import javax.inject.Inject;
 import org.apache.commons.io.FileUtils;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.core.platform.PluginInfo;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.server.platform.ServerFileSystem;
 import org.sonar.updatecenter.common.Version;
 
@@ -63,17 +63,17 @@ public class PluginJarLoader {
   private static final String LOAD_ERROR_GENERIC_MESSAGE = "Startup failed: Plugins can't be loaded. See web logs for more information";
 
   private final ServerFileSystem fs;
-  private final SonarRuntime runtime;
+  private final SonarQubeVersion sonarQubeVersion;
   private final Set<String> blacklistedPluginKeys;
 
   @Inject
-  public PluginJarLoader(ServerFileSystem fs, SonarRuntime runtime) {
-    this(fs, runtime, DEFAULT_BLACKLISTED_PLUGINS);
+  public PluginJarLoader(ServerFileSystem fs, SonarQubeVersion sonarQubeVersion) {
+    this(fs, sonarQubeVersion, DEFAULT_BLACKLISTED_PLUGINS);
   }
 
-  PluginJarLoader(ServerFileSystem fs, SonarRuntime runtime, Set<String> blacklistedPluginKeys) {
+  PluginJarLoader(ServerFileSystem fs, SonarQubeVersion sonarQubeVersion, Set<String> blacklistedPluginKeys) {
     this.fs = fs;
-    this.runtime = runtime;
+    this.sonarQubeVersion = sonarQubeVersion;
     this.blacklistedPluginKeys = blacklistedPluginKeys;
   }
 
@@ -261,7 +261,7 @@ public class PluginJarLoader {
       return false;
     }
 
-    if (!info.isCompatibleWith(runtime.getApiVersion().toString())) {
+    if (!info.isCompatibleWith(sonarQubeVersion.get().toString())) {
       throw MessageException.of(format("Plugin %s [%s] requires at least SonarQube %s", info.getName(), info.getKey(), info.getMinimalSqVersion()));
     }
     return true;
index d9c6262e58bf3eb4689417230978c7dc7094af5c..a7c40bc165963dbf0cde91c3fe19406e783dc98b 100644 (file)
@@ -20,7 +20,7 @@
 package org.sonar.server.plugins;
 
 import java.util.Optional;
-import org.sonar.api.SonarRuntime;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.updatecenter.common.UpdateCenter;
 import org.sonar.updatecenter.common.Version;
 
@@ -30,20 +30,20 @@ import org.sonar.updatecenter.common.Version;
 public class UpdateCenterMatrixFactory {
 
   private final UpdateCenterClient centerClient;
-  private final SonarRuntime sonarRuntime;
+  private final SonarQubeVersion sonarQubeVersion;
   private final InstalledPluginReferentialFactory installedPluginReferentialFactory;
 
-  public UpdateCenterMatrixFactory(UpdateCenterClient centerClient, SonarRuntime runtime,
+  public UpdateCenterMatrixFactory(UpdateCenterClient centerClient, SonarQubeVersion sonarQubeVersion,
     InstalledPluginReferentialFactory installedPluginReferentialFactory) {
     this.centerClient = centerClient;
-    this.sonarRuntime = runtime;
+    this.sonarQubeVersion = sonarQubeVersion;
     this.installedPluginReferentialFactory = installedPluginReferentialFactory;
   }
 
   public Optional<UpdateCenter> getUpdateCenter(boolean refreshUpdateCenter) {
     Optional<UpdateCenter> updateCenter = centerClient.getUpdateCenter(refreshUpdateCenter);
     if (updateCenter.isPresent()) {
-      org.sonar.api.utils.Version fullVersion = sonarRuntime.getApiVersion();
+      org.sonar.api.utils.Version fullVersion = sonarQubeVersion.get();
       org.sonar.api.utils.Version semanticVersion = org.sonar.api.utils.Version.create(fullVersion.major(), fullVersion.minor(), fullVersion.patch());
 
       return Optional.of(updateCenter.get().setInstalledSonarVersion(Version.create(semanticVersion.toString())).registerInstalledPlugins(
index a5105eefd6babef32a76e6966847f809fb2debbb..c8be970cc3d16c1a6ea5966554478167d659d00b 100644 (file)
@@ -35,10 +35,10 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.log.LogTester;
 import org.sonar.core.platform.PluginInfo;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.server.platform.ServerFileSystem;
 import org.sonar.updatecenter.common.PluginManifest;
 
@@ -54,14 +54,14 @@ public class PluginJarLoaderTest {
   @Rule
   public LogTester logs = new LogTester();
 
-  private ServerFileSystem fs = mock(ServerFileSystem.class);
-  private Set<String> blacklisted = new HashSet<>();
-  private SonarRuntime runtime = mock(SonarRuntime.class);
-  private PluginJarLoader underTest = new PluginJarLoader(fs, runtime, blacklisted);
+  private final ServerFileSystem fs = mock(ServerFileSystem.class);
+  private final Set<String> blacklisted = new HashSet<>();
+  private final SonarQubeVersion sonarQubeVersion = mock(SonarQubeVersion.class);
+  private final PluginJarLoader underTest = new PluginJarLoader(fs, sonarQubeVersion, blacklisted);
 
   @Before
   public void setUp() throws IOException {
-    when(runtime.getApiVersion()).thenReturn(org.sonar.api.utils.Version.parse("5.2"));
+    when(sonarQubeVersion.get()).thenReturn(org.sonar.api.utils.Version.parse("5.2"));
     when(fs.getDeployedPluginsDir()).thenReturn(temp.newFolder("deployed"));
     when(fs.getDownloadedPluginsDir()).thenReturn(temp.newFolder("downloaded"));
     when(fs.getHomeDir()).thenReturn(temp.newFolder("home"));
@@ -270,7 +270,7 @@ public class PluginJarLoaderTest {
   public void fail_when_report_is_installed() throws Exception {
     copyTestPluginTo("fake-report-plugin", fs.getInstalledExternalPluginsDir());
 
-    assertThatThrownBy(() ->  underTest.loadPlugins())
+    assertThatThrownBy(() -> underTest.loadPlugins())
       .isInstanceOf(MessageException.class)
       .hasMessage("The following plugin is no longer compatible with this version of SonarQube: 'report'");
   }
@@ -286,7 +286,7 @@ public class PluginJarLoaderTest {
 
   @Test
   public void fail_if_plugin_does_not_support_sq_version() throws Exception {
-    when(runtime.getApiVersion()).thenReturn(org.sonar.api.utils.Version.parse("1.0"));
+    when(sonarQubeVersion.get()).thenReturn(org.sonar.api.utils.Version.parse("1.0"));
     copyTestPluginTo("test-base-plugin", fs.getInstalledExternalPluginsDir());
 
     assertThatThrownBy(() -> underTest.loadPlugins())
index 56865125f6c6004a67db1a4f22d4fd745462872e..a3992ff03d7aa1997b3f5797acecaf76a1764d08 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.plugins;
 
 import java.util.Optional;
 import org.junit.Test;
-import org.sonar.api.SonarRuntime;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.updatecenter.common.UpdateCenter;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -38,7 +38,7 @@ public class UpdateCenterMatrixFactoryTest {
     UpdateCenterClient updateCenterClient = mock(UpdateCenterClient.class);
     when(updateCenterClient.getUpdateCenter(anyBoolean())).thenReturn(Optional.empty());
 
-    underTest = new UpdateCenterMatrixFactory(updateCenterClient, mock(SonarRuntime.class), mock(InstalledPluginReferentialFactory.class));
+    underTest = new UpdateCenterMatrixFactory(updateCenterClient, mock(SonarQubeVersion.class), mock(InstalledPluginReferentialFactory.class));
 
     Optional<UpdateCenter> updateCenter = underTest.getUpdateCenter(false);
 
index c9228750aabfd7e404018f4243339819eaea760c..abf95fbb6f97190cb9298fe387ea9ce9d15878de 100644 (file)
@@ -23,27 +23,27 @@ import com.google.common.base.Joiner;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.Startable;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.Version;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.platform.SonarQubeVersion;
 
 @ServerSide
 public class LogServerVersion implements Startable {
 
   private static final Logger LOG = Loggers.get(LogServerVersion.class);
-  private final SonarRuntime runtime;
+  private final SonarQubeVersion sonarQubeVersion;
 
-  public LogServerVersion(SonarRuntime runtime) {
-    this.runtime = runtime;
+  public LogServerVersion(SonarQubeVersion sonarQubeVersion) {
+    this.sonarQubeVersion = sonarQubeVersion;
   }
 
   @Override
   public void start() {
     String scmRevision = read("/build.properties").getProperty("Implementation-Build");
-    Version version = runtime.getApiVersion();
+    Version version = sonarQubeVersion.get();
     LOG.info("SonarQube {}", Joiner.on(" / ").skipNulls().join("Server", version, scmRevision));
   }
 
index f3acfaccdcbc6fb1595cc4c2369659e40c77f2c4..89ffe5944ec5778177707896d3af1d7d4910a3ae 100644 (file)
@@ -23,7 +23,6 @@ import java.time.Clock;
 import java.util.Properties;
 import org.sonar.api.SonarEdition;
 import org.sonar.api.SonarQubeSide;
-import org.sonar.api.SonarQubeVersion;
 import org.sonar.api.internal.MetadataLoader;
 import org.sonar.api.internal.SonarRuntimeImpl;
 import org.sonar.api.utils.System2;
@@ -88,11 +87,13 @@ public class PlatformLevel1 extends PlatformLevel {
   public void configureLevel() {
     add(platform, properties);
     addExtraRootComponents();
-    Version apiVersion = MetadataLoader.loadVersion(System2.INSTANCE);
+    Version apiVersion = MetadataLoader.loadApiVersion(System2.INSTANCE);
+    Version sqVersion = MetadataLoader.loadSQVersion(System2.INSTANCE);
     SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE);
 
     add(
-      new SonarQubeVersion(apiVersion),
+      new org.sonar.api.SonarQubeVersion(sqVersion),
+      new org.sonar.core.platform.SonarQubeVersion(sqVersion),
       SonarRuntimeImpl.forSonarQube(apiVersion, SonarQubeSide.SERVER, edition),
       ThreadLocalSettings.class,
       ConfigurationProvider.class,
diff --git a/sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersion.java b/sonar-core/src/main/java/org/sonar/core/platform/SonarQubeVersion.java
new file mode 100644 (file)
index 0000000..322fcc3
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info 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.core.platform;
+
+import javax.annotation.concurrent.Immutable;
+import org.sonar.api.ce.ComputeEngineSide;
+import org.sonar.api.scanner.ScannerSide;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.Version;
+
+import static java.util.Objects.requireNonNull;
+
+@ScannerSide
+@ServerSide
+@ComputeEngineSide
+@Immutable
+public class SonarQubeVersion {
+
+  private final Version version;
+
+  public SonarQubeVersion(Version version) {
+    requireNonNull(version);
+    this.version = version;
+  }
+
+  public Version get() {
+    return this.version;
+  }
+
+  public boolean isGreaterThanOrEqual(Version than) {
+    return this.version.isGreaterThanOrEqual(than);
+  }
+
+  @Override
+  public String toString() {
+    return version.toString();
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionTest.java b/sonar-core/src/test/java/org/sonar/core/platform/SonarQubeVersionTest.java
new file mode 100644 (file)
index 0000000..5a457f0
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info 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.core.platform;
+
+import org.junit.Test;
+import org.sonar.api.utils.Version;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SonarQubeVersionTest {
+
+  @Test
+  public void verify_methods() {
+    var version = Version.create(9, 5);
+    SonarQubeVersion underTest = new SonarQubeVersion(version);
+    assertThat(underTest).extracting(SonarQubeVersion::toString, SonarQubeVersion::get)
+      .containsExactly("9.5", version);
+
+    var otherVersion = Version.create(8, 5);
+    assertThat(underTest.isGreaterThanOrEqual(otherVersion)).isTrue();
+  }
+}
index dc5acf17136a0dd99d1763c53bdff8f76bafa467..98b48f1238241f1e8efb96a761e49d6b94992e3d 100644 (file)
@@ -125,7 +125,7 @@ public class SensorContextTester implements SensorContext {
     this.sensorStorage = new InMemorySensorStorage();
     this.project = new DefaultInputProject(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile()));
     this.module = new DefaultInputModule(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile()));
-    this.runtime = SonarRuntimeImpl.forSonarQube(MetadataLoader.loadVersion(System2.INSTANCE), SonarQubeSide.SCANNER, MetadataLoader.loadEdition(System2.INSTANCE));
+    this.runtime = SonarRuntimeImpl.forSonarQube(MetadataLoader.loadApiVersion(System2.INSTANCE), SonarQubeSide.SCANNER, MetadataLoader.loadEdition(System2.INSTANCE));
   }
 
   public static SensorContextTester create(File moduleBaseDir) {
index 33dae76c3f8703c69d90d1ea020fff6203245fb4..77fe9cc89b5e12c828dfbe474258c54045fb4025 100644 (file)
@@ -38,20 +38,28 @@ import static org.apache.commons.lang.StringUtils.trimToEmpty;
 public class MetadataLoader {
 
   private static final String SQ_VERSION_FILE_PATH = "/sq-version.txt";
+  private static final String SONAR_API_VERSION_FILE_PATH = "/sonar-api-version.txt";
   private static final String EDITION_FILE_PATH = "/sonar-edition.txt";
 
   private MetadataLoader() {
     // only static methods
   }
 
-  public static Version loadVersion(System2 system) {
-    URL url = system.getResource(SQ_VERSION_FILE_PATH);
+  public static Version loadApiVersion(System2 system) {
+    return getVersion(system, SONAR_API_VERSION_FILE_PATH);
+  }
+  public static Version loadSQVersion(System2 system) {
+    return getVersion(system, SQ_VERSION_FILE_PATH);
+  }
+
+  private static Version getVersion(System2 system, String versionFilePath) {
+    URL url = system.getResource(versionFilePath);
 
     try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.name())) {
       String versionInFile = scanner.nextLine();
       return Version.parse(versionInFile);
     } catch (IOException e) {
-      throw new IllegalStateException("Can not load " + SQ_VERSION_FILE_PATH + " from classpath ", e);
+      throw new IllegalStateException("Can not load " + versionFilePath + " from classpath ", e);
     }
   }
 
index 0b1a9b51225b82850bf9ac20ca031bd22027d1cf..051b567bc54b57e4caba4476e5f80956250a9f8e 100644 (file)
@@ -36,24 +36,24 @@ import static org.sonar.api.utils.Preconditions.checkArgument;
 @Immutable
 public class SonarRuntimeImpl implements SonarRuntime {
 
-  private final Version version;
+  private final Version apiVersion;
   private final SonarProduct product;
   private final SonarQubeSide sonarQubeSide;
   private final SonarEdition edition;
 
-  private SonarRuntimeImpl(Version version, SonarProduct product, @Nullable SonarQubeSide sonarQubeSide, @Nullable SonarEdition edition) {
+  private SonarRuntimeImpl(Version apiVersion, SonarProduct product, @Nullable SonarQubeSide sonarQubeSide, @Nullable SonarEdition edition) {
     this.edition = edition;
     requireNonNull(product);
     checkArgument((product == SonarProduct.SONARQUBE) == (sonarQubeSide != null), "sonarQubeSide should be provided only for SonarQube product");
     checkArgument((product == SonarProduct.SONARQUBE) == (edition != null), "edition should be provided only for SonarQube product");
-    this.version = requireNonNull(version);
+    this.apiVersion = requireNonNull(apiVersion);
     this.product = product;
     this.sonarQubeSide = sonarQubeSide;
   }
 
   @Override
   public Version getApiVersion() {
-    return version;
+    return apiVersion;
   }
 
   @Override
index e7f1e23021d5bbbb943c7d5b6f8ae051dda57b1e..fc340b90d87a816efd41378423b04e7664eb7f32 100644 (file)
@@ -33,11 +33,18 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public class MetadataLoaderTest {
-  private System2 system = mock(System2.class);
+  private final System2 system = mock(System2.class);
 
   @Test
-  public void load_version_from_file_in_classpath() {
-    Version version = MetadataLoader.loadVersion(System2.INSTANCE);
+  public void load_api_version_from_file_in_classpath() {
+    Version version = MetadataLoader.loadApiVersion(System2.INSTANCE);
+    assertThat(version).isNotNull();
+    assertThat(version.major()).isGreaterThanOrEqualTo(5);
+  }
+
+  @Test
+  public void load_sq_version_from_file_in_classpath() {
+    Version version = MetadataLoader.loadSQVersion(System2.INSTANCE);
     assertThat(version).isNotNull();
     assertThat(version.major()).isGreaterThanOrEqualTo(5);
   }
@@ -66,9 +73,9 @@ public class MetadataLoaderTest {
   public void throw_ISE_if_fail_to_load_version() throws Exception {
     when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL());
 
-    assertThatThrownBy(() -> MetadataLoader.loadVersion(system))
+    assertThatThrownBy(() -> MetadataLoader.loadApiVersion(system))
       .isInstanceOf(IllegalStateException.class)
-      .hasMessageContaining("Can not load /sq-version.txt from classpath");
+      .hasMessageContaining("Can not load /sonar-api-version.txt from classpath");
   }
 
 }
index 4e85fc6c1cc4c8ee2be28498bac4900d25a0006f..dca880238dc6baeb9c87afaba7e26b9e07103ba1 100644 (file)
@@ -28,7 +28,6 @@ import org.sonar.api.CoreProperties;
 import org.sonar.api.Plugin;
 import org.sonar.api.SonarEdition;
 import org.sonar.api.SonarQubeSide;
-import org.sonar.api.SonarQubeVersion;
 import org.sonar.api.internal.MetadataLoader;
 import org.sonar.api.internal.SonarRuntimeImpl;
 import org.sonar.api.utils.MessageException;
@@ -79,10 +78,11 @@ public class SpringGlobalContainer extends SpringComponentContainer {
   }
 
   private void addBootstrapComponents() {
-    Version apiVersion = MetadataLoader.loadVersion(System2.INSTANCE);
+    Version apiVersion = MetadataLoader.loadApiVersion(System2.INSTANCE);
+    Version sqVersion = MetadataLoader.loadSQVersion(System2.INSTANCE);
     SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE);
     DefaultAnalysisWarnings analysisWarnings = new DefaultAnalysisWarnings(System2.INSTANCE);
-    LOG.debug("{} {}", edition.getLabel(), apiVersion);
+    LOG.debug("{} {}", edition.getLabel(), sqVersion);
     add(
       // plugins
       ScannerPluginRepository.class,
@@ -90,7 +90,8 @@ public class SpringGlobalContainer extends SpringComponentContainer {
       PluginClassloaderFactory.class,
       ScannerPluginJarExploder.class,
       ExtensionInstaller.class,
-      new SonarQubeVersion(apiVersion),
+      new org.sonar.api.SonarQubeVersion(sqVersion),
+      new org.sonar.core.platform.SonarQubeVersion(sqVersion),
       new GlobalServerSettingsProvider(),
       new GlobalConfigurationProvider(),
       new ScannerWsClientProvider(),
index e3b871d37c826bcf8ea7201840d09cd06a499429..2d22dc268884883a29fb9b27001f51ad64b4d2a1 100644 (file)
@@ -22,10 +22,10 @@ package org.sonar.scanner.platform;
 import java.util.Date;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.CoreProperties;
-import org.sonar.api.SonarRuntime;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.platform.Server;
 import org.sonar.api.utils.DateUtils;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
 
 import static org.apache.commons.lang.StringUtils.trimToEmpty;
@@ -34,12 +34,12 @@ public class DefaultServer extends Server {
 
   private final Configuration settings;
   private final DefaultScannerWsClient client;
-  private final SonarRuntime runtime;
+  private final SonarQubeVersion sonarQubeVersion;
 
-  public DefaultServer(Configuration settings, DefaultScannerWsClient client, SonarRuntime runtime) {
+  public DefaultServer(Configuration settings, DefaultScannerWsClient client, SonarQubeVersion sonarQubeVersion) {
     this.settings = settings;
     this.client = client;
-    this.runtime = runtime;
+    this.sonarQubeVersion = sonarQubeVersion;
   }
 
   @Override
@@ -49,7 +49,7 @@ public class DefaultServer extends Server {
 
   @Override
   public String getVersion() {
-    return runtime.getApiVersion().toString();
+    return sonarQubeVersion.get().toString();
   }
 
   @Override
index c8979538a88e9413442cd0332a147d37c5d3d14b..ee676ebbc0eee2e3fa231cff93321acf206097e5 100644 (file)
@@ -21,12 +21,9 @@ package org.sonar.scanner.platform;
 
 import org.junit.Test;
 import org.sonar.api.CoreProperties;
-import org.sonar.api.SonarEdition;
-import org.sonar.api.SonarQubeSide;
 import org.sonar.api.config.internal.MapSettings;
-import org.sonar.api.config.internal.Settings;
-import org.sonar.api.internal.SonarRuntimeImpl;
 import org.sonar.api.utils.Version;
+import org.sonar.core.platform.SonarQubeVersion;
 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -43,8 +40,7 @@ public class DefaultServerTest {
     DefaultScannerWsClient client = mock(DefaultScannerWsClient.class);
     when(client.baseUrl()).thenReturn("http://foo.com");
 
-    DefaultServer metadata = new DefaultServer((settings).asConfig(), client,
-      SonarRuntimeImpl.forSonarQube(Version.parse("2.2"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
+    DefaultServer metadata = new DefaultServer((settings).asConfig(), client, new SonarQubeVersion(Version.parse("2.2")));
 
     assertThat(metadata.getId()).isEqualTo("123");
     assertThat(metadata.getVersion()).isEqualTo("2.2");