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.MigrationStep;
30 import org.sonar.server.platform.db.migration.step.MigrationStepRegistryImpl;
31 import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor;
32 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
34 import static org.assertj.core.api.Assertions.assertThat;
36 public class MigrationContainerImplTest {
37 private final SpringComponentContainer parent = new SpringComponentContainer();
38 private MigrationContainerImpl underTest;
42 InternalMigrationStepRegistry registry = new MigrationStepRegistryImpl();
43 registry.add(1, "test", NoOpMigrationStep.class);
45 parent.add(registry.build());
46 parent.startComponents();
47 underTest = new MigrationContainerImpl(parent, NoOpExecutor.class);
48 underTest.add(StartCallCounter.class);
52 public void adds_migration_steps_to_migration_container() {
53 assertThat(underTest.getComponentByType(MigrationStep.class)).isInstanceOf(NoOpMigrationStep.class);
57 public void context_of_migration_container_has_specified_context_as_parent() {
58 assertThat(underTest.context().getParent()).isEqualTo(parent.context());
62 public void context_of_migration_container_is_started_in_constructor() {
63 assertThat(underTest.context().isActive()).isTrue();
67 public void add_duplicate_steps_has_no_effect() {
68 InternalMigrationStepRegistry registry = new MigrationStepRegistryImpl();
69 registry.add(1, "test", NoOpMigrationStep.class);
70 registry.add(2, "test2", NoOpMigrationStep.class);
72 SpringComponentContainer parent = new SpringComponentContainer();
73 parent.add(registry.build());
74 parent.startComponents();
75 MigrationContainerImpl underTest = new MigrationContainerImpl(parent, NoOpExecutor.class);
76 assertThat(underTest.getComponentsByType(MigrationStep.class)).hasSize(1);
80 public void migration_container_lazily_instance_components() {
81 assertThat(StartCallCounter.startCalls).isZero();
83 StartCallCounter startCallCounter = underTest.getComponentByType(StartCallCounter.class);
85 assertThat(startCallCounter).isNotNull();
86 assertThat(StartCallCounter.startCalls).isOne();
90 public void cleanup_does_not_fail_even_if_stop_of_component_fails() {
91 parent.add(StopFailing.class);
92 MigrationContainerImpl underTest = new MigrationContainerImpl(parent, NoOpExecutor.class);
97 private static class NoOpExecutor implements MigrationStepsExecutor {
99 public void execute(List<RegisteredMigrationStep> steps) {
104 private static class NoOpMigrationStep implements MigrationStep {
106 public void execute() throws SQLException {
111 public static final class StartCallCounter implements Startable {
112 private static int startCalls = 0;
115 public void start() {
125 public static final class StopFailing implements Startable {
127 public void start() {
133 throw new RuntimeException("Faking stop call failing");