]> source.dussan.org Git - sonarqube.git/blob
13e99bb6bbc22b3f9850b46b990dd8e32ac7354b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.db.migration.engine;
21
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;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36
37 public class MigrationContainerImplTest {
38   private final SpringComponentContainer parent = new SpringComponentContainer();
39   private MigrationContainerImpl underTest;
40
41   @Before
42   public void setUp() {
43     InternalMigrationStepRegistry registry = new MigrationStepRegistryImpl();
44     registry.add(1, "test", NoOpMigrationStep.class);
45
46     parent.add(registry.build());
47     parent.startComponents();
48     underTest = new MigrationContainerImpl(parent, NoOpExecutor.class);
49     underTest.add(StartCallCounter.class);
50   }
51
52   @Test
53   public void adds_migration_steps_to_migration_container() {
54     assertThat(underTest.getComponentByType(MigrationStep.class)).isInstanceOf(NoOpMigrationStep.class);
55   }
56
57   @Test
58   public void context_of_migration_container_has_specified_context_as_parent() {
59     assertThat(underTest.context().getParent()).isEqualTo(parent.context());
60   }
61
62   @Test
63   public void context_of_migration_container_is_started_in_constructor() {
64     assertThat(underTest.context().isActive()).isTrue();
65   }
66
67   @Test
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);
72
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);
78   }
79
80   @Test
81   public void migration_container_lazily_instance_components() {
82     assertThat(StartCallCounter.startCalls).isZero();
83
84     StartCallCounter startCallCounter = underTest.getComponentByType(StartCallCounter.class);
85
86     assertThat(startCallCounter).isNotNull();
87     assertThat(StartCallCounter.startCalls).isOne();
88   }
89
90   @Test
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);
94
95     underTest.cleanup();
96   }
97
98   private static class NoOpExecutor implements MigrationStepsExecutor {
99     @Override
100     public void execute(List<RegisteredMigrationStep> steps, MigrationStatusListener listener) {
101
102     }
103   }
104
105   private static class NoOpMigrationStep implements MigrationStep {
106     @Override
107     public void execute() throws SQLException {
108
109     }
110   }
111
112   public static final class StartCallCounter implements Startable {
113     private static int startCalls = 0;
114
115     @Override
116     public void start() {
117       startCalls++;
118     }
119
120     @Override
121     public void stop() {
122       // do nothing
123     }
124   }
125
126   public static final class StopFailing implements Startable {
127     @Override
128     public void start() {
129       // do nothing
130     }
131
132     @Override
133     public void stop() {
134       throw new RuntimeException("Faking stop call failing");
135     }
136   }
137 }