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;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.slf4j.event.Level;
26 import org.sonar.api.testfixtures.log.LogTester;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.dialect.Dialect;
29 import org.sonar.db.dialect.H2;
30 import org.sonar.db.dialect.MsSql;
31 import org.sonar.db.dialect.Oracle;
32 import org.sonar.db.dialect.PostgreSql;
33 import org.sonar.server.platform.DefaultServerUpgradeStatus;
34 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyNoInteractions;
40 import static org.mockito.Mockito.when;
42 public class AutoDbMigrationTest {
44 public LogTester logTester = new LogTester();
46 private DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
47 private DefaultServerUpgradeStatus serverUpgradeStatus = mock(DefaultServerUpgradeStatus.class);
48 private MigrationEngine migrationEngine = mock(MigrationEngine.class);
49 private AutoDbMigration underTest = new AutoDbMigration(serverUpgradeStatus, migrationEngine);
52 public void start_runs_MigrationEngine_on_h2_if_fresh_install() {
53 start_runs_MigrationEngine_for_dialect_if_fresh_install(new H2());
57 public void start_runs_MigrationEngine_on_postgre_if_fresh_install() {
58 start_runs_MigrationEngine_for_dialect_if_fresh_install(new PostgreSql());
62 public void start_runs_MigrationEngine_on_Oracle_if_fresh_install() {
63 start_runs_MigrationEngine_for_dialect_if_fresh_install(new Oracle());
67 public void start_runs_MigrationEngine_on_MsSQL_if_fresh_install() {
68 start_runs_MigrationEngine_for_dialect_if_fresh_install(new MsSql());
71 private void start_runs_MigrationEngine_for_dialect_if_fresh_install(Dialect dialect) {
73 mockFreshInstall(true);
77 verify(migrationEngine).execute();
82 public void start_does_nothing_if_not_fresh_install() {
83 mockFreshInstall(false);
87 verifyNoInteractions(migrationEngine);
88 assertThat(logTester.logs(Level.INFO)).isEmpty();
92 public void start_runs_MigrationEngine_if_autoDbMigration_enabled() {
93 mockFreshInstall(false);
94 when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
95 when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
99 verify(migrationEngine).execute();
100 assertThat(logTester.logs(Level.INFO)).contains("Automatically perform DB migration, as automatic database upgrade is enabled");
104 public void start_does_nothing_if_autoDbMigration_but_no_upgrade() {
105 mockFreshInstall(false);
106 when(serverUpgradeStatus.isUpgraded()).thenReturn(false);
107 when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
111 verifyNoInteractions(migrationEngine);
112 assertThat(logTester.logs(Level.INFO)).isEmpty();
116 public void stop_has_no_effect() {
120 private void mockFreshInstall(boolean value) {
121 when(serverUpgradeStatus.isFreshInstall()).thenReturn(value);
124 private void mockDialect(Dialect dialect) {
125 when(dbClient.getDatabase().getDialect()).thenReturn(dialect);
128 private void verifyInfoLog() {
129 assertThat(logTester.logs()).hasSize(1);
130 assertThat(logTester.logs(Level.INFO)).containsExactly("Automatically perform DB migration on fresh install");