diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-02-01 15:16:25 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-02-22 20:02:46 +0000 |
commit | 60c1a4038e041a342dda9810e6fd761d66b01bdb (patch) | |
tree | 0e76b4252e4d7d257cf4ddcb6f081996bb1e03ab /server | |
parent | 9694d4113bf401b84e86e0223dbea8f5339388d8 (diff) | |
download | sonarqube-60c1a4038e041a342dda9810e6fd761d66b01bdb.tar.gz sonarqube-60c1a4038e041a342dda9810e6fd761d66b01bdb.zip |
SONAR-15994 Migrate Sonarqube IOC framework from Pico to Spring
Diffstat (limited to 'server')
294 files changed, 1686 insertions, 2008 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java index 71d08189bd9..f84d1e6b458 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java @@ -27,6 +27,7 @@ import java.util.Objects; import java.util.function.Function; import java.util.function.UnaryOperator; import javax.annotation.Nullable; +import javax.inject.Inject; import okhttp3.Credentials; import okhttp3.FormBody; import okhttp3.HttpUrl; @@ -60,6 +61,7 @@ public class BitbucketCloudRestClient { private final String bitbucketCloudEndpoint; private final String accessTokenEndpoint; + @Inject public BitbucketCloudRestClient(OkHttpClient okHttpClient) { this(okHttpClient, ENDPOINT, ACCESS_TOKEN_ENDPOINT); } diff --git a/server/sonar-auth-bitbucket/src/test/java/org/sonar/auth/bitbucket/BitbucketModuleTest.java b/server/sonar-auth-bitbucket/src/test/java/org/sonar/auth/bitbucket/BitbucketModuleTest.java index 7206c1e22d8..de3bc3b67e6 100755 --- a/server/sonar-auth-bitbucket/src/test/java/org/sonar/auth/bitbucket/BitbucketModuleTest.java +++ b/server/sonar-auth-bitbucket/src/test/java/org/sonar/auth/bitbucket/BitbucketModuleTest.java @@ -20,18 +20,17 @@ package org.sonar.auth.bitbucket; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class BitbucketModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new BitbucketModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 9); + assertThat(container.getAddedObjects()).hasSize(9); } } diff --git a/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubModuleTest.java b/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubModuleTest.java index 0592b53e50e..f05919bc518 100644 --- a/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubModuleTest.java +++ b/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubModuleTest.java @@ -20,18 +20,17 @@ package org.sonar.auth.github; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class GitHubModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new GitHubModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 13); + assertThat(container.getAddedObjects()).hasSize(13); } } diff --git a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabModuleTest.java b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabModuleTest.java index 5139aa62c72..1e4cff031d1 100644 --- a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabModuleTest.java +++ b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabModuleTest.java @@ -19,19 +19,54 @@ */ package org.sonar.auth.gitlab; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.Container; +import static java.util.Collections.unmodifiableList; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class GitLabModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new GitLabModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 10); + assertThat(container.getAddedObjects()).hasSize(10); } + private static class ListContainer implements Container { + private final List<Object> objects = new ArrayList<>(); + + @Override + public Container add(Object... objects) { + this.objects.add(objects); + return this; + } + + public List<Object> getAddedObjects() { + return unmodifiableList(new ArrayList<>(objects)); + } + + @Override + public <T> T getComponentByType(Class<T> type) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> Optional<T> getOptionalComponentByType(Class<T> type) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> List<T> getComponentsByType(Class<T> type) { + throw new UnsupportedOperationException(); + } + + @Override + public Container getParent() { + throw new UnsupportedOperationException(); + } + } } diff --git a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapModuleTest.java b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapModuleTest.java index 73455f48e7d..6496078dcc9 100644 --- a/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapModuleTest.java +++ b/server/sonar-auth-ldap/src/test/java/org/sonar/auth/ldap/LdapModuleTest.java @@ -20,18 +20,17 @@ package org.sonar.auth.ldap; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class LdapModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new LdapModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 3); + assertThat(container.getAddedObjects()).hasSize(3); } } diff --git a/server/sonar-auth-saml/src/test/java/org/sonar/auth/saml/SamlModuleTest.java b/server/sonar-auth-saml/src/test/java/org/sonar/auth/saml/SamlModuleTest.java index 034352c9f6a..2479812fcfb 100644 --- a/server/sonar-auth-saml/src/test/java/org/sonar/auth/saml/SamlModuleTest.java +++ b/server/sonar-auth-saml/src/test/java/org/sonar/auth/saml/SamlModuleTest.java @@ -20,17 +20,16 @@ package org.sonar.auth.saml; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class SamlModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new SamlModule().configure(container); - assertThat(container.size()).isGreaterThan(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); + assertThat(container.getAddedObjects()).hasSize(13); } } diff --git a/server/sonar-ce-common/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java b/server/sonar-ce-common/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java index 875eb00c851..3328cd71016 100644 --- a/server/sonar-ce-common/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java +++ b/server/sonar-ce-common/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java @@ -23,7 +23,7 @@ import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; /** - * When an implementation of this interface is available in Pico, the Compute Engine will use the value returned by + * When an implementation of this interface is available in the ioc container, the Compute Engine will use the value returned by * {@link #get()} as the number of worker the Compute Engine should run on. */ @ComputeEngineSide diff --git a/server/sonar-ce-task-projectanalysis/build.gradle b/server/sonar-ce-task-projectanalysis/build.gradle index b6d0b390b0d..bca65c795f6 100644 --- a/server/sonar-ce-task-projectanalysis/build.gradle +++ b/server/sonar-ce-task-projectanalysis/build.gradle @@ -29,7 +29,6 @@ dependencies { compile 'com.google.protobuf:protobuf-java' compile 'com.googlecode.java-diff-utils:diffutils' compile 'org.mybatis:mybatis' - compile 'org.picocontainer:picocontainer' compile project(':sonar-core') compile project(':server:sonar-ce-common') diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java index 3ede8d76372..b7ec255e4b0 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java @@ -72,14 +72,6 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor private final PostProjectAnalysisTask[] postProjectAnalysisTasks; private final BatchReportReader reportReader; - /** - * Constructor used by Pico when there is no {@link PostProjectAnalysisTask} in the container. - */ - public PostProjectAnalysisTasksExecutor(org.sonar.ce.task.CeTask ceTask, AnalysisMetadataHolder analysisMetadataHolder, - QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder, BatchReportReader reportReader) { - this(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, null); - } - public PostProjectAnalysisTasksExecutor(org.sonar.ce.task.CeTask ceTask, AnalysisMetadataHolder analysisMetadataHolder, QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder, BatchReportReader reportReader, @Nullable PostProjectAnalysisTask[] postProjectAnalysisTasks) { this.analysisMetadataHolder = analysisMetadataHolder; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java index b9253ca5213..3eb24b243ac 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java @@ -19,13 +19,14 @@ */ package org.sonar.ce.task.projectanalysis.component; -import javax.annotation.Nullable; import org.sonar.api.utils.MessageException; import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolder; import org.sonar.scanner.protocol.output.ScannerReport; - import static org.sonar.scanner.protocol.output.ScannerReport.Metadata.BranchType.UNSET; +import javax.annotation.Nullable; +import javax.inject.Inject; + public class BranchLoader { private final MutableAnalysisMetadataHolder metadataHolder; private final BranchLoaderDelegate delegate; @@ -34,6 +35,7 @@ public class BranchLoader { this(metadataHolder, null); } + @Inject public BranchLoader(MutableAnalysisMetadataHolder metadataHolder, @Nullable BranchLoaderDelegate delegate) { this.metadataHolder = metadataHolder; this.delegate = delegate; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactory.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactory.java index 12638017f9f..7ceb456ab12 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactory.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactory.java @@ -22,9 +22,9 @@ package org.sonar.ce.task.projectanalysis.container; import javax.annotation.Nullable; import org.sonar.ce.task.CeTask; import org.sonar.ce.task.container.TaskContainer; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; public interface ContainerFactory { - TaskContainer create(ComponentContainer parent, CeTask task, @Nullable ReportAnalysisComponentProvider[] componentProviders); + TaskContainer create(SpringComponentContainer parent, CeTask task, @Nullable ReportAnalysisComponentProvider[] componentProviders); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactoryImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactoryImpl.java index c6635e05877..7e58e1b22ed 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactoryImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ContainerFactoryImpl.java @@ -23,11 +23,11 @@ import javax.annotation.Nullable; import org.sonar.ce.task.CeTask; import org.sonar.ce.task.container.TaskContainer; import org.sonar.ce.task.container.TaskContainerImpl; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; public class ContainerFactoryImpl implements ContainerFactory { @Override - public TaskContainer create(ComponentContainer parent, CeTask task, @Nullable ReportAnalysisComponentProvider[] componentProviders) { + public TaskContainer create(SpringComponentContainer parent, CeTask task, @Nullable ReportAnalysisComponentProvider[] componentProviders) { return new TaskContainerImpl(parent, new ProjectAnalysisTaskContainerPopulator(task, componentProviders)); } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java index 4764a7a9446..faa28a22ba6 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java @@ -165,15 +165,15 @@ public final class ProjectAnalysisTaskContainerPopulator implements ContainerPop container.add(SettingsLoader.class); container.add(task); container.add(steps); - container.addSingletons(componentClasses()); + container.add(componentClasses()); for (ReportAnalysisComponentProvider componentProvider : componentProviders) { - container.addSingletons(componentProvider.getComponents()); + container.add(componentProvider.getComponents()); } - container.addSingletons(steps.orderedStepClasses()); + container.add(steps.orderedStepClasses()); } /** - * List of all objects to be injected in the picocontainer dedicated to computation stack. + * List of all objects to be injected in the ioc container dedicated to computation stack. * Does not contain the steps declared in {@link ReportComputationSteps#orderedStepClasses()}. */ private static List<Object> componentClasses() { @@ -191,7 +191,7 @@ public final class ProjectAnalysisTaskContainerPopulator implements ContainerPop new ComputationTempFolderProvider(), ReportModulesPath.class, - MetricModule.class, + new MetricModule(), // holders AnalysisMetadataHolderImpl.class, diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java index 3ba3eec0daf..0091e343184 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java @@ -20,7 +20,7 @@ package org.sonar.ce.task.projectanalysis.duplication; import javax.annotation.CheckForNull; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasures.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasures.java index f180f5a5bbb..103ef9dd4ce 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasures.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasures.java @@ -26,6 +26,7 @@ import java.util.Optional; import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import javax.inject.Inject; import org.sonar.ce.task.projectanalysis.component.Component; import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler; import org.sonar.ce.task.projectanalysis.component.TreeRootHolder; @@ -53,6 +54,7 @@ public class DuplicationMeasures { protected final MeasureRepository measureRepository; private final DuplicationRepository duplicationRepository; + @Inject public DuplicationMeasures(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository, @Nullable DuplicationRepository duplicationRepository) { this.treeRootHolder = treeRootHolder; @@ -63,9 +65,6 @@ public class DuplicationMeasures { this.formulas = List.of(new DuplicationFormula()); } - /** - * Constructor used by Pico in Views where no DuplicationRepository is available. - */ public DuplicationMeasures(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) { this(treeRootHolder, metricRepository, measureRepository, null); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/filesystem/ComputationTempFolderProvider.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/filesystem/ComputationTempFolderProvider.java index 49487780cf3..90ef49f2328 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/filesystem/ComputationTempFolderProvider.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/filesystem/ComputationTempFolderProvider.java @@ -21,64 +21,27 @@ package org.sonar.ce.task.projectanalysis.filesystem; import java.io.File; import java.io.IOException; -import javax.annotation.CheckForNull; import org.apache.commons.io.FileUtils; -import org.picocontainer.ComponentLifecycle; -import org.picocontainer.PicoContainer; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.impl.utils.DefaultTempFolder; import org.sonar.api.utils.TempFolder; import org.sonar.server.platform.ServerFileSystem; +import org.springframework.context.annotation.Bean; /** * Provides a TempFolder instance pointing to a directory dedicated to the processing of a specific item. * This directory will be deleted at the end of the processing. * This directory is located in the "ce" directory of the temp directory of the SonarQube instance. */ -public class ComputationTempFolderProvider extends ProviderAdapter implements ComponentLifecycle<TempFolder> { - private boolean started = false; - @CheckForNull - private DefaultTempFolder tempFolder = null; - +public class ComputationTempFolderProvider { + @Bean("ComputationTempFolder") public TempFolder provide(ServerFileSystem fs) { - if (this.tempFolder == null) { - File tempDir = new File(fs.getTempDir(), "ce"); - try { - FileUtils.forceMkdir(tempDir); - } catch (IOException e) { - throw new IllegalStateException("Unable to create computation temp directory " + tempDir, e); - } - File computationDir = new DefaultTempFolder(tempDir).newDir(); - this.tempFolder = new DefaultTempFolder(computationDir, true); + File tempDir = new File(fs.getTempDir(), "ce"); + try { + FileUtils.forceMkdir(tempDir); + } catch (IOException e) { + throw new IllegalStateException("Unable to create computation temp directory " + tempDir, e); } - return this.tempFolder; - } - - @Override - public void start(PicoContainer container) { - this.started = true; + File computationDir = new DefaultTempFolder(tempDir).newDir(); + return new DefaultTempFolder(computationDir, true); } - - @Override - public void stop(PicoContainer container) { - if (tempFolder != null) { - tempFolder.stop(); - } - } - - @Override - public void dispose(PicoContainer container) { - // nothing to do - } - - @Override - public boolean componentHasLifecycle() { - return true; - } - - @Override - public boolean isStarted() { - return started; - } - } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IssueLifecycle.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IssueLifecycle.java index 60fa00dea49..b57b06cbd00 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IssueLifecycle.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IssueLifecycle.java @@ -23,6 +23,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import java.util.Date; import java.util.Optional; +import javax.inject.Inject; import org.sonar.api.issue.Issue; import org.sonar.api.rules.RuleType; import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; @@ -54,6 +55,7 @@ public class IssueLifecycle { private final DebtCalculator debtCalculator; private final AnalysisMetadataHolder analysisMetadataHolder; + @Inject public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator, RuleRepository ruleRepository) { this(analysisMetadataHolder, IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator, ruleRepository); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/ProtoIssueCache.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/ProtoIssueCache.java index 38a5c6373f4..c2e460dd80d 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/ProtoIssueCache.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/ProtoIssueCache.java @@ -19,18 +19,19 @@ */ package org.sonar.ce.task.projectanalysis.issue; -import java.io.File; import org.sonar.api.utils.System2; import org.sonar.api.utils.TempFolder; import org.sonar.ce.task.projectanalysis.util.cache.ProtobufIssueDiskCache; +import javax.inject.Inject; +import java.io.File; + /** * Cache of all the issues involved in the analysis. Their state is as it will be * persisted in database (after issue tracking, auto-assignment, ...) */ public class ProtoIssueCache extends ProtobufIssueDiskCache { - - // this constructor is used by picocontainer + @Inject public ProtoIssueCache(TempFolder tempFolder, System2 system2) { super(tempFolder.newFile("issues", ".dat"), system2); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMerger.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMerger.java index cb9310473b4..2d4492bd066 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMerger.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMerger.java @@ -25,16 +25,19 @@ import org.sonar.ce.task.projectanalysis.component.Component; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.tracking.SimpleTracker; import org.sonar.core.issue.tracking.Tracking; +import org.springframework.beans.factory.annotation.Autowired; public class SiblingsIssueMerger { private final SiblingsIssuesLoader siblingsIssuesLoader; private final SimpleTracker<DefaultIssue, SiblingIssue> tracker; private final IssueLifecycle issueLifecycle; + @Autowired(required = false) public SiblingsIssueMerger(SiblingsIssuesLoader resolvedSiblingsIssuesLoader, IssueLifecycle issueLifecycle) { this(resolvedSiblingsIssuesLoader, new SimpleTracker<>(), issueLifecycle); } + @Autowired(required = false) public SiblingsIssueMerger(SiblingsIssuesLoader siblingsIssuesLoader, SimpleTracker<DefaultIssue, SiblingIssue> tracker, IssueLifecycle issueLifecycle) { this.siblingsIssuesLoader = siblingsIssuesLoader; this.tracker = tracker; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java index 544766e1026..33a36e45fa4 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Optional; import javax.annotation.Nonnull; import org.sonar.api.resources.Language; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Predicates.notNull; import static com.google.common.collect.Iterables.filter; @@ -38,10 +39,12 @@ public class LanguageRepositoryImpl implements LanguageRepository { private final Map<String, Language> languagesByKey; + @Autowired(required = false) public LanguageRepositoryImpl() { this.languagesByKey = Collections.emptyMap(); } + @Autowired(required = false) public LanguageRepositoryImpl(Language... languages) { this.languagesByKey = uniqueIndex(filter(asList(languages), notNull()), LanguageToKey.INSTANCE); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/PostMeasuresComputationChecksStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/PostMeasuresComputationChecksStep.java index a0826dea9a7..1cc12aa21dd 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/PostMeasuresComputationChecksStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/PostMeasuresComputationChecksStep.java @@ -28,6 +28,7 @@ import org.sonar.ce.task.projectanalysis.component.TreeRootHolder; import org.sonar.ce.task.projectanalysis.metric.Metric; import org.sonar.ce.task.projectanalysis.metric.MetricRepository; import org.sonar.ce.task.step.ComputationStep; +import org.springframework.beans.factory.annotation.Autowired; /** * Execute {@link PostMeasuresComputationCheck} instances in no specific order. @@ -43,6 +44,7 @@ public class PostMeasuresComputationChecksStep implements ComputationStep { private final AnalysisMetadataHolder analysisMetadataHolder; private final PostMeasuresComputationCheck[] extensions; + @Autowired(required = false) public PostMeasuresComputationChecksStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository, AnalysisMetadataHolder analysisMetadataHolder, PostMeasuresComputationCheck[] extensions) { this.treeRootHolder = treeRootHolder; @@ -55,6 +57,7 @@ public class PostMeasuresComputationChecksStep implements ComputationStep { /** * Used when zero {@link PostMeasuresComputationCheck} are registered into container. */ + @Autowired(required = false) public PostMeasuresComputationChecksStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository, AnalysisMetadataHolder analysisMetadataHolder) { this(treeRootHolder, metricRepository, measureRepository, analysisMetadataHolder, new PostMeasuresComputationCheck[0]); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/metric/MetricRepositoryImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/metric/MetricRepositoryImpl.java index dd122c1f692..c94934e0a3e 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/metric/MetricRepositoryImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/metric/MetricRepositoryImpl.java @@ -24,7 +24,7 @@ import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import javax.annotation.CheckForNull; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.metric.MetricDto; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/AbstractComputationSteps.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/AbstractComputationSteps.java index ad981b68bba..708b35f6b7f 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/AbstractComputationSteps.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/AbstractComputationSteps.java @@ -37,14 +37,6 @@ public abstract class AbstractComputationSteps implements ComputationSteps { @Override public Iterable<ComputationStep> instances() { - return Iterables.transform( - orderedStepClasses(), - input -> { - ComputationStep computationStepType = container.getComponentByType(input); - if (computationStepType == null) { - throw new IllegalStateException(String.format("Component not found: %s", input)); - } - return computationStepType; - }); + return Iterables.transform(orderedStepClasses(), container::getComponentByType); } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/CoverageMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/CoverageMeasuresStep.java index 7d9ae017ac0..2db3a2d6e13 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/CoverageMeasuresStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/CoverageMeasuresStep.java @@ -20,6 +20,8 @@ package org.sonar.ce.task.projectanalysis.step; import java.util.List; +import javax.annotation.Nullable; +import javax.inject.Inject; import org.sonar.ce.task.projectanalysis.batch.BatchReportReader; import org.sonar.ce.task.projectanalysis.component.Component; import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit; @@ -77,7 +79,8 @@ public class CoverageMeasuresStep implements ComputationStep { /** * Constructor used when processing a Report (ie. a {@link BatchReportReader} instance is available in the container) */ - public CoverageMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository, BatchReportReader reportReader) { + @Inject + public CoverageMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository, @Nullable BatchReportReader reportReader) { this.treeRootHolder = treeRootHolder; this.metricRepository = metricRepository; this.measureRepository = measureRepository; @@ -88,20 +91,6 @@ public class CoverageMeasuresStep implements ComputationStep { this.uncoveredConditionsMetric = metricRepository.getByKey(UNCOVERED_CONDITIONS_KEY); } - /** - * Constructor used when processing Views (ie. no {@link BatchReportReader} instance is available in the container) - */ - public CoverageMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) { - this.treeRootHolder = treeRootHolder; - this.metricRepository = metricRepository; - this.measureRepository = measureRepository; - this.linesToCoverMetric = metricRepository.getByKey(LINES_TO_COVER_KEY); - this.uncoveredLinesMetric = metricRepository.getByKey(UNCOVERED_LINES_KEY); - this.conditionsToCoverMetric = metricRepository.getByKey(CONDITIONS_TO_COVER_KEY); - this.uncoveredConditionsMetric = metricRepository.getByKey(UNCOVERED_CONDITIONS_KEY); - this.reportReader = null; - } - @Override public void execute(ComputationStep.Context context) { if (reportReader != null) { @@ -109,7 +98,7 @@ public class CoverageMeasuresStep implements ComputationStep { } new PathAwareCrawler<>( FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(COVERAGE_FORMULAS)) - .visit(treeRootHolder.getReportTreeRoot()); + .visit(treeRootHolder.getReportTreeRoot()); } private class FileCoverageVisitor extends TypeAwareVisitorAdapter { diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java index 9f6e3b045ac..ee8d604da2d 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java @@ -40,6 +40,7 @@ import org.sonar.ce.task.projectanalysis.api.measurecomputer.MeasureComputerDefi import org.sonar.ce.task.projectanalysis.api.measurecomputer.MeasureComputerWrapper; import org.sonar.ce.task.projectanalysis.measure.MutableMeasureComputersHolder; import org.sonar.ce.task.step.ComputationStep; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.FluentIterable.from; @@ -53,6 +54,7 @@ public class LoadMeasureComputersStep implements ComputationStep { private final MutableMeasureComputersHolder measureComputersHolder; private final MeasureComputer[] measureComputers; + @Autowired(required = false) public LoadMeasureComputersStep(MutableMeasureComputersHolder measureComputersHolder, Metrics[] metricsRepositories, MeasureComputer[] measureComputers) { this.measureComputersHolder = measureComputersHolder; this.measureComputers = measureComputers; @@ -63,22 +65,25 @@ public class LoadMeasureComputersStep implements ComputationStep { } /** - * Constructor override used by Pico to instantiate the class when no plugin is defining metrics + * Constructor override used by the ioc container to instantiate the class when no plugin is defining metrics */ + @Autowired(required = false) public LoadMeasureComputersStep(MutableMeasureComputersHolder measureComputersHolder, MeasureComputer[] measureComputers) { this(measureComputersHolder, new Metrics[] {}, measureComputers); } /** - * Constructor override used by Pico to instantiate the class when no plugin is defining measure computers + * Constructor override used by the ioc container to instantiate the class when no plugin is defining measure computers */ + @Autowired(required = false) public LoadMeasureComputersStep(MutableMeasureComputersHolder measureComputersHolder, Metrics[] metricsRepositories) { this(measureComputersHolder, metricsRepositories, new MeasureComputer[] {}); } /** - * Constructor override used by Pico to instantiate the class when no plugin is defining metrics neither measure computers + * Constructor override used by the ioc container to instantiate the class when no plugin is defining metrics neither measure computers */ + @Autowired(required = false) public LoadMeasureComputersStep(MutableMeasureComputersHolder measureComputersHolder) { this(measureComputersHolder, new Metrics[] {}, new MeasureComputer[] {}); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/TriggerViewRefreshStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/TriggerViewRefreshStep.java index b6333ee3bb4..60cb4bc9d10 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/TriggerViewRefreshStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/TriggerViewRefreshStep.java @@ -23,6 +23,7 @@ import java.util.OptionalInt; import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; import org.sonar.ce.task.projectanalysis.view.TriggerViewRefreshDelegate; import org.sonar.ce.task.step.ComputationStep; +import org.springframework.beans.factory.annotation.Autowired; /** * This step will trigger refresh of Portfolios and Applications that include the current project. @@ -34,16 +35,18 @@ public class TriggerViewRefreshStep implements ComputationStep { private final AnalysisMetadataHolder analysisMetadata; /** - * Constructor used by Pico when no implementation of {@link TriggerViewRefreshDelegate} is available + * Constructor used by the ioc container when no implementation of {@link TriggerViewRefreshDelegate} is available */ + @Autowired(required = false) public TriggerViewRefreshStep(AnalysisMetadataHolder analysisMetadata) { this.analysisMetadata = analysisMetadata; this.triggerViewRefreshDelegates = new TriggerViewRefreshDelegate[0]; } /** - * Constructor used by Pico when an implementation of {@link TriggerViewRefreshDelegate} is available + * Constructor used by the ioc container when an implementation of {@link TriggerViewRefreshDelegate} is available */ + @Autowired(required = false) public TriggerViewRefreshStep(AnalysisMetadataHolder analysisMetadata, TriggerViewRefreshDelegate[] triggerViewRefreshDelegates) { this.analysisMetadata = analysisMetadata; this.triggerViewRefreshDelegates = triggerViewRefreshDelegates; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessor.java index be26b4774f4..7f9163911ca 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessor.java @@ -31,18 +31,18 @@ import org.sonar.ce.task.projectanalysis.step.AbstractComputationSteps; import org.sonar.ce.task.step.ComputationStep; import org.sonar.ce.task.step.ComputationStepExecutor; import org.sonar.ce.task.taskprocessor.CeTaskProcessor; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.Container; import org.sonar.core.platform.ContainerPopulator; +import org.sonar.core.platform.SpringComponentContainer; import static org.sonar.db.ce.CeTaskTypes.AUDIT_PURGE; public class AuditPurgeTaskProcessor implements CeTaskProcessor { private static final Set<String> HANDLED_TYPES = Set.of(AUDIT_PURGE); - private final ComponentContainer ceEngineContainer; + private final SpringComponentContainer ceEngineContainer; - public AuditPurgeTaskProcessor(ComponentContainer ceEngineContainer) { + public AuditPurgeTaskProcessor(SpringComponentContainer ceEngineContainer) { this.ceEngineContainer = ceEngineContainer; } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessor.java index 29879730aa9..edef1621342 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessor.java @@ -32,18 +32,18 @@ import org.sonar.ce.task.projectanalysis.step.AbstractComputationSteps; import org.sonar.ce.task.step.ComputationStep; import org.sonar.ce.task.step.ComputationStepExecutor; import org.sonar.ce.task.taskprocessor.CeTaskProcessor; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.Container; import org.sonar.core.platform.ContainerPopulator; +import org.sonar.core.platform.SpringComponentContainer; import static org.sonar.db.ce.CeTaskTypes.BRANCH_ISSUE_SYNC; public class IssueSyncTaskProcessor implements CeTaskProcessor { private static final Set<String> HANDLED_TYPES = ImmutableSet.of(BRANCH_ISSUE_SYNC); - private final ComponentContainer ceEngineContainer; + private final SpringComponentContainer ceEngineContainer; - public IssueSyncTaskProcessor(ComponentContainer ceEngineContainer) { + public IssueSyncTaskProcessor(SpringComponentContainer ceEngineContainer) { this.ceEngineContainer = ceEngineContainer; } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/ReportTaskProcessor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/ReportTaskProcessor.java index 16fac974efd..b0939163ce5 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/ReportTaskProcessor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/taskprocessor/ReportTaskProcessor.java @@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.taskprocessor; import java.util.Collections; import java.util.Set; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.ce.task.CeTask; import org.sonar.ce.task.CeTaskResult; import org.sonar.ce.task.container.TaskContainer; @@ -29,43 +30,36 @@ import org.sonar.ce.task.projectanalysis.container.ContainerFactory; import org.sonar.ce.task.step.ComputationStepExecutor; import org.sonar.ce.task.taskprocessor.CeTaskProcessor; import org.sonar.ce.task.taskprocessor.TaskResultHolder; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.db.ce.CeTaskTypes; import org.sonar.ce.task.projectanalysis.container.ReportAnalysisComponentProvider; +import org.springframework.beans.factory.annotation.Autowired; public class ReportTaskProcessor implements CeTaskProcessor { private static final Set<String> HANDLED_TYPES = Collections.singleton(CeTaskTypes.REPORT); private final ContainerFactory containerFactory; - private final ComponentContainer serverContainer; + private final SpringComponentContainer serverContainer; @CheckForNull private final ReportAnalysisComponentProvider[] componentProviders; - /** - * Used when at least one Privileged plugin is installed - */ - public ReportTaskProcessor(ContainerFactory containerFactory, ComponentContainer serverContainer, ReportAnalysisComponentProvider[] componentProviders) { + @Autowired(required = false) + public ReportTaskProcessor(ContainerFactory containerFactory, SpringComponentContainer serverContainer, @Nullable ReportAnalysisComponentProvider[] componentProviders) { this.containerFactory = containerFactory; this.serverContainer = serverContainer; this.componentProviders = componentProviders; } /** - * Used when no privileged plugin is installed - */ - public ReportTaskProcessor(ContainerFactory containerFactory, ComponentContainer serverContainer) { - this.containerFactory = containerFactory; - this.serverContainer = serverContainer; - this.componentProviders = null; - } - - /** * Used when loaded in WebServer where none of the dependencies are available and where only * {@link #getHandledCeTaskTypes()} will be called. */ + @Autowired(required = false) public ReportTaskProcessor() { - this(null, null, null); + this.containerFactory = null; + this.serverContainer = null; + this.componentProviders = null; } @Override diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulator.java index 26e9084cc4e..30f57010550 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulator.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulator.java @@ -54,8 +54,8 @@ public class ProjectExportContainerPopulator implements ContainerPopulator<TaskC container.add(projectDescriptor); container.add(steps); - container.addSingletons(COMPONENT_CLASSES); - container.addSingletons(steps.orderedStepClasses()); + container.add(COMPONENT_CLASSES); + container.add(steps.orderedStepClasses()); } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java index 68c03bebf1e..24cbdd9bf4c 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java @@ -27,15 +27,15 @@ import org.sonar.ce.task.container.TaskContainerImpl; import org.sonar.ce.task.projectexport.ProjectExportContainerPopulator; import org.sonar.ce.task.projectexport.ProjectExportProcessor; import org.sonar.ce.task.taskprocessor.CeTaskProcessor; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static org.sonar.db.ce.CeTaskTypes.PROJECT_EXPORT; public class ProjectExportTaskProcessor implements CeTaskProcessor { - private final ComponentContainer componentContainer; + private final SpringComponentContainer componentContainer; - public ProjectExportTaskProcessor(ComponentContainer componentContainer) { + public ProjectExportTaskProcessor(SpringComponentContainer componentContainer) { this.componentContainer = componentContainer; } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectExportDumpFSImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectExportDumpFSImpl.java index 6be47c09411..bd44654516b 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectExportDumpFSImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectExportDumpFSImpl.java @@ -20,7 +20,7 @@ package org.sonar.ce.task.projectexport.util; import java.io.File; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectImportDumpFSImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectImportDumpFSImpl.java index 434312438c7..fc23a0602b4 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectImportDumpFSImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/util/ProjectImportDumpFSImpl.java @@ -20,7 +20,7 @@ package org.sonar.ce.task.projectexport.util; import java.io.File; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java index f134ff9a828..b3c6aff2ef5 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java @@ -116,7 +116,7 @@ public class PostProjectAnalysisTasksExecutorTest { @Test @UseDataProvider("booleanValues") public void does_not_fail_when_there_is_no_PostProjectAnalysisTasksExecutor(boolean allStepsExecuted) { - new PostProjectAnalysisTasksExecutor(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader) + new PostProjectAnalysisTasksExecutor(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, null) .finished(allStepsExecuted); } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulatorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulatorTest.java index 0777349a152..911f1415d19 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulatorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulatorTest.java @@ -21,21 +21,15 @@ package org.sonar.ce.task.projectanalysis.container; import com.google.common.collect.ImmutableList; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import org.junit.Test; -import org.picocontainer.DefaultPicoContainer; -import org.picocontainer.PicoContainer; import org.reflections.Reflections; import org.sonar.ce.task.CeTask; -import org.sonar.ce.task.container.TaskContainer; import org.sonar.ce.task.projectanalysis.step.PersistComponentsStep; +import org.sonar.ce.task.projectanalysis.task.ListTaskContainer; import org.sonar.ce.task.step.ComputationStep; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.util.stream.MoreCollectors; import static com.google.common.collect.Sets.difference; @@ -46,25 +40,23 @@ import static org.mockito.Mockito.when; public class ProjectAnalysisTaskContainerPopulatorTest { private static final String PROJECTANALYSIS_STEP_PACKAGE = "org.sonar.ce.task.projectanalysis.step"; - private CeTask task = mock(CeTask.class); - private ProjectAnalysisTaskContainerPopulator underTest; + private final CeTask task = mock(CeTask.class); + private final ProjectAnalysisTaskContainerPopulator underTest = new ProjectAnalysisTaskContainerPopulator(task, null); @Test public void item_is_added_to_the_container() { - underTest = new ProjectAnalysisTaskContainerPopulator(task, null); - AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer(); + ListTaskContainer container = new ListTaskContainer(); underTest.populateContainer(container); - assertThat(container.added).contains(task); + assertThat(container.getAddedComponents()).contains(task); } @Test public void all_computation_steps_are_added_in_order_to_the_container() { - underTest = new ProjectAnalysisTaskContainerPopulator(task, null); - AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer(); + ListTaskContainer container = new ListTaskContainer(); underTest.populateContainer(container); - Set<String> computationStepClassNames = container.added.stream() + Set<String> computationStepClassNames = container.getAddedComponents().stream() .map(s -> { if (s instanceof Class) { return (Class<?>) s; @@ -94,11 +86,10 @@ public class ProjectAnalysisTaskContainerPopulatorTest { @Test public void at_least_one_core_step_is_added_to_the_container() { - underTest = new ProjectAnalysisTaskContainerPopulator(task, null); - AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer(); + ListTaskContainer container = new ListTaskContainer(); underTest.populateContainer(container); - assertThat(container.added).contains(PersistComponentsStep.class); + assertThat(container.getAddedComponents()).contains(PersistComponentsStep.class); } @Test @@ -108,74 +99,16 @@ public class ProjectAnalysisTaskContainerPopulatorTest { ReportAnalysisComponentProvider componentProvider = mock(ReportAnalysisComponentProvider.class); when(componentProvider.getComponents()).thenReturn(ImmutableList.of(object, clazz)); - underTest = new ProjectAnalysisTaskContainerPopulator(task, new ReportAnalysisComponentProvider[] {componentProvider}); - AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer(); + ProjectAnalysisTaskContainerPopulator populator = new ProjectAnalysisTaskContainerPopulator(task, new ReportAnalysisComponentProvider[] {componentProvider}); + ListTaskContainer container = new ListTaskContainer(); container.add(componentProvider); - underTest.populateContainer(container); + populator.populateContainer(container); - assertThat(container.added).contains(object, clazz); + assertThat(container.getAddedComponents()).contains(object, clazz); } private static final class MyClass { } - private static class AddedObjectsRecorderTaskContainer implements TaskContainer { - private static final DefaultPicoContainer SOME_EMPTY_PICO_CONTAINER = new DefaultPicoContainer(); - - private List<Object> added = new ArrayList<>(); - - @Override - public void bootup() { - // no effect - } - - @Override - public ComponentContainer getParent() { - throw new UnsupportedOperationException("getParent is not implemented"); - } - - @Override - public void close() { - throw new UnsupportedOperationException("cleanup is not implemented"); - } - - @Override - public PicoContainer getPicoContainer() { - return SOME_EMPTY_PICO_CONTAINER; - } - - @Override - public ComponentContainer add(Object... objects) { - added.addAll(Arrays.asList(objects)); - return null; // not used anyway - } - - @Override - public ComponentContainer addSingletons(Iterable<?> components) { - for (Object component : components) { - added.add(component); - } - return null; // not used anyway - } - - @Override - public <T> T getComponentByType(Class<T> type) { - for (Object add : added) { - if (add.getClass().getSimpleName().contains(type.getSimpleName())) { - return (T) add; - } - } - return null; - } - - @Override - public <T> List<T> getComponentsByType(final Class<T> type) { - return added.stream() - .filter(input -> input.getClass().getSimpleName().contains(type.getSimpleName())) - .map(input -> (T) input) - .collect(Collectors.toList()); - } - } - } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filesystem/ComputationTempFolderProviderTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filesystem/ComputationTempFolderProviderTest.java new file mode 100644 index 00000000000..896f45618a8 --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filesystem/ComputationTempFolderProviderTest.java @@ -0,0 +1,67 @@ +/* + * 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.ce.task.projectanalysis.filesystem; + +import org.apache.commons.io.FileUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.sonar.api.utils.TempFolder; +import org.sonar.server.platform.ServerFileSystem; + +import java.io.File; + +public class ComputationTempFolderProviderTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private final ComputationTempFolderProvider underTest = new ComputationTempFolderProvider(); + + @Test + public void existing_temp_dir() throws Exception { + ServerFileSystem fs = mock(ServerFileSystem.class); + File tmpDir = temp.newFolder(); + when(fs.getTempDir()).thenReturn(tmpDir); + + TempFolder folder = underTest.provide(fs); + assertThat(folder).isNotNull(); + File newDir = folder.newDir(); + assertThat(newDir).exists().isDirectory(); + assertThat(newDir.getParentFile().getCanonicalPath()).startsWith(tmpDir.getCanonicalPath()); + } + + @Test + public void create_temp_dir_if_missing() throws Exception { + ServerFileSystem fs = mock(ServerFileSystem.class); + File tmpDir = temp.newFolder(); + when(fs.getTempDir()).thenReturn(tmpDir); + FileUtils.forceDelete(tmpDir); + + TempFolder folder = underTest.provide(fs); + assertThat(folder).isNotNull(); + File newDir = folder.newDir(); + assertThat(newDir).exists().isDirectory(); + assertThat(newDir.getParentFile().getCanonicalPath()).startsWith(tmpDir.getCanonicalPath()); + } +} diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComputationStepsTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComputationStepsTest.java index bb0a6f6b177..247885957a5 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComputationStepsTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportComputationStepsTest.java @@ -19,11 +19,12 @@ */ package org.sonar.ce.task.projectanalysis.step; -import com.google.common.collect.Lists; import org.junit.Test; import org.sonar.ce.task.container.TaskContainerImpl; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.ce.task.step.ComputationStep; +import org.sonar.core.platform.SpringComponentContainer; +import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; @@ -31,33 +32,30 @@ public class ReportComputationStepsTest { @Test public void instances_throws_ISE_if_container_does_not_have_any_step() { - assertThatThrownBy(() -> { - TaskContainerImpl computeEngineContainer = new TaskContainerImpl(new ComponentContainer(), container -> { - // do nothing - }); - - Lists.newArrayList(new ReportComputationSteps(computeEngineContainer).instances()); - }) + TaskContainerImpl computeEngineContainer = new TaskContainerImpl(new SpringComponentContainer(), container -> { + // do nothing + }); + Iterable<ComputationStep> instances = new ReportComputationSteps(computeEngineContainer).instances(); + assertThatThrownBy(() -> newArrayList(instances)) .isInstanceOf(IllegalStateException.class) - .hasMessage("Component not found: " + ExtractReportStep.class); + .hasMessageContaining(ExtractReportStep.class.getName()); } @Test public void instances_throws_ISE_if_container_does_not_have_second_step() { - assertThatThrownBy(() -> { - final ExtractReportStep reportExtractionStep = mock(ExtractReportStep.class); - ComponentContainer componentContainer = new ComponentContainer() { - { - addSingleton(reportExtractionStep); - } - }; - TaskContainerImpl computeEngineContainer = new TaskContainerImpl(componentContainer, container -> { - // do nothing - }); - - Lists.newArrayList(new ReportComputationSteps(computeEngineContainer).instances()); - }) + ExtractReportStep reportExtractionStep = mock(ExtractReportStep.class); + SpringComponentContainer componentContainer = new SpringComponentContainer() { + { + add(reportExtractionStep); + } + }.startComponents(); + TaskContainerImpl computeEngineContainer = new TaskContainerImpl(componentContainer, container -> { + // do nothing + }); + computeEngineContainer.startComponents(); + Iterable<ComputationStep> instances = new ReportComputationSteps(computeEngineContainer).instances(); + assertThatThrownBy(() -> newArrayList(instances)) .isInstanceOf(IllegalStateException.class) - .hasMessage("Component not found: class org.sonar.ce.task.projectanalysis.step.PersistScannerContextStep"); + .hasMessageContaining("org.sonar.ce.task.projectanalysis.step.PersistScannerContextStep"); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsCoverageMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsCoverageMeasuresStepTest.java index e01374588d5..2f5300b77d1 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsCoverageMeasuresStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsCoverageMeasuresStepTest.java @@ -62,7 +62,7 @@ public class ViewsCoverageMeasuresStepTest { @Rule public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); - CoverageMeasuresStep underTest = new CoverageMeasuresStep(treeRootHolder, metricRepository, measureRepository); + CoverageMeasuresStep underTest = new CoverageMeasuresStep(treeRootHolder, metricRepository, measureRepository, null); @Before public void setUp() { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskModuleTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskModuleTest.java index 9be01d4a363..4c52736de16 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskModuleTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskModuleTest.java @@ -20,18 +20,15 @@ package org.sonar.ce.task.projectanalysis.taskprocessor; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class AuditPurgeTaskModuleTest { - @Test public void verifyCountOfAddedComponents() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new AuditPurgeTaskModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 1); + assertThat(container.getAddedObjects()).hasSize(1); } - } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessorTest.java index 53e0165c2f6..3ed573ec317 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/AuditPurgeTaskProcessorTest.java @@ -26,18 +26,18 @@ import org.mockito.Mockito; import org.sonar.ce.task.CeTask; import org.sonar.ce.task.container.TaskContainer; import org.sonar.ce.task.step.ComputationStep; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.sonar.ce.task.projectanalysis.taskprocessor.AuditPurgeTaskProcessor.AuditPurgeComputationSteps; import static org.sonar.db.ce.CeTaskTypes.AUDIT_PURGE; -import static org.assertj.core.api.Assertions.assertThatThrownBy; public class AuditPurgeTaskProcessorTest { - private ComponentContainer ceEngineContainer = Mockito.mock(ComponentContainer.class); - - private AuditPurgeTaskProcessor underTest = new AuditPurgeTaskProcessor(ceEngineContainer); - private TaskContainer container = Mockito.spy(TaskContainer.class); + private final SpringComponentContainer ceEngineContainer = mock(SpringComponentContainer.class); + private final AuditPurgeTaskProcessor underTest = new AuditPurgeTaskProcessor(ceEngineContainer); + private final TaskContainer container = Mockito.spy(TaskContainer.class); @Test public void getHandledCeTaskTypes() { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessorTest.java index df577f2df5e..9f2929f8eb3 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/taskprocessor/IssueSyncTaskProcessorTest.java @@ -26,18 +26,18 @@ import org.mockito.Mockito; import org.sonar.ce.task.CeTask; import org.sonar.ce.task.container.TaskContainer; import org.sonar.ce.task.step.ComputationStep; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.sonar.ce.task.projectanalysis.taskprocessor.IssueSyncTaskProcessor.SyncComputationSteps; import static org.sonar.db.ce.CeTaskTypes.BRANCH_ISSUE_SYNC; public class IssueSyncTaskProcessorTest { - private ComponentContainer ceEngineContainer = Mockito.mock(ComponentContainer.class); - - private IssueSyncTaskProcessor underTest = new IssueSyncTaskProcessor(ceEngineContainer); - private TaskContainer container = Mockito.spy(TaskContainer.class); + private final SpringComponentContainer ceEngineContainer = mock(SpringComponentContainer.class); + private final IssueSyncTaskProcessor underTest = new IssueSyncTaskProcessor(ceEngineContainer); + private final TaskContainer container = Mockito.spy(TaskContainer.class); @Test public void getHandledCeTaskTypes() { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportComputationStepsTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportComputationStepsTest.java index 3dcc9c5a2d3..9e63aaaa01a 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportComputationStepsTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportComputationStepsTest.java @@ -24,18 +24,19 @@ import org.junit.Test; import org.sonar.ce.task.container.TaskContainer; import org.sonar.ce.task.container.TaskContainerImpl; import org.sonar.ce.task.projectanalysis.step.ComplexityMeasuresStep; -import org.sonar.ce.task.projectexport.steps.LoadProjectStep; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.ce.task.step.ComputationStep; +import org.sonar.core.platform.SpringComponentContainer; import static com.google.common.collect.ImmutableList.copyOf; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ProjectExportComputationStepsTest { - - private TaskContainer container = mock(TaskContainer.class); - private ProjectExportComputationSteps underTest = new ProjectExportComputationSteps(container); + private final TaskContainer container = mock(TaskContainer.class); + private final ProjectExportComputationSteps underTest = new ProjectExportComputationSteps(container); @Test public void count_step_classes() { @@ -44,29 +45,30 @@ public class ProjectExportComputationStepsTest { @Test public void instances_throws_ISE_if_steps_do_not_exist_in_container() { - assertThatThrownBy(() -> copyOf(underTest.instances())) + when(container.getComponentByType(any())).thenThrow(new IllegalStateException("Error")); + Iterable<ComputationStep> instances = underTest.instances(); + assertThatThrownBy(() -> copyOf(instances)) .isInstanceOf(IllegalStateException.class) - .hasMessage("Component not found: " + LoadProjectStep.class); + .hasMessage("Error"); } @Test public void instances_throws_ISE_if_container_does_not_have_second_step() { - assertThatThrownBy(() -> { - final ComplexityMeasuresStep reportExtractionStep = mock(ComplexityMeasuresStep.class); - - ComponentContainer componentContainer = new ComponentContainer() { - { - addSingleton(reportExtractionStep); - } - }; - TaskContainerImpl computeEngineContainer = new TaskContainerImpl(componentContainer, container -> { - // do nothing - }); + ComplexityMeasuresStep reportExtractionStep = mock(ComplexityMeasuresStep.class); - Lists.newArrayList(new ProjectExportComputationSteps(computeEngineContainer).instances()); - }) + SpringComponentContainer componentContainer = new SpringComponentContainer() { + { + add(reportExtractionStep); + } + }.startComponents(); + TaskContainerImpl computeEngineContainer = new TaskContainerImpl(componentContainer, container -> { + // do nothing + }); + computeEngineContainer.startComponents(); + Iterable<ComputationStep> instances = new ProjectExportComputationSteps(computeEngineContainer).instances(); + assertThatThrownBy(() -> Lists.newArrayList(instances)) .isInstanceOf(IllegalStateException.class) - .hasMessage("Component not found: class org.sonar.ce.task.projectexport.steps.LoadProjectStep"); + .hasMessageContaining("class org.sonar.ce.task.projectexport.steps.LoadProjectStep"); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulatorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulatorTest.java index 999f78f1220..d6760965d93 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulatorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/ProjectExportContainerPopulatorTest.java @@ -19,87 +19,23 @@ */ package org.sonar.ce.task.projectexport; -import com.google.common.base.Predicate; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; import org.junit.Test; -import org.picocontainer.PicoContainer; -import org.sonar.ce.task.container.TaskContainer; +import org.sonar.ce.task.projectanalysis.task.ListTaskContainer; import org.sonar.ce.task.projectexport.taskprocessor.ProjectDescriptor; import org.sonar.ce.task.setting.SettingsLoader; -import org.sonar.ce.task.step.ComputationStep; -import org.sonar.core.platform.ComponentContainer; import static org.assertj.core.api.Assertions.assertThat; public class ProjectExportContainerPopulatorTest { - - private static final int COMPONENTS_BY_DEFAULT_IN_CONTAINER = 2; - private final ProjectDescriptor descriptor = new ProjectDescriptor("project_uuid", "project_key", "Project Name"); private final ProjectExportContainerPopulator underTest = new ProjectExportContainerPopulator(descriptor); @Test public void test_populateContainer() { - RecorderTaskContainer container = new RecorderTaskContainer(); + ListTaskContainer container = new ListTaskContainer(); underTest.populateContainer(container); - assertThat(container.addedComponents) - .hasSize(COMPONENTS_BY_DEFAULT_IN_CONTAINER + 8) + assertThat(container.getAddedComponents()) + .hasSize(29) .contains(descriptor, SettingsLoader.class); } - - private static class RecorderTaskContainer implements TaskContainer { - private final List<Object> addedComponents = new ArrayList<>(); - - @Override - public ComponentContainer add(Object... objects) { - addedComponents.addAll(Arrays.asList(objects)); - // not used anyway - return null; - } - - @Override - public ComponentContainer addSingletons(Iterable<?> components) { - List<Object> filteredComponents = StreamSupport.stream(components.spliterator(), false) - .filter((Predicate<Object>) input -> !(input instanceof Class) || !ComputationStep.class.isAssignableFrom((Class<?>) input)) - .collect(Collectors.toList()); - - addedComponents.addAll(filteredComponents); - // not used anyway - return null; - } - - @Override - public ComponentContainer getParent() { - throw new UnsupportedOperationException("getParent is not implemented"); - } - - @Override - public void bootup() { - throw new UnsupportedOperationException("bootup is not implemented"); - } - - @Override - public void close() { - throw new UnsupportedOperationException("close is not implemented"); - } - - @Override - public PicoContainer getPicoContainer() { - throw new UnsupportedOperationException("getParent is not implemented"); - } - - @Override - public <T> T getComponentByType(Class<T> type) { - throw new UnsupportedOperationException("getParent is not implemented"); - } - - @Override - public <T> List<T> getComponentsByType(final Class<T> type) { - throw new UnsupportedOperationException("getParent is not implemented"); - } - } } diff --git a/server/sonar-ce-task-projectanalysis/src/testFixtures/java/org/sonar/ce/task/projectanalysis/task/ListTaskContainer.java b/server/sonar-ce-task-projectanalysis/src/testFixtures/java/org/sonar/ce/task/projectanalysis/task/ListTaskContainer.java new file mode 100644 index 00000000000..c22b9aee83f --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/testFixtures/java/org/sonar/ce/task/projectanalysis/task/ListTaskContainer.java @@ -0,0 +1,80 @@ +/* + * 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.ce.task.projectanalysis.task; + +import com.google.common.collect.Iterables; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.sonar.ce.task.container.TaskContainer; +import org.sonar.core.platform.Module; +import org.sonar.core.platform.SpringComponentContainer; + +public class ListTaskContainer implements TaskContainer { + private final List<Object> addedComponents = new ArrayList<>(); + + @Override + public SpringComponentContainer add(Object... objects) { + for (Object o : objects) { + if (o instanceof Module) { + ((Module) o).configure(this); + } else if (o instanceof Iterable) { + add(Iterables.toArray((Iterable<?>) o, Object.class)); + } else { + this.addedComponents.add(o); + } + } + // not used anyway + return null; + } + + public List<Object> getAddedComponents() { + return addedComponents; + } + + @Override + public SpringComponentContainer getParent() { + throw new UnsupportedOperationException("getParent is not implemented"); + } + + @Override + public void bootup() { + throw new UnsupportedOperationException("bootup is not implemented"); + } + + @Override + public void close() { + throw new UnsupportedOperationException("close is not implemented"); + } + + @Override + public <T> T getComponentByType(Class<T> type) { + throw new UnsupportedOperationException("getParent is not implemented"); + } + + @Override public <T> Optional<T> getOptionalComponentByType(Class<T> type) { + throw new UnsupportedOperationException("getParent is not implemented"); + } + + @Override + public <T> List<T> getComponentsByType(final Class<T> type) { + throw new UnsupportedOperationException("getParent is not implemented"); + } +} diff --git a/server/sonar-ce-task/build.gradle b/server/sonar-ce-task/build.gradle index 6a18c0ce329..d7d426b8b51 100644 --- a/server/sonar-ce-task/build.gradle +++ b/server/sonar-ce-task/build.gradle @@ -18,7 +18,6 @@ dependencies { // please keep the list grouped by configuration and ordered by name compile 'com.google.guava:guava' - compile 'org.picocontainer:picocontainer' compile 'org.slf4j:jul-to-slf4j' compile 'org.slf4j:slf4j-api' diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientStopperTest.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/LazyUnlessEagerAnnotationStrategy.java index 6787d21af65..b5b438892a4 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientStopperTest.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/LazyUnlessEagerAnnotationStrategy.java @@ -17,25 +17,15 @@ * 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.server.es; +package org.sonar.ce.task.container; -import org.junit.Test; +import javax.annotation.Nullable; +import org.sonar.core.platform.SpringInitStrategy; +import org.springframework.beans.factory.config.BeanDefinition; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -public class EsClientStopperTest { - - private EsClient client = mock(EsClient.class); - private EsClientStopper underTest = new EsClientStopper(client); - - @Test - public void stop_client() { - underTest.start(); - verifyNoMoreInteractions(client); - - underTest.stop(); - verify(client).close(); +public class LazyUnlessEagerAnnotationStrategy extends SpringInitStrategy { + @Override + protected boolean isLazyInit(BeanDefinition beanDefinition, @Nullable Class<?> clazz) { + return clazz == null || clazz.getAnnotation(EagerStart.class) == null; } } diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java index 2a3979f1c7f..c38d70e5002 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java @@ -19,17 +19,16 @@ */ package org.sonar.ce.task.container; -import org.picocontainer.PicoContainer; import org.sonar.ce.task.CeTask; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.Container; +import org.sonar.core.platform.ExtensionContainer; /** - * The Compute Engine task container. Created for a specific parent {@link ComponentContainer} and a specific {@link CeTask}. + * The Compute Engine task container. Created for a specific parent {@link ExtensionContainer} and a specific {@link CeTask}. */ public interface TaskContainer extends Container, AutoCloseable { - ComponentContainer getParent(); + ExtensionContainer getParent(); /** * Starts task container, starting any startable component in it. @@ -41,10 +40,4 @@ public interface TaskContainer extends Container, AutoCloseable { */ @Override void close(); - - /** - * Access to the underlying pico container. - */ - PicoContainer getPicoContainer(); - } diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java index c8d67ab2a73..6c514a93911 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java @@ -19,55 +19,21 @@ */ package org.sonar.ce.task.container; -import java.util.List; -import org.picocontainer.ComponentAdapter; -import org.picocontainer.DefaultPicoContainer; -import org.picocontainer.LifecycleStrategy; -import org.picocontainer.MutablePicoContainer; -import org.picocontainer.behaviors.OptInCaching; -import org.picocontainer.monitors.NullComponentMonitor; -import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.utils.log.Loggers; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.ContainerPopulator; -import org.sonar.core.platform.Module; -import org.sonar.core.platform.StartableCloseableSafeLifecyleStrategy; +import org.sonar.core.platform.SpringComponentContainer; import static java.util.Objects.requireNonNull; -public class TaskContainerImpl extends ComponentContainer implements TaskContainer { - - public TaskContainerImpl(ComponentContainer parent, ContainerPopulator<TaskContainer> populator) { - super(createContainer(requireNonNull(parent)), parent.getComponentByType(PropertyDefinitions.class)); +public class TaskContainerImpl extends SpringComponentContainer implements TaskContainer { + public TaskContainerImpl(SpringComponentContainer parent, ContainerPopulator<TaskContainer> populator) { + super(parent, new LazyUnlessEagerAnnotationStrategy()); populateContainer(requireNonNull(populator)); } private void populateContainer(ContainerPopulator<TaskContainer> populator) { populator.populateContainer(this); - populateFromModules(); - } - - private void populateFromModules() { - List<Module> modules = getComponentsByType(Module.class); - for (Module module : modules) { - module.configure(this); - } - } - - /** - * Creates a PicContainer which extends the specified ComponentContainer <strong>but is not referenced in return</strong> - * and lazily starts its components. - */ - private static MutablePicoContainer createContainer(ComponentContainer parent) { - LifecycleStrategy lifecycleStrategy = new StartableCloseableSafeLifecyleStrategy() { - @Override - public boolean isLazy(ComponentAdapter<?> adapter) { - return adapter.getComponentImplementation().getAnnotation(EagerStart.class) == null; - } - }; - - return new DefaultPicoContainer(new OptInCaching(), lifecycleStrategy, parent.getPicoContainer(), new NullComponentMonitor()); } @Override diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java index d5fc039e991..cae9ed665da 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java @@ -19,7 +19,7 @@ */ package org.sonar.ce.task.setting; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.internal.Settings; import org.sonar.ce.task.container.EagerStart; diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java index b2ecc7d421a..b00f8b1432d 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java @@ -25,6 +25,7 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.ce.task.CeTaskInterrupter; import org.sonar.core.util.logs.Profiler; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; @@ -38,14 +39,12 @@ public final class ComputationStepExecutor { @CheckForNull private final Listener listener; - /** - * Used when no {@link ComputationStepExecutor.Listener} is available in pico - * container. - */ + @Autowired(required = false) public ComputationStepExecutor(ComputationSteps steps, CeTaskInterrupter taskInterrupter) { this(steps, taskInterrupter, null); } + @Autowired(required = false) public ComputationStepExecutor(ComputationSteps steps, CeTaskInterrupter taskInterrupter, @Nullable Listener listener) { this.steps = steps; this.taskInterrupter = taskInterrupter; diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java index 04294b1dff9..08f609e9cdf 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java @@ -20,6 +20,7 @@ package org.sonar.ce.task.step; import org.sonar.api.ce.ComputeEngineSide; +import org.springframework.beans.factory.annotation.Autowired; /** * Execute {@link StatelessInitExtension} instances in no specific order. @@ -31,6 +32,7 @@ public class ExecuteStatelessInitExtensionsStep implements ComputationStep { private final StatelessInitExtension[] extensions; + @Autowired(required = false) public ExecuteStatelessInitExtensionsStep(StatelessInitExtension[] extensions) { this.extensions = extensions; } @@ -38,6 +40,7 @@ public class ExecuteStatelessInitExtensionsStep implements ComputationStep { /** * Used when zero {@link StatelessInitExtension} are registered into container. */ + @Autowired(required = false) public ExecuteStatelessInitExtensionsStep() { this(new StatelessInitExtension[0]); } diff --git a/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java b/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java index b39be56616b..c2df2f4923c 100644 --- a/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java +++ b/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java @@ -19,10 +19,11 @@ */ package org.sonar.ce.task.container; +import org.junit.Before; import org.junit.Test; -import org.picocontainer.Startable; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.api.Startable; import org.sonar.core.platform.ContainerPopulator; +import org.sonar.core.platform.SpringComponentContainer; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -30,8 +31,13 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; public class TaskContainerImplTest { - private ComponentContainer parent = new ComponentContainer(); - private ContainerPopulator<TaskContainer> populator = spy(new DummyContainerPopulator()); + private final SpringComponentContainer parent = new SpringComponentContainer(); + private final ContainerPopulator<TaskContainer> populator = spy(new DummyContainerPopulator()); + + @Before + public void before() { + parent.startComponents(); + } @Test public void constructor_fails_fast_on_null_container() { @@ -41,7 +47,8 @@ public class TaskContainerImplTest { @Test public void constructor_fails_fast_on_null_item() { - assertThatThrownBy(() -> new TaskContainerImpl(new ComponentContainer(), null)) + SpringComponentContainer c = new SpringComponentContainer(); + assertThatThrownBy(() -> new TaskContainerImpl(c, null)) .isInstanceOf(NullPointerException.class); } @@ -53,17 +60,9 @@ public class TaskContainerImplTest { } @Test - public void ce_container_is_not_child_of_specified_container() { - TaskContainerImpl ceContainer = new TaskContainerImpl(parent, populator); - - assertThat(parent.getChildren()).isEmpty(); - verify(populator).populateContainer(ceContainer); - } - - @Test public void bootup_starts_components_lazily_unless_they_are_annotated_with_EagerStart() { - final DefaultStartable defaultStartable = new DefaultStartable(); - final EagerStartable eagerStartable = new EagerStartable(); + DefaultStartable defaultStartable = new DefaultStartable(); + EagerStartable eagerStartable = new EagerStartable(); TaskContainerImpl ceContainer = new TaskContainerImpl(parent, container -> { container.add(defaultStartable); container.add(eagerStartable); diff --git a/server/sonar-ce/build.gradle b/server/sonar-ce/build.gradle index 1cb412e850d..a495260bd6f 100644 --- a/server/sonar-ce/build.gradle +++ b/server/sonar-ce/build.gradle @@ -15,7 +15,6 @@ dependencies { compile 'commons-io:commons-io' compile 'org.apache.commons:commons-dbcp2' compile 'org.nanohttpd:nanohttpd' - compile 'org.picocontainer:picocontainer' compile project(':server:sonar-ce-common') compile project(':server:sonar-ce-task') compile project(':server:sonar-ce-task-projectanalysis') @@ -36,7 +35,7 @@ dependencies { testCompile 'org.awaitility:awaitility' testCompile 'org.mockito:mockito-core' testCompile 'org.slf4j:slf4j-api' - + testCompile testFixtures(project(':server:sonar-db-dao')) } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java index cd7aa8c5ba5..d05bf940db4 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java @@ -24,7 +24,7 @@ import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.concurrent.locks.Lock; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.ce.taskprocessor.CeWorker; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java index 90409c1abe5..7f9e14f4ec4 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java @@ -23,6 +23,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.MessageException; +import org.springframework.beans.factory.annotation.Autowired; import static java.lang.String.format; import static org.sonar.process.ProcessProperties.Property.CE_GRACEFUL_STOP_TIMEOUT; @@ -47,10 +48,12 @@ public class CeConfigurationImpl implements CeConfiguration { private final long gracefulStopTimeoutInMs; private int workerCount; + @Autowired(required = false) public CeConfigurationImpl(Configuration configuration) { this(configuration, null); } + @Autowired(required = false) public CeConfigurationImpl(Configuration configuration, @Nullable WorkerCountProvider workerCountProvider) { this.workerCountProvider = workerCountProvider; this.gracefulStopTimeoutInMs = configuration.getLong(CE_GRACEFUL_STOP_TIMEOUT.getKey()) diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeWorkerCountSettingWarning.java b/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeWorkerCountSettingWarning.java index 04314bd1622..600af1d5a30 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeWorkerCountSettingWarning.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeWorkerCountSettingWarning.java @@ -19,7 +19,7 @@ */ package org.sonar.ce.configuration; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java index bd8fe3ccfba..5e2a0f50a95 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/CePluginRepository.java @@ -28,7 +28,7 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.Plugin; import org.sonar.api.utils.log.Loggers; import org.sonar.core.platform.ExplodedPlugin; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 0db125b2406..ed0329121e5 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -31,7 +31,6 @@ import org.sonar.api.internal.MetadataLoader; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.profiles.XMLProfileParser; import org.sonar.api.profiles.XMLProfileSerializer; -import org.sonar.api.resources.Languages; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.rules.AnnotationRuleParser; import org.sonar.api.server.profile.BuiltInQualityProfileAnnotationLoader; @@ -71,19 +70,19 @@ import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.config.CorePropertyDefinitions; import org.sonar.core.extension.CoreExtensionRepositoryImpl; import org.sonar.core.extension.CoreExtensionsLoader; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.language.LanguagesProvider; +import org.sonar.core.platform.Container; import org.sonar.core.platform.EditionProvider; -import org.sonar.core.platform.Module; import org.sonar.core.platform.PlatformEditionProvider; import org.sonar.core.platform.PluginClassLoader; import org.sonar.core.platform.PluginClassloaderFactory; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.core.util.UuidFactoryImpl; import org.sonar.db.DBSessionsImpl; import org.sonar.db.DaoModule; import org.sonar.db.DbClient; import org.sonar.db.DefaultDatabase; import org.sonar.db.MyBatis; -import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.purge.PurgeProfiler; import org.sonar.process.NetworkUtilsImpl; @@ -161,11 +160,11 @@ import static org.sonar.process.ProcessProperties.Property.SONARCLOUD_ENABLED; public class ComputeEngineContainerImpl implements ComputeEngineContainer { - private ComputeEngineStatus computeEngineStatus; + private ComputeEngineStatus computeEngineStatus = null; @CheckForNull - private ComponentContainer level1; + private SpringComponentContainer level1 = null; @CheckForNull - private ComponentContainer level4; + private SpringComponentContainer level4 = null; @Override public void setComputeEngineStatus(ComputeEngineStatus computeEngineStatus) { @@ -174,24 +173,20 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { @Override public ComputeEngineContainer start(Props props) { - this.level1 = new ComponentContainer(); + this.level1 = new SpringComponentContainer(); populateLevel1(this.level1, props, requireNonNull(computeEngineStatus)); - configureFromModules(this.level1); startLevel1(this.level1); - ComponentContainer level2 = this.level1.createChild(); + SpringComponentContainer level2 = this.level1.createChild(); populateLevel2(level2); - configureFromModules(level2); startLevel2(level2); - ComponentContainer level3 = level2.createChild(); + SpringComponentContainer level3 = level2.createChild(); populateLevel3(level3); - configureFromModules(level3); startLevel3(level3); this.level4 = level3.createChild(); populateLevel4(this.level4, props); - configureFromModules(this.level4); startLevel4(this.level4); startupTasks(); @@ -199,36 +194,32 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { return this; } - private static void startLevel1(ComponentContainer level1) { + private static void startLevel1(SpringComponentContainer level1) { + level1.startComponents(); level1.getComponentByType(CoreExtensionsLoader.class) .load(); level1.getComponentByType(CECoreExtensionsInstaller.class) .install(level1, hasPlatformLevel(1), noAdditionalSideFilter()); - - if (level1.getComponentByType(AuditPersister.class) == null) { - level1.add(NoOpAuditPersister.class); - } - level1.startComponents(); } - private static void startLevel2(ComponentContainer level2) { - level2.getComponentByType(CECoreExtensionsInstaller.class) + private static void startLevel2(SpringComponentContainer level2) { + level2.getParent().getComponentByType(CECoreExtensionsInstaller.class) .install(level2, hasPlatformLevel(2), noAdditionalSideFilter()); level2.startComponents(); } - private static void startLevel3(ComponentContainer level3) { - level3.getComponentByType(CECoreExtensionsInstaller.class) + private static void startLevel3(SpringComponentContainer level3) { + level3.getParent().getComponentByType(CECoreExtensionsInstaller.class) .install(level3, hasPlatformLevel(3), noAdditionalSideFilter()); level3.startComponents(); } - private static void startLevel4(ComponentContainer level4) { - level4.getComponentByType(CECoreExtensionsInstaller.class) + private static void startLevel4(SpringComponentContainer level4) { + level4.getParent().getComponentByType(CECoreExtensionsInstaller.class) .install(level4, hasPlatformLevel4OrNone(), noAdditionalSideFilter()); - level4.getComponentByType(ServerExtensionInstaller.class) + level4.getParent().getComponentByType(ServerExtensionInstaller.class) .installExtensions(level4); level4.startComponents(); @@ -239,7 +230,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { } private void startupTasks() { - ComponentContainer startupLevel = this.level4.createChild(); + SpringComponentContainer startupLevel = this.level4.createChild(); startupLevel.add(startupComponents()); startupLevel.startComponents(); // done in PlatformLevelStartup @@ -272,11 +263,11 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { } @VisibleForTesting - protected ComponentContainer getComponentContainer() { + protected SpringComponentContainer getComponentContainer() { return level4; } - private static void populateLevel1(ComponentContainer container, Props props, ComputeEngineStatus computeEngineStatus) { + private static void populateLevel1(Container container, Props props, ComputeEngineStatus computeEngineStatus) { Version apiVersion = MetadataLoader.loadVersion(System2.INSTANCE); SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE); container.add( @@ -300,19 +291,20 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { Clock.systemDefaultZone(), // DB - DaoModule.class, + new DaoModule(), ReadOnlyPropertiesDao.class, DBSessionsImpl.class, DbClient.class, // Elasticsearch - EsModule.class, + new EsModule(), // rules/qprofiles RuleIndex.class, new OkHttpClientProvider(), computeEngineStatus, + NoOpAuditPersister.class, CoreExtensionRepositoryImpl.class, CoreExtensionsLoader.class, @@ -320,9 +312,9 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { container.add(toArray(CorePropertyDefinitions.all())); } - private static void populateLevel2(ComponentContainer container) { + private static void populateLevel2(Container container) { container.add( - MigrationConfigurationModule.class, + new MigrationConfigurationModule(), DatabaseVersion.class, DatabaseCompatibility.class, @@ -347,7 +339,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { ); } - private static void populateLevel3(ComponentContainer container) { + private static void populateLevel3(Container container) { container.add( new StartupMetadataProvider(), JdbcUrlSanitizer.class, @@ -357,7 +349,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { SynchronousAsyncExecution.class); } - private static void populateLevel4(ComponentContainer container, Props props) { + private static void populateLevel4(Container container, Props props) { container.add( ResourceTypes.class, DefaultResourceTypes.get(), @@ -377,7 +369,8 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { RuleIndexer.class, // languages - Languages.class, // used by CommonRuleDefinitionsImpl + // used by CommonRuleDefinitionsImpl + LanguagesProvider.class, // measure MetricFinder.class, @@ -412,7 +405,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { NewIssuesNotificationHandler.newMetadata(), MyNewIssuesNotificationHandler.class, MyNewIssuesNotificationHandler.newMetadata(), - IssuesChangesNotificationModule.class, + new IssuesChangesNotificationModule(), // Notifications QGChangeEmailTemplate.class, @@ -420,7 +413,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { NotificationService.class, DefaultNotificationManager.class, EmailNotificationChannel.class, - ReportAnalysisFailureNotificationModule.class, + new ReportAnalysisFailureNotificationModule(), // System ServerLogging.class, @@ -433,21 +426,21 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { CoreExtensionStopper.class, // Compute engine (must be after Views and Developer Cockpit) - CeConfigurationModule.class, - CeQueueModule.class, - CeHttpModule.class, - CeTaskCommonsModule.class, - ProjectAnalysisTaskModule.class, - IssueSyncTaskModule.class, - AuditPurgeTaskModule.class, - CeTaskProcessorModule.class, + new CeConfigurationModule(), + new CeQueueModule(), + new CeHttpModule(), + new CeTaskCommonsModule(), + new ProjectAnalysisTaskModule(), + new IssueSyncTaskModule(), + new AuditPurgeTaskModule(), + new CeTaskProcessorModule(), OfficialDistribution.class, InternalPropertiesImpl.class, ProjectConfigurationFactory.class, // webhooks - WebhookModule.class, + new WebhookModule(), QualityGateFinder.class, QualityGateEvaluatorImpl.class @@ -462,7 +455,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { CEQueueStatusImpl.class); } else if (props.valueAsBoolean(CLUSTER_ENABLED.getKey())) { container.add( - CeCleaningModule.class, + new CeCleaningModule(), // system health CeDistributedInformationImpl.class, @@ -474,7 +467,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { DistributedCEQueueStatusImpl.class); } else { container.add( - CeCleaningModule.class, + new CeCleaningModule(), StandaloneCeDistributedInformation.class, CEQueueStatusImpl.class); } @@ -490,11 +483,4 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { private static Object[] toArray(List<?> list) { return list.toArray(new Object[list.size()]); } - - private static void configureFromModules(ComponentContainer container) { - List<Module> modules = container.getComponentsByType(Module.class); - for (Module module : modules) { - module.configure(container); - } - } } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java index c89d4d26292..53289686c41 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java @@ -29,7 +29,7 @@ import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.Properties; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.slf4j.LoggerFactory; import org.sonar.process.sharedmemoryfile.DefaultProcessCommands; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java index 78c6076f348..06b26bd035c 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java @@ -27,7 +27,7 @@ import fi.iki.elonen.NanoHTTPD.Response; * * <p> * Method {@link #register(ActionRegistry)} of the action will be called right before the HTTP server is started (server - * is started by the Pico Container). It's the action's responsibility to call the method + * is started by the ioc container). It's the action's responsibility to call the method * {@link ActionRegistry#register(ActionRegistry)} to register itself for a given path. * </p> * <p> diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeDatabaseMBeanImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeDatabaseMBeanImpl.java index 2fda18722c7..c392abd1e05 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeDatabaseMBeanImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeDatabaseMBeanImpl.java @@ -20,7 +20,7 @@ package org.sonar.ce.monitoring; import org.apache.commons.dbcp2.BasicDataSource; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.db.DbClient; import org.sonar.process.Jmx; import org.sonar.process.systeminfo.SystemInfoSection; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeTasksMBeanImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeTasksMBeanImpl.java index 9927d4e5b5a..ec7bc99fdbe 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeTasksMBeanImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/monitoring/CeTasksMBeanImpl.java @@ -22,7 +22,7 @@ package org.sonar.ce.monitoring; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.ce.configuration.CeConfiguration; import org.sonar.ce.taskprocessor.CeWorker; import org.sonar.ce.taskprocessor.CeWorkerController; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java b/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java index 916df2843a6..be950ba895d 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java @@ -21,14 +21,15 @@ package org.sonar.ce.platform; import org.sonar.api.SonarRuntime; import org.sonar.api.ce.ComputeEngineSide; +import org.sonar.api.config.Configuration; import org.sonar.core.platform.PluginRepository; import org.sonar.server.plugins.ServerExtensionInstaller; import static java.util.Collections.singleton; public class ComputeEngineExtensionInstaller extends ServerExtensionInstaller { - public ComputeEngineExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository) { - super(sonarRuntime, pluginRepository, singleton(ComputeEngineSide.class)); + public ComputeEngineExtensionInstaller(Configuration configuration, SonarRuntime sonarRuntime, PluginRepository pluginRepository) { + super(configuration, sonarRuntime, pluginRepository, singleton(ComputeEngineSide.class)); } } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/platform/DatabaseCompatibility.java b/server/sonar-ce/src/main/java/org/sonar/ce/platform/DatabaseCompatibility.java index c7925a69706..5ffaa8ae783 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/platform/DatabaseCompatibility.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/platform/DatabaseCompatibility.java @@ -19,7 +19,7 @@ */ package org.sonar.ce.platform; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.server.platform.db.migration.version.DatabaseVersion; import static com.google.common.base.Preconditions.checkState; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProvider.java b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProvider.java index 072d1243c92..386516d5c62 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProvider.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProvider.java @@ -19,28 +19,24 @@ */ package org.sonar.ce.taskprocessor; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.config.Configuration; import org.sonar.api.utils.System2; import org.sonar.ce.task.CeTaskInterrupter; +import org.springframework.context.annotation.Bean; import static com.google.common.base.Preconditions.checkState; -public class CeTaskInterrupterProvider extends ProviderAdapter { +public class CeTaskInterrupterProvider { private static final String PROPERTY_CE_TASK_TIMEOUT = "sonar.ce.task.timeoutSeconds"; - private CeTaskInterrupter instance; - + @Bean("CeTaskInterrupter") public CeTaskInterrupter provide(Configuration configuration, CeWorkerController ceWorkerController, System2 system2) { - if (instance == null) { - instance = configuration.getLong(PROPERTY_CE_TASK_TIMEOUT) - .filter(timeOutInSeconds -> { - checkState(timeOutInSeconds >= 1, "The property '%s' must be a long value >= 1. Got '%s'", PROPERTY_CE_TASK_TIMEOUT, timeOutInSeconds); - return true; - }) - .map(timeOutInSeconds -> (CeTaskInterrupter) new TimeoutCeTaskInterrupter(timeOutInSeconds * 1_000L, ceWorkerController, system2)) - .orElseGet(SimpleCeTaskInterrupter::new); - } - return instance; + return configuration.getLong(PROPERTY_CE_TASK_TIMEOUT) + .filter(timeOutInSeconds -> { + checkState(timeOutInSeconds >= 1, "The property '%s' must be a long value >= 1. Got '%s'", PROPERTY_CE_TASK_TIMEOUT, timeOutInSeconds); + return true; + }) + .map(timeOutInSeconds -> (CeTaskInterrupter) new TimeoutCeTaskInterrupter(timeOutInSeconds * 1_000L, ceWorkerController, system2)) + .orElseGet(SimpleCeTaskInterrupter::new); } } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskProcessorRepositoryImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskProcessorRepositoryImpl.java index ba52236abba..8496a8c158d 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskProcessorRepositoryImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeTaskProcessorRepositoryImpl.java @@ -36,7 +36,7 @@ import static java.lang.String.format; /** * {@link CeTaskProcessorRepository} implementation which provides access to the {@link CeTaskProcessor} existing in the - * PicoContainer the current object belongs to. + * ioc container the current object belongs to. */ public class CeTaskProcessorRepositoryImpl implements CeTaskProcessorRepository { private final Map<String, CeTaskProcessor> taskProcessorByCeTaskType; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerFactoryImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerFactoryImpl.java index dc8005b8e35..3f0cf3720dd 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerFactoryImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerFactoryImpl.java @@ -25,6 +25,7 @@ import java.util.stream.Stream; import org.sonar.ce.queue.InternalCeQueue; import org.sonar.core.util.UuidFactory; import org.sonar.core.util.stream.MoreCollectors; +import org.springframework.beans.factory.annotation.Autowired; public class CeWorkerFactoryImpl implements CeWorkerFactory { private final UuidFactory uuidFactory; @@ -35,15 +36,15 @@ public class CeWorkerFactoryImpl implements CeWorkerFactory { private Set<CeWorker> ceWorkers = Collections.emptySet(); /** - * Used by Pico when there is no {@link CeWorker.ExecutionListener} in the container. + * Used by the ioc container when there is no {@link CeWorker.ExecutionListener} in the container. */ - public CeWorkerFactoryImpl(InternalCeQueue queue, CeTaskProcessorRepository taskProcessorRepository, - UuidFactory uuidFactory, CeWorkerController ceWorkerController) { + @Autowired(required = false) + public CeWorkerFactoryImpl(InternalCeQueue queue, CeTaskProcessorRepository taskProcessorRepository, UuidFactory uuidFactory, CeWorkerController ceWorkerController) { this(queue, taskProcessorRepository, uuidFactory, ceWorkerController, new CeWorker.ExecutionListener[0]); } - public CeWorkerFactoryImpl(InternalCeQueue queue, CeTaskProcessorRepository taskProcessorRepository, - UuidFactory uuidFactory, CeWorkerController ceWorkerController, + @Autowired(required = false) + public CeWorkerFactoryImpl(InternalCeQueue queue, CeTaskProcessorRepository taskProcessorRepository, UuidFactory uuidFactory, CeWorkerController ceWorkerController, CeWorker.ExecutionListener[] executionListeners) { this.queue = queue; this.taskProcessorRepository = taskProcessorRepository; diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java index f23f1b17acb..8274a6f07e9 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java @@ -29,21 +29,21 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class ComputeEngineImplTest { - private ComputeEngineContainer computeEngineContainer = new NoOpComputeEngineContainer(); - private ComputeEngine underTest = new ComputeEngineImpl(new Props(new Properties()), computeEngineContainer); + private final ComputeEngineContainer computeEngineContainer = new NoOpComputeEngineContainer(); + private final ComputeEngine underTest = new ComputeEngineImpl(new Props(new Properties()), computeEngineContainer); @Test public void startup_throws_ISE_when_called_twice() { underTest.startup(); - assertThatThrownBy(() -> underTest.startup()) + assertThatThrownBy(underTest::startup) .isInstanceOf(IllegalStateException.class) .hasMessage("startup() can not be called multiple times"); } @Test public void stopProcessing_throws_ISE_if_startup_was_not_called_before() { - assertThatThrownBy(() -> underTest.stopProcessing()) + assertThatThrownBy(underTest::stopProcessing) .isInstanceOf(IllegalStateException.class) .hasMessage("stopProcessing() must not be called before startup()"); } @@ -53,7 +53,7 @@ public class ComputeEngineImplTest { underTest.startup(); underTest.shutdown(); - assertThatThrownBy(() -> underTest.stopProcessing()) + assertThatThrownBy(underTest::stopProcessing) .isInstanceOf(IllegalStateException.class) .hasMessage("stopProcessing() can not be called after shutdown()"); } @@ -63,14 +63,14 @@ public class ComputeEngineImplTest { underTest.startup(); underTest.stopProcessing(); - assertThatThrownBy(() -> underTest.stopProcessing()) + assertThatThrownBy(underTest::stopProcessing) .isInstanceOf(IllegalStateException.class) .hasMessage("stopProcessing() can not be called multiple times"); } @Test public void shutdown_throws_ISE_if_startup_was_not_called_before() { - assertThatThrownBy(() -> underTest.shutdown()) + assertThatThrownBy(underTest::shutdown) .isInstanceOf(IllegalStateException.class) .hasMessage("shutdown() must not be called before startup()"); } @@ -80,7 +80,7 @@ public class ComputeEngineImplTest { underTest.startup(); underTest.shutdown(); - assertThatThrownBy(() -> underTest.shutdown()) + assertThatThrownBy(underTest::shutdown) .isInstanceOf(IllegalStateException.class) .hasMessage("shutdown() can not be called multiple times"); } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index de401a23f52..3e14671ea1b 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -25,20 +25,15 @@ import java.io.IOException; import java.util.Date; import java.util.Locale; import java.util.Properties; -import java.util.stream.Collectors; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.picocontainer.ComponentAdapter; -import org.picocontainer.MutablePicoContainer; import org.sonar.api.CoreProperties; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; -import org.sonar.ce.CeDistributedInformationImpl; -import org.sonar.ce.StandaloneCeDistributedInformation; import org.sonar.core.extension.ServiceLoaderWrapper; import org.sonar.db.DbTester; import org.sonar.db.property.PropertyDto; @@ -46,6 +41,7 @@ import org.sonar.process.ProcessId; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; import org.sonar.server.property.InternalProperties; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import static java.lang.String.valueOf; import static org.assertj.core.api.Assertions.assertThat; @@ -68,14 +64,13 @@ public class ComputeEngineContainerImplTest { public DbTester db = DbTester.create(System2.INSTANCE); - private ComputeEngineContainerImpl underTest; - private ServiceLoaderWrapper serviceLoaderWrapper = mock(ServiceLoaderWrapper.class); - private ProcessProperties processProperties = new ProcessProperties(serviceLoaderWrapper); + private final ServiceLoaderWrapper serviceLoaderWrapper = mock(ServiceLoaderWrapper.class); + private final ProcessProperties processProperties = new ProcessProperties(serviceLoaderWrapper); + private final ComputeEngineContainerImpl underTest = new ComputeEngineContainerImpl(); @Before public void setUp() { when(serviceLoaderWrapper.load()).thenReturn(ImmutableSet.of()); - underTest = new ComputeEngineContainerImpl(); underTest.setComputeEngineStatus(mock(ComputeEngineStatus.class)); } @@ -95,26 +90,18 @@ public class ComputeEngineContainerImplTest { underTest.start(new Props(properties)); - MutablePicoContainer picoContainer = underTest.getComponentContainer().getPicoContainer(); + AnnotationConfigApplicationContext context = underTest.getComponentContainer().context(); try { - assertThat(picoContainer.getComponentAdapters()).hasSizeGreaterThan(1); - assertThat(picoContainer.getParent().getComponentAdapters()).hasSizeGreaterThan(1); - assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSizeGreaterThan(1); - assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSizeGreaterThan(1); - assertThat( - picoContainer.getComponentAdapters().stream() - .map(ComponentAdapter::getComponentImplementation) - .collect(Collectors.toList())).doesNotContain( - (Class) CeDistributedInformationImpl.class).contains( - StandaloneCeDistributedInformation.class); - assertThat(picoContainer.getParent().getParent().getParent().getParent()).isNull(); + assertThat(context.getBeanDefinitionNames()).hasSizeGreaterThan(1); + assertThat(context.getParent().getBeanDefinitionNames()).hasSizeGreaterThan(1); + assertThat(context.getParent().getParent().getBeanDefinitionNames()).hasSizeGreaterThan(1); + assertThat(context.getParent().getParent().getParent().getBeanDefinitionNames()).hasSizeGreaterThan(1); + assertThat(context.getParent().getParent().getParent().getParent()).isNull(); } finally { underTest.stop(); } - assertThat(picoContainer.getLifecycleState().isStarted()).isFalse(); - assertThat(picoContainer.getLifecycleState().isStopped()).isFalse(); - assertThat(picoContainer.getLifecycleState().isDisposed()).isTrue(); + assertThat(context.isActive()).isFalse(); } private String cleanJdbcUrl() { diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeDatabaseMBeanImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeDatabaseMBeanImplTest.java index b93f9dc7737..35894dd9fdb 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeDatabaseMBeanImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeDatabaseMBeanImplTest.java @@ -24,20 +24,29 @@ import javax.annotation.CheckForNull; import javax.management.InstanceNotFoundException; import javax.management.ObjectInstance; import javax.management.ObjectName; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.process.Jmx; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.ce.monitoring.CeDatabaseMBean.OBJECT_NAME; public class CeDatabaseMBeanImplTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - private CeDatabaseMBeanImpl underTest = new CeDatabaseMBeanImpl(dbTester.getDbClient()); + private final CeDatabaseMBeanImpl underTest = new CeDatabaseMBeanImpl(dbTester.getDbClient()); + + @BeforeClass + public static void beforeClass() { + // if any other class starts a container where CeDatabaseMBeanImpl is added, it will have been registered + Jmx.unregister(OBJECT_NAME); + } @Test public void register_and_unregister() throws Exception { @@ -62,7 +71,7 @@ public class CeDatabaseMBeanImplTest { @CheckForNull private ObjectInstance getMBean() throws Exception { try { - return ManagementFactory.getPlatformMBeanServer().getObjectInstance(new ObjectName(CeDatabaseMBean.OBJECT_NAME)); + return ManagementFactory.getPlatformMBeanServer().getObjectInstance(new ObjectName(OBJECT_NAME)); } catch (InstanceNotFoundException e) { return null; } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeTasksMBeanImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeTasksMBeanImplTest.java index 8e072f09d60..390f846546e 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeTasksMBeanImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/monitoring/CeTasksMBeanImplTest.java @@ -33,17 +33,20 @@ import javax.management.InstanceNotFoundException; import javax.management.ObjectInstance; import javax.management.ObjectName; import org.apache.commons.lang.RandomStringUtils; +import org.junit.BeforeClass; import org.junit.Test; import org.sonar.ce.configuration.CeConfiguration; import org.sonar.ce.taskprocessor.CeWorker; import org.sonar.ce.taskprocessor.CeWorkerController; import org.sonar.ce.taskprocessor.CeWorkerFactory; import org.sonar.core.util.stream.MoreCollectors; +import org.sonar.process.Jmx; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.ce.monitoring.CeTasksMBean.OBJECT_NAME; public class CeTasksMBeanImplTest { private static final long PENDING_COUNT = 2; @@ -63,8 +66,14 @@ public class CeTasksMBeanImplTest { }) .collect(MoreCollectors.toSet()); - private CeWorkerController ceWorkerController = mock(CeWorkerController.class); - private CeTasksMBeanImpl underTest = new CeTasksMBeanImpl(new DumbCEQueueStatus(), new DumbCeConfiguration(), new DumbCeWorkerFactory(), ceWorkerController); + private final CeWorkerController ceWorkerController = mock(CeWorkerController.class); + private final CeTasksMBeanImpl underTest = new CeTasksMBeanImpl(new DumbCEQueueStatus(), new DumbCeConfiguration(), new DumbCeWorkerFactory(), ceWorkerController); + + @BeforeClass + public static void beforeClass() { + // if any other class starts a container where CeTasksMBeanImpl is added, it will have been registered + Jmx.unregister(OBJECT_NAME); + } @Test public void register_and_unregister() throws Exception { @@ -84,7 +93,7 @@ public class CeTasksMBeanImplTest { @CheckForNull private ObjectInstance getMBean() throws Exception { try { - return ManagementFactory.getPlatformMBeanServer().getObjectInstance(new ObjectName(CeTasksMBean.OBJECT_NAME)); + return ManagementFactory.getPlatformMBeanServer().getObjectInstance(new ObjectName(OBJECT_NAME)); } catch (InstanceNotFoundException e) { return null; } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/platform/CECoreExtensionsInstallerTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/platform/CECoreExtensionsInstallerTest.java index 33e3616f6bd..6e270946699 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/platform/CECoreExtensionsInstallerTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/platform/CECoreExtensionsInstallerTest.java @@ -25,6 +25,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.stream.Stream; + import org.junit.Test; import org.sonar.api.SonarRuntime; import org.sonar.api.batch.ScannerSide; @@ -32,7 +33,7 @@ import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.core.extension.CoreExtension; import org.sonar.core.extension.CoreExtensionRepository; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -61,14 +62,13 @@ public class CECoreExtensionsInstallerTest { NoAnnotationClass.class, OtherAnnotationClass.class, MultipleAnnotationClass.class); } })); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.install(container, noExtensionFilter(), noAdditionalSideFilter()); - assertThat(container.getPicoContainer().getComponentAdapters()) - .hasSize(ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); - assertThat(container.getComponentByType(CeClass.class)).isNotNull(); - assertThat(container.getComponentByType(MultipleAnnotationClass.class)).isNotNull(); + assertThat(container.getAddedObjects()) + .hasSize(2) + .contains(CeClass.class, MultipleAnnotationClass.class); } @ComputeEngineSide diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProviderTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProviderTest.java index d1108df0260..77c5b75f5c0 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProviderTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskInterrupterProviderTest.java @@ -32,10 +32,10 @@ import static org.mockito.Mockito.mock; public class CeTaskInterrupterProviderTest { - private MapSettings settings = new MapSettings(); - private CeWorkerController ceWorkerController = mock(CeWorkerController.class); - private System2 system2 = mock(System2.class); - private CeTaskInterrupterProvider underTest = new CeTaskInterrupterProvider(); + private final MapSettings settings = new MapSettings(); + private final CeWorkerController ceWorkerController = mock(CeWorkerController.class); + private final System2 system2 = mock(System2.class); + private final CeTaskInterrupterProvider underTest = new CeTaskInterrupterProvider(); @Test public void provide_returns_a_SimpleCeTaskInterrupter_instance_if_configuration_is_empty() { @@ -46,15 +46,6 @@ public class CeTaskInterrupterProviderTest { } @Test - public void provide_always_return_the_same_SimpleCeTaskInterrupter_instance() { - CeTaskInterrupter instance = underTest.provide(settings.asConfig(), ceWorkerController, system2); - - assertThat(instance) - .isSameAs(underTest.provide(settings.asConfig(), ceWorkerController, system2)) - .isSameAs(underTest.provide(new MapSettings().asConfig(), ceWorkerController, system2)); - } - - @Test public void provide_returns_a_TimeoutCeTaskInterrupter_instance_if_property_taskTimeout_has_a_value() throws IllegalAccessException, NoSuchFieldException { int timeout = 1 + new Random().nextInt(2222); settings.setProperty("sonar.ce.task.timeoutSeconds", timeout); @@ -100,18 +91,6 @@ public class CeTaskInterrupterProviderTest { .hasMessage("The property 'sonar.ce.task.timeoutSeconds' must be a long value >= 1. Got '" + negativeValue + "'"); } - @Test - public void provide_always_return_the_same_TimeoutCeTaskInterrupter_instance() { - int timeout = 1 + new Random().nextInt(2222); - settings.setProperty("sonar.ce.task.timeoutSeconds", timeout); - - CeTaskInterrupter instance = underTest.provide(settings.asConfig(), ceWorkerController, system2); - - assertThat(instance) - .isSameAs(underTest.provide(settings.asConfig(), ceWorkerController, system2)) - .isSameAs(underTest.provide(new MapSettings().setProperty("sonar.ce.task.timeoutSeconds", 999).asConfig(), ceWorkerController, system2)); - } - private static Object readField(CeTaskInterrupter instance, String fieldName) throws NoSuchFieldException, IllegalAccessException { Class<?> clazz = instance.getClass(); Field timeoutField = clazz.getDeclaredField(fieldName); diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskProcessorModuleTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskProcessorModuleTest.java index 11d95dca8d0..3921b60b92e 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskProcessorModuleTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeTaskProcessorModuleTest.java @@ -20,46 +20,30 @@ package org.sonar.ce.taskprocessor; import org.junit.Test; -import org.picocontainer.ComponentAdapter; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import org.sonar.ce.notification.ReportAnalysisFailureNotificationExecutionListener; -import org.sonar.ce.task.CeTaskInterrupter; -import org.sonar.core.platform.ComponentContainer; - -import static org.assertj.core.api.Assertions.assertThat; +import org.sonar.core.platform.ExtensionContainer; public class CeTaskProcessorModuleTest { private CeTaskProcessorModule underTest = new CeTaskProcessorModule(); @Test - public void defines_CeWorker_ExecutionListener_for_CeLogging() { - ComponentContainer container = new ComponentContainer(); + public void defines_module() { + var container = mock(ExtensionContainer.class); underTest.configure(container); - assertThat(container.getPicoContainer().getComponentAdapters(CeWorker.ExecutionListener.class) - .stream() - .map(ComponentAdapter::getComponentImplementation)) - .contains(CeLoggingWorkerExecutionListener.class); - } - - @Test - public void defines_ExecutionListener_for_report_processing_failure_notifications() { - ComponentContainer container = new ComponentContainer(); - - underTest.configure(container); + verify(container).add(CeTaskProcessorRepositoryImpl.class); + verify(container).add(CeLoggingWorkerExecutionListener.class); + verify(container).add(ReportAnalysisFailureNotificationExecutionListener.class); + verify(container).add(any(CeTaskInterrupterProvider.class)); + verify(container).add(CeTaskInterrupterWorkerExecutionListener.class); + verify(container).add(CeWorkerFactoryImpl.class); + verify(container).add(CeWorkerControllerImpl.class); + verify(container).add(CeProcessingSchedulerExecutorServiceImpl.class); + verify(container).add(CeProcessingSchedulerImpl.class); - assertThat(container.getPicoContainer().getComponentAdapters(CeWorker.ExecutionListener.class) - .stream() - .map(ComponentAdapter::getComponentImplementation)) - .contains(ReportAnalysisFailureNotificationExecutionListener.class); - } - - @Test - public void defines_CeTaskInterrupterProvider_object() { - ComponentContainer container = new ComponentContainer(); - - underTest.configure(container); - assertThat(container.getPicoContainer().getComponentAdapter(CeTaskInterrupter.class)) - .isInstanceOf(CeTaskInterrupterProvider.class); } } diff --git a/server/sonar-db-core/build.gradle b/server/sonar-db-core/build.gradle index 2466304fb75..3e1290bfd7c 100644 --- a/server/sonar-db-core/build.gradle +++ b/server/sonar-db-core/build.gradle @@ -14,7 +14,6 @@ dependencies { compile 'commons-lang:commons-lang' compile 'org.apache.commons:commons-dbcp2' compile 'org.mybatis:mybatis' - compile 'org.picocontainer:picocontainer' compile 'org.slf4j:slf4j-api' compile project(':server:sonar-process') compile project(path: ':sonar-plugin-api', configuration: 'shadow') diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/Database.java b/server/sonar-db-core/src/main/java/org/sonar/db/Database.java index 515e4b6fcf2..d9071ece541 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/Database.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/Database.java @@ -20,7 +20,7 @@ package org.sonar.db; import javax.sql.DataSource; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.db.dialect.Dialect; /** diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index bf75d7a5965..d128c08158f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -157,16 +157,19 @@ import org.sonar.db.user.UserTokenDto; import org.sonar.db.user.UserTokenMapper; import org.sonar.db.webhook.WebhookDeliveryMapper; import org.sonar.db.webhook.WebhookMapper; +import org.springframework.beans.factory.annotation.Autowired; public class MyBatis implements Startable { private final List<MyBatisConfExtension> confExtensions; private final Database database; private SqlSessionFactory sessionFactory; + @Autowired(required = false) public MyBatis(Database database) { this(database, null); } + @Autowired(required = false) public MyBatis(Database database, @Nullable MyBatisConfExtension[] confExtensions) { this.confExtensions = confExtensions == null ? Collections.emptyList() : Arrays.asList(confExtensions); this.database = database; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java index eb1d6a09169..2567bfbc8f0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java @@ -19,6 +19,7 @@ */ package org.sonar.db.audit; +import javax.annotation.Priority; import org.sonar.db.DbSession; import org.sonar.db.audit.model.ComponentKeyNewValue; import org.sonar.db.audit.model.ComponentNewValue; @@ -38,6 +39,7 @@ import org.sonar.db.audit.model.UserPermissionNewValue; import org.sonar.db.audit.model.UserTokenNewValue; import org.sonar.db.audit.model.WebhookNewValue; +@Priority(2) public class NoOpAuditPersister implements AuditPersister { @Override public void addUserGroup(DbSession dbSession, UserGroupNewValue newValue) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java index 235b832177a..132e5b08c93 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java @@ -57,11 +57,7 @@ public class InternalPropertiesDao implements Dao { private static final Optional<String> OPTIONAL_OF_EMPTY_STRING = Optional.of(""); private final System2 system2; - private AuditPersister auditPersister; - - public InternalPropertiesDao(System2 system2) { - this.system2 = system2; - } + private final AuditPersister auditPersister; public InternalPropertiesDao(System2 system2, AuditPersister auditPersister) { this.system2 = system2; @@ -89,7 +85,7 @@ public class InternalPropertiesDao implements Dao { mapper.insertAsText(key, value, now); } - if (auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (auditPersister.isTrackedProperty(key)) { if (deletedRows > 0) { auditPersister.updateProperty(dbSession, new PropertyNewValue(key, value), false); } else { @@ -112,7 +108,7 @@ public class InternalPropertiesDao implements Dao { int deletedRows = mapper.deleteByKey(key); mapper.insertAsEmpty(key, system2.now()); - if (auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (auditPersister.isTrackedProperty(key)) { if (deletedRows > 0) { auditPersister.updateProperty(dbSession, new PropertyNewValue(key, ""), false); } else { @@ -124,7 +120,7 @@ public class InternalPropertiesDao implements Dao { public void delete(DbSession dbSession, String key) { int deletedRows = getMapper(dbSession).deleteByKey(key); - if (auditPersister != null && deletedRows > 0 && auditPersister.isTrackedProperty(key)) { + if (deletedRows > 0 && auditPersister.isTrackedProperty(key)) { auditPersister.deleteProperty(dbSession, new PropertyNewValue(key), false); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java index b312959a5ce..96f261556af 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.db; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class DaoModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new DaoModule().configure(container); - assertThat(container.size()).isGreaterThan(1); + assertThat(container.getAddedObjects()).hasSizeGreaterThan(1); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java index 63b11946b31..4414ef3309a 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java @@ -21,7 +21,6 @@ package org.sonar.db; import org.apache.ibatis.session.Configuration; import org.hamcrest.core.Is; -import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -46,11 +45,6 @@ public class MyBatisTest { private MyBatis underTest = new MyBatis(database); - @After - public void tearDown() { - underTest.stop(); - } - @Test public void shouldConfigureMyBatis() { underTest.start(); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java index 9e3e83f17e8..d8d9e8bb483 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.stream.Stream; import javax.annotation.Nullable; import org.apache.commons.dbcp2.BasicDataSource; -import org.picocontainer.containers.TransientPicoContainer; import org.sonar.api.utils.System2; import org.sonar.core.util.SequenceUuidFactory; import org.sonar.core.util.UuidFactory; @@ -145,15 +144,16 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { } private void initDbClient() { - TransientPicoContainer ioc = new TransientPicoContainer(); - ioc.addComponent(auditPersister); - ioc.addComponent(db.getMyBatis()); - ioc.addComponent(system2); - ioc.addComponent(uuidFactory); - for (Class daoClass : DaoModule.classes()) { - ioc.addComponent(daoClass); + FastSpringContainer ioc = new FastSpringContainer(); + ioc.add(auditPersister); + ioc.add(db.getMyBatis()); + ioc.add(system2); + ioc.add(uuidFactory); + for (Class<?> daoClass : DaoModule.classes()) { + ioc.add(daoClass); } - List<Dao> daos = ioc.getComponents(Dao.class); + ioc.start(); + List<Dao> daos = ioc.getComponentsByType(Dao.class); client = new DbClient(db.getDatabase(), db.getMyBatis(), new TestDBSessions(db.getMyBatis()), daos.toArray(new Dao[daos.size()])); } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/FastSpringContainer.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/FastSpringContainer.java new file mode 100644 index 00000000000..47b5cded6b8 --- /dev/null +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/FastSpringContainer.java @@ -0,0 +1,104 @@ +/* + * 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.db; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; +import org.sonar.core.platform.Container; +import org.sonar.core.platform.StartableBeanPostProcessor; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory; +import org.springframework.context.support.GenericApplicationContext; + +/** + * A fast(er) Spring container. It doesn't support several things that are supported in + * {@link org.sonar.core.platform.SpringComponentContainer}, such as: + * <ul> + * <li>Adding extensions</li> + * <li>Use of annotations</li> + * <li>Adding duplicate components from different Classloaders</li> + * <li>Hierarchy of container</li> + * <li>Different initialization strategies</li> + * </ul> + */ +public class FastSpringContainer implements Container { + private final GenericApplicationContext context = new GenericApplicationContext(); + + public FastSpringContainer() { + ((AbstractAutowireCapableBeanFactory) context.getBeanFactory()).setParameterNameDiscoverer(null); + add(StartableBeanPostProcessor.class); + } + + @Override + public Container add(Object... objects) { + for (Object o : objects) { + if (o instanceof Class) { + Class<?> clazz = (Class<?>) o; + context.registerBean(clazz.getName(), clazz); + } else { + registerInstance(o); + } + } + return this; + } + + public void start() { + context.refresh(); + } + + private <T> void registerInstance(T instance) { + Supplier<T> supplier = () -> instance; + Class<T> clazz = (Class<T>) instance.getClass(); + context.registerBean(clazz.getName(), clazz, supplier); + } + + @Override + public <T> T getComponentByType(Class<T> type) { + try { + return context.getBean(type); + } catch (Exception t) { + throw new IllegalStateException("Unable to load component " + type, t); + } + } + + @Override public <T> Optional<T> getOptionalComponentByType(Class<T> type) { + try { + return Optional.of(context.getBean(type)); + } catch (NoSuchBeanDefinitionException t) { + return Optional.empty(); + } + } + + @Override + public <T> List<T> getComponentsByType(Class<T> type) { + try { + return new ArrayList<>(context.getBeansOfType(type).values()); + } catch (Exception t) { + throw new IllegalStateException("Unable to load components " + type, t); + } + } + + @Override + public Container getParent() { + return null; + } +} diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/SQDatabase.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/SQDatabase.java index 169bf9763c2..dc515bfcd4f 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/SQDatabase.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/SQDatabase.java @@ -35,7 +35,8 @@ 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.ComponentContainer; +import org.sonar.core.platform.Container; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.core.util.UuidFactoryFast; import org.sonar.core.util.logs.Profiler; import org.sonar.db.dialect.Dialect; @@ -43,15 +44,12 @@ import org.sonar.process.logging.LogbackHelper; import org.sonar.server.platform.db.migration.MigrationConfigurationModule; import org.sonar.server.platform.db.migration.engine.MigrationContainer; import org.sonar.server.platform.db.migration.engine.MigrationContainerImpl; -import org.sonar.server.platform.db.migration.engine.MigrationContainerPopulator; -import org.sonar.server.platform.db.migration.engine.MigrationContainerPopulatorImpl; import org.sonar.server.platform.db.migration.history.MigrationHistoryTableImpl; import org.sonar.server.platform.db.migration.step.MigrationStep; import org.sonar.server.platform.db.migration.step.MigrationStepExecutionException; import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; -import org.sonar.server.platform.db.migration.version.DbVersion; import static com.google.common.base.Preconditions.checkState; @@ -101,29 +99,23 @@ public class SQDatabase extends DefaultDatabase { } } - public static final class H2MigrationContainerPopulator extends MigrationContainerPopulatorImpl { - public H2MigrationContainerPopulator(DbVersion... dbVersions) { - super(H2StepExecutor.class, dbVersions); - } - } - public static final class H2StepExecutor implements MigrationStepsExecutor { private static final String STEP_START_PATTERN = "{}..."; private static final String STEP_STOP_PATTERN = "{}: {}"; - private final ComponentContainer componentContainer; + private final Container container; - public H2StepExecutor(ComponentContainer componentContainer) { - this.componentContainer = componentContainer; + public H2StepExecutor(Container container) { + this.container = container; } @Override public void execute(List<RegisteredMigrationStep> steps) { - steps.forEach(step -> execute(step, componentContainer)); + steps.forEach(step -> execute(step, container)); } - private void execute(RegisteredMigrationStep step, ComponentContainer componentContainer) { - MigrationStep migrationStep = componentContainer.getComponentByType(step.getStepClass()); + private void execute(RegisteredMigrationStep step, Container container) { + MigrationStep migrationStep = container.getComponentByType(step.getStepClass()); checkState(migrationStep != null, "Can not find instance of " + step.getStepClass()); execute(step, migrationStep); @@ -149,24 +141,23 @@ public class SQDatabase extends DefaultDatabase { } private void executeDbMigrations(NoopDatabase noopDatabase) { - ComponentContainer parentContainer = new ComponentContainer(); - parentContainer.add(noopDatabase); - parentContainer.add(H2MigrationContainerPopulator.class); + SpringComponentContainer container = new SpringComponentContainer(); + container.add(noopDatabase); MigrationConfigurationModule migrationConfigurationModule = new MigrationConfigurationModule(); - migrationConfigurationModule.configure(parentContainer); + migrationConfigurationModule.configure(container); // dependencies required by DB migrations - parentContainer.add(SonarRuntimeImpl.forSonarQube(Version.create(8, 0), SonarQubeSide.SERVER, SonarEdition.COMMUNITY)); - parentContainer.add(UuidFactoryFast.getInstance()); - parentContainer.add(System2.INSTANCE); - parentContainer.add(MapSettings.class); + container.add(SonarRuntimeImpl.forSonarQube(Version.create(8, 0), SonarQubeSide.SERVER, SonarEdition.COMMUNITY)); + container.add(UuidFactoryFast.getInstance()); + container.add(System2.INSTANCE); + container.add(MapSettings.class); - parentContainer.startComponents(); - - MigrationContainer migrationContainer = new MigrationContainerImpl(parentContainer, parentContainer.getComponentByType(MigrationContainerPopulator.class)); + container.startComponents(); + MigrationContainer migrationContainer = new MigrationContainerImpl(container, H2StepExecutor.class); MigrationSteps migrationSteps = migrationContainer.getComponentByType(MigrationSteps.class); - migrationContainer.getComponentByType(MigrationStepsExecutor.class) - .execute(migrationSteps.readAll()); + MigrationStepsExecutor executor = migrationContainer.getComponentByType(MigrationStepsExecutor.class); + + executor.execute(migrationSteps.readAll()); } private void createMigrationHistoryTable(NoopDatabase noopDatabase) { diff --git a/server/sonar-db-migration/build.gradle b/server/sonar-db-migration/build.gradle index 466b47e251b..1cb23f4fdce 100644 --- a/server/sonar-db-migration/build.gradle +++ b/server/sonar-db-migration/build.gradle @@ -11,7 +11,6 @@ dependencies { compile 'commons-lang:commons-lang' compile 'commons-codec:commons-codec' compile 'com.fasterxml.staxmate:staxmate' - compile 'org.picocontainer:picocontainer' compile project(':server:sonar-db-core') compile project(':server:sonar-process') diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java index e24c7f9e6e2..7598ec022bf 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java @@ -26,10 +26,6 @@ public interface DatabaseMigration { * <p> * Migration can not be started twice but calling this method wont raise an error. * </p> - * <p> - * <strong>This method should be named {@code start} but it can not be because it will be called by the pico container - * and this will cause unwanted behavior</strong> - * </p> */ void startIt(); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java index 6351152b24a..a6e8762c030 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java @@ -22,6 +22,7 @@ package org.sonar.server.platform.db.migration; import org.sonar.core.platform.Module; import org.sonar.server.platform.db.migration.history.MigrationHistoryImpl; import org.sonar.server.platform.db.migration.history.MigrationHistoryMeddler; +import org.sonar.server.platform.db.migration.history.MigrationHistoryTableImpl; import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder; import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator; import org.sonar.server.platform.db.migration.step.MigrationStepRegistryImpl; @@ -36,6 +37,7 @@ public class MigrationConfigurationModule extends Module { @Override protected void configureModule() { add( + MigrationHistoryTableImpl.class, // DbVersion implementations DbVersion00.class, DbVersion90.class, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationEngineModule.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationEngineModule.java index 47aaeadb3a0..5632ee164ae 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationEngineModule.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationEngineModule.java @@ -20,8 +20,8 @@ package org.sonar.server.platform.db.migration; import org.sonar.core.platform.Module; -import org.sonar.server.platform.db.migration.engine.MigrationContainerPopulatorImpl; import org.sonar.server.platform.db.migration.engine.MigrationEngineImpl; +import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl; /** * Defines the components for the migration engine. @@ -30,7 +30,7 @@ public class MigrationEngineModule extends Module { @Override protected void configureModule() { add( - MigrationContainerPopulatorImpl.class, + MigrationStepsExecutorImpl.class, MigrationEngineImpl.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/charset/DatabaseCharsetChecker.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/charset/DatabaseCharsetChecker.java index 8e222273a43..d046b1730d7 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/charset/DatabaseCharsetChecker.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/charset/DatabaseCharsetChecker.java @@ -23,6 +23,8 @@ import com.google.common.annotations.VisibleForTesting; import java.sql.Connection; import java.sql.SQLException; import javax.annotation.CheckForNull; +import javax.inject.Inject; + import org.sonar.db.Database; import org.sonar.db.dialect.Dialect; import org.sonar.db.dialect.H2; @@ -46,6 +48,7 @@ public class DatabaseCharsetChecker { private final Database db; private final SqlExecutor sqlExecutor; + @Inject public DatabaseCharsetChecker(Database db) { this(db, new SqlExecutor()); } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImpl.java index 7785c369247..1977660bdbe 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImpl.java @@ -19,42 +19,35 @@ */ package org.sonar.server.platform.db.migration.engine; -import org.picocontainer.ComponentAdapter; -import org.picocontainer.DefaultPicoContainer; -import org.picocontainer.LifecycleStrategy; -import org.picocontainer.MutablePicoContainer; -import org.picocontainer.behaviors.OptInCaching; -import org.picocontainer.monitors.NullComponentMonitor; +import java.util.HashSet; +import java.util.Set; import org.sonar.api.config.PropertyDefinitions; -import org.sonar.core.platform.ComponentContainer; -import org.sonar.core.platform.StartableCloseableSafeLifecyleStrategy; +import org.sonar.core.platform.LazyStrategy; +import org.sonar.core.platform.SpringComponentContainer; +import org.sonar.server.platform.db.migration.step.MigrationStep; +import org.sonar.server.platform.db.migration.step.MigrationSteps; +import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor; -import static java.util.Objects.requireNonNull; +import static java.util.Collections.emptyList; -public class MigrationContainerImpl extends ComponentContainer implements MigrationContainer { +public class MigrationContainerImpl extends SpringComponentContainer implements MigrationContainer { - public MigrationContainerImpl(ComponentContainer parent, MigrationContainerPopulator populator) { - super(createContainer(requireNonNull(parent)), parent.getComponentByType(PropertyDefinitions.class)); - - populateContainer(requireNonNull(populator)); + public MigrationContainerImpl(SpringComponentContainer parent, Class<? extends MigrationStepsExecutor> executor) { + super(parent, parent.getComponentByType(PropertyDefinitions.class), emptyList(), new LazyStrategy()); + add(executor); + addSteps(parent.getComponentByType(MigrationSteps.class)); startComponents(); } - private void populateContainer(MigrationContainerPopulator populator) { - populator.populateContainer(this); - } - - /** - * Creates a PicContainer which extends the specified ComponentContainer <strong>but is not referenced in return</strong>. - */ - private static MutablePicoContainer createContainer(ComponentContainer parent) { - LifecycleStrategy lifecycleStrategy = new StartableCloseableSafeLifecyleStrategy() { - @Override - public boolean isLazy(ComponentAdapter<?> adapter) { - return true; + private void addSteps(MigrationSteps migrationSteps) { + Set<Class<? extends MigrationStep>> classes = new HashSet<>(); + migrationSteps.readAll().forEach(step -> { + Class<? extends MigrationStep> stepClass = step.getStepClass(); + if (!classes.contains(stepClass)) { + add(stepClass); + classes.add(stepClass); } - }; - return new DefaultPicoContainer(new OptInCaching(), lifecycleStrategy, parent.getPicoContainer(), new NullComponentMonitor()); + }); } @Override diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImpl.java index 0b9469d1085..22a92d3c059 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImpl.java @@ -32,37 +32,26 @@ import org.sonar.server.platform.db.migration.version.DbVersion; * Responsible for: * <ul> * <li>adding all the {@link MigrationStep} classes to the container after building it</li> - * <li>adding dependencies for them to the container if there aren't already available in parent container - * (see {@link DbVersion#getSupportComponents()})</li> * <li>adding the {@link MigrationStepsExecutorImpl} to the container</li> * </ul> */ public class MigrationContainerPopulatorImpl implements MigrationContainerPopulator { - private final DbVersion[] dbVersions; private final Class<? extends MigrationStepsExecutor> executorType; - public MigrationContainerPopulatorImpl(DbVersion... dbVersions) { - this(MigrationStepsExecutorImpl.class, dbVersions); + public MigrationContainerPopulatorImpl() { + this(MigrationStepsExecutorImpl.class); } - protected MigrationContainerPopulatorImpl(Class<? extends MigrationStepsExecutor> executorType, DbVersion... dbVersions) { - this.dbVersions = dbVersions; + protected MigrationContainerPopulatorImpl(Class<? extends MigrationStepsExecutor> executorType) { this.executorType = executorType; } @Override public void populateContainer(MigrationContainer container) { container.add(executorType); - populateFromDbVersion(container); populateFromMigrationSteps(container); } - private void populateFromDbVersion(MigrationContainer container) { - Arrays.stream(dbVersions) - .flatMap(DbVersion::getSupportComponents) - .forEach(container::add); - } - private static void populateFromMigrationSteps(MigrationContainer container) { MigrationSteps migrationSteps = container.getComponentByType(MigrationSteps.class); Set<Class<? extends MigrationStep>> classes = new HashSet<>(); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImpl.java index b4cb6d18e1d..0ceee8817f2 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImpl.java @@ -23,35 +23,33 @@ import java.util.List; import java.util.Optional; import org.sonar.api.config.Configuration; import org.sonar.api.utils.AnnotationUtils; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.process.ProcessProperties; import org.sonar.server.platform.db.migration.SupportsBlueGreen; import org.sonar.server.platform.db.migration.history.MigrationHistory; import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor; +import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; import static java.lang.String.format; public class MigrationEngineImpl implements MigrationEngine { private final MigrationHistory migrationHistory; - private final ComponentContainer serverContainer; - private final MigrationContainerPopulator populator; + private final SpringComponentContainer serverContainer; private final MigrationSteps migrationSteps; private final Configuration configuration; - public MigrationEngineImpl(MigrationHistory migrationHistory, ComponentContainer serverContainer, - MigrationContainerPopulator populator, MigrationSteps migrationSteps, Configuration configuration) { + public MigrationEngineImpl(MigrationHistory migrationHistory, SpringComponentContainer serverContainer, MigrationSteps migrationSteps, Configuration configuration) { this.migrationHistory = migrationHistory; this.serverContainer = serverContainer; - this.populator = populator; this.migrationSteps = migrationSteps; this.configuration = configuration; } @Override public void execute() { - MigrationContainer migrationContainer = new MigrationContainerImpl(serverContainer, populator); + MigrationContainer migrationContainer = new MigrationContainerImpl(serverContainer, MigrationStepsExecutorImpl.class); boolean blueGreen = configuration.getBoolean(ProcessProperties.Property.BLUE_GREEN_ENABLED.getKey()).orElse(false); try { MigrationStepsExecutor stepsExecutor = migrationContainer.getComponentByType(MigrationStepsExecutor.class); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java index 8aca6291592..e8ab9e4bc25 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java @@ -22,8 +22,8 @@ package org.sonar.server.platform.db.migration.step; import java.util.List; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.core.platform.Container; import org.sonar.core.util.logs.Profiler; -import org.sonar.server.platform.db.migration.engine.MigrationContainer; import org.sonar.server.platform.db.migration.history.MigrationHistory; import static com.google.common.base.Preconditions.checkState; @@ -35,10 +35,10 @@ public class MigrationStepsExecutorImpl implements MigrationStepsExecutor { private static final String STEP_START_PATTERN = "{}..."; private static final String STEP_STOP_PATTERN = "{}: {}"; - private final MigrationContainer migrationContainer; + private final Container migrationContainer; private final MigrationHistory migrationHistory; - public MigrationStepsExecutorImpl(MigrationContainer migrationContainer, MigrationHistory migrationHistory) { + public MigrationStepsExecutorImpl(Container migrationContainer, MigrationHistory migrationHistory) { this.migrationContainer = migrationContainer; this.migrationHistory = migrationHistory; } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsProvider.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsProvider.java index ab10e8b1cb9..d5213656d28 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsProvider.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsProvider.java @@ -20,24 +20,16 @@ package org.sonar.server.platform.db.migration.step; import java.util.Arrays; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.server.platform.db.migration.version.DbVersion; +import org.springframework.context.annotation.Bean; /** * This class is responsible for providing the {@link MigrationSteps} to be injected in classes that need it and * ensures that there's only one such instance. */ -public class MigrationStepsProvider extends ProviderAdapter { - private MigrationSteps migrationSteps; - +public class MigrationStepsProvider { + @Bean("MigrationSteps") public MigrationSteps provide(InternalMigrationStepRegistry migrationStepRegistry, DbVersion... dbVersions) { - if (migrationSteps == null) { - migrationSteps = buildMigrationSteps(migrationStepRegistry, dbVersions); - } - return migrationSteps; - } - - private static MigrationSteps buildMigrationSteps(InternalMigrationStepRegistry migrationStepRegistry, DbVersion[] dbVersions) { Arrays.stream(dbVersions).forEach(dbVersion -> dbVersion.addSteps(migrationStepRegistry)); return migrationStepRegistry.build(); } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/DbVersion.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/DbVersion.java index 2d831fc1b35..e3be1104258 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/DbVersion.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/DbVersion.java @@ -19,21 +19,8 @@ */ package org.sonar.server.platform.db.migration.version; -import java.util.stream.Stream; import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; public interface DbVersion { - /** - * Components (if any) supporting the {@link org.sonar.server.platform.db.migration.step.MigrationStep} classes - * added to the registry in {@link #addSteps(MigrationStepRegistry)}. - * <p> - * These components will be added to the {@link org.sonar.server.platform.db.migration.engine.MigrationContainer} in - * which the {@link org.sonar.server.platform.db.migration.step.MigrationStep} classes will be instantiated and run. - * </p> - */ - default Stream<Object> getSupportComponents() { - return Stream.empty(); - } - void addSteps(MigrationStepRegistry registry); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java index 0e4814f45f2..711fc3fc0a2 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java @@ -20,7 +20,7 @@ package org.sonar.server.platform.db.migration; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; @@ -29,11 +29,11 @@ public class MigrationConfigurationModuleTest { @Test public void verify_component_count() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.getPicoContainer().getComponentAdapters()).isNotEmpty(); + assertThat(container.getAddedObjects()).isNotEmpty(); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationEngineModuleTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationEngineModuleTest.java index 274db07c793..d04ce11ed72 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationEngineModuleTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationEngineModuleTest.java @@ -20,21 +20,19 @@ package org.sonar.server.platform.db.migration; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class MigrationEngineModuleTest { private MigrationEngineModule underTest = new MigrationEngineModule(); @Test public void verify_component_count() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.getPicoContainer().getComponentAdapters()) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); + assertThat(container.getAddedObjects()).hasSize(2); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java index 1e3a9dd4509..f0534766f3c 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java @@ -19,31 +19,48 @@ */ package org.sonar.server.platform.db.migration.engine; +import java.sql.SQLException; +import java.util.List; +import org.junit.Before; import org.junit.Test; -import org.picocontainer.Startable; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.api.Startable; +import org.sonar.core.platform.SpringComponentContainer; +import org.sonar.server.platform.db.migration.step.InternalMigrationStepRegistry; +import org.sonar.server.platform.db.migration.step.MigrationStep; +import org.sonar.server.platform.db.migration.step.MigrationStepRegistryImpl; +import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor; +import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; import static org.assertj.core.api.Assertions.assertThat; public class MigrationContainerImplTest { - private ComponentContainer parent = new ComponentContainer(); - private MigrationContainerPopulator populator = container -> container.add(StartCallCounter.class); - - private MigrationContainerImpl underTest = new MigrationContainerImpl(parent, populator); + private final SpringComponentContainer parent = new SpringComponentContainer(); + private MigrationContainerImpl underTest; + + @Before + public void setUp() { + InternalMigrationStepRegistry registry = new MigrationStepRegistryImpl(); + registry.add(1, "test", NoOpMigrationStep.class); + + parent.add(registry.build()); + parent.startComponents(); + underTest = new MigrationContainerImpl(parent, NoOpExecutor.class); + underTest.add(StartCallCounter.class); + } @Test - public void pico_container_of_migration_container_has_pico_container_of_specified_container_as_parent() { - assertThat(underTest.getPicoContainer().getParent()).isEqualTo(parent.getPicoContainer()); + public void adds_migration_steps_to_migration_container() { + assertThat(underTest.getComponentByType(MigrationStep.class)).isInstanceOf(NoOpMigrationStep.class); } @Test - public void pico_container_of_parent_does_not_have_pico_container_of_migration_container_as_child() { - assertThat(parent.getPicoContainer().removeChildContainer(underTest.getPicoContainer())).isFalse(); + public void context_of_migration_container_has_specified_context_as_parent() { + assertThat(underTest.context().getParent()).isEqualTo(parent.context()); } @Test - public void pico_container_of_migration_container_is_started_in_constructor() { - assertThat(underTest.getPicoContainer().getLifecycleState().isStarted()).isTrue(); + public void context_of_migration_container_is_started_in_constructor() { + assertThat(underTest.context().isActive()).isTrue(); } @Test @@ -58,11 +75,26 @@ public class MigrationContainerImplTest { @Test public void cleanup_does_not_fail_even_if_stop_of_component_fails() { - MigrationContainerImpl underTest = new MigrationContainerImpl(parent, (container -> container.add(StopFailing.class))); + parent.add(StopFailing.class); + MigrationContainerImpl underTest = new MigrationContainerImpl(parent, NoOpExecutor.class); underTest.cleanup(); } + private static class NoOpExecutor implements MigrationStepsExecutor { + @Override + public void execute(List<RegisteredMigrationStep> steps) { + + } + } + + private static class NoOpMigrationStep implements MigrationStep { + @Override + public void execute() throws SQLException { + + } + } + public static final class StartCallCounter implements Startable { private static int startCalls = 0; diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java index fb937d4c77f..2ce177c874e 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java @@ -19,16 +19,13 @@ */ package org.sonar.server.platform.db.migration.engine; -import java.util.stream.Stream; import org.junit.Before; import org.junit.Test; import org.sonar.server.platform.db.migration.history.MigrationHistory; import org.sonar.server.platform.db.migration.step.MigrationStep; -import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; -import org.sonar.server.platform.db.migration.version.DbVersion; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; @@ -37,9 +34,9 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class MigrationContainerPopulatorImplTest { - private MigrationContainer migrationContainer = new SimpleMigrationContainer(); - private MigrationSteps migrationSteps = mock(MigrationSteps.class); - private MigrationContainerPopulatorImpl underTest = new MigrationContainerPopulatorImpl(); + private final SimpleMigrationContainer migrationContainer = new SimpleMigrationContainer(); + private final MigrationSteps migrationSteps = mock(MigrationSteps.class); + private final MigrationContainerPopulatorImpl underTest = new MigrationContainerPopulatorImpl(); @Before public void setUp() { @@ -47,37 +44,13 @@ public class MigrationContainerPopulatorImplTest { } @Test - public void populateContainer_adds_components_of_DbVersion_getSupportComponents() { - MigrationContainerPopulatorImpl underTest = new MigrationContainerPopulatorImpl( - new NoRegistryDbVersion() { - @Override - public Stream<Object> getSupportComponents() { - return Stream.of(Clazz2.class); - } - }, - new NoRegistryDbVersion(), - new NoRegistryDbVersion() { - @Override - public Stream<Object> getSupportComponents() { - return Stream.of(Clazz1.class, Clazz3.class); - } - }); - when(migrationSteps.readAll()).thenReturn(emptyList()); - - underTest.populateContainer(migrationContainer); - - assertThat(migrationContainer.getComponentsByType(Clazz1.class)).isNotNull(); - assertThat(migrationContainer.getComponentsByType(Clazz2.class)).isNotNull(); - assertThat(migrationContainer.getComponentsByType(Clazz3.class)).isNotNull(); - } - - @Test public void populateContainer_adds_MigrationStepsExecutorImpl() { when(migrationSteps.readAll()).thenReturn(emptyList()); // add MigrationStepsExecutorImpl's dependencies migrationContainer.add(mock(MigrationHistory.class)); + migrationContainer.startComponents(); underTest.populateContainer(migrationContainer); assertThat(migrationContainer.getComponentByType(MigrationStepsExecutorImpl.class)).isNotNull(); @@ -90,6 +63,7 @@ public class MigrationContainerPopulatorImplTest { new RegisteredMigrationStep(2, "bar", MigrationStep2.class), new RegisteredMigrationStep(3, "dor", MigrationStep3.class))); + migrationContainer.startComponents(); underTest.populateContainer(migrationContainer); assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull(); @@ -106,6 +80,7 @@ public class MigrationContainerPopulatorImplTest { new RegisteredMigrationStep(4, "foo2", MigrationStep1.class), new RegisteredMigrationStep(5, "dor", MigrationStep3.class))); + migrationContainer.startComponents(); underTest.populateContainer(migrationContainer); assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull(); @@ -143,16 +118,4 @@ public class MigrationContainerPopulatorImplTest { public static final class Clazz3 { } - - /** - * An implementation of DbVersion to be passed to {@link MigrationContainerPopulatorImpl}'s constructor which is - * not supposed to call the {@link DbVersion#addSteps(MigrationStepRegistry)} method and therefor has an - * implementation that throws a {@link UnsupportedOperationException} when called. - */ - private static class NoRegistryDbVersion implements DbVersion { - @Override - public void addSteps(MigrationStepRegistry registry) { - throw new UnsupportedOperationException("addSteps is not supposed to be called"); - } - } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java index 0506326ca7d..f0b31e5c9aa 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java @@ -19,12 +19,14 @@ */ package org.sonar.server.platform.db.migration.engine; +import java.sql.SQLException; import java.util.List; import java.util.Optional; +import org.junit.Before; import org.junit.Test; import org.sonar.api.config.internal.ConfigurationBridge; import org.sonar.api.config.internal.MapSettings; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.process.ProcessProperties; import org.sonar.server.platform.db.migration.SupportsBlueGreen; import org.sonar.server.platform.db.migration.history.MigrationHistory; @@ -38,42 +40,50 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; public class MigrationEngineImplTest { - private MigrationHistory migrationHistory = mock(MigrationHistory.class); - private ComponentContainer serverContainer = new ComponentContainer(); - private MigrationStepsExecutor stepsExecutor = mock(MigrationStepsExecutor.class); - private MigrationContainerPopulator populator = container -> container.add(stepsExecutor); - private MigrationSteps migrationSteps = mock(MigrationSteps.class); - - private MapSettings settings = new MapSettings(); - private MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, populator, migrationSteps, new ConfigurationBridge(settings)); + private final MigrationHistory migrationHistory = mock(MigrationHistory.class); + private final SpringComponentContainer serverContainer = new SpringComponentContainer(); + private final MigrationSteps migrationSteps = mock(MigrationSteps.class); + private final StepRegistry stepRegistry = new StepRegistry(); + private final MapSettings settings = new MapSettings(); + private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps, new ConfigurationBridge(settings)); + + @Before + public void before() { + serverContainer.add(migrationSteps); + serverContainer.add(migrationHistory); + serverContainer.add(stepRegistry); + serverContainer.startComponents(); + } @Test public void execute_execute_all_steps_of_there_is_no_last_migration_number() { when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.empty()); - List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", MigrationStep.class)); + List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class)); when(migrationSteps.readAll()).thenReturn(steps); underTest.execute(); - verify(migrationSteps).readAll(); - verify(stepsExecutor).execute(steps); + verify(migrationSteps, times(2)).readAll(); + assertThat(stepRegistry.stepRan).isTrue(); } @Test public void execute_execute_steps_from_last_migration_number_plus_1() { when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.of(50L)); - List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", MigrationStep.class)); + List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class)); when(migrationSteps.readFrom(51)).thenReturn(steps); + when(migrationSteps.readAll()).thenReturn(steps); underTest.execute(); verify(migrationSteps).readFrom(51); - verify(stepsExecutor).execute(steps); + assertThat(stepRegistry.stepRan).isTrue(); } @Test @@ -82,11 +92,12 @@ public class MigrationEngineImplTest { when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.of(50L)); List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestBlueGreenMigrationStep.class)); when(migrationSteps.readFrom(51)).thenReturn(steps); + when(migrationSteps.readAll()).thenReturn(steps); underTest.execute(); verify(migrationSteps).readFrom(51); - verify(stepsExecutor).execute(steps); + assertThat(stepRegistry.stepRan).isTrue(); } @Test @@ -95,7 +106,7 @@ public class MigrationEngineImplTest { when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.of(50L)); List<RegisteredMigrationStep> steps = asList( new RegisteredMigrationStep(1, "foo", TestBlueGreenMigrationStep.class), - new RegisteredMigrationStep(2, "bar", MigrationStep.class)); + new RegisteredMigrationStep(2, "bar", TestMigrationStep.class)); when(migrationSteps.readFrom(51)).thenReturn(steps); try { @@ -103,16 +114,43 @@ public class MigrationEngineImplTest { fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("All migrations canceled. #2 does not support blue/green deployment: bar"); - verifyZeroInteractions(stepsExecutor); + assertThat(stepRegistry.stepRan).isFalse(); + } + } + + private static class NoOpExecutor implements MigrationStepsExecutor { + @Override + public void execute(List<RegisteredMigrationStep> steps) { + // no op + } + } + + private static class StepRegistry { + boolean stepRan = false; + } + + private static class TestMigrationStep implements MigrationStep { + private final StepRegistry registry; + + public TestMigrationStep(StepRegistry registry) { + this.registry = registry; + } + @Override + public void execute() throws SQLException { + registry.stepRan = true; } } @SupportsBlueGreen private static class TestBlueGreenMigrationStep implements MigrationStep { + private final StepRegistry registry; + public TestBlueGreenMigrationStep(StepRegistry registry) { + this.registry = registry; + } @Override - public void execute() { - + public void execute() throws SQLException { + registry.stepRan = true; } } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/SimpleMigrationContainer.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/SimpleMigrationContainer.java index cf7a049ef45..3fc3bb69a9c 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/SimpleMigrationContainer.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/SimpleMigrationContainer.java @@ -19,9 +19,9 @@ */ package org.sonar.server.platform.db.migration.engine; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; -public final class SimpleMigrationContainer extends ComponentContainer implements MigrationContainer { +public final class SimpleMigrationContainer extends SpringComponentContainer implements MigrationContainer { @Override public void cleanup() { diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java index c4f61331303..cdd6cf86ac9 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java @@ -29,6 +29,7 @@ import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.platform.db.migration.engine.MigrationContainer; import org.sonar.server.platform.db.migration.engine.SimpleMigrationContainer; import org.sonar.server.platform.db.migration.history.MigrationHistory; @@ -59,11 +60,12 @@ public class MigrationStepsExecutorImplTest { public void execute_fails_with_ISE_if_no_instance_of_computation_step_exist_in_container() { List<RegisteredMigrationStep> steps = asList(registeredStepOf(1, MigrationStep1.class)); + ((SpringComponentContainer) migrationContainer).startComponents(); try { underTest.execute(steps); fail("execute should have thrown a IllegalStateException"); } catch (IllegalStateException e) { - assertThat(e).hasMessage("Can not find instance of " + MigrationStep1.class); + assertThat(e).hasMessage("Unable to load component " + MigrationStep1.class); } finally { assertThat(logTester.logs()).hasSize(2); assertLogLevel(LoggerLevel.INFO, "Executing DB migrations..."); @@ -87,6 +89,7 @@ public class MigrationStepsExecutorImplTest { @Test public void execute_execute_the_instance_of_type_specified_in_step_in_stream_order() { migrationContainer.add(MigrationStep1.class, MigrationStep2.class, MigrationStep3.class); + ((SpringComponentContainer) migrationContainer).startComponents(); underTest.execute(asList( registeredStepOf(1, MigrationStep2.class), @@ -119,6 +122,7 @@ public class MigrationStepsExecutorImplTest { registeredStepOf(2, SqlExceptionFailingMigrationStep.class), registeredStepOf(3, MigrationStep3.class)); + ((SpringComponentContainer) migrationContainer).startComponents(); try { underTest.execute(steps); fail("a MigrationStepExecutionException should have been thrown"); @@ -147,6 +151,7 @@ public class MigrationStepsExecutorImplTest { registeredStepOf(2, RuntimeExceptionFailingMigrationStep.class), registeredStepOf(3, MigrationStep3.class)); + ((SpringComponentContainer) migrationContainer).startComponents(); try { underTest.execute(steps); fail("should throw MigrationStepExecutionException"); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsProviderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsProviderTest.java index 40bc5fb99b5..2f58a1dfa54 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsProviderTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsProviderTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.platform.db.migration.step; -import java.util.Random; import org.junit.Test; import org.mockito.InOrder; import org.sonar.server.platform.db.migration.version.DbVersion; @@ -30,13 +29,12 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; public class MigrationStepsProviderTest { - private InternalMigrationStepRegistry internalMigrationStepRegistry = mock(InternalMigrationStepRegistry.class); - private MigrationStepsProvider underTest = new MigrationStepsProvider(); + private final InternalMigrationStepRegistry internalMigrationStepRegistry = mock(InternalMigrationStepRegistry.class); + private final MigrationStepsProvider underTest = new MigrationStepsProvider(); @Test public void provide_throws_ISE_with_registry_build_throws_ISE_because_it_is_empty() { @@ -49,7 +47,7 @@ public class MigrationStepsProviderTest { } @Test - public void provide_calls_DbVersion_addStep_in_order_and_only_once() { + public void provide_calls_DbVersion_addStep_in_order() { DbVersion dbVersion1 = newMockFailingOnSecondBuildCall(); DbVersion dbVersion2 = newMockFailingOnSecondBuildCall(); DbVersion dbVersion3 = newMockFailingOnSecondBuildCall(); @@ -64,24 +62,6 @@ public class MigrationStepsProviderTest { inOrder.verify(dbVersion2).addSteps(internalMigrationStepRegistry); inOrder.verify(dbVersion3).addSteps(internalMigrationStepRegistry); inOrder.verifyNoMoreInteractions(); - - // calling a second time with another argument, it's just ignored - DbVersion dbVersion4 = newMockFailingOnSecondBuildCall(); - assertThat(underTest.provide(internalMigrationStepRegistry, dbVersion4)).isSameAs(expected); - verifyZeroInteractions(dbVersion4); - } - - @Test - public void provide_always_returns_the_same_MigrationSteps_instance_and_calls_registry_build_only_once() { - MigrationSteps migrationSteps = mock(MigrationSteps.class); - when(internalMigrationStepRegistry.build()) - .thenReturn(migrationSteps) - .thenThrow(new RuntimeException("method build should not be called twice")); - - for (int i = 0; i < Math.abs(new Random().nextInt(50)) + 1; i++) { - assertThat(underTest.provide(internalMigrationStepRegistry)).isSameAs(migrationSteps); - } - } private static DbVersion newMockFailingOnSecondBuildCall() { diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/DbVersion00Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/DbVersion00Test.java index d4941aa97c1..92a05b1c213 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/DbVersion00Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/DbVersion00Test.java @@ -21,7 +21,6 @@ package org.sonar.server.platform.db.migration.version.v00; import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; @@ -29,11 +28,6 @@ public class DbVersion00Test { private DbVersion00 underTest = new DbVersion00(); @Test - public void verify_no_support_component() { - assertThat(underTest.getSupportComponents()).isEmpty(); - } - - @Test public void migrationNumber_starts_at_1153() { verifyMinimumMigrationNumber(underTest, 1); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v90/DbVersion90Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v90/DbVersion90Test.java index b9c1960fa6b..6549102afc9 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v90/DbVersion90Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v90/DbVersion90Test.java @@ -21,7 +21,6 @@ package org.sonar.server.platform.db.migration.version.v90; import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; @@ -30,11 +29,6 @@ public class DbVersion90Test { private final DbVersion90 underTest = new DbVersion90(); @Test - public void verify_no_support_component() { - assertThat(underTest.getSupportComponents()).isEmpty(); - } - - @Test public void migrationNumber_starts_at_5001() { verifyMinimumMigrationNumber(underTest, 5001); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java index 3b118ed75bd..d8650e4bb11 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v91/DbVersion91Test.java @@ -21,7 +21,6 @@ package org.sonar.server.platform.db.migration.version.v91; import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; @@ -30,11 +29,6 @@ public class DbVersion91Test { private final DbVersion91 underTest = new DbVersion91(); @Test - public void verify_no_support_component() { - assertThat(underTest.getSupportComponents()).isEmpty(); - } - - @Test public void migrationNumber_starts_at_6001() { verifyMinimumMigrationNumber(underTest, 6001); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java index 6df422e23e9..bf036b4622c 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java @@ -21,7 +21,6 @@ package org.sonar.server.platform.db.migration.version.v92; import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationNotEmpty; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; @@ -30,11 +29,6 @@ public class DbVersion92Test { private final DbVersion92 underTest = new DbVersion92(); @Test - public void verify_no_support_component() { - assertThat(underTest.getSupportComponents()).isEmpty(); - } - - @Test public void migrationNumber_starts_at_6101() { verifyMinimumMigrationNumber(underTest, 6101); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93Test.java index d4479465020..12934c1456f 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93Test.java @@ -21,7 +21,6 @@ package org.sonar.server.platform.db.migration.version.v93; import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationNotEmpty; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; @@ -30,11 +29,6 @@ public class DbVersion93Test { private final DbVersion93 underTest = new DbVersion93(); @Test - public void verify_no_support_component() { - assertThat(underTest.getSupportComponents()).isEmpty(); - } - - @Test public void migrationNumber_starts_at_6101() { verifyMinimumMigrationNumber(underTest, 6201); } diff --git a/server/sonar-process/src/main/java/org/sonar/process/cluster/health/SharedHealthStateImpl.java b/server/sonar-process/src/main/java/org/sonar/process/cluster/health/SharedHealthStateImpl.java index b4a58551d30..b2a5b7d5746 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/cluster/health/SharedHealthStateImpl.java +++ b/server/sonar-process/src/main/java/org/sonar/process/cluster/health/SharedHealthStateImpl.java @@ -32,6 +32,7 @@ import org.sonar.process.cluster.hz.HazelcastMember; import org.sonar.process.cluster.hz.HazelcastObjects; import static java.util.Objects.requireNonNull; +import org.springframework.beans.factory.annotation.Autowired; public class SharedHealthStateImpl implements SharedHealthState { private static final Logger LOG = LoggerFactory.getLogger(SharedHealthStateImpl.class); @@ -39,10 +40,12 @@ public class SharedHealthStateImpl implements SharedHealthState { private final HazelcastMember hzMember; + @Autowired(required = false) public SharedHealthStateImpl(HazelcastMember hzMember) { this.hzMember = hzMember; } + @Autowired(required = false) public SharedHealthStateImpl() { this(null); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java b/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java index d0f6d8c6ce5..87c8f749fd2 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java @@ -19,7 +19,7 @@ */ package org.sonar.server.async; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.process.Jmx; public class AsyncExecutionMBeanImpl implements AsyncExecutionMBean, Startable { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java index b77f46ef4f0..cadee3af797 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java @@ -36,6 +36,8 @@ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_ import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; import static org.sonar.server.permission.index.IndexAuthorizationConstants.TYPE_AUTHORIZATION; +import javax.inject.Inject; + public class ComponentIndexDefinition implements IndexDefinition { public static final Index DESCRIPTOR = Index.withRelations("components"); @@ -58,6 +60,7 @@ public class ComponentIndexDefinition implements IndexDefinition { this.enableSource = enableSource; } + @Inject public ComponentIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java index babc5971a4e..f4a8c7cca58 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java @@ -19,26 +19,22 @@ */ package org.sonar.server.config; -import java.util.Optional; -import java.util.function.UnaryOperator; +import static java.util.function.UnaryOperator.identity; import org.apache.commons.lang.ArrayUtils; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.config.Configuration; import org.sonar.api.config.PropertyDefinition; -import org.sonar.api.config.internal.Settings; - -import static java.util.function.UnaryOperator.identity; import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv; +import org.sonar.api.config.internal.Settings; +import org.springframework.context.annotation.Bean; -public class ConfigurationProvider extends ProviderAdapter { +import java.util.Optional; +import java.util.function.UnaryOperator; - private Configuration configuration; +public class ConfigurationProvider { + @Bean("Configuration") public Configuration provide(Settings settings) { - if (configuration == null) { - configuration = new ServerConfigurationAdapter(settings); - } - return configuration; + return new ServerConfigurationAdapter(settings); } private static class ServerConfigurationAdapter implements Configuration { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java index 16afc5b7e08..41f87cb5c97 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java @@ -28,13 +28,13 @@ import java.util.List; import java.util.stream.Collectors; import org.apache.http.HttpHost; import org.elasticsearch.common.settings.Settings; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.process.cluster.NodeType; +import org.springframework.context.annotation.Bean; import static org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED; import static org.sonar.process.ProcessProperties.Property.CLUSTER_NAME; @@ -47,38 +47,33 @@ import static org.sonar.process.cluster.NodeType.SEARCH; @ComputeEngineSide @ServerSide -public class EsClientProvider extends ProviderAdapter { - +public class EsClientProvider { private static final Logger LOGGER = Loggers.get(EsClientProvider.class); - private EsClient cache; - + @Bean("EsClient") public EsClient provide(Configuration config) { - if (cache == null) { - Settings.Builder esSettings = Settings.builder(); + Settings.Builder esSettings = Settings.builder(); - // mandatory property defined by bootstrap process - esSettings.put("cluster.name", config.get(CLUSTER_NAME.getKey()).get()); + // mandatory property defined by bootstrap process + esSettings.put("cluster.name", config.get(CLUSTER_NAME.getKey()).get()); - boolean clusterEnabled = config.getBoolean(CLUSTER_ENABLED.getKey()).orElse(false); - boolean searchNode = !clusterEnabled || SEARCH.equals(NodeType.parse(config.get(CLUSTER_NODE_TYPE.getKey()).orElse(null))); - List<HttpHost> httpHosts; - if (clusterEnabled && !searchNode) { - httpHosts = getHttpHosts(config); + boolean clusterEnabled = config.getBoolean(CLUSTER_ENABLED.getKey()).orElse(false); + boolean searchNode = !clusterEnabled || SEARCH.equals(NodeType.parse(config.get(CLUSTER_NODE_TYPE.getKey()).orElse(null))); + List<HttpHost> httpHosts; + if (clusterEnabled && !searchNode) { + httpHosts = getHttpHosts(config); - LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(httpHosts)); - } else { - // defaults provided in: - // * in org.sonar.process.ProcessProperties.Property.SEARCH_HOST - // * in org.sonar.process.ProcessProperties.Property.SEARCH_PORT - HostAndPort host = HostAndPort.fromParts(config.get(SEARCH_HOST.getKey()).get(), config.getInt(SEARCH_PORT.getKey()).get()); - httpHosts = Collections.singletonList(toHttpHost(host)); - LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(httpHosts)); - } - - cache = new EsClient(config.get(CLUSTER_SEARCH_PASSWORD.getKey()).orElse(null), httpHosts.toArray(new HttpHost[0])); + LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(httpHosts)); + } else { + // defaults provided in: + // * in org.sonar.process.ProcessProperties.Property.SEARCH_HOST + // * in org.sonar.process.ProcessProperties.Property.SEARCH_PORT + HostAndPort host = HostAndPort.fromParts(config.get(SEARCH_HOST.getKey()).get(), config.getInt(SEARCH_PORT.getKey()).get()); + httpHosts = Collections.singletonList(toHttpHost(host)); + LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(httpHosts)); } - return cache; + + return new EsClient(config.get(CLUSTER_SEARCH_PASSWORD.getKey()).orElse(null), httpHosts.toArray(new HttpHost[0])); } private static List<HttpHost> getHttpHosts(Configuration config) { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientStopper.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientStopper.java deleted file mode 100644 index ef0957ceb4f..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientStopper.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.server.es; - -import org.sonar.api.Startable; - -/** - * Workaround of a behaviour of picocontainer: components - * instantiated by {@link org.picocontainer.injectors.ProviderAdapter} - * can't have a lifecycle. The methods start() and stop() - * of {@link Startable} are not executed. - * The same behaviour exists for the {@link org.picocontainer.injectors.ProviderAdapter} - * itself. - * - * As {@link EsClientStopper} implements {@link Startable}, it can - * close {@link EsClient} when process shutdowns. - * - */ -public class EsClientStopper implements Startable { - - private final EsClient esClient; - - public EsClientStopper(EsClient esClient) { - this.esClient = esClient; - } - - @Override - public void start() { - // nothing to do - } - - @Override - public void stop() { - esClient.close(); - } -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java index 8d5762a7e88..f6697d6e85d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java @@ -25,6 +25,5 @@ public class EsModule extends Module { @Override protected void configureModule() { add(new EsClientProvider()); - add(EsClientStopper.class); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java index fe6600e00e2..6e3d37b922a 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java @@ -25,7 +25,7 @@ import org.sonar.api.platform.ServerStartHandler; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static java.lang.String.format; @@ -35,9 +35,9 @@ import static java.lang.String.format; public class CoreExtensionBootstraper implements ServerStartHandler { private static final Logger LOGGER = Loggers.get(CoreExtensionBootstraper.class); - private final ComponentContainer componentContainer; + private final SpringComponentContainer componentContainer; - public CoreExtensionBootstraper(ComponentContainer componentContainer) { + public CoreExtensionBootstraper(SpringComponentContainer componentContainer) { this.componentContainer = componentContainer; } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java index f1f9e379fae..f68235dee48 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java @@ -19,7 +19,7 @@ */ package org.sonar.server.extension; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; /** * Interface implemented by the Extension point exposed by the Core Extensions that serves as the unique access @@ -32,11 +32,11 @@ public interface CoreExtensionBridge { /** * Bootstraps the plugin. * - * @param parent the parent ComponentContainer which provides Platform components for the Privileged plugin to use. + * @param parent the parent SpringComponentContainer which provides Platform components for the Privileged plugin to use. * * @throws IllegalStateException if called more than once */ - void startPlugin(ComponentContainer parent); + void startPlugin(SpringComponentContainer parent); /** * This method is called when Platform is shutting down. diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java index 6ed17663943..d03548ac46d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java @@ -20,11 +20,11 @@ package org.sonar.server.extension; import java.util.List; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.Container; import static java.lang.String.format; @@ -35,9 +35,9 @@ import static java.lang.String.format; public class CoreExtensionStopper implements Startable { private static final Logger LOGGER = Loggers.get(CoreExtensionStopper.class); - private final ComponentContainer platformContainer; + private final Container platformContainer; - public CoreExtensionStopper(ComponentContainer platformContainer) { + public CoreExtensionStopper(Container platformContainer) { this.platformContainer = platformContainer; } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java index 34c211d318b..fed04f29c97 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java @@ -32,6 +32,8 @@ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_ import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; import static org.sonar.server.permission.index.IndexAuthorizationConstants.TYPE_AUTHORIZATION; +import javax.inject.Inject; + /** * Definition of ES index "issues", including settings and fields. */ @@ -110,6 +112,7 @@ public class IssueIndexDefinition implements IndexDefinition { private final Configuration config; private final boolean enableSource; + @Inject public IssueIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java index c39ebfee08c..f3b53317538 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java @@ -20,7 +20,7 @@ package org.sonar.server.issue.workflow; import java.util.List; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.issue.DefaultTransitions; import org.sonar.api.issue.Issue; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java b/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java index 8e3c7fa4f46..196fb2253db 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java @@ -23,7 +23,8 @@ import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import com.google.common.annotations.VisibleForTesting; import java.io.File; -import org.picocontainer.Startable; +import javax.inject.Inject; +import org.sonar.api.Startable; import org.slf4j.LoggerFactory; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.Configuration; @@ -47,6 +48,7 @@ public class ServerLogging implements Startable { private final ServerProcessLogging serverProcessLogging; private final Database database; + @Inject public ServerLogging(Configuration config, ServerProcessLogging serverProcessLogging, Database database) { this(new LogbackHelper(), config, serverProcessLogging, database); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java index 5ade001ab94..a4d3534e391 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java @@ -34,6 +34,8 @@ import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL; import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; +import javax.inject.Inject; + public class ProjectMeasuresIndexDefinition implements IndexDefinition { public static final Index DESCRIPTOR = Index.withRelations("projectmeasures"); @@ -71,6 +73,7 @@ public class ProjectMeasuresIndexDefinition implements IndexDefinition { this.enableSource = enableSource; } + @Inject public ProjectMeasuresIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java index aafa02f2c6f..c3bd24b8bc3 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java @@ -56,13 +56,10 @@ public class DefaultNotificationManager implements NotificationManager { private static final String UNABLE_TO_READ_NOTIFICATION = "Unable to read notification"; - private NotificationChannel[] notificationChannels; + private final NotificationChannel[] notificationChannels; private final DbClient dbClient; private boolean alreadyLoggedDeserializationIssue = false; - /** - * Default constructor used by Pico - */ public DefaultNotificationManager(NotificationChannel[] channels, DbClient dbClient) { this.notificationChannels = channels; this.dbClient = dbClient; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java index f31572a35b2..925cc6bf436 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java @@ -32,7 +32,7 @@ import static java.util.Objects.requireNonNull; /** * The notification manager receives notifications and is in charge of storing them so that they are processed by the notification service. * <p> - * Pico provides an instance of this class, and plugins just need to create notifications and pass them to this manager with + * The ioc container provides an instance of this class, and plugins just need to create notifications and pass them to this manager with * the {@link NotificationManager#scheduleForSending(Notification)} method. * </p> */ diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java index 8092ef3ad54..710658ca998 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java @@ -39,6 +39,7 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkArgument; @@ -52,6 +53,7 @@ public class NotificationService { private final List<NotificationHandler<? extends Notification>> handlers; private final DbClient dbClient; + @Autowired(required = false) public NotificationService(DbClient dbClient, NotificationDispatcher[] dispatchers, NotificationHandler<? extends Notification>[] handlers) { this.dbClient = dbClient; this.dispatchers = ImmutableList.copyOf(dispatchers); @@ -59,22 +61,25 @@ public class NotificationService { } /** - * Used by Pico when there are no handler nor dispatcher. + * Used by the ioc container when there are no handler nor dispatcher. */ + @Autowired(required = false) public NotificationService(DbClient dbClient) { this(dbClient, new NotificationDispatcher[0], new NotificationHandler[0]); } /** - * Used by Pico when there are no dispatcher. + * Used by the ioc container when there are no dispatcher. */ + @Autowired(required = false) public NotificationService(DbClient dbClient, NotificationHandler[] handlers) { this(dbClient, new NotificationDispatcher[0], handlers); } /** - * Used by Pico when there are no handler. + * Used by the ioc container when there are no handler. */ + @Autowired(required = false) public NotificationService(DbClient dbClient, NotificationDispatcher[] dispatchers) { this(dbClient, dispatchers, new NotificationHandler[0]); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java index 687d490057a..eed1210d9b7 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java @@ -22,7 +22,7 @@ package org.sonar.server.platform; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java index f71654e8b35..fa720a5239b 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java @@ -19,44 +19,33 @@ */ package org.sonar.server.platform; -import org.picocontainer.Startable; -import org.sonar.api.utils.log.Loggers; +import javax.annotation.Nullable; +import org.sonar.api.Startable; import org.sonar.api.platform.Server; import org.sonar.api.platform.ServerStartHandler; import org.sonar.api.platform.ServerStopHandler; +import org.sonar.api.utils.log.Loggers; /** * @since 2.2 */ public class ServerLifecycleNotifier implements Startable { - private ServerStartHandler[] startHandlers; - private ServerStopHandler[] stopHandlers; - private Server server; + private final ServerStartHandler[] startHandlers; + private final ServerStopHandler[] stopHandlers; + private final Server server; - public ServerLifecycleNotifier(Server server, ServerStartHandler[] startHandlers, ServerStopHandler[] stopHandlers) { - this.startHandlers = startHandlers; - this.stopHandlers = stopHandlers; + public ServerLifecycleNotifier(Server server, @Nullable ServerStartHandler[] startHandlers, @Nullable ServerStopHandler[] stopHandlers) { + this.startHandlers = startHandlers != null ? startHandlers : new ServerStartHandler[0]; + this.stopHandlers = stopHandlers != null ? stopHandlers: new ServerStopHandler[0]; this.server = server; } - public ServerLifecycleNotifier(Server server, ServerStartHandler[] startHandlers) { - this(server, startHandlers, new ServerStopHandler[0]); - } - - public ServerLifecycleNotifier(Server server, ServerStopHandler[] stopHandlers) { - this(server, new ServerStartHandler[0], stopHandlers); - } - - public ServerLifecycleNotifier(Server server) { - this(server, new ServerStartHandler[0], new ServerStopHandler[0]); - } - @Override public void start() { /* IMPORTANT : we want to be sure that handlers are notified when all other services are started. - That's why the class Platform explicitely executes the method notifyStart(), instead of letting picocontainer + That's why the class Platform explicitely executes the method notifyStart(), instead of letting the ioc container choose the startup order. */ } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java index 88efc4d81f8..19e714c9a28 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java @@ -19,7 +19,6 @@ */ package org.sonar.server.platform; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.CoreProperties; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; @@ -30,6 +29,7 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.property.PropertyDto; +import org.springframework.context.annotation.Bean; import static com.google.common.base.Preconditions.checkState; import static org.apache.commons.lang.StringUtils.isBlank; @@ -37,19 +37,14 @@ import static org.sonar.api.CoreProperties.SERVER_STARTTIME; @ComputeEngineSide @ServerSide -public class StartupMetadataProvider extends ProviderAdapter { - - private StartupMetadata cache = null; - +public class StartupMetadataProvider { + @Bean("StartupMetadata") public StartupMetadata provide(System2 system, SonarRuntime runtime, WebServer webServer, DbClient dbClient) { - if (cache == null) { - if (runtime.getSonarQubeSide() == SonarQubeSide.SERVER && webServer.isStartupLeader()) { - cache = generate(system); - } else { - cache = load(dbClient); - } + if (runtime.getSonarQubeSide() == SonarQubeSide.SERVER && webServer.isStartupLeader()) { + return generate(system); + } else { + return load(dbClient); } - return cache; } /** diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java index af3c93c31dd..9c5d476b045 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java @@ -22,24 +22,19 @@ package org.sonar.server.platform; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.impl.utils.DefaultTempFolder; import org.sonar.api.utils.TempFolder; +import org.springframework.context.annotation.Bean; -public class TempFolderProvider extends ProviderAdapter { - - private TempFolder tempFolder; - +public class TempFolderProvider { + @Bean("TempFolder") public TempFolder provide(ServerFileSystem fs) { - if (tempFolder == null) { - File tempDir = new File(fs.getTempDir(), "tmp"); - try { - FileUtils.forceMkdir(tempDir); - } catch (IOException e) { - throw new IllegalStateException("Unable to create temp directory " + tempDir, e); - } - tempFolder = new DefaultTempFolder(tempDir); + File tempDir = new File(fs.getTempDir(), "tmp"); + try { + FileUtils.forceMkdir(tempDir); + } catch (IOException e) { + throw new IllegalStateException("Unable to create temp directory " + tempDir, e); } - return tempFolder; + return new DefaultTempFolder(tempDir); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java index 78530477e26..a93f2d80d73 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java @@ -19,7 +19,7 @@ */ package org.sonar.server.plugins; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.core.platform.PluginRepository; import org.sonar.updatecenter.common.PluginReferential; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java index 89b64ce6365..523d70fe1dc 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java @@ -22,18 +22,19 @@ package org.sonar.server.plugins; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ListMultimap; -import java.lang.annotation.Annotation; -import java.util.Collection; -import java.util.Set; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; import org.sonar.api.config.Configuration; import org.sonar.api.internal.PluginContextImpl; import org.sonar.api.utils.AnnotationUtils; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ExtensionContainer; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; +import java.lang.annotation.Annotation; +import java.util.Collection; +import java.util.Set; + import static java.lang.String.format; import static java.util.Objects.requireNonNull; @@ -41,18 +42,21 @@ import static java.util.Objects.requireNonNull; * Loads the plugins server extensions and injects them to DI container */ public abstract class ServerExtensionInstaller { + private final Configuration configuration; private final SonarRuntime sonarRuntime; private final PluginRepository pluginRepository; private final Set<Class<? extends Annotation>> supportedAnnotationTypes; - protected ServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository, Collection<Class<? extends Annotation>> supportedAnnotationTypes) { + protected ServerExtensionInstaller(Configuration configuration, SonarRuntime sonarRuntime, PluginRepository pluginRepository, + Collection<Class<? extends Annotation>> supportedAnnotationTypes) { requireNonNull(supportedAnnotationTypes, "At least one supported annotation type must be specified"); + this.configuration = configuration; this.sonarRuntime = sonarRuntime; this.pluginRepository = pluginRepository; this.supportedAnnotationTypes = ImmutableSet.copyOf(supportedAnnotationTypes); } - public void installExtensions(ComponentContainer container) { + public void installExtensions(ExtensionContainer container) { ListMultimap<PluginInfo, Object> installedExtensionsByPlugin = ArrayListMultimap.create(); for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { try { @@ -62,7 +66,7 @@ public abstract class ServerExtensionInstaller { Plugin.Context context = new PluginContextImpl.Builder() .setSonarRuntime(sonarRuntime) - .setBootConfiguration(container.getComponentByType(Configuration.class)) + .setBootConfiguration(configuration) .build(); plugin.define(context); for (Object extension : context.getExtensions()) { @@ -79,7 +83,7 @@ public abstract class ServerExtensionInstaller { } } - private Object installExtension(ComponentContainer container, PluginInfo pluginInfo, Object extension) { + private Object installExtension(ExtensionContainer container, PluginInfo pluginInfo, Object extension) { for (Class<? extends Annotation> supportedAnnotationType : supportedAnnotationTypes) { if (AnnotationUtils.getAnnotation(extension, supportedAnnotationType) != null) { container.addExtension(pluginInfo, extension); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java index b4f42b8fb57..23b0b2a2693 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java @@ -37,6 +37,8 @@ import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL; import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; +import javax.inject.Inject; + /** * Definition of ES index "rules", including settings and fields. */ @@ -82,6 +84,7 @@ public class RuleIndexDefinition implements IndexDefinition { private final Configuration config; private final boolean enableSource; + @Inject public RuleIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java index 530f318adf9..0bc77907753 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Properties; +import javax.inject.Inject; import org.apache.ibatis.exceptions.PersistenceException; import org.sonar.api.CoreProperties; import org.sonar.api.ce.ComputeEngineSide; @@ -60,6 +61,7 @@ public class ThreadLocalSettings extends Settings { private Map<String, String> getPropertiesDbFailureCache = Collections.emptyMap(); private SettingLoader settingLoader; + @Inject public ThreadLocalSettings(PropertyDefinitions definitions, Properties props) { this(definitions, props, new NopSettingLoader()); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java index ca822c8cb97..8c1a88e9484 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java @@ -20,12 +20,12 @@ package org.sonar.server.util; import okhttp3.OkHttpClient; -import org.picocontainer.injectors.ProviderAdapter; 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.sonarqube.ws.client.OkHttpClientBuilder; +import org.springframework.context.annotation.Bean; import static java.lang.String.format; import static org.sonar.process.ProcessProperties.Property.HTTP_PROXY_PASSWORD; @@ -43,28 +43,24 @@ import static org.sonar.process.ProcessProperties.Property.HTTP_PROXY_USER; */ @ServerSide @ComputeEngineSide -public class OkHttpClientProvider extends ProviderAdapter { +public class OkHttpClientProvider { private static final int DEFAULT_CONNECT_TIMEOUT_IN_MS = 10_000; private static final int DEFAULT_READ_TIMEOUT_IN_MS = 10_000; - private okhttp3.OkHttpClient okHttpClient; - /** * @return a {@link OkHttpClient} singleton */ + @Bean("OkHttpClient") public OkHttpClient provide(Configuration config, SonarRuntime runtime) { - if (okHttpClient == null) { - OkHttpClientBuilder builder = new OkHttpClientBuilder(); - builder.setConnectTimeoutMs(DEFAULT_CONNECT_TIMEOUT_IN_MS); - builder.setReadTimeoutMs(DEFAULT_READ_TIMEOUT_IN_MS); - // no need to define proxy URL as system-wide proxy is used and properly - // 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())); - okHttpClient = builder.build(); - } - return okHttpClient; + OkHttpClientBuilder builder = new OkHttpClientBuilder(); + builder.setConnectTimeoutMs(DEFAULT_CONNECT_TIMEOUT_IN_MS); + builder.setReadTimeoutMs(DEFAULT_READ_TIMEOUT_IN_MS); + // no need to define proxy URL as system-wide proxy is used and properly + // 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())); + return builder.build(); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java index a576ecaea83..07b97ad2c0d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java @@ -20,10 +20,10 @@ package org.sonar.server.util; import java.util.concurrent.ExecutorService; -import org.picocontainer.Startable; +import org.sonar.api.Startable; /** - * ExecutorService that exposes a {@code stop} method which can be invoked by Pico container to shutdown properly + * ExecutorService that exposes a {@code stop} method which can be invoked by the ioc container to shutdown properly * the service. */ public interface StoppableExecutorService extends ExecutorService, Startable { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java index 4d16122024e..a6cf185477a 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java @@ -22,7 +22,7 @@ package org.sonar.server.util; import java.util.concurrent.ScheduledExecutorService; /** - * ScheduledExecutorService that exposes a {@code stop} method which can be invoked by Pico container to shutdown + * ScheduledExecutorService that exposes a {@code stop} method which can be invoked by the ioc container to shutdown * properly the service. */ public interface StoppableScheduledExecutorService extends ScheduledExecutorService, StoppableExecutorService { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java index 959488fd09b..a4957a6b68b 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java @@ -42,8 +42,6 @@ import static org.sonar.process.ProcessProperties.Property.SEARCH_HOST; import static org.sonar.process.ProcessProperties.Property.SEARCH_PORT; public class EsClientProviderTest { - - @Rule public LogTester logTester = new LogTester(); @@ -74,9 +72,6 @@ public class EsClientProviderTest { assertThat(node.getHost().getPort()).isEqualTo(9000); assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to local Elasticsearch: [http://" + localhostHostname + ":9000]"), "")); - - // keep in cache - assertThat(underTest.provide(settings.asConfig())).isSameAs(client); } @Test @@ -99,9 +94,6 @@ public class EsClientProviderTest { assertThat(logTester.logs(LoggerLevel.INFO)) .has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [http://" + localhostHostname + ":8080, http://" + localhostHostname + ":8081]"), "")); - - // keep in cache - assertThat(underTest.provide(settings.asConfig())).isSameAs(client); } @Test @@ -147,8 +139,5 @@ public class EsClientProviderTest { assertThat(logTester.logs(LoggerLevel.INFO)) .has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [http://" + localhostHostname + ":9001, http://" + localhostHostname + ":8081]"), "")); - - // keep in cache - assertThat(underTest.provide(settings.asConfig())).isSameAs(client); } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java index c48c2d0bbbc..3c4a5ebe4a7 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.es; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class EsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new EsModule().configure(container); - assertThat(container.size()).isEqualTo(2 + COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); + assertThat(container.getAddedObjects()).hasSize(1); } - } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java index a316b2f46ae..c000fe9ded9 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java @@ -21,17 +21,16 @@ package org.sonar.server.extension; import org.junit.Test; import org.sonar.api.platform.Server; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; public class CoreExtensionBootstraperTest { - private ComponentContainer componentContainer = new ComponentContainer(); - private CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); - - private CoreExtensionBootstraper underTest = new CoreExtensionBootstraper(componentContainer); + private final SpringComponentContainer componentContainer = new SpringComponentContainer(); + private final CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); + private final CoreExtensionBootstraper underTest = new CoreExtensionBootstraper(componentContainer); @Test public void onServerStart_calls_startPlugin_if_Bridge_exists_in_container() { @@ -47,6 +46,7 @@ public class CoreExtensionBootstraperTest { @Test public void onServerStart_does_not_call_startPlugin_if_Bridge_does_not_exist_in_container() { + componentContainer.startComponents(); underTest.onServerStart(mock(Server.class)); verifyNoMoreInteractions(bridge); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java index c93913c2078..995e03a9b74 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java @@ -20,17 +20,16 @@ package org.sonar.server.extension; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; public class CoreExtensionStopperTest { - private ComponentContainer componentContainer = new ComponentContainer(); - private CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); - - private CoreExtensionStopper underTest = new CoreExtensionStopper(componentContainer); + private final SpringComponentContainer componentContainer = new SpringComponentContainer(); + private final CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); + private final CoreExtensionStopper underTest = new CoreExtensionStopper(componentContainer); @Test public void stop_calls_stopPlugin_if_Bridge_exists_in_container() { @@ -46,6 +45,7 @@ public class CoreExtensionStopperTest { @Test public void stop_does_not_call_stopPlugin_if_Bridge_does_not_exist_in_container() { + componentContainer.startComponents(); underTest.stop(); verifyNoMoreInteractions(bridge); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java index 9bfc49467bd..66e7e3912f1 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java @@ -20,17 +20,16 @@ package org.sonar.server.issue.notification; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class IssuesChangesNotificationModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new IssuesChangesNotificationModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7); + assertThat(container.getAddedObjects()).hasSize(7); } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java index d6e54529be7..e09a0ba8824 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java @@ -26,6 +26,7 @@ import org.sonar.api.platform.Server; import org.sonar.api.platform.ServerStartHandler; import org.sonar.api.platform.ServerStopHandler; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -52,7 +53,7 @@ public class ServerLifecycleNotifierTest { */ @Test public void doNotNotifyWithTheStartMethod() { - ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop2}); + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[] {start1, start2}, new ServerStopHandler[] {stop2}); notifier.start(); verify(start1, never()).onServerStart(server); @@ -62,7 +63,7 @@ public class ServerLifecycleNotifierTest { @Test public void notifyOnStart() { - ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop2}); + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[] {start1, start2}, new ServerStopHandler[] {stop2}); notifier.notifyStart(); verify(start1).onServerStart(server); @@ -70,10 +71,9 @@ public class ServerLifecycleNotifierTest { verify(stop1, never()).onServerStop(server); } - @Test public void notifyOnStop() { - ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop1, stop2}); + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[] {start1, start2}, new ServerStopHandler[] {stop1, stop2}); notifier.stop(); verify(start1, never()).onServerStart(server); @@ -81,6 +81,13 @@ public class ServerLifecycleNotifierTest { verify(stop1).onServerStop(server); verify(stop2).onServerStop(server); } + + @Test + public void null_handler_param_wont_lead_to_NPE() { + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, null, null); + assertThatNoException().isThrownBy(notifier::notifyStart); + assertThatNoException().isThrownBy(notifier::stop); + } } class FakeServer extends Server { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java index 724e54efda2..1b1eb7f902a 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java @@ -34,21 +34,19 @@ import org.sonar.db.property.PropertyDto; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import static org.sonar.api.utils.DateUtils.formatDateTime; public class StartupMetadataProviderTest { - private static final long A_DATE = 1_500_000_000_000L; - @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - private StartupMetadataProvider underTest = new StartupMetadataProvider(); - private System2 system = mock(System2.class); - private WebServer webServer = mock(WebServer.class); + private final StartupMetadataProvider underTest = new StartupMetadataProvider(); + private final System2 system = mock(System2.class); + private final WebServer webServer = mock(WebServer.class); @Test public void generate_SERVER_STARTIME_but_do_not_persist_it_if_server_is_startup_leader() { @@ -60,10 +58,6 @@ public class StartupMetadataProviderTest { assertThat(metadata.getStartedAt()).isEqualTo(A_DATE); assertNotPersistedProperty(CoreProperties.SERVER_STARTTIME); - - // keep a cache - StartupMetadata secondMetadata = underTest.provide(system, runtime, webServer, dbTester.getDbClient()); - assertThat(secondMetadata).isSameAs(metadata); } @Test @@ -109,11 +103,7 @@ public class StartupMetadataProviderTest { // still in database assertPersistedProperty(CoreProperties.SERVER_STARTTIME, formatDateTime(A_DATE)); - // keep a cache - StartupMetadata secondMetadata = underTest.provide(system, runtime, webServer, dbTester.getDbClient()); - assertThat(secondMetadata).isSameAs(metadata); - - verifyZeroInteractions(system); + verifyNoInteractions(system); } private void assertPersistedProperty(String propertyKey, String expectedValue) { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java index 7ea17aeca90..f8a82450dff 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java @@ -31,12 +31,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class TempFolderProviderTest { - - @Rule public TemporaryFolder temp = new TemporaryFolder(); - TempFolderProvider underTest = new TempFolderProvider(); + private final TempFolderProvider underTest = new TempFolderProvider(); @Test public void existing_temp_dir() throws Exception { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java index c8b3e0db118..ce7004ea6cb 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java @@ -27,10 +27,11 @@ import org.sonar.api.Plugin; 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.internal.SonarRuntimeImpl; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.Version; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; @@ -42,7 +43,6 @@ import static org.mockito.Mockito.when; public class ServerExtensionInstallerTest { private SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.parse("8.0"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY); private TestPluginRepository pluginRepository = new TestPluginRepository(); - private TestServerExtensionInstaller underTest = new TestServerExtensionInstaller(sonarRuntime, pluginRepository); @Test @@ -50,11 +50,11 @@ public class ServerExtensionInstallerTest { PluginInfo fooPluginInfo = newPlugin("foo", "Foo"); Plugin fooPlugin = mock(Plugin.class); pluginRepository.add(fooPluginInfo, fooPlugin); - ComponentContainer componentContainer = new ComponentContainer(); + ListContainer componentContainer = new ListContainer(); underTest.installExtensions(componentContainer); - assertThat(componentContainer.getPicoContainer().getComponents()).contains(fooPlugin); + assertThat(componentContainer.getAddedObjects()).contains(fooPlugin); } private static PluginInfo newPlugin(String key, String name) { @@ -108,7 +108,7 @@ public class ServerExtensionInstallerTest { private static class TestServerExtensionInstaller extends ServerExtensionInstaller { protected TestServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository) { - super(sonarRuntime, pluginRepository, singleton(ServerSide.class)); + super(mock(Configuration.class), sonarRuntime, pluginRepository, singleton(ServerSide.class)); } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java index 729ac9654dc..01879d1053a 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java @@ -41,8 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat; public class OkHttpClientProviderTest { - private MapSettings settings = new MapSettings(); - private SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("6.2"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY); + private final MapSettings settings = new MapSettings(); + private final SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("6.2"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY); private final OkHttpClientProvider underTest = new OkHttpClientProvider(); @Rule @@ -74,13 +74,6 @@ public class OkHttpClientProviderTest { assertThat(request.header("Proxy-Authorization")).isEqualTo("Basic " + Base64.getEncoder().encodeToString("the-login:the-password".getBytes())); } - @Test - public void get_returns_a_singleton() { - OkHttpClient client1 = underTest.provide(settings.asConfig(), runtime); - OkHttpClient client2 = underTest.provide(settings.asConfig(), runtime); - assertThat(client2).isNotNull().isSameAs(client1); - } - private RecordedRequest call(OkHttpClient client) throws IOException, InterruptedException { server.enqueue(new MockResponse().setBody("pong")); client.newCall(new Request.Builder().url(server.url("/ping")).build()).execute(); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java index f53ad77b459..20bd6df587e 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java @@ -20,7 +20,7 @@ package org.sonar.server.webhook; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; @@ -30,10 +30,10 @@ public class WebhookModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.size()).isNotZero(); + assertThat(container.getAddedObjects().size()).isNotZero(); } } diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java index 9a156bbc4fe..208827927e7 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java @@ -19,15 +19,13 @@ */ package org.sonar.server.branch; +import javax.annotation.Nullable; + public class BranchFeatureProxyImpl implements BranchFeatureProxy { private final BranchFeatureExtension branchFeatureExtension; - public BranchFeatureProxyImpl() { - this.branchFeatureExtension = null; - } - - public BranchFeatureProxyImpl(BranchFeatureExtension branchFeatureExtension) { + public BranchFeatureProxyImpl(@Nullable BranchFeatureExtension branchFeatureExtension) { this.branchFeatureExtension = branchFeatureExtension; } diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/platform/Platform.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/platform/Platform.java index 90327ca6b60..2881354d7f6 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/platform/Platform.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/platform/Platform.java @@ -19,14 +19,14 @@ */ package org.sonar.server.platform; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ExtensionContainer; public interface Platform { void doStart(); Status status(); - ComponentContainer getContainer(); + ExtensionContainer getContainer(); enum Status { BOOTING, SAFEMODE, STARTING, UP diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginConsentVerifier.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginConsentVerifier.java index 67388bab01a..c50fb1209df 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginConsentVerifier.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginConsentVerifier.java @@ -21,7 +21,7 @@ package org.sonar.server.plugins; import java.util.Optional; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.core.extension.PluginRiskConsent; diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginDownloader.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginDownloader.java index eea3869e8fd..d1dc564756a 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginDownloader.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginDownloader.java @@ -28,7 +28,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import org.apache.commons.io.FileUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginJarLoader.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginJarLoader.java index 504fae1fba3..0f2eb7ccab9 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginJarLoader.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginJarLoader.java @@ -50,6 +50,8 @@ import static org.sonar.server.log.ServerProcessLogging.STARTUP_LOGGER_NAME; import static org.sonar.server.plugins.PluginType.BUNDLED; import static org.sonar.server.plugins.PluginType.EXTERNAL; +import javax.inject.Inject; + public class PluginJarLoader { private static final Logger LOG = Loggers.get(PluginJarLoader.class); @@ -65,6 +67,7 @@ public class PluginJarLoader { private final SonarRuntime runtime; private final Set<String> blacklistedPluginKeys; + @Inject public PluginJarLoader(ServerFileSystem fs, SonarRuntime runtime) { this(fs, runtime, DEFAULT_BLACKLISTED_PLUGINS); } diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginUninstaller.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginUninstaller.java index af64eb42f9b..c33155e39ad 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginUninstaller.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/PluginUninstaller.java @@ -26,7 +26,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; import org.apache.commons.io.FileUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.core.platform.PluginInfo; diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginManager.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginManager.java index 36ec05cd55d..6f83c583991 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginManager.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/ServerPluginManager.java @@ -24,7 +24,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.Plugin; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/WebServerExtensionInstaller.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/WebServerExtensionInstaller.java index b384b808fcf..b25cd9b9644 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/WebServerExtensionInstaller.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/plugins/WebServerExtensionInstaller.java @@ -20,6 +20,7 @@ package org.sonar.server.plugins; import org.sonar.api.SonarRuntime; +import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonar.core.platform.PluginRepository; @@ -27,7 +28,7 @@ import static java.util.Collections.singleton; @ServerSide public class WebServerExtensionInstaller extends ServerExtensionInstaller { - public WebServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository) { - super(sonarRuntime, pluginRepository, singleton(ServerSide.class)); + public WebServerExtensionInstaller(Configuration configuration, SonarRuntime sonarRuntime, PluginRepository pluginRepository) { + super(configuration, sonarRuntime, pluginRepository, singleton(ServerSide.class)); } } diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectLifeCycleListenersImpl.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectLifeCycleListenersImpl.java index f752797d580..89c69236aa0 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectLifeCycleListenersImpl.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectLifeCycleListenersImpl.java @@ -24,6 +24,7 @@ import java.util.Set; import java.util.function.Consumer; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkNotNull; @@ -33,15 +34,17 @@ public class ProjectLifeCycleListenersImpl implements ProjectLifeCycleListeners private final ProjectLifeCycleListener[] listeners; /** - * Used by Pico when there is no ProjectLifeCycleListener implementation in container. + * Used by the container when there is no ProjectLifeCycleListener implementation in container. */ + @Autowired(required = false) public ProjectLifeCycleListenersImpl() { this.listeners = new ProjectLifeCycleListener[0]; } /** - * Used by Pico when there is at least one ProjectLifeCycleListener implementation in container. + * Used by the container when there is at least one ProjectLifeCycleListener implementation in container. */ + @Autowired(required = false) public ProjectLifeCycleListenersImpl(ProjectLifeCycleListener[] listeners) { this.listeners = listeners; } diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java index b08dadf5ea1..543a446e935 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.Set; +import javax.annotation.Nullable; import org.sonar.api.issue.Issue; import org.sonar.api.rules.RuleType; import org.sonar.api.utils.log.Logger; @@ -37,7 +38,7 @@ import static org.sonar.core.util.stream.MoreCollectors.toSet; /** * Broadcast a given collection of {@link QGChangeEvent} for a specific trigger to all the registered - * {@link QGChangeEventListener} in Pico. + * {@link QGChangeEventListener} in the ioc container. * * This class ensures that an {@link Exception} occurring calling one of the {@link QGChangeEventListener} doesn't * prevent from calling the others. @@ -47,15 +48,8 @@ public class QGChangeEventListenersImpl implements QGChangeEventListeners { private final QGChangeEventListener[] listeners; - /** - * Used by Pico when there is no QGChangeEventListener instance in container. - */ - public QGChangeEventListenersImpl() { - this.listeners = new QGChangeEventListener[0]; - } - - public QGChangeEventListenersImpl(QGChangeEventListener[] listeners) { - this.listeners = listeners; + public QGChangeEventListenersImpl(@Nullable QGChangeEventListener[] listeners) { + this.listeners = listeners != null ? listeners : new QGChangeEventListener[0]; } @Override diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/setting/SettingsChangeNotifier.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/setting/SettingsChangeNotifier.java index 01c7a51a57f..41aaef46e01 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/setting/SettingsChangeNotifier.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/setting/SettingsChangeNotifier.java @@ -23,16 +23,19 @@ import com.google.common.annotations.VisibleForTesting; import org.sonar.api.config.GlobalPropertyChangeHandler; import javax.annotation.Nullable; +import org.springframework.beans.factory.annotation.Autowired; public class SettingsChangeNotifier { @VisibleForTesting GlobalPropertyChangeHandler[] changeHandlers; + @Autowired(required = false) public SettingsChangeNotifier(GlobalPropertyChangeHandler[] changeHandlers) { this.changeHandlers = changeHandlers; } + @Autowired(required = false) public SettingsChangeNotifier() { this(new GlobalPropertyChangeHandler[0]); } diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java index 61c37f59fb4..650ca6373a6 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java @@ -31,7 +31,7 @@ public class BranchFeatureProxyImplTest { @Test public void return_false_when_no_extension() { - assertThat(new BranchFeatureProxyImpl().isEnabled()).isFalse(); + assertThat(new BranchFeatureProxyImpl(null).isEnabled()).isFalse(); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImplTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImplTest.java index ecbf74b275a..01fdf0140ab 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImplTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImplTest.java @@ -167,7 +167,7 @@ public class QGChangeEventListenersImplTest { @Test public void broadcastOnIssueChange_has_no_effect_when_no_listener() { - QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(); + QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(null); underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent)); diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/TypeValidationModuleTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/TypeValidationModuleTest.java index d5a3a16963e..a877b82a077 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/TypeValidationModuleTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/TypeValidationModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.util; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class TypeValidationModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new TypeValidationModule().configure(container); - assertThat(container.size()).isEqualTo(11); + assertThat(container.getAddedObjects()).hasSize(9); } } diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/IdentityProviderRepository.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/IdentityProviderRepository.java index ed4788ddc37..bc5bef18587 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/IdentityProviderRepository.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/IdentityProviderRepository.java @@ -19,35 +19,31 @@ */ package org.sonar.server.authentication; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.Ordering; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; -import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.sonar.api.server.authentication.IdentityProvider; public class IdentityProviderRepository { + private static final Predicate<IdentityProvider> IS_ENABLED_FILTER = IdentityProvider::isEnabled; + private static final Function<IdentityProvider, String> TO_NAME = IdentityProvider::getName; protected final Map<String, IdentityProvider> providersByKey = new HashMap<>(); - public IdentityProviderRepository(List<IdentityProvider> identityProviders) { - this.providersByKey.putAll(FluentIterable.from(identityProviders).uniqueIndex(ToKey.INSTANCE)); - } - - /** - * Used by pico when no identity provider available - */ - public IdentityProviderRepository() { - // nothing to do + public IdentityProviderRepository(@Nullable List<IdentityProvider> identityProviders) { + Optional.ofNullable(identityProviders) + .ifPresent(list -> list.forEach(i -> providersByKey.put(i.getKey(), i))); } public IdentityProvider getEnabledByKey(String key) { IdentityProvider identityProvider = providersByKey.get(key); - if (identityProvider != null && IsEnabledFilter.INSTANCE.apply(identityProvider)) { + if (identityProvider != null && IS_ENABLED_FILTER.test(identityProvider)) { return identityProvider; } throw new IllegalArgumentException(String.format("Identity provider %s does not exist or is not enabled", key)); @@ -55,35 +51,9 @@ public class IdentityProviderRepository { public List<IdentityProvider> getAllEnabledAndSorted() { return providersByKey.values().stream() - .filter(IsEnabledFilter.INSTANCE) - .sorted(Ordering.natural().onResultOf(ToName.INSTANCE)) + .filter(IS_ENABLED_FILTER) + .sorted(Comparator.comparing(TO_NAME)) .collect(Collectors.toList()); } - private enum IsEnabledFilter implements Predicate<IdentityProvider> { - INSTANCE; - - @Override - public boolean apply(@Nonnull IdentityProvider input) { - return input.isEnabled(); - } - } - - private enum ToKey implements Function<IdentityProvider, String> { - INSTANCE; - - @Override - public String apply(@Nonnull IdentityProvider input) { - return input.getKey(); - } - } - - private enum ToName implements Function<IdentityProvider, String> { - INSTANCE; - - @Override - public String apply(@Nonnull IdentityProvider input) { - return input.getName(); - } - } } diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LogOAuthWarning.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LogOAuthWarning.java index ebf56285f57..6942d6abcef 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LogOAuthWarning.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LogOAuthWarning.java @@ -24,20 +24,23 @@ import org.sonar.api.Startable; import org.sonar.api.platform.Server; import org.sonar.api.server.authentication.OAuth2IdentityProvider; import org.sonar.api.utils.log.Loggers; +import org.springframework.beans.factory.annotation.Autowired; public class LogOAuthWarning implements Startable { private final Server server; private final OAuth2IdentityProvider[] providers; + @Autowired(required = false) public LogOAuthWarning(Server server, OAuth2IdentityProvider[] providers) { this.server = server; this.providers = providers; } /** - * Used by default by picocontainer when no OAuth2IdentityProvider are present + * Used by default by the ioc container when no OAuth2IdentityProvider are present */ + @Autowired(required = false) public LogOAuthWarning(Server server) { this(server, new OAuth2IdentityProvider[0]); } diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/RequestAuthenticatorImpl.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/RequestAuthenticatorImpl.java index 78c9a8761ff..4f7871f594a 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/RequestAuthenticatorImpl.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/RequestAuthenticatorImpl.java @@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletResponse; import org.sonar.db.user.UserDto; import org.sonar.server.user.UserSession; import org.sonar.server.user.UserSessionFactory; +import org.springframework.beans.factory.annotation.Autowired; public class RequestAuthenticatorImpl implements RequestAuthenticator { @@ -36,6 +37,7 @@ public class RequestAuthenticatorImpl implements RequestAuthenticator { private final UserSessionFactory userSessionFactory; private final List<CustomAuthentication> customAuthentications; + @Autowired(required = false) public RequestAuthenticatorImpl(JwtHttpHandler jwtHttpHandler, BasicAuthentication basicAuthentication, HttpHeadersAuthentication httpHeadersAuthentication, UserSessionFactory userSessionFactory, CustomAuthentication[] customAuthentications) { this.jwtHttpHandler = jwtHttpHandler; @@ -45,6 +47,7 @@ public class RequestAuthenticatorImpl implements RequestAuthenticator { this.customAuthentications = Arrays.asList(customAuthentications); } + @Autowired(required = false) public RequestAuthenticatorImpl(JwtHttpHandler jwtHttpHandler, BasicAuthentication basicAuthentication, HttpHeadersAuthentication httpHeadersAuthentication, UserSessionFactory userSessionFactory) { this(jwtHttpHandler, basicAuthentication, httpHeadersAuthentication, userSessionFactory, new CustomAuthentication[0]); diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/NewUserNotifier.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/NewUserNotifier.java index 9fd9aee4c46..c6cbc705e3f 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/NewUserNotifier.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/NewUserNotifier.java @@ -22,6 +22,7 @@ package org.sonar.server.user; import org.sonar.api.server.ServerSide; import org.sonar.api.platform.NewUserHandler; import org.sonar.api.utils.log.Loggers; +import org.springframework.beans.factory.annotation.Autowired; /** * @since 3.2 @@ -29,12 +30,14 @@ import org.sonar.api.utils.log.Loggers; @ServerSide public class NewUserNotifier { - private NewUserHandler[] handlers; + private final NewUserHandler[] handlers; + @Autowired(required = false) public NewUserNotifier(NewUserHandler[] handlers) { this.handlers = handlers; } + @Autowired(required = false) public NewUserNotifier() { this(new NewUserHandler[0]); } diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/SecurityRealmFactory.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/SecurityRealmFactory.java index 650423f4c79..1330478d0f3 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/SecurityRealmFactory.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/SecurityRealmFactory.java @@ -21,7 +21,7 @@ package org.sonar.server.user; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.CoreProperties; import org.sonar.api.config.Configuration; import org.sonar.api.security.LoginPasswordAuthenticator; @@ -33,6 +33,7 @@ import org.sonar.api.utils.log.Loggers; import static org.sonar.process.ProcessProperties.Property.SONAR_AUTHENTICATOR_IGNORE_STARTUP_FAILURE; import static org.sonar.process.ProcessProperties.Property.SONAR_SECURITY_REALM; +import org.springframework.beans.factory.annotation.Autowired; /** * @since 2.14 @@ -43,6 +44,7 @@ public class SecurityRealmFactory implements Startable { private final boolean ignoreStartupFailure; private final SecurityRealm realm; + @Autowired(required = false) public SecurityRealmFactory(Configuration config, SecurityRealm[] realms, LoginPasswordAuthenticator[] authenticators) { ignoreStartupFailure = config.getBoolean(SONAR_AUTHENTICATOR_IGNORE_STARTUP_FAILURE.getKey()).orElse(false); String realmName = config.get(SONAR_SECURITY_REALM.getKey()).orElse(null); @@ -66,14 +68,17 @@ public class SecurityRealmFactory implements Startable { realm = selectedRealm; } + @Autowired(required = false) public SecurityRealmFactory(Configuration config, LoginPasswordAuthenticator[] authenticators) { this(config, new SecurityRealm[0], authenticators); } + @Autowired(required = false) public SecurityRealmFactory(Configuration config, SecurityRealm[] realms) { this(config, realms, new LoginPasswordAuthenticator[0]); } + @Autowired(required = false) public SecurityRealmFactory(Configuration config) { this(config, new SecurityRealm[0], new LoginPasswordAuthenticator[0]); } diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java index 33838f06444..d8d9fca10f3 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java @@ -29,6 +29,7 @@ import java.util.Objects; import java.util.function.Consumer; import java.util.stream.Stream; import javax.annotation.Nullable; +import javax.inject.Inject; import org.apache.commons.lang.math.RandomUtils; import org.sonar.api.config.Configuration; import org.sonar.api.platform.NewUserHandler; @@ -78,20 +79,9 @@ public class UserUpdater { private final DefaultGroupFinder defaultGroupFinder; private final Configuration config; private final AuditPersister auditPersister; - private final CredentialsLocalAuthentication localAuthentication; - public UserUpdater(NewUserNotifier newUserNotifier, DbClient dbClient, UserIndexer userIndexer, DefaultGroupFinder defaultGroupFinder, Configuration config, - CredentialsLocalAuthentication localAuthentication) { - this.newUserNotifier = newUserNotifier; - this.dbClient = dbClient; - this.userIndexer = userIndexer; - this.defaultGroupFinder = defaultGroupFinder; - this.config = config; - this.auditPersister = null; - this.localAuthentication = localAuthentication; - } - + @Inject public UserUpdater(NewUserNotifier newUserNotifier, DbClient dbClient, UserIndexer userIndexer, DefaultGroupFinder defaultGroupFinder, Configuration config, AuditPersister auditPersister, CredentialsLocalAuthentication localAuthentication) { this.newUserNotifier = newUserNotifier; @@ -265,9 +255,7 @@ public class UserUpdater { if (updateUser.isPasswordChanged() && validatePasswords(password, messages) && checkPasswordChangeAllowed(userDto, messages)) { localAuthentication.storeHashPassword(userDto, password); userDto.setResetPassword(false); - if (auditPersister != null) { - auditPersister.updateUserPassword(dbSession, new SecretNewValue("userLogin", userDto.getLogin())); - } + auditPersister.updateUserPassword(dbSession, new SecretNewValue("userLogin", userDto.getLogin())); return true; } return false; diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/AuthenticationModuleTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/AuthenticationModuleTest.java index 77b133e1e09..7c2e2b5370a 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/AuthenticationModuleTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/AuthenticationModuleTest.java @@ -20,18 +20,15 @@ package org.sonar.server.authentication; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class AuthenticationModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new AuthenticationModule().configure(container); - assertThat(container.size()).isGreaterThan(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); + assertThat(container.getAddedObjects()).isNotEmpty(); } - } diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/IdentityProviderRepositoryTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/IdentityProviderRepositoryTest.java index ec689859ada..442a911ddc4 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/IdentityProviderRepositoryTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/IdentityProviderRepositoryTest.java @@ -63,6 +63,15 @@ public class IdentityProviderRepositoryTest { } @Test + public void fail_on_non_exist_provider() { + IdentityProviderRepository underTest = new IdentityProviderRepository(asList(GITHUB, BITBUCKET, DISABLED)); + + assertThatThrownBy(() -> underTest.getEnabledByKey("NotExist")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Identity provider NotExist does not exist or is not enabled"); + } + + @Test public void return_all_enabled_providers() { IdentityProviderRepository underTest = new IdentityProviderRepository(asList(GITHUB, BITBUCKET, DISABLED)); @@ -80,7 +89,7 @@ public class IdentityProviderRepositoryTest { @Test public void return_nothing_when_no_identity_provider() { - IdentityProviderRepository underTest = new IdentityProviderRepository(); + IdentityProviderRepository underTest = new IdentityProviderRepository(null); assertThat(underTest.getAllEnabledAndSorted()).isEmpty(); } diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenModuleTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenModuleTest.java index 77e8d741e52..563e316ab5c 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenModuleTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/UserTokenModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.usertoken; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class UserTokenModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new UserTokenModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); + assertThat(container.getAddedObjects()).hasSize(2); } } diff --git a/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/authentication/IdentityProviderRepositoryRule.java b/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/authentication/IdentityProviderRepositoryRule.java index 16cdbeb333f..840a099e613 100644 --- a/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/authentication/IdentityProviderRepositoryRule.java +++ b/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/authentication/IdentityProviderRepositoryRule.java @@ -26,6 +26,10 @@ import org.sonar.api.server.authentication.IdentityProvider; public class IdentityProviderRepositoryRule extends IdentityProviderRepository implements TestRule { + public IdentityProviderRepositoryRule() { + super(null); + } + public IdentityProviderRepositoryRule addIdentityProvider(IdentityProvider identityProvider) { providersByKey.put(identityProvider.getKey(), identityProvider); return this; diff --git a/server/sonar-webserver-core/build.gradle b/server/sonar-webserver-core/build.gradle index 9aba24d6f0c..3c76f65098e 100644 --- a/server/sonar-webserver-core/build.gradle +++ b/server/sonar-webserver-core/build.gradle @@ -29,7 +29,6 @@ dependencies { compile 'org.apache.logging.log4j:log4j-api' compile 'org.apache.tomcat.embed:tomcat-embed-core' compile 'org.apache.commons:commons-dbcp2' - compile 'org.picocontainer:picocontainer' compile 'org.slf4j:jul-to-slf4j' compile 'org.slf4j:slf4j-api' compile 'org.sonarsource.update-center:sonar-update-center-common' diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/notification/NotificationDaemon.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/notification/NotificationDaemon.java index 31311378be9..7e5e9f2aadb 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/notification/NotificationDaemon.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/notification/NotificationDaemon.java @@ -24,7 +24,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.Properties; import org.sonar.api.Property; import org.sonar.api.config.Configuration; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ClusterVerification.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ClusterVerification.java index 98731483b89..25a8e4c1ce0 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ClusterVerification.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ClusterVerification.java @@ -19,11 +19,13 @@ */ package org.sonar.server.platform; -import javax.annotation.Nullable; import org.sonar.api.Startable; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.MessageException; +import javax.annotation.Nullable; +import javax.inject.Inject; + @ServerSide public class ClusterVerification implements Startable { @@ -31,6 +33,7 @@ public class ClusterVerification implements Startable { @Nullable private final ClusterFeature feature; + @Inject public ClusterVerification(WebServer server, @Nullable ClusterFeature feature) { this.server = server; this.feature = feature; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java index 060c8fee4ff..350358dee54 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java @@ -20,7 +20,7 @@ package org.sonar.server.platform; import java.util.Optional; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Logger; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java index 432e55dc694..10755afccaa 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java @@ -22,7 +22,7 @@ package org.sonar.server.platform; import java.util.Optional; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.platform.ServerUpgradeStatus; import org.sonar.process.ProcessProperties; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/CheckDatabaseCharsetAtStartup.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/CheckDatabaseCharsetAtStartup.java index b25aa42ea1f..50140f61be2 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/CheckDatabaseCharsetAtStartup.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/CheckDatabaseCharsetAtStartup.java @@ -19,7 +19,7 @@ */ package org.sonar.server.platform.db; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.platform.ServerUpgradeStatus; import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabase.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabase.java index a31e30d8493..f53945274ed 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabase.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabase.java @@ -25,7 +25,7 @@ import java.sql.DriverManager; import java.sql.SQLException; import org.h2.Driver; import org.h2.tools.Server; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabaseFactory.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabaseFactory.java index 255c5b744f4..f052b57ad70 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabaseFactory.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/EmbeddedDatabaseFactory.java @@ -20,7 +20,7 @@ package org.sonar.server.platform.db; import com.google.common.annotations.VisibleForTesting; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.System2; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/migration/AutoDbMigration.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/migration/AutoDbMigration.java index 4c68c16faf9..140f4f297b7 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/migration/AutoDbMigration.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/db/migration/AutoDbMigration.java @@ -19,7 +19,7 @@ */ package org.sonar.server.platform.db.migration; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.log.Loggers; import org.sonar.server.platform.DefaultServerUpgradeStatus; import org.sonar.server.platform.db.migration.engine.MigrationEngine; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BaseSectionMBean.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BaseSectionMBean.java index ad2ee011800..b5afeaff0e2 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BaseSectionMBean.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/BaseSectionMBean.java @@ -19,7 +19,7 @@ */ package org.sonar.server.platform.monitoring; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.process.Jmx; import org.sonar.process.systeminfo.SystemInfoSection; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdManager.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdManager.java index 87052b55224..86ecd608095 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdManager.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdManager.java @@ -20,7 +20,7 @@ package org.sonar.server.platform.serverid; import java.util.Optional; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; import org.sonar.api.utils.log.Logger; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/qualitygate/ProjectsInWarningDaemon.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/qualitygate/ProjectsInWarningDaemon.java index c4373e7ad6e..0bf0a72adf1 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/qualitygate/ProjectsInWarningDaemon.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/qualitygate/ProjectsInWarningDaemon.java @@ -25,7 +25,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java index a5c63e36b70..5309cbb697f 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -37,7 +37,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.resources.Languages; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleScope; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java index 795100eb921..c3571946f31 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java @@ -22,6 +22,7 @@ package org.sonar.server.rule; import org.sonar.api.server.rule.RulesDefinition; import org.sonar.api.impl.server.RulesDefinitionContext; import org.sonar.server.plugins.ServerPluginRepository; +import org.springframework.beans.factory.annotation.Autowired; /** * Loads all instances of {@link RulesDefinition}. Used during server startup @@ -33,6 +34,7 @@ public class RuleDefinitionsLoader { private final RulesDefinition[] pluginDefs; private final ServerPluginRepository serverPluginRepository; + @Autowired(required = false) public RuleDefinitionsLoader(CommonRuleDefinitions coreCommonDefs, ServerPluginRepository serverPluginRepository, RulesDefinition[] pluginDefs) { this.coreCommonDefs = coreCommonDefs; this.serverPluginRepository = serverPluginRepository; @@ -42,6 +44,7 @@ public class RuleDefinitionsLoader { /** * Used when no definitions at all. */ + @Autowired(required = false) public RuleDefinitionsLoader(CommonRuleDefinitions coreCommonDefs, ServerPluginRepository serverPluginRepository) { this(coreCommonDefs, serverPluginRepository, new RulesDefinition[0]); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/LogServerId.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/LogServerId.java index 056a68d482f..96fe41c24b6 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/LogServerId.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/LogServerId.java @@ -19,7 +19,7 @@ */ package org.sonar.server.startup; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.platform.Server; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterMetrics.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterMetrics.java index 6a07999d5c1..57122ba35b6 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterMetrics.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterMetrics.java @@ -23,7 +23,7 @@ import com.google.common.annotations.VisibleForTesting; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; import org.sonar.api.measures.Metrics; @@ -38,6 +38,7 @@ import org.sonar.server.metric.MetricToDto; import static com.google.common.collect.FluentIterable.concat; import static com.google.common.collect.Lists.newArrayList; +import org.springframework.beans.factory.annotation.Autowired; public class RegisterMetrics implements Startable { @@ -47,6 +48,7 @@ public class RegisterMetrics implements Startable { private final UuidFactory uuidFactory; private final Metrics[] metricsRepositories; + @Autowired(required = false) public RegisterMetrics(DbClient dbClient, UuidFactory uuidFactory, Metrics[] metricsRepositories) { this.dbClient = dbClient; this.uuidFactory = uuidFactory; @@ -56,6 +58,7 @@ public class RegisterMetrics implements Startable { /** * Used when no plugin is defining Metrics */ + @Autowired(required = false) public RegisterMetrics(DbClient dbClient, UuidFactory uuidFactory) { this(dbClient, uuidFactory, new Metrics[] {}); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java index faf8d24af05..8a9732a738e 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java @@ -21,7 +21,7 @@ package org.sonar.server.startup; import java.util.Date; import java.util.Optional; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.security.DefaultGroups; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java index 05b41f65867..fc7c89f216a 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java @@ -20,7 +20,7 @@ package org.sonar.server.startup; import com.google.common.base.Strings; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/UpgradeSuggestionsCleaner.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/UpgradeSuggestionsCleaner.java index 9d2497cc544..414c6de5c36 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/UpgradeSuggestionsCleaner.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/startup/UpgradeSuggestionsCleaner.java @@ -19,7 +19,7 @@ */ package org.sonar.server.startup; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.SonarEdition; import org.sonar.api.SonarRuntime; import org.sonar.api.server.ServerSide; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java index 89dbeaa01a4..8880b874b73 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java @@ -28,7 +28,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.System2; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java index ab9ec5daaa6..8caa743ca16 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java @@ -29,6 +29,7 @@ import java.util.function.Function; import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import javax.inject.Inject; import org.sonar.api.config.Configuration; import org.sonar.api.platform.Server; import org.sonar.api.server.ServerSide; @@ -75,11 +76,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { @CheckForNull private final LicenseReader licenseReader; - public TelemetryDataLoaderImpl(Server server, DbClient dbClient, PluginRepository pluginRepository, UserIndex userIndex, ProjectMeasuresIndex projectMeasuresIndex, - PlatformEditionProvider editionProvider, InternalProperties internalProperties, Configuration configuration, DockerSupport dockerSupport) { - this(server, dbClient, pluginRepository, userIndex, projectMeasuresIndex, editionProvider, internalProperties, configuration, dockerSupport, null); - } - + @Inject public TelemetryDataLoaderImpl(Server server, DbClient dbClient, PluginRepository pluginRepository, UserIndex userIndex, ProjectMeasuresIndex projectMeasuresIndex, PlatformEditionProvider editionProvider, InternalProperties internalProperties, Configuration configuration, DockerSupport dockerSupport, @Nullable LicenseReader licenseReader) { diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/ce/CeModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/ce/CeModuleTest.java index 537822c47db..0bfece6f850 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/ce/CeModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/ce/CeModuleTest.java @@ -20,19 +20,15 @@ package org.sonar.server.ce; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class CeModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new CeModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 3); + assertThat(container.getAddedObjects()).hasSize(3); } - - } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationModuleTest.java index dc7bea41056..680768120e3 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.notification; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class NotificationModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new NotificationModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 5); + assertThat(container.getAddedObjects()).hasSize(5); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java index 1d586a9ac43..21f300bd07b 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java @@ -19,42 +19,31 @@ */ package org.sonar.server.platform; -import java.util.Collection; import org.junit.Test; -import org.picocontainer.ComponentAdapter; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class SystemInfoWriterModuleTest { - private WebServer webServer = mock(WebServer.class); - private SystemInfoWriterModule underTest = new SystemInfoWriterModule(webServer); + private final WebServer webServer = mock(WebServer.class); + private final SystemInfoWriterModule underTest = new SystemInfoWriterModule(webServer); @Test public void verify_system_info_configuration_in_cluster_mode() { when(webServer.isStandalone()).thenReturn(false); - ComponentContainer container = new ComponentContainer(); - + ListContainer container = new ListContainer(); underTest.configure(container); - - Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters(); - assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 20); + assertThat(container.getAddedObjects()).hasSize(20); } @Test public void verify_system_info_configuration_in_standalone_mode() { when(webServer.isStandalone()).thenReturn(true); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - - Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters(); - assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 14); + assertThat(container.getAddedObjects()).hasSize(14); } - } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/WebCoreExtensionsInstallerTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/WebCoreExtensionsInstallerTest.java index 58799305fa0..0b4df5dcbf7 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/WebCoreExtensionsInstallerTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/WebCoreExtensionsInstallerTest.java @@ -32,7 +32,7 @@ import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.core.extension.CoreExtension; import org.sonar.core.extension.CoreExtensionRepository; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -41,10 +41,9 @@ import static org.sonar.core.extension.CoreExtensionsInstaller.noAdditionalSideF import static org.sonar.core.extension.CoreExtensionsInstaller.noExtensionFilter; public class WebCoreExtensionsInstallerTest { - private SonarRuntime sonarRuntime = mock(SonarRuntime.class); - private CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class); - - private WebCoreExtensionsInstaller underTest = new WebCoreExtensionsInstaller(sonarRuntime, coreExtensionRepository); + private final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + private final CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class); + private final WebCoreExtensionsInstaller underTest = new WebCoreExtensionsInstaller(sonarRuntime, coreExtensionRepository); @Test public void install_only_adds_ServerSide_annotated_extension_to_container() { @@ -61,14 +60,10 @@ public class WebCoreExtensionsInstallerTest { NoAnnotationClass.class, OtherAnnotationClass.class, MultipleAnnotationClass.class); } })); - ComponentContainer container = new ComponentContainer(); - + ListContainer container = new ListContainer(); underTest.install(container, noExtensionFilter(), noAdditionalSideFilter()); - assertThat(container.getPicoContainer().getComponentAdapters()) - .hasSize(ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); - assertThat(container.getComponentByType(WebServerClass.class)).isNotNull(); - assertThat(container.getComponentByType(MultipleAnnotationClass.class)).isNotNull(); + assertThat(container.getAddedObjects()).containsOnly(WebServerClass.class, MultipleAnnotationClass.class); } @ComputeEngineSide diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java index 394f2c5669d..9bf681d74aa 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java @@ -20,19 +20,17 @@ package org.sonar.server.platform.serverid; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ServerIdModuleTest { - private ServerIdModule underTest = new ServerIdModule(); + private final ServerIdModule underTest = new ServerIdModule(); @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 4); + assertThat(container.getAddedObjects()).hasSize(4); } - } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java index 0036b24f104..ba1be304225 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java @@ -20,20 +20,17 @@ package org.sonar.server.platform.web.requestid; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class HttpRequestIdModuleTest { - private HttpRequestIdModule underTest = new HttpRequestIdModule(); + private final HttpRequestIdModule underTest = new HttpRequestIdModule(); @Test public void count_components_in_module() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - - assertThat(container.getPicoContainer().getComponentAdapters()) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 3); + assertThat(container.getAddedObjects()).hasSize(3); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/qualitygate/ProjectsInWarningModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/qualitygate/ProjectsInWarningModuleTest.java index f2f6e3a11dd..e2615268469 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/qualitygate/ProjectsInWarningModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/qualitygate/ProjectsInWarningModuleTest.java @@ -20,18 +20,15 @@ package org.sonar.server.qualitygate; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ProjectsInWarningModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new ProjectsInWarningModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); + assertThat(container.getAddedObjects()).hasSize(2); } - } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java index 0ba187b2971..1d3fac27fe6 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java @@ -41,7 +41,6 @@ import org.sonar.server.measure.index.ProjectMeasuresIndexer; import org.sonar.server.platform.DockerSupport; import org.sonar.server.property.InternalProperties; import org.sonar.server.property.MapInternalProperties; -import org.sonar.server.pushapi.sonarlint.SonarLintClientsRegistry; import org.sonar.server.user.index.UserIndex; import org.sonar.server.user.index.UserIndexer; import org.sonar.updatecenter.common.Version; @@ -86,7 +85,7 @@ public class TelemetryDataLoaderImplTest { private final LicenseReader licenseReader = mock(LicenseReader.class); private final TelemetryDataLoader communityUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2), - new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, configuration, dockerSupport); + new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, configuration, dockerSupport, null); private final TelemetryDataLoader commercialUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2), new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, configuration, dockerSupport, licenseReader); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java index 796d2eacd63..4f72c5df932 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.updatecenter; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class UpdateCenterModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new UpdateCenterModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); + assertThat(container.getAddedObjects()).hasSize(2); } } diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexCreator.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexCreator.java index b458e009c75..2948713edca 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexCreator.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexCreator.java @@ -35,7 +35,7 @@ import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.settings.Settings; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Logger; diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexDefinitions.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexDefinitions.java index f1872183948..0715da27abc 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexDefinitions.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/es/IndexDefinitions.java @@ -21,7 +21,7 @@ package org.sonar.server.es; import java.util.HashMap; import java.util.Map; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonar.server.es.newindex.BuiltIndex; diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java index 3b570e90edb..0429f41f0ca 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java @@ -43,6 +43,7 @@ import org.sonar.server.es.ProjectIndexer; import static java.util.Collections.emptyList; import static org.sonar.core.util.stream.MoreCollectors.toArrayList; +import org.springframework.beans.factory.annotation.Autowired; /** * Populates the types "authorization" of each index requiring project @@ -55,6 +56,7 @@ public class PermissionIndexer implements ProjectIndexer { private final Collection<AuthorizationScope> authorizationScopes; private final Map<String, IndexType> indexTypeByFormat; + @Autowired(required = false) public PermissionIndexer(DbClient dbClient, EsClient esClient, NeedAuthorizationIndexer... needAuthorizationIndexers) { this(dbClient, esClient, Arrays.stream(needAuthorizationIndexers) .map(NeedAuthorizationIndexer::getAuthorizationScope) @@ -62,6 +64,7 @@ public class PermissionIndexer implements ProjectIndexer { } @VisibleForTesting + @Autowired(required = false) public PermissionIndexer(DbClient dbClient, EsClient esClient, Collection<AuthorizationScope> authorizationScopes) { this.dbClient = dbClient; this.esClient = esClient; diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectsEsModuleTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectsEsModuleTest.java index 694f2c25b0c..78fa109aa90 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectsEsModuleTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectsEsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.measure.index; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ProjectsEsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new ProjectsEsModule().configure(container); - assertThat(container.size()).isEqualTo(3 + COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); + assertThat(container.getAddedObjects()).hasSize(3); } } diff --git a/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/MainCollector.java b/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/MainCollector.java index 7935a8213ec..e3878a6d58f 100644 --- a/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/MainCollector.java +++ b/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/MainCollector.java @@ -23,7 +23,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import static java.util.concurrent.TimeUnit.MILLISECONDS; diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsModuleTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsModuleTest.java index 11f5cfa5d6b..9ef3dec04f2 100644 --- a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsModuleTest.java +++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsModuleTest.java @@ -20,20 +20,16 @@ package org.sonar.server.pushapi; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class ServerPushWsModuleTest { - - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); - + ListContainer container = new ListContainer(); new ServerPushWsModule().configure(container); - - assertThat(container.size()).isPositive(); + assertThat(container.getAddedObjects()).isNotEmpty(); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/batch/BatchIndex.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/batch/BatchIndex.java index 279a84e4082..26f896856ef 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/batch/BatchIndex.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/batch/BatchIndex.java @@ -29,7 +29,7 @@ import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.HiddenFileFilter; import org.apache.commons.lang.CharUtils; import org.apache.commons.lang.StringUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.server.ServerSide; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.platform.ServerFileSystem; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/BranchSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/BranchSupport.java index 3c7e42d954f..af8e1b3e547 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/BranchSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/BranchSupport.java @@ -41,13 +41,6 @@ public class BranchSupport { @CheckForNull private final BranchSupportDelegate delegate; - /** - * Constructor called by Pico when no implementation of {@link BranchSupportDelegate} is available. - */ - public BranchSupport() { - this(null); - } - public BranchSupport(@Nullable BranchSupportDelegate delegate) { this.delegate = delegate; } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/CeQueueCleaner.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/CeQueueCleaner.java index 9b2b7a7f2fc..f150d586154 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/CeQueueCleaner.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/CeQueueCleaner.java @@ -20,7 +20,7 @@ package org.sonar.server.ce.queue; import java.util.List; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.platform.ServerUpgradeStatus; import org.sonar.api.server.ServerSide; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java index 356eb1b83e7..3c0d3ae5185 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java @@ -20,6 +20,8 @@ package org.sonar.server.ce.ws; import javax.annotation.Nullable; +import javax.inject.Inject; + import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -37,6 +39,7 @@ public class WorkerCountAction implements CeWsAction { private final UserSession userSession; private final WorkerCountProvider workerCountProvider; + @Inject public WorkerCountAction(UserSession userSession, @Nullable WorkerCountProvider workerCountProvider) { this.userSession = userSession; this.workerCountProvider = workerCountProvider; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java index 78237518a57..5e3399ab4d1 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java @@ -20,13 +20,7 @@ package org.sonar.server.duplication.ws; import com.google.common.annotations.VisibleForTesting; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; +import static java.util.Optional.ofNullable; import org.apache.commons.lang.StringUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -36,12 +30,20 @@ import org.sonarqube.ws.Duplications; import org.sonarqube.ws.Duplications.Block; import org.sonarqube.ws.Duplications.ShowResponse; -import static java.util.Optional.ofNullable; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; public class ShowResponseBuilder { private final ComponentDao componentDao; + @Inject public ShowResponseBuilder(DbClient dbClient) { this.componentDao = dbClient.componentDao(); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java index 2fa39a1085c..96547b3ed53 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java @@ -26,6 +26,7 @@ import javax.annotation.Nullable; import org.sonar.process.cluster.health.NodeHealth; import org.sonar.process.cluster.health.SharedHealthState; import org.sonar.server.platform.WebServer; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.ImmutableList.copyOf; @@ -42,15 +43,17 @@ public class HealthCheckerImpl implements HealthChecker { private final SharedHealthState sharedHealthState; /** - * Constructor used by Pico in standalone mode and in safe mode. + * Constructor used by the ioc container in standalone mode and in safe mode. */ + @Autowired(required = false) public HealthCheckerImpl(WebServer webServer, NodeHealthCheck[] nodeHealthChecks) { this(webServer, nodeHealthChecks, new ClusterHealthCheck[0], null); } /** - * Constructor used by Pico in cluster mode. + * Constructor used by the ioc container in cluster mode. */ + @Autowired(required = false) public HealthCheckerImpl(WebServer webServer, NodeHealthCheck[] nodeHealthChecks, ClusterHealthCheck[] clusterHealthChecks, @Nullable SharedHealthState sharedHealthState) { this.webServer = webServer; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/language/LanguageValidation.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/language/LanguageValidation.java index c23d7e103f9..a5c8e8e97cd 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/language/LanguageValidation.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/language/LanguageValidation.java @@ -21,20 +21,23 @@ package org.sonar.server.language; import java.util.Arrays; import java.util.stream.Collectors; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.resources.Language; import org.sonar.server.plugins.ServerPluginRepository; +import org.springframework.beans.factory.annotation.Autowired; public class LanguageValidation implements Startable { private final ServerPluginRepository pluginRepository; private final Language[] languages; + @Autowired(required = false) public LanguageValidation(ServerPluginRepository pluginRepository) { this.pluginRepository = pluginRepository; this.languages = new Language[0]; } + @Autowired(required = false) public LanguageValidation(ServerPluginRepository pluginRepository, Language... languages) { this.pluginRepository = pluginRepository; this.languages = languages; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationCenter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationCenter.java index c98a90460f9..70645e46f5a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationCenter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationCenter.java @@ -27,6 +27,7 @@ import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.server.notification.NotificationDispatcherMetadata; +import org.springframework.beans.factory.annotation.Autowired; public class NotificationCenter { @@ -35,6 +36,7 @@ public class NotificationCenter { private final NotificationDispatcherMetadata[] dispatchersMetadata; private final NotificationChannel[] channels; + @Autowired(required = false) public NotificationCenter(NotificationDispatcherMetadata[] metadata, NotificationChannel[] channels) { this.dispatchersMetadata = metadata; this.channels = channels; @@ -43,6 +45,7 @@ public class NotificationCenter { /** * Default constructor when no channels. */ + @Autowired(required = false) public NotificationCenter(NotificationDispatcherMetadata[] metadata) { this(metadata, new NotificationChannel[0]); LOG.warn("There is no notification channel - no notification will be delivered!"); @@ -51,10 +54,12 @@ public class NotificationCenter { /** * Default constructor when no dispatcher metadata. */ + @Autowired(required = false) public NotificationCenter(NotificationChannel[] channels) { this(new NotificationDispatcherMetadata[0], channels); } + @Autowired(required = false) public NotificationCenter() { this(new NotificationDispatcherMetadata[0], new NotificationChannel[0]); LOG.warn("There is no notification channel - no notification will be delivered!"); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessCheckerImpl.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessCheckerImpl.java index 37ce04d4062..a5f71f548cc 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessCheckerImpl.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessCheckerImpl.java @@ -19,6 +19,7 @@ */ package org.sonar.server.platform.ws; +import javax.annotation.Nullable; import org.sonar.server.health.CeStatusNodeCheck; import org.sonar.server.health.DbConnectionNodeCheck; import org.sonar.server.health.EsStatusNodeCheck; @@ -29,28 +30,18 @@ public class LivenessCheckerImpl implements LivenessChecker { private final DbConnectionNodeCheck dbConnectionNodeCheck; private final CeStatusNodeCheck ceStatusNodeCheck; + @Nullable private final EsStatusNodeCheck esStatusNodeCheck; private final WebServerStatusNodeCheck webServerStatusNodeCheck; public LivenessCheckerImpl(DbConnectionNodeCheck dbConnectionNodeCheck, - WebServerStatusNodeCheck webServerStatusNodeCheck, CeStatusNodeCheck ceStatusNodeCheck, EsStatusNodeCheck esStatusNodeCheck) { + WebServerStatusNodeCheck webServerStatusNodeCheck, CeStatusNodeCheck ceStatusNodeCheck, @Nullable EsStatusNodeCheck esStatusNodeCheck) { this.dbConnectionNodeCheck = dbConnectionNodeCheck; this.webServerStatusNodeCheck = webServerStatusNodeCheck; this.ceStatusNodeCheck = ceStatusNodeCheck; this.esStatusNodeCheck = esStatusNodeCheck; } - /** - * Constructor used by Pico Container on non-standalone mode, so on a DCE App Node, where EsStatusNodeCheck is not available - */ - public LivenessCheckerImpl(DbConnectionNodeCheck dbConnectionNodeCheck, - WebServerStatusNodeCheck webServerStatusNodeCheck, CeStatusNodeCheck ceStatusNodeCheck) { - this.dbConnectionNodeCheck = dbConnectionNodeCheck; - this.webServerStatusNodeCheck = webServerStatusNodeCheck; - this.ceStatusNodeCheck = ceStatusNodeCheck; - this.esStatusNodeCheck = null; - } - public boolean liveness() { if (!Health.Status.GREEN.equals(dbConnectionNodeCheck.check().getStatus())) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java index 9ec2ae9fde1..0b49f5c0908 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java @@ -25,7 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import javax.annotation.CheckForNull; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java index 6659c0e7c80..f39c742db22 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java @@ -55,6 +55,7 @@ import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; +import org.springframework.beans.factory.annotation.Autowired; import static org.sonar.server.exceptions.BadRequestException.checkRequest; @@ -67,6 +68,7 @@ public class QProfileExporters { private final ProfileExporter[] exporters; private final ProfileImporter[] importers; + @Autowired(required = false) public QProfileExporters(DbClient dbClient, RuleFinder ruleFinder, QProfileRules qProfileRules, ProfileExporter[] exporters, ProfileImporter[] importers) { this.dbClient = dbClient; this.ruleFinder = ruleFinder; @@ -76,22 +78,25 @@ public class QProfileExporters { } /** - * Used by Pico if no {@link ProfileImporter} is found + * Used by the ioc container if no {@link ProfileImporter} is found */ + @Autowired(required = false) public QProfileExporters(DbClient dbClient, RuleFinder ruleFinder, QProfileRules qProfileRules, ProfileExporter[] exporters) { this(dbClient, ruleFinder, qProfileRules, exporters, new ProfileImporter[0]); } /** - * Used by Pico if no {@link ProfileExporter} is found + * Used by the ioc container if no {@link ProfileExporter} is found */ + @Autowired(required = false) public QProfileExporters(DbClient dbClient, RuleFinder ruleFinder, QProfileRules qProfileRules, ProfileImporter[] importers) { this(dbClient, ruleFinder, qProfileRules, new ProfileExporter[0], importers); } /** - * Used by Pico if no {@link ProfileImporter} nor {@link ProfileExporter} is found + * Used by the ioc container if no {@link ProfileImporter} nor {@link ProfileExporter} is found */ + @Autowired(required = false) public QProfileExporters(DbClient dbClient, RuleFinder ruleFinder, QProfileRules qProfileRules) { this(dbClient, ruleFinder, qProfileRules, new ProfileExporter[0], new ProfileImporter[0]); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index 8cc4762bd4a..7fbf4110563 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -27,7 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileLoader.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileLoader.java index f6d1215c066..2e5aee0b85b 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileLoader.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileLoader.java @@ -19,7 +19,7 @@ */ package org.sonar.server.qualityprofile.builtin; -import org.picocontainer.Startable; +import org.sonar.api.Startable; /** * Startable added to {@link org.sonar.server.platform.platformlevel.PlatformLevelStartup} responsible for initializing diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImpl.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImpl.java index 0da3b2531ab..6573c0eec58 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImpl.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImpl.java @@ -47,6 +47,7 @@ import org.sonar.db.DbSession; import org.sonar.db.rule.DeprecatedRuleKeyDto; import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.server.rule.ServerRuleFinder; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkState; @@ -61,12 +62,14 @@ public class BuiltInQProfileRepositoryImpl implements BuiltInQProfileRepository private List<BuiltInQProfile> qProfiles; /** - * Requires for pico container when no {@link BuiltInQualityProfilesDefinition} is defined at all + * Used by the ioc container when no {@link BuiltInQualityProfilesDefinition} is defined at all */ + @Autowired(required = false) public BuiltInQProfileRepositoryImpl(DbClient dbClient, ServerRuleFinder ruleFinder, Languages languages) { this(dbClient, ruleFinder, languages, new BuiltInQualityProfilesDefinition[0]); } + @Autowired(required = false) public BuiltInQProfileRepositoryImpl(DbClient dbClient, ServerRuleFinder ruleFinder, Languages languages, BuiltInQualityProfilesDefinition... definitions) { this.dbClient = dbClient; this.ruleFinder = ruleFinder; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java index 16f1be265bb..74ce65995c9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java @@ -45,6 +45,7 @@ import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_CREATE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_NAME; +import org.springframework.beans.factory.annotation.Autowired; public class CreateAction implements QProfileWsAction { @@ -59,6 +60,7 @@ public class CreateAction implements QProfileWsAction { private final UserSession userSession; private final ActiveRuleIndexer activeRuleIndexer; + @Autowired(required = false) public CreateAction(DbClient dbClient, QProfileFactory profileFactory, QProfileExporters exporters, Languages languages, UserSession userSession, ActiveRuleIndexer activeRuleIndexer, ProfileImporter... importers) { this.dbClient = dbClient; @@ -70,6 +72,7 @@ public class CreateAction implements QProfileWsAction { this.importers = importers; } + @Autowired(required = false) public CreateAction(DbClient dbClient, QProfileFactory profileFactory, QProfileExporters exporters, Languages languages, UserSession userSession, ActiveRuleIndexer activeRuleIndexer) { this(dbClient, profileFactory, exporters, languages, userSession, activeRuleIndexer, new ProfileImporter[0]); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ExportersAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ExportersAction.java index 1ae2f73ee75..dfc25177938 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ExportersAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ExportersAction.java @@ -24,18 +24,21 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService.NewController; import org.sonar.api.utils.text.JsonWriter; +import org.springframework.beans.factory.annotation.Autowired; public class ExportersAction implements QProfileWsAction { - private ProfileExporter[] exporters; + private final ProfileExporter[] exporters; + @Autowired(required = false) public ExportersAction(ProfileExporter[] exporters) { this.exporters = exporters; } /** - * Used by Pico if no {@link ProfileExporter} is found + * Used by the container if no {@link ProfileExporter} is found */ + @Autowired(required = false) public ExportersAction() { this(new ProfileExporter[0]); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ImportersAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ImportersAction.java index 8b8a6ae0e1b..64d42824f0f 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ImportersAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/ImportersAction.java @@ -24,15 +24,18 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; +import org.springframework.beans.factory.annotation.Autowired; public class ImportersAction implements QProfileWsAction { private final ProfileImporter[] importers; + @Autowired(required = false) public ImportersAction(ProfileImporter[] importers) { this.importers = importers; } + @Autowired(required = false) public ImportersAction() { this(new ProfileImporter[0]); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java index 24ab5b7876d..9e96c69f217 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java @@ -43,6 +43,7 @@ import static java.util.Objects.requireNonNull; import static org.sonar.api.web.page.Page.Scope.COMPONENT; import static org.sonar.api.web.page.Page.Scope.GLOBAL; import static org.sonar.core.util.stream.MoreCollectors.toList; +import org.springframework.beans.factory.annotation.Autowired; @ServerSide public class PageRepository implements Startable { @@ -53,8 +54,9 @@ public class PageRepository implements Startable { private List<Page> pages; /** - * Used by Pico when there is no {@link PageDefinition}. + * Used by the ioc container when there is no {@link PageDefinition}. */ + @Autowired(required = false) public PageRepository(PluginRepository pluginRepository, CoreExtensionRepository coreExtensionRepository) { this.pluginRepository = pluginRepository; this.coreExtensionRepository = coreExtensionRepository; @@ -64,10 +66,10 @@ public class PageRepository implements Startable { } /** - * Used by Pico when there is only {@link PageDefinition} provided both by Plugin(s). + * Used by the ioc container when there is only {@link PageDefinition} provided both by Plugin(s). */ - public PageRepository(PluginRepository pluginRepository, CoreExtensionRepository coreExtensionRepository, - PageDefinition[] pageDefinitions) { + @Autowired(required = false) + public PageRepository(PluginRepository pluginRepository, CoreExtensionRepository coreExtensionRepository, PageDefinition[] pageDefinitions) { this.pluginRepository = pluginRepository; this.coreExtensionRepository = coreExtensionRepository; this.definitions = copyOf(pageDefinitions); @@ -75,10 +77,10 @@ public class PageRepository implements Startable { } /** - * Used by Pico when there is only {@link PageDefinition} provided both by Core Extension(s). + * Used by the ioc container when there is only {@link PageDefinition} provided both by Core Extension(s). */ - public PageRepository(PluginRepository pluginRepository, CoreExtensionRepository coreExtensionRepository, - CorePageDefinition[] corePageDefinitions) { + @Autowired(required = false) + public PageRepository(PluginRepository pluginRepository, CoreExtensionRepository coreExtensionRepository, CorePageDefinition[] corePageDefinitions) { this.pluginRepository = pluginRepository; this.coreExtensionRepository = coreExtensionRepository; this.definitions = emptyList(); @@ -86,8 +88,9 @@ public class PageRepository implements Startable { } /** - * Used by Pico when there is {@link PageDefinition} provided both by Core Extension(s) and Plugin(s). + * Used by the ioc container when there is {@link PageDefinition} provided both by Core Extension(s) and Plugin(s). */ + @Autowired(required = false) public PageRepository(PluginRepository pluginRepository, CoreExtensionRepository coreExtensionRepository, PageDefinition[] pageDefinitions, CorePageDefinition[] corePageDefinitions) { this.pluginRepository = pluginRepository; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/WebAnalyticsLoaderImpl.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/WebAnalyticsLoaderImpl.java index e7ff70b68ee..d2d5b8e9501 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/WebAnalyticsLoaderImpl.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/WebAnalyticsLoaderImpl.java @@ -32,16 +32,16 @@ public class WebAnalyticsLoaderImpl implements WebAnalyticsLoader { @Nullable private final WebAnalytics analytics; - public WebAnalyticsLoaderImpl(WebAnalytics[] analytics) { - if (analytics.length > 1) { - List<String> classes = Arrays.stream(analytics).map(a -> a.getClass().getName()).collect(Collectors.toList()); - throw MessageException.of("Limited to only one web analytics plugin. Found multiple implementations: " + classes); + public WebAnalyticsLoaderImpl(@Nullable WebAnalytics[] analytics) { + if (analytics == null) { + this.analytics = null; + } else { + if (analytics.length > 1) { + List<String> classes = Arrays.stream(analytics).map(a -> a.getClass().getName()).collect(Collectors.toList()); + throw MessageException.of("Limited to only one web analytics plugin. Found multiple implementations: " + classes); + } + this.analytics = analytics.length == 1 ? analytics[0] : null; } - this.analytics = analytics.length == 1 ? analytics[0] : null; - } - - public WebAnalyticsLoaderImpl() { - this.analytics = null; } @Override diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UsersWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UsersWsModule.java index 01228d957ac..363701c2ddd 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UsersWsModule.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UsersWsModule.java @@ -19,16 +19,9 @@ */ package org.sonar.server.user.ws; -import org.sonar.api.config.Configuration; import org.sonar.core.platform.Module; -import org.sonar.process.ProcessProperties; public class UsersWsModule extends Module { - private final Configuration configuration; - - public UsersWsModule(Configuration configuration) { - this.configuration = configuration; - } @Override protected void configureModule() { @@ -50,9 +43,5 @@ public class UsersWsModule extends Module { SetSettingAction.class, UpdateIdentityProviderAction.class); - if (configuration.getBoolean(ProcessProperties.Property.SONARCLOUD_ENABLED.getKey()).orElse(false)) { - // onboarding tutorial is available only in SonarCloud - add(SkipOnboardingTutorialAction.class); - } } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModuleTest.java index 39f77901458..8f39fff17e1 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.almintegration.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class AlmIntegrationsWSModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new AlmIntegrationsWSModule().configure(container); - assertThat(container.size()).isPositive(); + assertThat(container.getAddedObjects()).isNotEmpty(); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java index ba25d5b43fe..36a6eca7a90 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.almsettings.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class AlmSettingsWsModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new AlmSettingsWsModule().configure(container); - assertThat(container.size()).isPositive(); + assertThat(container.getAddedObjects()).isNotEmpty(); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/authentication/ws/AuthenticationWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/authentication/ws/AuthenticationWsModuleTest.java index fce5bf4927d..fbeac758359 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/authentication/ws/AuthenticationWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/authentication/ws/AuthenticationWsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.authentication.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class AuthenticationWsModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new AuthenticationWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 4); + assertThat(container.getAddedObjects()).hasSize(4); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java index e7b70d60953..c331b750f41 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java @@ -20,20 +20,17 @@ package org.sonar.server.badge.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class ProjectBadgesWsModuleTest { - - private final ComponentContainer container = new ComponentContainer(); private final ProjectBadgesWsModule underTest = new ProjectBadgesWsModule(); @Test public void verify_count_of_added_components() { + ListContainer container = new ListContainer(); underTest.configure(container); - - assertThat(container.size()).isPositive(); + assertThat(container.getAddedObjects()).isNotEmpty(); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/BatchWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/BatchWsModuleTest.java index 7c671b43547..10b9a6d39be 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/BatchWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/BatchWsModuleTest.java @@ -20,16 +20,16 @@ package org.sonar.server.batch; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class BatchWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new BatchWsModule().configure(container); - assertThat(container.size()).isEqualTo(9); + assertThat(container.getAddedObjects()).hasSize(7); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestWsModuleTest.java index 9f1e37bf7ab..5439eaf98ec 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.branch.pr.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class PullRequestWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new PullRequestWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 3); + assertThat(container.getAddedObjects()).hasSize(3); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/ws/BranchWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/ws/BranchWsModuleTest.java index 217146ff626..57bf8cdb7e0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/ws/BranchWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/ws/BranchWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.branch.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class BranchWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new BranchWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 5); + assertThat(container.getAddedObjects()).hasSize(5); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchSupportTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchSupportTest.java index 5e2fd45e13e..67b4cecdf88 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchSupportTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchSupportTest.java @@ -45,7 +45,7 @@ public class BranchSupportTest { private static final Map<String, String> NO_CHARACTERISTICS = Collections.emptyMap(); private final BranchSupportDelegate branchSupportDelegate = mock(BranchSupportDelegate.class); - private final BranchSupport underTestNoBranch = new BranchSupport(); + private final BranchSupport underTestNoBranch = new BranchSupport(null); private final BranchSupport underTestWithBranch = new BranchSupport(branchSupportDelegate); @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java index b73e12e34d6..937a7072b80 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java @@ -89,7 +89,7 @@ public class ReportSubmitterTest { private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), mock(System2.class), permissionTemplateService, new FavoriteUpdater(db.getDbClient()), projectIndexers, new SequenceUuidFactory()); - private final BranchSupport ossEditionBranchSupport = new BranchSupport(); + private final BranchSupport ossEditionBranchSupport = new BranchSupport(null); private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), ossEditionBranchSupport, projectDefaultVisibility); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java index 2d76e6109da..93667bf35df 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java @@ -20,17 +20,16 @@ package org.sonar.server.ce.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class CeWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new CeWsModule().configure(container); - assertThat(container.size()).isEqualTo(20 + COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); + assertThat(container.getAddedObjects()).hasSize(20); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ComponentsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ComponentsWsModuleTest.java index 969d09fa3b7..88d4fa1d289 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ComponentsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ComponentsWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.component.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ComponentsWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new ComponentsWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7); + assertThat(container.getAddedObjects()).hasSize(7); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java index 0797027d10f..494b0ad06a3 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/EmailsWsModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.email.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class EmailsWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new EmailsWsModule().configure(container); - assertThat(container.size()).isEqualTo(2 + 2); + assertThat(container.getAddedObjects()).hasSize(2); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/FavoriteModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/FavoriteModuleTest.java index ecc4faee840..352897cab87 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/FavoriteModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/FavoriteModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.favorite; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class FavoriteModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new FavoriteModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); + assertThat(container.getAddedObjects()).hasSize(2); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/ws/FavoriteWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/ws/FavoriteWsModuleTest.java index 74bb9cd273c..ddd3842ab4a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/ws/FavoriteWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/favorite/ws/FavoriteWsModuleTest.java @@ -20,18 +20,15 @@ package org.sonar.server.favorite.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class FavoriteWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new FavoriteWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 4); + assertThat(container.getAddedObjects()).hasSize(4); } - - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthModuleTest.java index ce38df223b8..8abcb7e5f48 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthModuleTest.java @@ -19,20 +19,17 @@ */ package org.sonar.server.health; -import java.util.Collection; import java.util.Date; -import java.util.List; import java.util.Random; -import java.util.stream.Collectors; import org.junit.Test; -import org.picocontainer.ComponentAdapter; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.platform.Server; import org.sonar.api.utils.System2; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.process.NetworkUtils; import org.sonar.process.cluster.health.SharedHealthStateImpl; import org.sonar.process.cluster.hz.HazelcastMember; +import org.sonar.core.platform.ListContainer; import static java.lang.String.valueOf; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; @@ -41,13 +38,13 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class NodeHealthModuleTest { - private Random random = new Random(); - private MapSettings mapSettings = new MapSettings(); - private NodeHealthModule underTest = new NodeHealthModule(); + private final Random random = new Random(); + private final MapSettings mapSettings = new MapSettings(); + private final NodeHealthModule underTest = new NodeHealthModule(); @Test public void no_broken_dependencies() { - ComponentContainer container = new ComponentContainer(); + SpringComponentContainer container = new SpringComponentContainer(); Server server = mock(Server.class); NetworkUtils networkUtils = mock(NetworkUtils.class); // settings required by NodeHealthProvider @@ -72,16 +69,10 @@ public class NodeHealthModuleTest { @Test public void provides_implementation_of_SharedHealthState() { - ComponentContainer container = new ComponentContainer(); - + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(classesAddedToContainer(container)) + assertThat(container.getAddedObjects()) .contains(SharedHealthStateImpl.class); } - - private List<Class<?>> classesAddedToContainer(ComponentContainer container) { - Collection<ComponentAdapter<?>> componentAdapters = container.getPicoContainer().getComponentAdapters(); - return componentAdapters.stream().map(ComponentAdapter::getComponentImplementation).collect(Collectors.toList()); - } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/HotspotsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/HotspotsWsModuleTest.java index f96a975e844..40d256ffb7f 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/HotspotsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/HotspotsWsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.hotspot.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class HotspotsWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new HotspotsWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 10); + assertThat(container.getAddedObjects()).hasSize(10); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueWsModuleTest.java index c62170db954..764d59008e0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueWsModuleTest.java @@ -20,17 +20,16 @@ package org.sonar.server.issue.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class IssueWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new IssueWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 31); + assertThat(container.getAddedObjects()).hasSize(31); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/live/LiveMeasureModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/live/LiveMeasureModuleTest.java index 0853c3063c4..b8ec7cc9df3 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/live/LiveMeasureModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/live/LiveMeasureModuleTest.java @@ -20,18 +20,15 @@ package org.sonar.server.measure.live; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class LiveMeasureModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new LiveMeasureModule().configure(container); - assertThat(container.size()).isEqualTo(3 + 2); + assertThat(container.getAddedObjects()).hasSize(3); } - - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/MeasuresWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/MeasuresWsModuleTest.java index 246f4c774fe..515980abfa3 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/MeasuresWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/MeasuresWsModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.measure.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class MeasuresWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new MeasuresWsModule().configure(container); - assertThat(container.size()).isEqualTo(5 + 2); + assertThat(container.getAddedObjects()).hasSize(5); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/metric/ws/MetricsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/metric/ws/MetricsWsModuleTest.java index 7d50d15605e..c6866e47aa9 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/metric/ws/MetricsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/metric/ws/MetricsWsModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.metric.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class MetricsWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new MetricsWsModule().configure(container); - assertThat(container.size()).isEqualTo(5); + assertThat(container.getAddedObjects()).hasSize(3); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/monitoring/MonitoringWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/monitoring/MonitoringWsModuleTest.java index cf2e618dae5..8a0c2a692b7 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/monitoring/MonitoringWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/monitoring/MonitoringWsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.monitoring; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class MonitoringWsModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new MonitoringWsModule().configure(container); - assertThat(container.size()).isPositive(); + assertThat(container.getAddedObjects()).hasSize(3); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java index 256ea0c1d02..f3b31a15f14 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.newcodeperiod.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class NewCodePeriodsWsModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new NewCodePeriodsWsModule().configure(container); - assertThat(container.size()).isEqualTo(7); + assertThat(container.getAddedObjects()).hasSize(5); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/notification/ws/NotificationWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/notification/ws/NotificationWsModuleTest.java index f6b07471c0d..77eb7627ca0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/notification/ws/NotificationWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/notification/ws/NotificationWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.notification.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class NotificationWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new NotificationWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7); + assertThat(container.getAddedObjects()).hasSize(7); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java index 7d0f4a7e264..94e48b84432 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.permission.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class PermissionsWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new PermissionsWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 26); + assertThat(container.getAddedObjects()).hasSize(26); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelServiceModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelServiceModuleTest.java index 54f420f3892..d3ae5836616 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelServiceModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelServiceModuleTest.java @@ -19,53 +19,39 @@ */ package org.sonar.server.platform.ws; -import java.util.Collection; import org.junit.Test; -import org.picocontainer.ComponentAdapter; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import org.sonar.server.platform.WebServer; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ChangeLogLevelServiceModuleTest { - private WebServer webServer = mock(WebServer.class); - private ChangeLogLevelServiceModule underTest = new ChangeLogLevelServiceModule(webServer); + private final WebServer webServer = mock(WebServer.class); + private final ChangeLogLevelServiceModule underTest = new ChangeLogLevelServiceModule(webServer); @Test public void provide_returns_ChangeLogLevelClusterService_if_cluster_not_on_SonarCloud() { when(webServer.isStandalone()).thenReturn(false); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters(); - assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 1) - .extracting(ComponentAdapter::getComponentKey) - .contains(ChangeLogLevelClusterService.class) - .doesNotContain(ChangeLogLevelStandaloneService.class); + assertThat(container.getAddedObjects()).containsOnly(ChangeLogLevelClusterService.class); } @Test public void provide_returns_ChangeLogLevelStandaloneService_if_SQ_standalone() { when(webServer.isStandalone()).thenReturn(true); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); verifyInStandaloneSQ(container); } - private void verifyInStandaloneSQ(ComponentContainer container) { - Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters(); - assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 1) - .extracting(ComponentAdapter::getComponentKey) - .contains(ChangeLogLevelStandaloneService.class) - .doesNotContain(ChangeLogLevelClusterService.class); + private void verifyInStandaloneSQ(ListContainer container) { + assertThat(container.getAddedObjects()).containsOnly(ChangeLogLevelStandaloneService.class); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthCheckerModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthCheckerModuleTest.java index 03597f23059..b91322ca303 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthCheckerModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthCheckerModuleTest.java @@ -19,13 +19,10 @@ */ package org.sonar.server.platform.ws; -import java.util.Collection; import java.util.List; import java.util.Random; import java.util.stream.Collectors; import org.junit.Test; -import org.picocontainer.ComponentAdapter; -import org.sonar.core.platform.ComponentContainer; import org.sonar.server.health.AppNodeClusterCheck; import org.sonar.server.health.CeStatusNodeCheck; import org.sonar.server.health.ClusterHealthCheck; @@ -35,6 +32,7 @@ import org.sonar.server.health.EsStatusNodeCheck; import org.sonar.server.health.HealthCheckerImpl; import org.sonar.server.health.NodeHealthCheck; import org.sonar.server.health.WebServerStatusNodeCheck; +import org.sonar.core.platform.ListContainer; import org.sonar.server.platform.WebServer; import static org.assertj.core.api.Assertions.assertThat; @@ -42,18 +40,18 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class HealthCheckerModuleTest { - private WebServer webServer = mock(WebServer.class); - private HealthCheckerModule underTest = new HealthCheckerModule(webServer); + private final WebServer webServer = mock(WebServer.class); + private final HealthCheckerModule underTest = new HealthCheckerModule(webServer); @Test public void verify_HealthChecker() { boolean standalone = new Random().nextBoolean(); when(webServer.isStandalone()).thenReturn(standalone); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(classesAddedToContainer(container)) + assertThat(container.getAddedObjects()) .describedAs("Verifying action and HealthChecker with standalone=%s", standalone) .contains(HealthCheckerImpl.class) .doesNotContain(HealthActionSupport.class) @@ -64,62 +62,56 @@ public class HealthCheckerModuleTest { @Test public void verify_installed_NodeHealthChecks_implementations_when_standalone() { when(webServer.isStandalone()).thenReturn(true); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - List<Class<?>> checks = classesAddedToContainer(container).stream().filter(NodeHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); - assertThat(checks) - .hasSize(4) - .contains(WebServerStatusNodeCheck.class) - .contains(DbConnectionNodeCheck.class) - .contains(EsStatusNodeCheck.class) - .contains(CeStatusNodeCheck.class); + List<Class<?>> checks = container.getAddedObjects().stream() + .filter(o -> o instanceof Class) + .map(o -> (Class<?>) o) + .filter(NodeHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); + assertThat(checks).containsOnly(WebServerStatusNodeCheck.class, DbConnectionNodeCheck.class, EsStatusNodeCheck.class, CeStatusNodeCheck.class); } @Test public void verify_installed_NodeHealthChecks_implementations_when_clustered() { when(webServer.isStandalone()).thenReturn(false); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - List<Class<?>> checks = classesAddedToContainer(container).stream().filter(NodeHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); - assertThat(checks) - .hasSize(3) - .contains(WebServerStatusNodeCheck.class) - .contains(DbConnectionNodeCheck.class) - .contains(CeStatusNodeCheck.class) - .doesNotContain(EsStatusNodeCheck.class); + List<Class<?>> checks = container.getAddedObjects().stream() + .filter(o -> o instanceof Class) + .map(o -> (Class<?>) o) + .filter(NodeHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); + assertThat(checks).containsOnly(WebServerStatusNodeCheck.class, DbConnectionNodeCheck.class, CeStatusNodeCheck.class); } @Test public void verify_installed_ClusterHealthChecks_implementations_in_standalone() { when(webServer.isStandalone()).thenReturn(true); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - List<Class<?>> checks = classesAddedToContainer(container).stream().filter(ClusterHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); + List<Class<?>> checks = container.getAddedObjects().stream() + .filter(o -> o instanceof Class<?>) + .map(o -> (Class<?>) o) + .filter(ClusterHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); assertThat(checks).isEmpty(); } @Test public void verify_installed_ClusterHealthChecks_implementations_in_clustering() { when(webServer.isStandalone()).thenReturn(false); - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - List<Class<?>> checks = classesAddedToContainer(container).stream().filter(ClusterHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); - assertThat(checks) - .hasSize(2) - .contains(EsStatusClusterCheck.class) - .contains(AppNodeClusterCheck.class); - } - - private List<Class<?>> classesAddedToContainer(ComponentContainer container) { - Collection<ComponentAdapter<?>> componentAdapters = container.getPicoContainer().getComponentAdapters(); - return componentAdapters.stream().map(ComponentAdapter::getComponentImplementation).collect(Collectors.toList()); + List<Class<?>> checks = container.getAddedObjects().stream() + .filter(o -> o instanceof Class<?>) + .map(o -> (Class<?>) o) + .filter(ClusterHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); + assertThat(checks).containsOnly(EsStatusClusterCheck.class, AppNodeClusterCheck.class); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java index e0c477b910e..76d605c6403 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java @@ -40,7 +40,7 @@ public class LivenessCheckerImplTest { private final EsStatusNodeCheck esStatusNodeCheck = mock(EsStatusNodeCheck.class); LivenessCheckerImpl underTest = new LivenessCheckerImpl(dbConnectionNodeCheck, webServerStatusNodeCheck, ceStatusNodeCheck, esStatusNodeCheck); - LivenessCheckerImpl underTestDCE = new LivenessCheckerImpl(dbConnectionNodeCheck, webServerStatusNodeCheck, ceStatusNodeCheck); + LivenessCheckerImpl underTestDCE = new LivenessCheckerImpl(dbConnectionNodeCheck, webServerStatusNodeCheck, ceStatusNodeCheck, null); @Test public void fail_when_db_connection_check_fail() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthCheckerModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthCheckerModuleTest.java index 53c887f5322..958def4b490 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthCheckerModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthCheckerModuleTest.java @@ -19,30 +19,28 @@ */ package org.sonar.server.platform.ws; -import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import org.junit.Test; -import org.picocontainer.ComponentAdapter; -import org.sonar.core.platform.ComponentContainer; import org.sonar.server.health.DbConnectionNodeCheck; import org.sonar.server.health.EsStatusNodeCheck; import org.sonar.server.health.HealthCheckerImpl; import org.sonar.server.health.NodeHealthCheck; import org.sonar.server.health.WebServerSafemodeNodeCheck; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class SafeModeHealthCheckerModuleTest { - private SafeModeHealthCheckerModule underTest = new SafeModeHealthCheckerModule(); + private final SafeModeHealthCheckerModule underTest = new SafeModeHealthCheckerModule(); @Test public void verify_HealthChecker() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(classesAddedToContainer(container)) + assertThat(container.getAddedObjects()) .contains(HealthCheckerImpl.class) .doesNotContain(HealthActionSupport.class) .doesNotContain(SafeModeHealthAction.class) @@ -51,21 +49,15 @@ public class SafeModeHealthCheckerModuleTest { @Test public void verify_installed_HealthChecks_implementations() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - List<Class<?>> checks = classesAddedToContainer(container).stream().filter(NodeHealthCheck.class::isAssignableFrom).collect(Collectors.toList()); - assertThat(checks) - .hasSize(3) - .contains(WebServerSafemodeNodeCheck.class) - .contains(DbConnectionNodeCheck.class) - .contains(EsStatusNodeCheck.class); + List<Class<?>> checks = container.getAddedObjects().stream() + .filter(o -> o instanceof Class) + .map(o -> (Class<?>) o) + .filter(NodeHealthCheck.class::isAssignableFrom) + .collect(Collectors.toList()); + assertThat(checks).containsOnly(WebServerSafemodeNodeCheck.class, DbConnectionNodeCheck.class, EsStatusNodeCheck.class); } - - private List<Class<?>> classesAddedToContainer(ComponentContainer container) { - Collection<ComponentAdapter<?>> componentAdapters = container.getPicoContainer().getComponentAdapters(); - return componentAdapters.stream().map(ComponentAdapter::getComponentImplementation).collect(Collectors.toList()); - } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafemodeSystemWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafemodeSystemWsModuleTest.java index f8b5cfaf491..6973f35987c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafemodeSystemWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafemodeSystemWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.platform.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class SafemodeSystemWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new SafemodeSystemWsModule().configure(container); - assertThat(container.size()).isPositive(); + assertThat(container.getAddedObjects()).isNotEmpty(); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SystemWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SystemWsModuleTest.java index 4147f7f500a..f0dfe1e8203 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SystemWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SystemWsModuleTest.java @@ -20,18 +20,15 @@ package org.sonar.server.platform.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class SystemWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new SystemWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 15); + assertThat(container.getAddedObjects()).hasSize(15); } - - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java index 861fd500ef5..ec6d1c4283b 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java @@ -20,18 +20,15 @@ package org.sonar.server.project.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ProjectsWsModuleTest { - @Test public void verify_count_of_added_components_on_SonarQube() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new ProjectsWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 11); + assertThat(container.getAddedObjects()).hasSize(11); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java index 4460d7507de..603b49222b3 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.projectanalysis.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ProjectAnalysisWsModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new ProjectAnalysisWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 8); + assertThat(container.getAddedObjects()).hasSize(8); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectlink/ws/ProjectLinksWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectlink/ws/ProjectLinksWsModuleTest.java index aca2e2717db..2cb8b7cabd0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectlink/ws/ProjectLinksWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectlink/ws/ProjectLinksWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.projectlink.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class ProjectLinksWsModuleTest { - @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new ProjectLinksModule().configure(container); - assertThat(container.size()).isEqualTo(2 + 4); + assertThat(container.getAddedObjects()).hasSize(4); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projecttag/ws/ProjectTagsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projecttag/ws/ProjectTagsWsModuleTest.java index 4b48a6d4034..85a7ad57193 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projecttag/ws/ProjectTagsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projecttag/ws/ProjectTagsWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.projecttag.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ProjectTagsWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new ProjectTagsWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 4); + assertThat(container.getAddedObjects()).hasSize(4); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java index 1b48997c676..dc4bb464671 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.qualitygate; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class QualityGateModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new QualityGateModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 4); + assertThat(container.getAddedObjects()).hasSize(4); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/QualityGateWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/QualityGateWsModuleTest.java index 15528559678..97f2f942071 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/QualityGateWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/QualityGateWsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.qualitygate.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class QualityGateWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new QualityGateWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 23); + assertThat(container.getAddedObjects()).hasSize(23); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java index 7b17c5866e5..d41a7b8b593 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.qualityprofile.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class QProfilesWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new QProfilesWsModule().configure(container); - assertThat(container.size()).isEqualTo(31 + 2); + assertThat(container.getAddedObjects()).hasSize(31); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/root/ws/RootsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/root/ws/RootsWsModuleTest.java index 474b46e8a24..4f545125978 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/root/ws/RootsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/root/ws/RootsWsModuleTest.java @@ -20,21 +20,17 @@ package org.sonar.server.root.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class RootsWsModuleTest { private RootWsModule underTest = new RootWsModule(); @Test public void verify_number_of_components_added_by_module() { - ComponentContainer container = new ComponentContainer(); - + ListContainer container = new ListContainer(); underTest.configure(container); - - assertThat(container.getPicoContainer().getComponentAdapters()) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 4); + assertThat(container.getAddedObjects()).hasSize(4); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SettingsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SettingsWsModuleTest.java index 01aa16a0024..ad8181df85a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SettingsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SettingsWsModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.setting.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class SettingsWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new SettingsWsModule().configure(container); - assertThat(container.size()).isEqualTo(11 + 2); + assertThat(container.getAddedObjects()).hasSize(11); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java index c614cb0de77..47bb519dc52 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java @@ -20,19 +20,19 @@ package org.sonar.server.source.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import org.sonar.server.ws.WsAction; import static org.assertj.core.api.Assertions.assertThat; public class SourceWsModuleTest { - private SourceWsModule underTest = new SourceWsModule(); + private final SourceWsModule underTest = new SourceWsModule(); @Test public void verify_count_of_actions() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.getPicoContainer().getComponentAdapters(WsAction.class)).hasSize(6); + assertThat(container.getAddedObjects().stream().filter(o -> o instanceof Class && WsAction.class.isAssignableFrom((Class<?>) o))) + .hasSize(6); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/WebAnalyticsLoaderImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/WebAnalyticsLoaderImplTest.java index 449e2a1f9e1..6bd16a7f2a9 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/WebAnalyticsLoaderImplTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/WebAnalyticsLoaderImplTest.java @@ -32,7 +32,7 @@ public class WebAnalyticsLoaderImplTest { @Test public void return_empty_if_no_analytics_plugin() { - assertThat(new WebAnalyticsLoaderImpl().getUrlPathToJs()).isEmpty(); + assertThat(new WebAnalyticsLoaderImpl(null).getUrlPathToJs()).isEmpty(); assertThat(new WebAnalyticsLoaderImpl(new WebAnalytics[0]).getUrlPathToJs()).isEmpty(); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java index 36199d99fa3..1a67a8853bb 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java @@ -20,16 +20,15 @@ package org.sonar.server.ui.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class NavigationWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new NavigationWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 5); + assertThat(container.getAddedObjects()).hasSize(5); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java index 5c0ffda66aa..e9d71f23928 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java @@ -25,6 +25,7 @@ import org.junit.Test; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.es.EsTester; @@ -59,7 +60,7 @@ public class ChangePasswordActionTest { private final UserUpdater userUpdater = new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), new UserIndexer(db.getDbClient(), es.client()), new DefaultGroupFinder(db.getDbClient()), - new MapSettings().asConfig(), null, localAuthentication); + new MapSettings().asConfig(), new NoOpAuditPersister(), localAuthentication); private final WsActionTester tester = new WsActionTester(new ChangePasswordAction(db.getDbClient(), userUpdater, userSessionRule, localAuthentication)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java index 6f59b700c6e..c6d7d852d06 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java @@ -29,6 +29,7 @@ import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; @@ -77,7 +78,7 @@ public class CreateActionTest { private GroupDto defaultGroup; private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient(), settings.asConfig()); private final WsActionTester tester = new WsActionTester(new CreateAction(db.getDbClient(), new UserUpdater(mock(NewUserNotifier.class), - db.getDbClient(), userIndexer, new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), null, localAuthentication), userSessionRule)); + db.getDbClient(), userIndexer, new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), new NoOpAuditPersister(), localAuthentication), userSessionRule)); @Before public void setUp() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UsersWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UsersWsModuleTest.java index 95ae8fe2ce3..01687a155da 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UsersWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UsersWsModuleTest.java @@ -20,29 +20,16 @@ package org.sonar.server.user.ws; import org.junit.Test; -import org.sonar.api.config.internal.ConfigurationBridge; -import org.sonar.api.config.internal.MapSettings; -import org.sonar.core.platform.ComponentContainer; -import org.sonar.process.ProcessProperties; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class UsersWsModuleTest { - private MapSettings settings = new MapSettings(); @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); - new UsersWsModule(new ConfigurationBridge(settings)).configure(container); - assertThat(container.size()).isEqualTo(2 + 16); - } - - @Test - public void verify_count_of_added_components_in_sonarcloud() { - settings.setProperty(ProcessProperties.Property.SONARCLOUD_ENABLED.getKey(), true); - - ComponentContainer container = new ComponentContainer(); - new UsersWsModule(new ConfigurationBridge(settings)).configure(container); - assertThat(container.size()).isEqualTo(2 + 17); + ListContainer container = new ListContainer(); + new UsersWsModule().configure(container); + assertThat(container.getAddedObjects()).hasSize(16); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/UserGroupsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/UserGroupsModuleTest.java index a9431a8509e..e291b07a9a3 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/UserGroupsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/UserGroupsModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.usergroups.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class UserGroupsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new UserGroupsModule().configure(container); - assertThat(container.size()).isEqualTo(11); + assertThat(container.getAddedObjects()).hasSize(9); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/UserTokenWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/UserTokenWsModuleTest.java index fbd1a378122..e8c1178d06b 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/UserTokenWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/UserTokenWsModuleTest.java @@ -20,19 +20,16 @@ package org.sonar.server.usertoken.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import org.sonar.server.usertoken.UserTokenModule; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class UserTokenWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new UserTokenModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); + assertThat(container.getAddedObjects()).hasSize(2); } - - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/WebhooksWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/WebhooksWsModuleTest.java index cf3547e4717..09965cb0d23 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/WebhooksWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/WebhooksWsModuleTest.java @@ -20,19 +20,17 @@ package org.sonar.server.webhook.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class WebhooksWsModuleTest { - - private WebhooksWsModule underTest = new WebhooksWsModule(); + private final WebhooksWsModule underTest = new WebhooksWsModule(); @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.size()).isNotZero(); + assertThat(container.getAddedObjects()).isNotEmpty(); } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ws/ws/WebServicesWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ws/ws/WebServicesWsModuleTest.java index a293b75bb26..2fae9041169 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ws/ws/WebServicesWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ws/ws/WebServicesWsModuleTest.java @@ -20,15 +20,15 @@ package org.sonar.server.ws.ws; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; public class WebServicesWsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new WebServicesWsModule().configure(container); - assertThat(container.size()).isEqualTo(3 + 2); + assertThat(container.getAddedObjects()).hasSize(3); } } diff --git a/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java b/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java index 79b0e40b56a..aa0bd5ed8d1 100644 --- a/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java +++ b/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java @@ -27,7 +27,7 @@ import java.util.Locale; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.catalina.connector.ClientAbortException; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.impl.ws.ValidatingRequest; import org.sonar.api.server.ServerSide; import org.sonar.api.server.ws.LocalConnector; diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java index 3a206729672..f691b8e942b 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/PlatformImpl.java @@ -19,17 +19,17 @@ */ package org.sonar.server.platform; -import com.google.common.collect.Lists; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Properties; -import java.util.function.Supplier; import javax.annotation.Nullable; import javax.servlet.ServletContext; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ExtensionContainer; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.app.ProcessCommandWrapper; import org.sonar.server.platform.db.migration.version.DatabaseVersion; import org.sonar.server.platform.platformlevel.PlatformLevel; @@ -49,7 +49,6 @@ public class PlatformImpl implements Platform { private static final PlatformImpl INSTANCE = new PlatformImpl(); - private final Supplier<AutoStarter> autoStarterSupplier; private AutoStarter autoStarter = null; private Properties properties; private ServletContext servletContext; @@ -61,20 +60,9 @@ public class PlatformImpl implements Platform { private PlatformLevel currentLevel; private boolean dbConnected = false; private boolean started = false; - private final List<Object> level4AddedComponents = Lists.newArrayList(); + private final List<Object> level4AddedComponents = new ArrayList<>(); private final Profiler profiler = Profiler.createIfTrace(Loggers.get(PlatformImpl.class)); - private PlatformImpl() { - this.autoStarterSupplier = () -> { - ProcessCommandWrapper processCommandWrapper = getContainer().getComponentByType(ProcessCommandWrapper.class); - return new AsynchronousAutoStarter(processCommandWrapper); - }; - } - - protected PlatformImpl(Supplier<AutoStarter> autoStarterSupplier) { - this.autoStarterSupplier = autoStarterSupplier; - } - public static PlatformImpl getInstance() { return INSTANCE; } @@ -90,13 +78,8 @@ public class PlatformImpl implements Platform { } } - // Platform is injected in Pico, so do not rename this method "start" @Override public void doStart() { - doStart(Startup.ALL); - } - - protected void doStart(Startup startup) { if (started && !isInSafeMode()) { return; } @@ -110,7 +93,7 @@ public class PlatformImpl implements Platform { if (dbRequiresMigration()) { LOGGER.info("Database needs to be migrated. Please refer to https://docs.sonarqube.org/latest/setup/upgrading"); } else { - this.autoStarter = autoStarterSupplier.get(); + this.autoStarter = createAutoStarter(); this.autoStarter.execute(new AutoStarterRunnable(autoStarter) { @Override public void doRun() { @@ -119,7 +102,7 @@ public class PlatformImpl implements Platform { } runIfNotAborted(PlatformImpl.this::startLevel34Containers); - runIfNotAborted(() -> executeStartupTasks(startup)); + runIfNotAborted(PlatformImpl.this::executeStartupTasks); // switch current container last to avoid giving access to a partially initialized container runIfNotAborted(() -> { currentLevel = level4; @@ -133,6 +116,11 @@ public class PlatformImpl implements Platform { } } + private AutoStarter createAutoStarter() { + ProcessCommandWrapper processCommandWrapper = getContainer().getComponentByType(ProcessCommandWrapper.class); + return new AsynchronousAutoStarter(processCommandWrapper); + } + private boolean dbRequiresMigration() { return getDatabaseStatus() != DatabaseVersion.Status.UP_TO_DATE; } @@ -187,18 +175,11 @@ public class PlatformImpl implements Platform { level4 = start(new PlatformLevel4(level3, level4AddedComponents)); } - public void executeStartupTasks() { - executeStartupTasks(Startup.ALL); - } - - private void executeStartupTasks(Startup startup) { - if (startup.ordinal() >= Startup.ALL.ordinal()) { - new PlatformLevelStartup(level4) - .configure() - .start() - .stop() - .destroy(); - } + private void executeStartupTasks() { + new PlatformLevelStartup(level4) + .configure() + .start() + .stop(); } private void startSafeModeContainer() { @@ -230,7 +211,7 @@ public class PlatformImpl implements Platform { * Stops level 2, 3 and 4 containers cleanly if they exists. * Call this method before {@link #startLevel1Container()} to avoid duplicate attempt to stop safemode container * components (since calling stop on a container calls stop on its children too, see - * {@link ComponentContainer#stopComponents()}). + * {@link SpringComponentContainer#stopComponents()}). */ private void stopLevel234Containers() { if (level2 != null) { @@ -245,7 +226,7 @@ public class PlatformImpl implements Platform { * Stops safemode container cleanly if it exists. * Call this method before {@link #stopLevel234Containers()} and {@link #stopLevel1Container()} to avoid duplicate * attempt to stop safemode container components (since calling stop on a container calls stops on its children too, - * see {@link ComponentContainer#stopComponents()}). + * see {@link SpringComponentContainer#stopComponents()}). */ private void stopSafeModeContainer() { if (levelSafeMode != null) { @@ -259,7 +240,6 @@ public class PlatformImpl implements Platform { return version.getStatus(); } - // Do not rename "stop" public void doStop() { try { stopAutoStarter(); @@ -286,18 +266,10 @@ public class PlatformImpl implements Platform { } @Override - public ComponentContainer getContainer() { + public ExtensionContainer getContainer() { return currentLevel.getContainer(); } - public Object getComponent(Object key) { - return getContainer().getComponentByKey(key); - } - - public enum Startup { - NO_STARTUP_TASKS, ALL - } - public interface AutoStarter { /** * Let the autostarted execute the provided code. diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java index 8165c39056b..9d8bc2b26c2 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java @@ -20,12 +20,9 @@ package org.sonar.server.platform.platformlevel; import java.util.Collection; -import java.util.List; import java.util.Optional; -import javax.annotation.Nonnull; import javax.annotation.Nullable; -import org.sonar.core.platform.ComponentContainer; -import org.sonar.core.platform.Module; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.platform.WebServer; import static com.google.common.base.Preconditions.checkNotNull; @@ -34,25 +31,25 @@ import static java.util.Objects.requireNonNull; public abstract class PlatformLevel { private final String name; @Nullable - private final PlatformLevel parent; - private final ComponentContainer container; + protected final PlatformLevel parent; + private final SpringComponentContainer container; private AddIfStartupLeader addIfStartupLeader; private AddIfCluster addIfCluster; private AddIfStandalone addIfStandalone; - public PlatformLevel(String name) { + protected PlatformLevel(String name) { this.name = name; this.parent = null; this.container = createContainer(null); } - public PlatformLevel(String name, @Nonnull PlatformLevel parent) { + protected PlatformLevel(String name, PlatformLevel parent) { this.name = checkNotNull(name); this.parent = checkNotNull(parent); - this.container = createContainer(parent.container); + this.container = createContainer(parent.getContainer()); } - public ComponentContainer getContainer() { + public SpringComponentContainer getContainer() { return container; } @@ -63,21 +60,15 @@ public abstract class PlatformLevel { /** * Intended to be override by subclasses if needed */ - protected ComponentContainer createContainer(@Nullable ComponentContainer parent) { + protected SpringComponentContainer createContainer(@Nullable SpringComponentContainer parent) { if (parent == null) { - return new ComponentContainer(); + return new SpringComponentContainer(); } return parent.createChild(); } public PlatformLevel configure() { configureLevel(); - - List<Module> modules = container.getComponentsByType(Module.class); - for (Module module : modules) { - module.configure(container); - } - return this; } @@ -88,7 +79,6 @@ public abstract class PlatformLevel { */ public PlatformLevel start() { container.startComponents(); - return this; } @@ -97,36 +87,21 @@ public abstract class PlatformLevel { */ public PlatformLevel stop() { container.stopComponents(); - - return this; - } - - /** - * Intended to be override by subclasses if needed - */ - public PlatformLevel destroy() { - if (parent != null) { - parent.container.removeChild(container); - } return this; } protected <T> T get(Class<T> tClass) { - return requireNonNull(container.getComponentByType(tClass)); - } - - protected <T> List<T> getAll(Class<T> tClass) { - return container.getComponentsByType(tClass); + return container.getComponentByType(tClass); } protected <T> Optional<T> getOptional(Class<T> tClass) { - return Optional.ofNullable(container.getComponentByType(tClass)); + return container.getOptionalComponentByType(tClass); } protected void add(Object... objects) { for (Object object : objects) { if (object != null) { - container.addComponent(object, true); + container.add(object); } } } @@ -171,8 +146,10 @@ public abstract class PlatformLevel { } protected WebServer getWebServer() { - return getOptional(WebServer.class) - .orElseThrow(() -> new IllegalStateException("WebServer not available in Pico yet")); + return Optional.ofNullable(parent) + .flatMap(p -> p.getOptional(WebServer.class)) + .or(() -> getOptional(WebServer.class)) + .orElseThrow(() -> new IllegalStateException("WebServer not available in the container")); } protected abstract class AddIf { diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java index 2adc88219e2..d576c3d585f 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java @@ -21,7 +21,6 @@ package org.sonar.server.platform.platformlevel; import java.time.Clock; import java.util.Properties; -import javax.annotation.Nullable; import org.sonar.api.SonarEdition; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarQubeVersion; @@ -29,13 +28,6 @@ import org.sonar.api.internal.MetadataLoader; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.utils.System2; import org.sonar.api.utils.Version; -import org.sonar.db.audit.AuditPersister; -import org.sonar.db.audit.NoOpAuditPersister; -import org.sonar.server.issue.index.IssueIndexSyncProgressChecker; -import org.sonar.server.platform.DockerSupportImpl; -import org.sonar.server.util.GlobalLockManagerImpl; -import org.sonar.server.util.Paths2Impl; -import org.sonar.server.util.TempFolderCleaner; import org.sonar.core.config.CorePropertyDefinitions; import org.sonar.core.extension.CoreExtensionRepositoryImpl; import org.sonar.core.extension.CoreExtensionsLoader; @@ -45,6 +37,8 @@ import org.sonar.db.DaoModule; import org.sonar.db.DbClient; import org.sonar.db.DefaultDatabase; import org.sonar.db.MyBatis; +import org.sonar.db.audit.AuditPersister; +import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.purge.PurgeProfiler; import org.sonar.process.NetworkUtilsImpl; import org.sonar.process.logging.LogbackHelper; @@ -54,7 +48,9 @@ import org.sonar.server.app.WebServerProcessLogging; import org.sonar.server.config.ConfigurationProvider; import org.sonar.server.es.EsModule; import org.sonar.server.issue.index.IssueIndex; +import org.sonar.server.issue.index.IssueIndexSyncProgressChecker; import org.sonar.server.permission.index.WebAuthorizationTypeSupport; +import org.sonar.server.platform.DockerSupportImpl; import org.sonar.server.platform.LogServerVersion; import org.sonar.server.platform.Platform; import org.sonar.server.platform.ServerFileSystemImpl; @@ -67,7 +63,10 @@ import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.setting.ThreadLocalSettings; import org.sonar.server.user.SystemPasscodeImpl; import org.sonar.server.user.ThreadLocalUserSession; +import org.sonar.server.util.GlobalLockManagerImpl; import org.sonar.server.util.OkHttpClientProvider; +import org.sonar.server.util.Paths2Impl; +import org.sonar.server.util.TempFolderCleaner; import static org.sonar.core.extension.CoreExtensionsInstaller.noAdditionalSideFilter; import static org.sonar.core.extension.PlatformLevelPredicates.hasPlatformLevel; @@ -75,7 +74,6 @@ import static org.sonar.core.extension.PlatformLevelPredicates.hasPlatformLevel; public class PlatformLevel1 extends PlatformLevel { private final Platform platform; private final Properties properties; - @Nullable private final Object[] extraRootComponents; public PlatformLevel1(Platform platform, Properties properties, Object... extraRootComponents) { @@ -96,7 +94,7 @@ public class PlatformLevel1 extends PlatformLevel { new SonarQubeVersion(apiVersion), SonarRuntimeImpl.forSonarQube(apiVersion, SonarQubeSide.SERVER, edition), ThreadLocalSettings.class, - new ConfigurationProvider(), + ConfigurationProvider.class, LogServerVersion.class, ProcessCommandWrapperImpl.class, RestartFlagHolderImpl.class, @@ -124,11 +122,11 @@ public class PlatformLevel1 extends PlatformLevel { // DB DBSessionsImpl.class, DbClient.class, - DaoModule.class, + new DaoModule(), // Elasticsearch WebAuthorizationTypeSupport.class, - EsModule.class, + new EsModule(), // rules/qprofiles RuleIndex.class, @@ -160,6 +158,7 @@ public class PlatformLevel1 extends PlatformLevel { @Override public PlatformLevel start() { + PlatformLevel start = super.start(); get(CoreExtensionsLoader.class) .load(); get(WebCoreExtensionsInstaller.class) @@ -168,6 +167,6 @@ public class PlatformLevel1 extends PlatformLevel { add(NoOpAuditPersister.class); } - return super.start(); + return start; } } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java index fb39bd30744..b1dbde2980d 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java @@ -21,9 +21,9 @@ package org.sonar.server.platform.platformlevel; import org.sonar.api.utils.Durations; import org.sonar.core.extension.CoreExtensionsInstaller; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.PluginClassLoader; import org.sonar.core.platform.PluginClassloaderFactory; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.es.MigrationEsClientImpl; import org.sonar.server.l18n.ServerI18n; import org.sonar.server.platform.DatabaseServerCompatibility; @@ -36,8 +36,6 @@ import org.sonar.server.platform.db.migration.DatabaseMigrationExecutorServiceIm import org.sonar.server.platform.db.migration.DatabaseMigrationStateImpl; import org.sonar.server.platform.db.migration.MigrationConfigurationModule; import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker; -import org.sonar.server.platform.db.migration.history.MigrationHistoryTable; -import org.sonar.server.platform.db.migration.history.MigrationHistoryTableImpl; import org.sonar.server.platform.db.migration.version.DatabaseVersion; import org.sonar.server.platform.web.WebPagesCache; import org.sonar.server.plugins.InstalledPluginReferentialFactory; @@ -59,7 +57,7 @@ public class PlatformLevel2 extends PlatformLevel { @Override protected void configureLevel() { add( - MigrationConfigurationModule.class, + new MigrationConfigurationModule(), DatabaseVersion.class, DatabaseServerCompatibility.class, MigrationEsClientImpl.class, @@ -90,7 +88,6 @@ public class PlatformLevel2 extends PlatformLevel { // Migration state must be kept at level2 to survive moving in and then out of safe mode // ExecutorService must be kept at level2 because stopping it when stopping safe mode level causes error making SQ fail add( - MigrationHistoryTableImpl.class, DatabaseMigrationStateImpl.class, DatabaseMigrationExecutorServiceImpl.class); @@ -101,11 +98,8 @@ public class PlatformLevel2 extends PlatformLevel { @Override public PlatformLevel start() { - // ensuring the HistoryTable exists must be the first thing done when this level is started - getOptional(MigrationHistoryTable.class).ifPresent(MigrationHistoryTable::start); - - ComponentContainer container = getContainer(); - CoreExtensionsInstaller coreExtensionsInstaller = get(WebCoreExtensionsInstaller.class); + SpringComponentContainer container = getContainer(); + CoreExtensionsInstaller coreExtensionsInstaller = parent.get(WebCoreExtensionsInstaller.class); coreExtensionsInstaller.install(container, hasPlatformLevel(2), noAdditionalSideFilter()); return super.start(); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel3.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel3.java index 105a2f5597a..e4194cd0235 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel3.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel3.java @@ -21,7 +21,7 @@ package org.sonar.server.platform.platformlevel; import org.sonar.api.utils.UriReader; import org.sonar.core.extension.CoreExtensionsInstaller; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.core.util.DefaultHttpDownloader; import org.sonar.server.async.AsyncExecutionModule; import org.sonar.server.platform.ServerImpl; @@ -48,19 +48,19 @@ public class PlatformLevel3 extends PlatformLevel { DetectPluginChange.class); add( NoopDatabaseMigrationImpl.class, - ServerIdModule.class, + new ServerIdModule(), ServerImpl.class, DatabaseSettingLoader.class, DatabaseSettingsEnabler.class, UriReader.class, DefaultHttpDownloader.class, - AsyncExecutionModule.class); + new AsyncExecutionModule()); } @Override public PlatformLevel start() { - ComponentContainer container = getContainer(); - CoreExtensionsInstaller coreExtensionsInstaller = get(WebCoreExtensionsInstaller.class); + SpringComponentContainer container = getContainer(); + CoreExtensionsInstaller coreExtensionsInstaller = parent.get(WebCoreExtensionsInstaller.class); coreExtensionsInstaller.install(container, hasPlatformLevel(3), noAdditionalSideFilter()); return super.start(); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 9a89c83b8e0..25dcbf24c63 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -35,7 +35,6 @@ import org.sonar.alm.client.gitlab.GitlabGlobalSettingsValidator; import org.sonar.alm.client.gitlab.GitlabHttpClient; import org.sonar.api.profiles.XMLProfileParser; import org.sonar.api.profiles.XMLProfileSerializer; -import org.sonar.api.resources.Languages; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.rules.AnnotationRuleParser; import org.sonar.api.server.rule.RulesDefinitionXmlLoader; @@ -51,8 +50,9 @@ import org.sonar.ce.task.projectanalysis.taskprocessor.ReportTaskProcessor; import org.sonar.ce.task.projectexport.taskprocessor.ProjectExportTaskProcessor; import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.extension.CoreExtensionsInstaller; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.language.LanguagesProvider; import org.sonar.core.platform.PlatformEditionProvider; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.almintegration.ws.AlmIntegrationsWSModule; import org.sonar.server.almintegration.ws.CredentialsEncoderHelper; import org.sonar.server.almintegration.ws.ImportHelper; @@ -275,7 +275,7 @@ public class PlatformLevel4 extends PlatformLevel { MetadataIndexImpl.class, EsDbCompatibilityImpl.class); - addIfCluster(NodeHealthModule.class); + addIfCluster(new NodeHealthModule()); addIfCluster(DistributedRuleActivatorEventsDistributor.class); addIfStandalone(StandaloneRuleActivatorEventsDistributor.class); @@ -293,13 +293,13 @@ public class PlatformLevel4 extends PlatformLevel { ServerWs.class, IndexDefinitions.class, WebAnalyticsLoaderImpl.class, - MonitoringWsModule.class, + new MonitoringWsModule(), // batch - BatchWsModule.class, + new BatchWsModule(), // update center - UpdateCenterModule.class, + new UpdateCenterModule(), // quality profile BuiltInQProfileRepositoryImpl.class, @@ -317,7 +317,7 @@ public class PlatformLevel4 extends PlatformLevel { QProfileBackuperImpl.class, QProfileParser.class, QProfileResetImpl.class, - QProfilesWsModule.class, + new QProfilesWsModule(), // rule RuleIndexDefinition.class, @@ -345,24 +345,24 @@ public class PlatformLevel4 extends PlatformLevel { org.sonar.server.rule.ws.AppAction.class, // languages - Languages.class, + LanguagesProvider.class, LanguageWs.class, LanguageValidation.class, org.sonar.server.language.ws.ListAction.class, // measure - MetricsWsModule.class, - MeasuresWsModule.class, + new MetricsWsModule(), + new MeasuresWsModule(), MetricFinder.class, UnanalyzedLanguageMetrics.class, - QualityGateModule.class, - ProjectsInWarningModule.class, - QualityGateWsModule.class, + new QualityGateModule(), + new ProjectsInWarningModule(), + new QualityGateWsModule(), // web services WebServiceEngine.class, - WebServicesWsModule.class, + new WebServicesWsModule(), SonarLintConnectionFilter.class, WebServiceFilter.class, WebServiceReroutingFilter.class, @@ -372,13 +372,13 @@ public class PlatformLevel4 extends PlatformLevel { org.sonar.server.platform.ws.IndexAction.class, // authentication - AuthenticationModule.class, - AuthenticationWsModule.class, - BitbucketModule.class, - GitHubModule.class, - GitLabModule.class, - LdapModule.class, - SamlModule.class, + new AuthenticationModule(), + new AuthenticationWsModule(), + new BitbucketModule(), + new GitHubModule(), + new GitLabModule(), + new LdapModule(), + new SamlModule(), DefaultAdminCredentialsVerifierImpl.class, DefaultAdminCredentialsVerifierNotificationTemplate.class, DefaultAdminCredentialsVerifierNotificationHandler.class, @@ -391,29 +391,29 @@ public class PlatformLevel4 extends PlatformLevel { UserIndexer.class, UserIndex.class, UserUpdater.class, - UsersWsModule.class, - UserTokenModule.class, - UserTokenWsModule.class, + new UsersWsModule(), + new UserTokenModule(), + new UserTokenWsModule(), // groups - UserGroupsModule.class, + new UserGroupsModule(), DefaultGroupFinder.class, // permissions DefaultTemplatesResolverImpl.class, - PermissionsWsModule.class, + new PermissionsWsModule(), PermissionTemplateService.class, PermissionUpdater.class, UserPermissionChanger.class, GroupPermissionChanger.class, // components - BranchWsModule.class, - PullRequestWsModule.class, - ProjectsWsModule.class, - ProjectsEsModule.class, - ProjectTagsWsModule.class, - ComponentsWsModule.class, + new BranchWsModule(), + new PullRequestWsModule(), + new ProjectsWsModule(), + new ProjectsEsModule(), + new ProjectTagsWsModule(), + new ComponentsWsModule(), ComponentService.class, ComponentUpdater.class, ComponentFinder.class, @@ -423,13 +423,13 @@ public class PlatformLevel4 extends PlatformLevel { ComponentIndexDefinition.class, ComponentIndex.class, ComponentIndexer.class, - LiveMeasureModule.class, + new LiveMeasureModule(), ComponentViewerJsonWriter.class, - DevelopersWsModule.class, + new DevelopersWsModule(), - FavoriteModule.class, - FavoriteWsModule.class, + new FavoriteModule(), + new FavoriteWsModule(), // views ViewIndexDefinition.class, @@ -442,10 +442,10 @@ public class PlatformLevel4 extends PlatformLevel { IssueIndexer.class, IssueIteratorFactory.class, PermissionIndexer.class, - IssueWsModule.class, + new IssueWsModule(), NewIssuesEmailTemplate.class, MyNewIssuesEmailTemplate.class, - IssuesChangesNotificationModule.class, + new IssuesChangesNotificationModule(), NewIssuesNotificationHandler.class, NewIssuesNotificationHandler.newMetadata(), MyNewIssuesNotificationHandler.class, @@ -462,10 +462,10 @@ public class PlatformLevel4 extends PlatformLevel { IssueChangePostProcessorImpl.class, // hotspots - HotspotsWsModule.class, + new HotspotsWsModule(), // source - SourceWsModule.class, + new SourceWsModule(), // Duplications DuplicationsParser.class, @@ -482,31 +482,31 @@ public class PlatformLevel4 extends PlatformLevel { BuiltInQPChangeNotificationTemplate.class, BuiltInQPChangeNotificationHandler.class, - NotificationModule.class, - NotificationWsModule.class, - EmailsWsModule.class, + new NotificationModule(), + new NotificationWsModule(), + new EmailsWsModule(), // Settings ProjectConfigurationLoaderImpl.class, PersistentSettings.class, - SettingsWsModule.class, + new SettingsWsModule(), - TypeValidationModule.class, + new TypeValidationModule(), // New Code Periods - NewCodePeriodsWsModule.class, + new NewCodePeriodsWsModule(), // Project Links - ProjectLinksModule.class, + new ProjectLinksModule(), // Project Analyses - ProjectAnalysisWsModule.class, + new ProjectAnalysisWsModule(), // System ServerLogging.class, - ChangeLogLevelServiceModule.class, - HealthCheckerModule.class, - SystemWsModule.class, + new ChangeLogLevelServiceModule(getWebServer()), + new HealthCheckerModule(getWebServer()), + new SystemWsModule(), // Plugins WS PluginUpdateAggregator.class, @@ -532,7 +532,7 @@ public class PlatformLevel4 extends PlatformLevel { BitbucketCloudRestClient.class, GitlabHttpClient.class, AzureDevOpsHttpClient.class, - AlmIntegrationsWSModule.class, + new AlmIntegrationsWSModule(), BitbucketCloudValidator.class, BitbucketServerSettingsValidator.class, GithubGlobalSettingsValidator.class, @@ -540,16 +540,16 @@ public class PlatformLevel4 extends PlatformLevel { AzureDevOpsValidator.class, // ALM settings - AlmSettingsWsModule.class, + new AlmSettingsWsModule(), // Project export - ProjectExportWsModule.class, + new ProjectExportWsModule(), // Branch BranchFeatureProxyImpl.class, // Project badges - ProjectBadgesWsModule.class, + new ProjectBadgesWsModule(), // Core Extensions CoreExtensionBootstraper.class, @@ -558,12 +558,12 @@ public class PlatformLevel4 extends PlatformLevel { MultipleAlmFeatureProvider.class, // ServerPush endpoints - ServerPushWsModule.class, + new ServerPushWsModule(), // Compute engine (must be after Views and Developer Cockpit) - ReportAnalysisFailureNotificationModule.class, - CeModule.class, - CeWsModule.class, + new ReportAnalysisFailureNotificationModule(), + new CeModule(), + new CeWsModule(), ReportTaskProcessor.class, IssueSyncTaskProcessor.class, AuditPurgeTaskProcessor.class, @@ -575,18 +575,18 @@ public class PlatformLevel4 extends PlatformLevel { InternalPropertiesImpl.class, // UI - NavigationWsModule.class, + new NavigationWsModule(), // root - RootWsModule.class, + new RootWsModule(), // webhooks WebhookQGChangeEventListener.class, - WebhookModule.class, - WebhooksWsModule.class, + new WebhookModule(), + new WebhooksWsModule(), // Http Request ID - HttpRequestIdModule.class, + new HttpRequestIdModule(), RecoveryIndexer.class, ProjectIndexersImpl.class, @@ -620,21 +620,23 @@ public class PlatformLevel4 extends PlatformLevel { ); // system info - add(SystemInfoWriterModule.class); + add(new SystemInfoWriterModule(getWebServer())); addAll(level4AddedComponents); } @Override public PlatformLevel start() { - ComponentContainer container = getContainer(); - CoreExtensionsInstaller coreExtensionsInstaller = get(WebCoreExtensionsInstaller.class); + SpringComponentContainer container = getContainer(); + CoreExtensionsInstaller coreExtensionsInstaller = parent.get(WebCoreExtensionsInstaller.class); coreExtensionsInstaller.install(container, hasPlatformLevel4OrNone(), noAdditionalSideFilter()); - ServerExtensionInstaller extensionInstaller = get(ServerExtensionInstaller.class); + + ServerExtensionInstaller extensionInstaller = parent.get(ServerExtensionInstaller.class); extensionInstaller.installExtensions(container); super.start(); return this; } + } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java index b18851b0898..cd3a77f8359 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java @@ -49,11 +49,11 @@ public class PlatformLevelSafeMode extends PlatformLevel { IndexAction.class, // Server WS - SafeModeHealthCheckerModule.class, - SafemodeSystemWsModule.class, + new SafeModeHealthCheckerModule(), + new SafemodeSystemWsModule(), // Listing WS - WebServicesWsModule.class, + new WebServicesWsModule(), // WS engine SafeModeUserSession.class, @@ -64,7 +64,7 @@ public class PlatformLevelSafeMode extends PlatformLevel { ServerMonitoringMetrics.class); addIfStartupLeader( DatabaseMigrationImpl.class, - MigrationEngineModule.class, + new MigrationEngineModule(), AutoDbMigration.class) .otherwiseAdd(NoopDatabaseMigrationImpl.class); } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java index 7aec25a4354..eb4f64bd212 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java @@ -100,9 +100,9 @@ public class PlatformLevelStartup extends PlatformLevel { } private boolean anyPluginChanged() { - return getOptional(DetectPluginChange.class) + return parent.getOptional(DetectPluginChange.class) .map(DetectPluginChange::anyPluginChanged) - .orElseThrow(() -> new IllegalStateException("DetectPluginChange not available in Pico yet")); + .orElseThrow(() -> new IllegalStateException("DetectPluginChange not available in the container yet")); } public final class AddIfStartupLeaderAndPluginsChanged extends AddIf { @@ -113,7 +113,7 @@ public class PlatformLevelStartup extends PlatformLevel { @Override public PlatformLevel start() { - DoPrivileged.execute(new DoPrivileged.Task(get(ThreadLocalUserSession.class)) { + DoPrivileged.execute(new DoPrivileged.Task(parent.get(ThreadLocalUserSession.class)) { @Override protected void doPrivileged() { PlatformLevelStartup.super.start(); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java index a278da715a8..715727a4a05 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java @@ -57,7 +57,7 @@ public class MasterServletFilter implements Filter { @Override public void init(FilterConfig config) { - // Filters are already available in picocontainer unless a database migration is required. See + // Filters are already available in the container unless a database migration is required. See // org.sonar.server.startup.RegisterServletFilters. init(config, PlatformImpl.getInstance().getContainer().getComponentsByType(ServletFilter.class)); } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RegisterServletFilters.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RegisterServletFilters.java index 1cd479220d8..9a44903a94e 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RegisterServletFilters.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RegisterServletFilters.java @@ -20,8 +20,9 @@ package org.sonar.server.platform.web; import java.util.Arrays; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.web.ServletFilter; +import org.springframework.beans.factory.annotation.Autowired; /** * @since 3.5 @@ -29,10 +30,12 @@ import org.sonar.api.web.ServletFilter; public class RegisterServletFilters implements Startable { private final ServletFilter[] filters; + @Autowired(required = false) public RegisterServletFilters(ServletFilter[] filters) { this.filters = filters; } + @Autowired(required = false) public RegisterServletFilters() { this(new ServletFilter[0]); } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RequestIdFilter.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RequestIdFilter.java index 0db1fcf534a..7728736c199 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RequestIdFilter.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/RequestIdFilter.java @@ -55,12 +55,12 @@ public class RequestIdFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - RequestIdGenerator requestIdGenerator = platform.getContainer().getComponentByType(RequestIdGenerator.class); + var requestIdGenerator = platform.getContainer().getOptionalComponentByType(RequestIdGenerator.class); - if (requestIdGenerator == null) { + if (requestIdGenerator.isEmpty()) { chain.doFilter(request, response); } else { - String requestId = requestIdGenerator.generate(); + String requestId = requestIdGenerator.get().generate(); try (RequestIdMDCStorage mdcStorage = new RequestIdMDCStorage(requestId)) { request.setAttribute("ID", requestId); chain.doFilter(request, response); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/UserSessionFilter.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/UserSessionFilter.java index 63ae20874cd..074443d0e2a 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/UserSessionFilter.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/UserSessionFilter.java @@ -58,7 +58,7 @@ public class UserSessionFilter implements Filter { DBSessions dbSessions = platform.getContainer().getComponentByType(DBSessions.class); ThreadLocalSettings settings = platform.getContainer().getComponentByType(ThreadLocalSettings.class); - UserSessionInitializer userSessionInitializer = platform.getContainer().getComponentByType(UserSessionInitializer.class); + UserSessionInitializer userSessionInitializer = platform.getContainer().getOptionalComponentByType(UserSessionInitializer.class).orElse(null); LOG.trace("{} serves {}", Thread.currentThread(), request.getRequestURI()); dbSessions.enableCaching(); diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel1Test.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel1Test.java index 4ad93fbf274..1f9041f33e4 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel1Test.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel1Test.java @@ -21,10 +21,7 @@ package org.sonar.server.platform.platformlevel; import java.util.Properties; import org.junit.Test; -import org.sonar.api.config.PropertyDefinition; -import org.sonar.api.utils.System2; import org.sonar.server.platform.Platform; -import org.sonar.server.platform.WebServer; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -32,14 +29,12 @@ import static org.mockito.Mockito.mock; public class PlatformLevel1Test { - private PlatformLevel1 underTest = new PlatformLevel1(mock(Platform.class), new Properties()); + private final PlatformLevel1 underTest = new PlatformLevel1(mock(Platform.class), new Properties()); @Test public void no_missing_dependencies_between_components() { underTest.configureLevel(); - assertThat(underTest.getAll(PropertyDefinition.class)).isNotEmpty(); - assertThat(underTest.getOptional(WebServer.class)).isPresent(); - assertThat(underTest.getOptional(System2.class)).isPresent(); + assertThat(underTest.getContainer().context().getBeanDefinitionNames()).isNotEmpty(); } } diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java index efc9935052e..145a1f9e6fe 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java @@ -19,19 +19,23 @@ */ package org.sonar.server.platform.platformlevel; +import java.util.Optional; import java.util.Properties; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.utils.System2; -import org.sonar.core.platform.PluginRepository; -import org.sonar.server.platform.Platform; +import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.platform.WebServer; import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker; +import org.sonar.server.plugins.ServerPluginRepository; -import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.sonar.process.ProcessProperties.Property.PATH_DATA; import static org.sonar.process.ProcessProperties.Property.PATH_HOME; import static org.sonar.process.ProcessProperties.Property.PATH_TEMP; @@ -53,38 +57,45 @@ public class PlatformLevel2Test { @Test public void add_all_components_by_default() { - PlatformLevel1 level1 = new PlatformLevel1(mock(Platform.class), props); - level1.configure(); - - PlatformLevel2 underTest = new PlatformLevel2(level1); + var parentContainer = mock(SpringComponentContainer.class); + var container = mock(SpringComponentContainer.class); + var platform = mock(PlatformLevel.class); + var webserver = mock(WebServer.class); + + when(parentContainer.createChild()).thenReturn(container); + when(platform.getContainer()).thenReturn(parentContainer); + when(parentContainer.getOptionalComponentByType(any())).thenReturn(Optional.empty()); + when(container.getOptionalComponentByType(WebServer.class)).thenReturn(Optional.of(webserver)); + when(webserver.isStartupLeader()).thenReturn(true); + + PlatformLevel2 underTest = new PlatformLevel2(platform); underTest.configure(); - // some level1 components - assertThat(underTest.getOptional(WebServer.class)).isPresent(); - assertThat(underTest.getOptional(System2.class)).isPresent(); - - // level2 component that does not depend on cluster state - assertThat(underTest.getOptional(PluginRepository.class)).isPresent(); - - // level2 component that is injected only on "startup leaders" - assertThat(underTest.getOptional(DatabaseCharsetChecker.class)).isPresent(); + verify(container).add(ServerPluginRepository.class); + verify(container).add(DatabaseCharsetChecker.class); + verify(container, times(23)).add(any()); } @Test public void do_not_add_all_components_when_startup_follower() { - props.setProperty("sonar.cluster.enabled", "true"); - PlatformLevel1 level1 = new PlatformLevel1(mock(Platform.class), props); - level1.configure(); - - PlatformLevel2 underTest = new PlatformLevel2(level1); + var parentContainer = mock(SpringComponentContainer.class); + var container = mock(SpringComponentContainer.class); + var platform = mock(PlatformLevel.class); + var webserver = mock(WebServer.class); + + when(parentContainer.createChild()).thenReturn(container); + when(platform.getContainer()).thenReturn(parentContainer); + when(parentContainer.getOptionalComponentByType(any())).thenReturn(Optional.empty()); + when(container.getOptionalComponentByType(WebServer.class)).thenReturn(Optional.of(webserver)); + when(webserver.isStartupLeader()).thenReturn(false); + + PlatformLevel2 underTest = new PlatformLevel2(platform); underTest.configure(); - assertThat(underTest.get(WebServer.class).isStartupLeader()).isFalse(); + verify(container).add(ServerPluginRepository.class); + verify(container, never()).add(DatabaseCharsetChecker.class); + verify(container, times(21)).add(any()); + } - // level2 component that does not depend on cluster state - assertThat(underTest.getOptional(PluginRepository.class)).isPresent(); - // level2 component that is injected only on "startup leaders" - assertThat(underTest.getOptional(DatabaseCharsetChecker.class)).isNotPresent(); - } } diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java index 321bb080b21..3908150d902 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java @@ -21,8 +21,11 @@ package org.sonar.server.platform.platformlevel; import java.util.Random; import java.util.stream.IntStream; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import static org.mockito.Mockito.mock; import org.sonar.server.platform.WebServer; import static org.assertj.core.api.Assertions.assertThat; @@ -30,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class PlatformLevelTest { - private PlatformLevel underTest = new PlatformLevel("name") { + private final PlatformLevel underTest = new PlatformLevel("name") { @Override protected void configureLevel() { @@ -38,16 +41,27 @@ public class PlatformLevelTest { } }; + @Before + public void setUp() { + underTest.start(); + } + + @After + public void tearDown() { + // clean up for next test + underTest.stop(); + } + @Test public void addIfStartupLeader_throws_ISE_if_container_does_not_have_WebServer_object() { - assertThatThrownBy(() -> underTest.addIfStartupLeader()) + assertThatThrownBy(underTest::addIfStartupLeader) .isInstanceOf(IllegalStateException.class) - .hasMessageContaining("WebServer not available in Pico yet"); + .hasMessageContaining("WebServer not available in the container"); } @Test public void addIfStartupLeader_always_returns_the_same_instance() { - underTest.add(Mockito.mock(WebServer.class)); + underTest.add(mock(WebServer.class)); PlatformLevel.AddIfStartupLeader addIfStartupLeader = underTest.addIfStartupLeader(); IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfStartupLeader()).isSameAs(addIfStartupLeader)); @@ -55,14 +69,14 @@ public class PlatformLevelTest { @Test public void addIfCluster_throws_ISE_if_container_does_not_have_WebServer_object() { - assertThatThrownBy(() -> underTest.addIfCluster()) + assertThatThrownBy(underTest::addIfCluster) .isInstanceOf(IllegalStateException.class) - .hasMessageContaining("WebServer not available in Pico yet"); + .hasMessageContaining("WebServer not available in the container"); } @Test public void addIfCluster_always_returns_the_same_instance() { - underTest.add(Mockito.mock(WebServer.class)); + underTest.add(mock(WebServer.class)); PlatformLevel.AddIfCluster addIfCluster = underTest.addIfCluster(); IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfCluster()).isSameAs(addIfCluster)); @@ -70,14 +84,14 @@ public class PlatformLevelTest { @Test public void addIfStandalone_throws_ISE_if_container_does_not_have_WebServer_object() { - assertThatThrownBy(() -> underTest.addIfCluster()) + assertThatThrownBy(underTest::addIfCluster) .isInstanceOf(IllegalStateException.class) - .hasMessageContaining("WebServer not available in Pico yet"); + .hasMessageContaining("WebServer not available in the container"); } @Test public void addIfStandalone_always_returns_the_same_instance() { - underTest.add(Mockito.mock(WebServer.class)); + underTest.add(mock(WebServer.class)); PlatformLevel.AddIfCluster addIfCluster = underTest.addIfCluster(); IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfCluster()).isSameAs(addIfCluster)); diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RequestIdFilterTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RequestIdFilterTest.java index 0996d7e231c..e49f84e616c 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RequestIdFilterTest.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RequestIdFilterTest.java @@ -20,6 +20,7 @@ package org.sonar.server.platform.web; import java.io.IOException; +import java.util.Optional; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; @@ -27,7 +28,7 @@ import javax.servlet.ServletResponse; import org.junit.Before; import org.junit.Test; import org.slf4j.MDC; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ExtensionContainer; import org.sonar.server.platform.Platform; import org.sonar.server.platform.web.requestid.RequestIdGenerator; @@ -41,17 +42,17 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class RequestIdFilterTest { - private Platform platform = mock(Platform.class); - private RequestIdGenerator requestIdGenerator = mock(RequestIdGenerator.class); - private ServletRequest servletRequest = mock(ServletRequest.class); - private ServletResponse servletResponse = mock(ServletResponse.class); - private FilterChain filterChain = mock(FilterChain.class); - private RequestIdFilter underTest = new RequestIdFilter(platform); + private final Platform platform = mock(Platform.class); + private final RequestIdGenerator requestIdGenerator = mock(RequestIdGenerator.class); + private final ServletRequest servletRequest = mock(ServletRequest.class); + private final ServletResponse servletResponse = mock(ServletResponse.class); + private final FilterChain filterChain = mock(FilterChain.class); + private final RequestIdFilter underTest = new RequestIdFilter(platform); @Before public void setUp() { - ComponentContainer container = new ComponentContainer(); - container.add(requestIdGenerator); + ExtensionContainer container = mock(ExtensionContainer.class); + when(container.getOptionalComponentByType(RequestIdGenerator.class)).thenReturn(Optional.of(requestIdGenerator)); when(platform.getContainer()).thenReturn(container); } @@ -102,8 +103,9 @@ public class RequestIdFilterTest { @Test public void filter_does_not_fail_when_there_is_no_RequestIdGenerator_in_container() throws IOException, ServletException { - Platform platform = mock(Platform.class); - when(platform.getContainer()).thenReturn(new ComponentContainer()); + ExtensionContainer container = mock(ExtensionContainer.class); + when(container.getOptionalComponentByType(RequestIdGenerator.class)).thenReturn(Optional.empty()); + when(platform.getContainer()).thenReturn(container); RequestIdFilter underTest = new RequestIdFilter(platform); underTest.doFilter(servletRequest, servletResponse, filterChain); @@ -111,8 +113,9 @@ public class RequestIdFilterTest { @Test public void filter_does_not_add_requestId_to_request_passed_on_to_chain_when_there_is_no_RequestIdGenerator_in_container() throws IOException, ServletException { - Platform platform = mock(Platform.class); - when(platform.getContainer()).thenReturn(new ComponentContainer()); + ExtensionContainer container = mock(ExtensionContainer.class); + when(container.getOptionalComponentByType(RequestIdGenerator.class)).thenReturn(Optional.empty()); + when(platform.getContainer()).thenReturn(container); RequestIdFilter underTest = new RequestIdFilter(platform); underTest.doFilter(servletRequest, servletResponse, filterChain); diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/UserSessionFilterTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/UserSessionFilterTest.java index 33741bb3c5c..7dba4429474 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/UserSessionFilterTest.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/UserSessionFilterTest.java @@ -20,6 +20,7 @@ package org.sonar.server.platform.web; import java.io.IOException; +import java.util.Optional; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; @@ -28,7 +29,7 @@ import javax.servlet.http.HttpServletResponse; import org.junit.Before; import org.junit.Test; import org.mockito.InOrder; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ExtensionContainer; import org.sonar.db.DBSessions; import org.sonar.server.authentication.UserSessionInitializer; import org.sonar.server.platform.Platform; @@ -46,19 +47,21 @@ import static org.mockito.Mockito.when; public class UserSessionFilterTest { - private UserSessionInitializer userSessionInitializer = mock(UserSessionInitializer.class); - private ComponentContainer container = new ComponentContainer(); - private Platform platform = mock(Platform.class); - private HttpServletRequest request = mock(HttpServletRequest.class); - private HttpServletResponse response = mock(HttpServletResponse.class); - private FilterChain chain = mock(FilterChain.class); - private DBSessions dbSessions = mock(DBSessions.class); - private ThreadLocalSettings settings = mock(ThreadLocalSettings.class); - private UserSessionFilter underTest = new UserSessionFilter(platform); + private final UserSessionInitializer userSessionInitializer = mock(UserSessionInitializer.class); + private final ExtensionContainer container = mock(ExtensionContainer.class); + private final Platform platform = mock(Platform.class); + private final HttpServletRequest request = mock(HttpServletRequest.class); + private final HttpServletResponse response = mock(HttpServletResponse.class); + private final FilterChain chain = mock(FilterChain.class); + private final DBSessions dbSessions = mock(DBSessions.class); + private final ThreadLocalSettings settings = mock(ThreadLocalSettings.class); + private final UserSessionFilter underTest = new UserSessionFilter(platform); @Before public void setUp() { - container.add(dbSessions, settings); + when(container.getComponentByType(DBSessions.class)).thenReturn(dbSessions); + when(container.getComponentByType(ThreadLocalSettings.class)).thenReturn(settings); + when(container.getOptionalComponentByType(UserSessionInitializer.class)).thenReturn(Optional.empty()); when(platform.getContainer()).thenReturn(container); } @@ -158,12 +161,12 @@ public class UserSessionFilterTest { } private void mockUserSessionInitializer(boolean value) { - container.add(userSessionInitializer); + when(container.getOptionalComponentByType(UserSessionInitializer.class)).thenReturn(Optional.of(userSessionInitializer)); when(userSessionInitializer.initUserSession(request, response)).thenReturn(value); } private RuntimeException mockUserSessionInitializerRemoveUserSessionFailing() { - container.add(userSessionInitializer); + when(container.getOptionalComponentByType(UserSessionInitializer.class)).thenReturn(Optional.of(userSessionInitializer)); RuntimeException thrown = new RuntimeException("Faking UserSessionInitializer.removeUserSession failing"); doThrow(thrown) .when(userSessionInitializer) |