3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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 java.sql.Connection;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.mockito.Mockito;
27 import org.sonar.api.platform.ServerUpgradeStatus;
28 import org.sonar.api.utils.log.LogTester;
29 import org.sonar.api.utils.log.LoggerLevel;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.dialect.Dialect;
33 import org.sonar.db.dialect.H2;
34 import org.sonar.db.dialect.MsSql;
35 import org.sonar.db.dialect.MySql;
36 import org.sonar.db.dialect.Oracle;
37 import org.sonar.db.dialect.PostgreSql;
38 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.verifyNoMoreInteractions;
45 import static org.mockito.Mockito.verifyZeroInteractions;
46 import static org.mockito.Mockito.when;
48 public class AutoDbMigrationTest {
50 public ExpectedException thrown = ExpectedException.none();
52 public LogTester logTester = new LogTester();
54 private DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
55 private ServerUpgradeStatus serverUpgradeStatus = mock(ServerUpgradeStatus.class);
56 private MigrationEngine migrationEngine = mock(MigrationEngine.class);
57 private AutoDbMigration underTest = new AutoDbMigration(serverUpgradeStatus, dbClient, migrationEngine);
59 private AutoDbMigration noRealH2Creation = spy(new AutoDbMigration(serverUpgradeStatus, dbClient, migrationEngine) {
61 protected void createSchema(Connection connection, String dialectId) {
66 public void start_creates_schema_on_h2_if_fresh_install() {
67 mockDialect(new H2());
68 mockDbClientOpenSession();
69 mockFreshInstall(true);
71 noRealH2Creation.start();
73 verify(noRealH2Creation).installH2();
78 public void start_runs_MigrationEngine_on_mysql_if_fresh_install() {
79 start_runs_MigrationEngine_for_dialect_if_fresh_install(new MySql());
83 public void start_runs_MigrationEngine_on_postgre_if_fresh_install() {
84 start_runs_MigrationEngine_for_dialect_if_fresh_install(new PostgreSql());
88 public void start_runs_MigrationEngine_on_Oracle_if_fresh_install() {
89 start_runs_MigrationEngine_for_dialect_if_fresh_install(new Oracle());
93 public void start_runs_MigrationEngine_on_MsSQL_if_fresh_install() {
94 start_runs_MigrationEngine_for_dialect_if_fresh_install(new MsSql());
97 private void start_runs_MigrationEngine_for_dialect_if_fresh_install(Dialect dialect) {
99 mockFreshInstall(true);
103 verify(migrationEngine).execute();
108 public void start_does_nothing_if_not_fresh_install() {
109 mockFreshInstall(false);
111 noRealH2Creation.start();
113 verify(noRealH2Creation).start();
114 verifyNoMoreInteractions(noRealH2Creation);
115 verifyZeroInteractions(migrationEngine);
116 assertThat(logTester.logs()).isEmpty();
120 public void stop_has_no_effect() {
124 private void mockFreshInstall(boolean value) {
125 when(serverUpgradeStatus.isFreshInstall()).thenReturn(value);
128 private void mockDialect(Dialect dialect) {
129 when(dbClient.getDatabase().getDialect()).thenReturn(dialect);
132 private void mockDbClientOpenSession() {
133 Connection connection = mock(Connection.class);
134 DbSession session = mock(DbSession.class);
135 when(session.getConnection()).thenReturn(connection);
136 when(dbClient.openSession(false)).thenReturn(session);
139 private void verifyInfoLog() {
140 assertThat(logTester.logs()).hasSize(1);
141 assertThat(logTester.logs(LoggerLevel.INFO)).containsExactly("Automatically perform DB migration on fresh install");