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 org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState;
25 import org.sonar.server.platform.db.migration.history.MigrationHistory;
26 import org.sonar.server.platform.db.migration.step.MigrationStep;
27 import org.sonar.server.platform.db.migration.step.MigrationSteps;
28 import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl;
29 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
30 import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider;
31 import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider;
32 import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider;
34 import static java.util.Arrays.asList;
35 import static java.util.Collections.emptyList;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
40 public class MigrationContainerPopulatorImplTest {
41 private final SimpleMigrationContainer migrationContainer = new SimpleMigrationContainer();
42 private final MigrationSteps migrationSteps = mock(MigrationSteps.class);
43 private final MigrationContainerPopulatorImpl underTest = new MigrationContainerPopulatorImpl();
47 migrationContainer.add(migrationSteps);
51 public void populateContainer_adds_MigrationStepsExecutorImpl() {
52 when(migrationSteps.readAll()).thenReturn(emptyList());
54 // add MigrationStepsExecutorImpl's dependencies
55 migrationContainer.add(mock(MigrationHistory.class));
56 migrationContainer.add(mock(MutableDatabaseMigrationState.class));
57 migrationContainer.add(mock(TelemetryDbMigrationStepsProvider.class));
58 migrationContainer.add(mock(TelemetryDbMigrationTotalTimeProvider.class));
59 migrationContainer.add(mock(TelemetryDbMigrationSuccessProvider.class));
61 migrationContainer.startComponents();
62 underTest.populateContainer(migrationContainer);
64 assertThat(migrationContainer.getComponentByType(MigrationStepsExecutorImpl.class)).isNotNull();
68 public void populateContainer_adds_classes_of_all_steps_defined_in_MigrationSteps() {
69 when(migrationSteps.readAll()).thenReturn(asList(
70 new RegisteredMigrationStep(1, "foo", MigrationStep1.class),
71 new RegisteredMigrationStep(2, "bar", MigrationStep2.class),
72 new RegisteredMigrationStep(3, "dor", MigrationStep3.class)));
74 migrationContainer.startComponents();
75 underTest.populateContainer(migrationContainer);
77 assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull();
78 assertThat(migrationContainer.getComponentsByType(MigrationStep2.class)).isNotNull();
79 assertThat(migrationContainer.getComponentsByType(MigrationStep3.class)).isNotNull();
83 public void populateCotnainer_does_not_fail_if_same_class_is_used_for_more_than_one_migration() {
84 when(migrationSteps.readAll()).thenReturn(asList(
85 new RegisteredMigrationStep(1, "foo", MigrationStep1.class),
86 new RegisteredMigrationStep(2, "bar", MigrationStep2.class),
87 new RegisteredMigrationStep(3, "bar2", MigrationStep2.class),
88 new RegisteredMigrationStep(4, "foo2", MigrationStep1.class),
89 new RegisteredMigrationStep(5, "dor", MigrationStep3.class)));
91 migrationContainer.startComponents();
92 underTest.populateContainer(migrationContainer);
94 assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull();
95 assertThat(migrationContainer.getComponentsByType(MigrationStep2.class)).isNotNull();
96 assertThat(migrationContainer.getComponentsByType(MigrationStep3.class)).isNotNull();
99 private static abstract class NoopMigrationStep implements MigrationStep {
101 public void execute() {
102 throw new UnsupportedOperationException("execute not implemented");
106 public static final class MigrationStep1 extends NoopMigrationStep {
110 public static final class MigrationStep2 extends NoopMigrationStep {
114 public static final class MigrationStep3 extends NoopMigrationStep {
118 public static final class Clazz1 {
122 public static final class Clazz2 {
126 public static final class Clazz3 {