]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8445 add a DAO for SCHEMA_MIGRATIONS
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 15 Dec 2016 13:30:58 +0000 (14:30 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 16 Dec 2016 13:35:28 +0000 (14:35 +0100)
and remove direct usages of MyBatis mapper

21 files changed:
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImpl.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImplTest.java
server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java
sonar-db/src/main/java/org/sonar/db/DaoModule.java
sonar-db/src/main/java/org/sonar/db/DbClient.java
sonar-db/src/main/java/org/sonar/db/MyBatis.java
sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDao.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationDto.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/schemamigration/SchemaMigrationMapper.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/schemamigration/package-info.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationDto.java [deleted file]
sonar-db/src/main/java/org/sonar/db/version/SchemaMigrationMapper.java [deleted file]
sonar-db/src/main/resources/org/sonar/db/schemamigration/SchemaMigrationMapper.xml [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/SchemaMigrationMapper.xml [deleted file]
sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java
sonar-db/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java
sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion.xml [deleted file]
sonar-db/src/test/resources/org/sonar/db/version/DatabaseVersionTest/getVersion_no_rows.xml [deleted file]

index 25c7fa6fe557e938d51cf25ac777ee7235b7e49c..b17c2a2d18315d14dfea69f56e28eed999efa49d 100644 (file)
@@ -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
index 9c00b983a8b748adda472b1e6cd92784f86afe8d..8e08992f19cc39873aa09373aaafde65257294ea 100644 (file)
@@ -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;
index 6ece014caf8010d298bfe60826540d1e4d8108a5..fd7c849b124992a7c799cf75ab016310bee80cde 100644 (file)
@@ -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;
 
index 5941af2a2ba584e9d13feaef4879085456985c4d..9fcd6f1e6ff505379228340e137f7e3617e98b2f 100644 (file)
@@ -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() {
index 74ab7e4bd9d2e9765b613b32220bedd0897d0ee9..72f66bf0c8f4b19f3077a8347617322e95f74664 100644 (file)
@@ -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,
index 02744a29ec16051ad0b7e441368d7bf58451984f..bec938392790f1c639d57a586c458fcf2723869d 100644 (file)
@@ -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;
   }
index 27291ca9fe0bea9d2252fc0caf036b030eb591cf..913c6898bb4d58beb7311f04036b9648751d6f9d 100644 (file)
@@ -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 (file)
index 0000000..2cd1ab9
--- /dev/null
@@ -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<Integer> 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 (file)
index 0000000..7f7911e
--- /dev/null
@@ -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 (file)
index 0000000..cec3c56
--- /dev/null
@@ -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<Integer> 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 (file)
index 0000000..4dc88d0
--- /dev/null
@@ -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;
+
index 13b24100a5d9f7c75b7be7dcf1a4525e82ebf3c2..6dcb89683e08d5a9ba9ef02e48d996fd631274a5 100644 (file)
@@ -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<Integer> versions = session.getMapper(SchemaMigrationMapper.class).selectVersions();
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      if (!DatabaseUtils.tableExists("SCHEMA_MIGRATIONS", dbSession.getConnection())) {
+        return null;
+      }
+
+      List<Integer> 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 (file)
index 00f5e07..0000000
+++ /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 (file)
index e2894ef..0000000
+++ /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<Integer> 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 (file)
index 0000000..efe2333
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.db.schemamigration.SchemaMigrationMapper">
+
+  <select id="selectVersions" resultType="int">
+    select version from schema_migrations
+  </select>
+
+  <insert id="insert" parameterType="string" useGeneratedKeys="false">
+    insert into schema_migrations(version) values (#{version,jdbcType=VARCHAR})
+  </insert>
+</mapper>
+
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 (file)
index cde3737..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
-<mapper namespace="org.sonar.db.version.SchemaMigrationMapper">
-
-  <select id="selectVersions" resultType="int">
-    select version from schema_migrations
-  </select>
-
-  <insert id="insert" parameterType="string" useGeneratedKeys="false">
-    insert into schema_migrations(version) values (#{version,jdbcType=VARCHAR})
-  </insert>
-</mapper>
-
index e666e3a2435893d3da3a11b025fca98768bd2249..c76d29e2dc1201981cb57e7004524031041f7976 100644 (file)
@@ -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 (file)
index 0000000..9a9c60a
--- /dev/null
@@ -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);
+  }
+}
index a36ae4045a019e2996e3217892953a73aaae6927..cb1e3219ce906c65809705f91bb36eb52e911032 100644 (file)
@@ -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 (file)
index 867616d..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<dataset>
-
-  <schema_migrations version="1"/>
-  <schema_migrations version="2"/>
-  <schema_migrations version="4"/>
-  <schema_migrations version="123"/>
-  <schema_migrations version="50"/>
-</dataset>
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 (file)
index 5ed00ba..0000000
+++ /dev/null
@@ -1 +0,0 @@
-<dataset></dataset>
\ No newline at end of file