]> source.dussan.org Git - sonarqube.git/blob
093d6541ad9719efd78d7fe3b60bac6c3db12cb2
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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;
21
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;
39
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;
47
48 public class AutoDbMigrationTest {
49   @Rule
50   public ExpectedException thrown = ExpectedException.none();
51   @Rule
52   public LogTester logTester = new LogTester();
53
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);
58
59   private AutoDbMigration noRealH2Creation = spy(new AutoDbMigration(serverUpgradeStatus, dbClient, migrationEngine) {
60     @Override
61     protected void createSchema(Connection connection, String dialectId) {
62     }
63   });
64
65   @Test
66   public void start_creates_schema_on_h2_if_fresh_install() {
67     mockDialect(new H2());
68     mockDbClientOpenSession();
69     mockFreshInstall(true);
70
71     noRealH2Creation.start();
72
73     verify(noRealH2Creation).installH2();
74     verifyInfoLog();
75   }
76
77   @Test
78   public void start_runs_MigrationEngine_on_mysql_if_fresh_install() {
79     start_runs_MigrationEngine_for_dialect_if_fresh_install(new MySql());
80   }
81
82   @Test
83   public void start_runs_MigrationEngine_on_postgre_if_fresh_install() {
84     start_runs_MigrationEngine_for_dialect_if_fresh_install(new PostgreSql());
85   }
86
87   @Test
88   public void start_runs_MigrationEngine_on_Oracle_if_fresh_install() {
89     start_runs_MigrationEngine_for_dialect_if_fresh_install(new Oracle());
90   }
91
92   @Test
93   public void start_runs_MigrationEngine_on_MsSQL_if_fresh_install() {
94     start_runs_MigrationEngine_for_dialect_if_fresh_install(new MsSql());
95   }
96
97   private void start_runs_MigrationEngine_for_dialect_if_fresh_install(Dialect dialect) {
98     mockDialect(dialect);
99     mockFreshInstall(true);
100
101     underTest.start();
102
103     verify(migrationEngine).execute();
104     verifyInfoLog();
105   }
106
107   @Test
108   public void start_does_nothing_if_not_fresh_install() {
109     mockFreshInstall(false);
110
111     noRealH2Creation.start();
112
113     verify(noRealH2Creation).start();
114     verifyNoMoreInteractions(noRealH2Creation);
115     verifyZeroInteractions(migrationEngine);
116     assertThat(logTester.logs()).isEmpty();
117   }
118
119   @Test
120   public void stop_has_no_effect() {
121     underTest.stop();
122   }
123
124   private void mockFreshInstall(boolean value) {
125     when(serverUpgradeStatus.isFreshInstall()).thenReturn(value);
126   }
127
128   private void mockDialect(Dialect dialect) {
129     when(dbClient.getDatabase().getDialect()).thenReturn(dialect);
130   }
131
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);
137   }
138
139   private void verifyInfoLog() {
140     assertThat(logTester.logs()).hasSize(1);
141     assertThat(logTester.logs(LoggerLevel.INFO)).containsExactly("Automatically perform DB migration on fresh install");
142   }
143
144 }