From: Sébastien Lesaint Date: Thu, 15 Dec 2016 13:30:58 +0000 (+0100) Subject: SONAR-8445 add a DAO for SCHEMA_MIGRATIONS X-Git-Tag: 6.3-RC1~729 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1af4105f69b467eca5233c164752923fce021884;p=sonarqube.git SONAR-8445 add a DAO for SCHEMA_MIGRATIONS and remove direct usages of MyBatis mapper --- diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 25c7fa6fe55..b17c2a2d183 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -106,7 +106,7 @@ public class ComputeEngineContainerImplTest { assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize( COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION + 27 // level 1 - + 47 // content of DaoModule + + 48 // content of DaoModule + 2 // content of EsSearchModule + 63 // content of CorePropertyDefinitions + 1 // content of CePropertyDefinitions diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImpl.java index 9c00b983a8b..8e08992f19c 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImpl.java @@ -28,7 +28,7 @@ import java.util.Optional; import org.sonar.db.DatabaseUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.db.version.SchemaMigrationMapper; +import org.sonar.db.schemamigration.SchemaMigrationMapper; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; import static com.google.common.base.Preconditions.checkState; diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImplTest.java index 6ece014caf8..fd7c849b124 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImplTest.java @@ -26,7 +26,7 @@ import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; -import org.sonar.db.version.SchemaMigrationMapper; +import org.sonar.db.schemamigration.SchemaMigrationMapper; import org.sonar.server.platform.db.migration.step.MigrationStep; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java index 5941af2a2ba..9fcd6f1e6ff 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java @@ -20,7 +20,6 @@ package org.sonar.server.platform.monitoring; import java.util.Map; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; @@ -34,13 +33,7 @@ public class DatabaseMonitorTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - DatabaseMonitor underTest; - - @Before - public void setUp() { - DatabaseVersion dbVersion = new DatabaseVersion(dbTester.myBatis()); - underTest = new DatabaseMonitor(dbVersion, dbTester.getDbClient()); - } + private DatabaseMonitor underTest = new DatabaseMonitor(new DatabaseVersion(dbTester.getDbClient()), dbTester.getDbClient()); @Test public void name_is_not_empty() { diff --git a/sonar-db/src/main/java/org/sonar/db/DaoModule.java b/sonar-db/src/main/java/org/sonar/db/DaoModule.java index 74ab7e4bd9d..72f66bf0c8f 100644 --- a/sonar-db/src/main/java/org/sonar/db/DaoModule.java +++ b/sonar-db/src/main/java/org/sonar/db/DaoModule.java @@ -58,6 +58,7 @@ import org.sonar.db.qualityprofile.QProfileChangeDao; import org.sonar.db.qualityprofile.QualityProfileDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; +import org.sonar.db.schemamigration.SchemaMigrationDao; import org.sonar.db.source.FileSourceDao; import org.sonar.db.user.AuthorDao; import org.sonar.db.user.GroupDao; @@ -113,6 +114,7 @@ public class DaoModule extends Module { RuleDao.class, RuleRepositoryDao.class, SnapshotDao.class, + SchemaMigrationDao.class, UserDao.class, UserGroupDao.class, UserPermissionDao.class, diff --git a/sonar-db/src/main/java/org/sonar/db/DbClient.java b/sonar-db/src/main/java/org/sonar/db/DbClient.java index 02744a29ec1..bec93839279 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -58,6 +58,7 @@ import org.sonar.db.qualityprofile.QProfileChangeDao; import org.sonar.db.qualityprofile.QualityProfileDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; +import org.sonar.db.schemamigration.SchemaMigrationDao; import org.sonar.db.source.FileSourceDao; import org.sonar.db.user.AuthorDao; import org.sonar.db.user.GroupDao; @@ -72,6 +73,7 @@ public class DbClient { private final Database database; private final MyBatis myBatis; + private final SchemaMigrationDao schemaMigrationDao; private final AuthorizationDao authorizationDao; private final OrganizationDao organizationDao; private final QualityProfileDao qualityProfileDao; @@ -126,6 +128,7 @@ public class DbClient { for (Dao dao : daos) { map.put(dao.getClass(), dao); } + schemaMigrationDao = getDao(map, SchemaMigrationDao.class); authorizationDao = getDao(map, AuthorizationDao.class); organizationDao = getDao(map, OrganizationDao.class); qualityProfileDao = getDao(map, QualityProfileDao.class); @@ -185,6 +188,10 @@ public class DbClient { return database; } + public SchemaMigrationDao schemaMigrationDao() { + return schemaMigrationDao; + } + public AuthorizationDao authorizationDao() { return authorizationDao; } diff --git a/sonar-db/src/main/java/org/sonar/db/MyBatis.java b/sonar-db/src/main/java/org/sonar/db/MyBatis.java index 27291ca9fe0..913c6898bb4 100644 --- a/sonar-db/src/main/java/org/sonar/db/MyBatis.java +++ b/sonar-db/src/main/java/org/sonar/db/MyBatis.java @@ -117,8 +117,8 @@ import org.sonar.db.user.UserMapper; import org.sonar.db.user.UserTokenCount; import org.sonar.db.user.UserTokenDto; import org.sonar.db.user.UserTokenMapper; -import org.sonar.db.version.SchemaMigrationDto; -import org.sonar.db.version.SchemaMigrationMapper; +import org.sonar.db.schemamigration.SchemaMigrationDto; +import org.sonar.db.schemamigration.SchemaMigrationMapper; import org.sonar.db.webhook.WebhookDeliveryMapper; public class MyBatis { diff --git a/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDao.java b/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDao.java new file mode 100644 index 00000000000..2cd1ab9526d --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDao.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.schemamigration; + +import java.util.List; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; + +public class SchemaMigrationDao implements Dao { + public List selectVersions(DbSession dbSession) { + return getMapper(dbSession).selectVersions(); + } + + public void insert(DbSession dbSession, String version) { + requireNonNull(version, "version can't be null"); + checkArgument(!version.isEmpty(), "version can't be empty"); + getMapper(dbSession).insert(version); + } + + private static SchemaMigrationMapper getMapper(DbSession dbSession) { + return dbSession.getMapper(SchemaMigrationMapper.class); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDto.java b/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDto.java new file mode 100644 index 00000000000..7f7911e4516 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDto.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.schemamigration; + +/** + * Maps the table SCHEMA_MIGRATIONS that is fed by Ruby on Rails Migrations + * @since 3.0 + */ +public class SchemaMigrationDto { + private String version;// NOSONAR this field is assigned by MyBatis + + public String getVersion() { + return version; + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationMapper.java b/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationMapper.java new file mode 100644 index 00000000000..cec3c56b746 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationMapper.java @@ -0,0 +1,28 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.schemamigration; + +import java.util.List; + +public interface SchemaMigrationMapper { + List selectVersions(); + + void insert(String version); +} diff --git a/sonar-db/src/main/java/org/sonar/db/schemamigration/package-info.java b/sonar-db/src/main/java/org/sonar/db/schemamigration/package-info.java new file mode 100644 index 00000000000..4dc88d0aabb --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/schemamigration/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@ParametersAreNonnullByDefault +package org.sonar.db.schemamigration; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index 13b24100a5d..6dcb89683e0 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -25,8 +25,9 @@ import java.util.Collections; import java.util.List; import java.util.Set; import javax.annotation.Nullable; -import org.apache.ibatis.session.SqlSession; -import org.sonar.db.MyBatis; +import org.sonar.db.DatabaseUtils; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; public class DatabaseVersion { @@ -108,10 +109,10 @@ public class DatabaseVersion { "user_tokens", "webhook_deliveries"); - private MyBatis mybatis; + private final DbClient dbClient; - public DatabaseVersion(MyBatis mybatis) { - this.mybatis = mybatis; + public DatabaseVersion(DbClient dbClient) { + this.dbClient = dbClient; } @VisibleForTesting @@ -134,21 +135,17 @@ public class DatabaseVersion { } public Integer getVersion() { - SqlSession session = mybatis.openSession(false); - try { - List versions = session.getMapper(SchemaMigrationMapper.class).selectVersions(); + try (DbSession dbSession = dbClient.openSession(false)) { + if (!DatabaseUtils.tableExists("SCHEMA_MIGRATIONS", dbSession.getConnection())) { + return null; + } + + List versions = dbClient.schemaMigrationDao().selectVersions(dbSession); if (!versions.isEmpty()) { Collections.sort(versions); return versions.get(versions.size() - 1); } return null; - } catch (RuntimeException e) { - // The table SCHEMA_MIGRATIONS does not exist. - // Ignore this exception -> it will be created by MigrationHistoryTable class. - return null; - - } finally { - MyBatis.closeQuietly(session); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationDto.java b/sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationDto.java deleted file mode 100644 index 00f5e07afc8..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationDto.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.version; - -/** - * Maps the table SCHEMA_MIGRATIONS that is fed by Ruby on Rails Migrations - * @since 3.0 - */ -public class SchemaMigrationDto { - private String version;// NOSONAR this field is assigned by MyBatis - - public String getVersion() { - return version; - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationMapper.java b/sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationMapper.java deleted file mode 100644 index e2894ef5c89..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationMapper.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.version; - -import java.util.List; - -public interface SchemaMigrationMapper { - List selectVersions(); - - void insert(String version); -} diff --git a/sonar-db/src/main/resources/org/sonar/db/schemamigration/SchemaMigrationMapper.xml b/sonar-db/src/main/resources/org/sonar/db/schemamigration/SchemaMigrationMapper.xml new file mode 100644 index 00000000000..efe2333954b --- /dev/null +++ b/sonar-db/src/main/resources/org/sonar/db/schemamigration/SchemaMigrationMapper.xml @@ -0,0 +1,14 @@ + + + + + + + + + insert into schema_migrations(version) values (#{version,jdbcType=VARCHAR}) + + + diff --git a/sonar-db/src/main/resources/org/sonar/db/version/SchemaMigrationMapper.xml b/sonar-db/src/main/resources/org/sonar/db/version/SchemaMigrationMapper.xml deleted file mode 100644 index cde37378a00..00000000000 --- a/sonar-db/src/main/resources/org/sonar/db/version/SchemaMigrationMapper.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - insert into schema_migrations(version) values (#{version,jdbcType=VARCHAR}) - - - diff --git a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java index e666e3a2435..c76d29e2dc1 100644 --- a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java @@ -29,6 +29,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(2 + 45); + assertThat(container.size()).isEqualTo(2 + 46); } } diff --git a/sonar-db/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java b/sonar-db/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java new file mode 100644 index 00000000000..9a9c60a3c30 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java @@ -0,0 +1,80 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.schemamigration; + +import java.sql.Statement; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SchemaMigrationDaoTest { + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private DbSession dbSession = dbTester.getSession(); + private SchemaMigrationDao underTest = dbTester.getDbClient().schemaMigrationDao(); + + @After + public void tearDown() throws Exception { + // schema_migration is not cleared by DbTester + try(Statement statement = dbTester.getSession().getConnection().createStatement()) { + statement.execute("truncate table schema_migrations"); + } + } + + @Test + public void insert_fails_with_NPE_if_argument_is_null() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("version can't be null"); + + underTest.insert(dbSession, null); + } + + @Test + public void insert_fails_with_IAE_if_argument_is_empty() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("version can't be empty"); + + underTest.insert(dbSession, ""); + } + + @Test + public void getVersions_returns_an_empty_list_if_table_is_empty() { + assertThat(underTest.selectVersions(dbSession)).isEmpty(); + } + + @Test + public void getVersions_returns_all_versions_in_table() { + underTest.insert(dbSession, "22"); + underTest.insert(dbSession, "1"); + underTest.insert(dbSession, "3"); + dbSession.commit(); + + assertThat(underTest.selectVersions(dbSession)).containsOnly(22, 1, 3); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java b/sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java index a36ae4045a0..cb1e3219ce9 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java @@ -22,30 +22,38 @@ package org.sonar.db.version; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static org.assertj.core.api.Assertions.assertThat; - public class DatabaseVersionTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); + private DbClient dbClient = dbTester.getDbClient(); + private DbSession dbSession = dbTester.getSession(); + private DatabaseVersion underTest = new DatabaseVersion(dbClient); + @Test public void getVersion() { - dbTester.prepareDbUnit(getClass(), "getVersion.xml"); + dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "1"); + dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "2"); + dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "4"); + dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "123"); + dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "50"); + dbSession.commit(); - Integer version = new DatabaseVersion(dbTester.myBatis()).getVersion(); + Integer version = underTest.getVersion(); assertThat(version).isEqualTo(123); } @Test public void getVersion_no_rows() { - dbTester.prepareDbUnit(getClass(), "getVersion_no_rows.xml"); - - Integer version = new DatabaseVersion(dbTester.myBatis()).getVersion(); + Integer version = underTest.getVersion(); assertThat(version).isNull(); } diff --git a/sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion.xml b/sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion.xml deleted file mode 100644 index 867616dc080..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion_no_rows.xml b/sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion_no_rows.xml deleted file mode 100644 index 5ed00ba028b..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion_no_rows.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file