3 * Copyright (C) 2009-2019 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.history;
22 import java.sql.Connection;
23 import java.sql.PreparedStatement;
24 import java.sql.SQLException;
25 import java.util.Arrays;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.db.CoreDbTester;
30 import org.sonar.server.platform.db.migration.step.MigrationStep;
31 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
33 import static org.assertj.core.api.Assertions.assertThat;
35 public class MigrationHistoryImplTest {
37 public CoreDbTester dbTester = CoreDbTester.createForSchema(MigrationHistoryImplTest.class, "schema_migration.sql");
39 public ExpectedException expectedException = ExpectedException.none();
41 private MigrationHistoryImpl underTest = new MigrationHistoryImpl(dbTester.database());
44 public void start_does_not_fail_if_table_history_exists() {
49 public void getLastMigrationNumber_returns_empty_if_history_table_is_empty() {
50 assertThat(underTest.getLastMigrationNumber()).isEmpty();
54 public void getLastMigrationNumber_returns_last_version_assuming_version_are_only_number() throws SQLException {
57 assertThat(underTest.getLastMigrationNumber()).contains(30L);
61 public void done_fails_with_NPE_if_argument_is_null() {
62 expectedException.expect(NullPointerException.class);
68 public void done_adds_migration_number_to_table() {
69 underTest.done(new RegisteredMigrationStep(12, "aa", MigrationStep.class));
71 assertThat(underTest.getLastMigrationNumber()).contains(12L);
74 private void insert(int... versions) throws SQLException {
75 try (Connection connection = dbTester.database().getDataSource().getConnection()) {
76 Arrays.stream(versions).forEach(version -> insert(connection, version));
80 private void insert(Connection connection, long version) {
81 try (PreparedStatement statement = connection.prepareStatement("insert into schema_migrations(version) values (?)")) {
82 statement.setString(1, String.valueOf(version));
85 } catch (SQLException e) {
86 throw new IllegalStateException(String.format("Failed to insert row with value %s", version), e);