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