3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.engine;
22 import java.sql.SQLException;
23 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.Startable;
27 import org.sonar.core.platform.SpringComponentContainer;
28 import org.sonar.server.platform.db.migration.step.InternalMigrationStepRegistry;
29 import org.sonar.server.platform.db.migration.step.MigrationStatusListener;
30 import org.sonar.server.platform.db.migration.step.MigrationStep;
31 import org.sonar.server.platform.db.migration.step.MigrationStepRegistryImpl;
32 import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor;
33 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class MigrationContainerImplTest {
38 private final SpringComponentContainer parent = new SpringComponentContainer();
39 private MigrationContainerImpl underTest;
43 InternalMigrationStepRegistry registry = new MigrationStepRegistryImpl();
44 registry.add(1, "test", NoOpMigrationStep.class);
46 parent.add(registry.build());
47 parent.startComponents();
48 underTest = new MigrationContainerImpl(parent, NoOpExecutor.class);
49 underTest.add(StartCallCounter.class);
53 public void adds_migration_steps_to_migration_container() {
54 assertThat(underTest.getComponentByType(MigrationStep.class)).isInstanceOf(NoOpMigrationStep.class);
58 public void context_of_migration_container_has_specified_context_as_parent() {
59 assertThat(underTest.context().getParent()).isEqualTo(parent.context());
63 public void context_of_migration_container_is_started_in_constructor() {
64 assertThat(underTest.context().isActive()).isTrue();
68 public void add_duplicate_steps_has_no_effect() {
69 InternalMigrationStepRegistry registry = new MigrationStepRegistryImpl();
70 registry.add(1, "test", NoOpMigrationStep.class);
71 registry.add(2, "test2", NoOpMigrationStep.class);
73 SpringComponentContainer parent = new SpringComponentContainer();
74 parent.add(registry.build());
75 parent.startComponents();
76 MigrationContainerImpl underTest = new MigrationContainerImpl(parent, NoOpExecutor.class);
77 assertThat(underTest.getComponentsByType(MigrationStep.class)).hasSize(1);
81 public void migration_container_lazily_instance_components() {
82 assertThat(StartCallCounter.startCalls).isZero();
84 StartCallCounter startCallCounter = underTest.getComponentByType(StartCallCounter.class);
86 assertThat(startCallCounter).isNotNull();
87 assertThat(StartCallCounter.startCalls).isOne();
91 public void cleanup_does_not_fail_even_if_stop_of_component_fails() {
92 parent.add(StopFailing.class);
93 MigrationContainerImpl underTest = new MigrationContainerImpl(parent, NoOpExecutor.class);
98 private static class NoOpExecutor implements MigrationStepsExecutor {
100 public void execute(List<RegisteredMigrationStep> steps, MigrationStatusListener listener) {
105 private static class NoOpMigrationStep implements MigrationStep {
107 public void execute() throws SQLException {
112 public static final class StartCallCounter implements Startable {
113 private static int startCalls = 0;
116 public void start() {
126 public static final class StopFailing implements Startable {
128 public void start() {
134 throw new RuntimeException("Faking stop call failing");