aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2014-05-13 12:10:48 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2014-05-13 12:11:01 +0200
commitc885cab7a0658c00ec29a70ea8b8ac30e7ec3e56 (patch)
tree4c86c4963fcec286264a6f406962bac04e5159bb
parent4de325e2394d44ea07ac2a2b58b5350f994e628c (diff)
downloadsonarqube-c885cab7a0658c00ec29a70ea8b8ac30e7ec3e56.tar.gz
sonarqube-c885cab7a0658c00ec29a70ea8b8ac30e7ec3e56.zip
Use the new DbClient in DatabaseMigrator
-rw-r--r--sonar-server/src/main/java/org/sonar/server/db/DbClient.java9
-rw-r--r--sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrator.java24
-rw-r--r--sonar-server/src/test/java/org/sonar/server/db/DbClientTest.java3
-rw-r--r--sonar-server/src/test/java/org/sonar/server/db/migrations/DatabaseMigratorTest.java25
4 files changed, 33 insertions, 28 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/db/DbClient.java b/sonar-server/src/main/java/org/sonar/server/db/DbClient.java
index b690d6c6504..83fd23c600e 100644
--- a/sonar-server/src/main/java/org/sonar/server/db/DbClient.java
+++ b/sonar-server/src/main/java/org/sonar/server/db/DbClient.java
@@ -20,6 +20,7 @@
package org.sonar.server.db;
import org.sonar.api.ServerComponent;
+import org.sonar.core.persistence.Database;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
@@ -31,19 +32,25 @@ import org.sonar.server.rule2.RuleDao;
*/
public class DbClient implements ServerComponent {
+ private final Database db;
private final MyBatis myBatis;
private final RuleDao ruleDao;
private final ActiveRuleDao activeRuleDao;
private final QualityProfileDao qProfileDao;
- public DbClient(MyBatis myBatis, RuleDao ruleDao, ActiveRuleDao activeRuleDao,
+ public DbClient(Database db, MyBatis myBatis, RuleDao ruleDao, ActiveRuleDao activeRuleDao,
QualityProfileDao qProfileDao) {
+ this.db = db;
this.myBatis = myBatis;
this.ruleDao = ruleDao;
this.activeRuleDao = activeRuleDao;
this.qProfileDao = qProfileDao;
}
+ public Database database() {
+ return db;
+ }
+
public DbSession openSession(boolean batch) {
return myBatis.openSession(batch);
}
diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrator.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrator.java
index c67b21fb7c1..9b69826fcd4 100644
--- a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrator.java
+++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrator.java
@@ -26,9 +26,8 @@ import org.picocontainer.Startable;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
import org.sonar.api.platform.ServerUpgradeStatus;
-import org.sonar.core.persistence.Database;
import org.sonar.core.persistence.DdlUtils;
-import org.sonar.core.persistence.MyBatis;
+import org.sonar.server.db.DbClient;
import org.sonar.server.plugins.ServerPluginRepository;
import java.sql.Connection;
@@ -41,29 +40,27 @@ import java.sql.Connection;
*/
public class DatabaseMigrator implements ServerComponent, Startable {
- private final MyBatis myBatis;
- private final Database database;
+ private final DbClient dbClient;
private final DatabaseMigration[] migrations;
private final ServerUpgradeStatus serverUpgradeStatus;
/**
* ServerPluginRepository is used to ensure H2 schema creation is done only after copy of bundle plugins have been done
*/
- public DatabaseMigrator(MyBatis myBatis, Database database, DatabaseMigration[] migrations, ServerUpgradeStatus serverUpgradeStatus,
+ public DatabaseMigrator(DbClient dbClient, DatabaseMigration[] migrations, ServerUpgradeStatus serverUpgradeStatus,
ServerPluginRepository serverPluginRepository) {
- this.myBatis = myBatis;
- this.database = database;
+ this.dbClient = dbClient;
this.migrations = migrations;
this.serverUpgradeStatus = serverUpgradeStatus;
}
@Override
- public void start(){
+ public void start() {
createDatabase();
}
@Override
- public void stop(){
+ public void stop() {
// Nothing to do
}
@@ -72,17 +69,16 @@ public class DatabaseMigrator implements ServerComponent, Startable {
*/
@VisibleForTesting
boolean createDatabase() {
- if (DdlUtils.supportsDialect(database.getDialect().getId()) && serverUpgradeStatus.isFreshInstall()) {
+ if (DdlUtils.supportsDialect(dbClient.database().getDialect().getId()) && serverUpgradeStatus.isFreshInstall()) {
LoggerFactory.getLogger(getClass()).info("Create database");
- SqlSession session = null;
+ SqlSession session = dbClient.openSession(false);
Connection connection = null;
try {
- session = myBatis.openSession(false);
connection = session.getConnection();
- createSchema(connection, database.getDialect().getId());
+ createSchema(connection, dbClient.database().getDialect().getId());
return true;
} finally {
- MyBatis.closeQuietly(session);
+ session.close();
// The connection is probably already closed by session.close()
// but it's not documented in mybatis javadoc.
diff --git a/sonar-server/src/test/java/org/sonar/server/db/DbClientTest.java b/sonar-server/src/test/java/org/sonar/server/db/DbClientTest.java
index ef296d23ecc..6a5edba1747 100644
--- a/sonar-server/src/test/java/org/sonar/server/db/DbClientTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/db/DbClientTest.java
@@ -43,8 +43,9 @@ public class DbClientTest {
ActiveRuleDao activeRuleDao = mock(ActiveRuleDao.class);
QualityProfileDao qualityProfileDao = mock(QualityProfileDao.class);
- DbClient client = new DbClient(myBatis, ruleDao, activeRuleDao, qualityProfileDao);
+ DbClient client = new DbClient(db.database(), myBatis, ruleDao, activeRuleDao, qualityProfileDao);
+ assertThat(client.database()).isSameAs(db.database());
DbSession dbSession = client.openSession(true);
assertThat(dbSession).isNotNull();
assertThat(dbSession.getConnection().isClosed()).isFalse();
diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/DatabaseMigratorTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/DatabaseMigratorTest.java
index 5add093adfb..c453676f57c 100644
--- a/sonar-server/src/test/java/org/sonar/server/db/migrations/DatabaseMigratorTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/DatabaseMigratorTest.java
@@ -23,20 +23,22 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
import org.sonar.api.platform.ServerUpgradeStatus;
import org.sonar.core.persistence.AbstractDaoTestCase;
-import org.sonar.core.persistence.Database;
import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
import org.sonar.core.persistence.dialect.Dialect;
import org.sonar.core.persistence.dialect.H2;
import org.sonar.core.persistence.dialect.MySql;
+import org.sonar.server.db.DbClient;
import java.sql.Connection;
import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class DatabaseMigratorTest extends AbstractDaoTestCase {
@@ -44,24 +46,22 @@ public class DatabaseMigratorTest extends AbstractDaoTestCase {
@Rule
public ExpectedException thrown = ExpectedException.none();
- MyBatis mybatis = mock(MyBatis.class);
- Database database = mock(Database.class);
+ DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
DatabaseMigration[] migrations = new DatabaseMigration[]{new FakeMigration()};
ServerUpgradeStatus serverUpgradeStatus = mock(ServerUpgradeStatus.class);
-
DatabaseMigrator migrator;
@Before
public void setUp() throws Exception {
- migrator = new DatabaseMigrator(mybatis, database, migrations, serverUpgradeStatus, null);
+ migrator = new DatabaseMigrator(dbClient, migrations, serverUpgradeStatus, null);
}
@Test
public void should_support_only_creation_of_h2_database() throws Exception {
- when(database.getDialect()).thenReturn(new MySql());
+ when(dbClient.database().getDialect()).thenReturn(new MySql());
assertThat(migrator.createDatabase()).isFalse();
- verifyZeroInteractions(mybatis);
+ verify(dbClient, never()).openSession(anyBoolean());
}
@Test
@@ -82,14 +82,14 @@ public class DatabaseMigratorTest extends AbstractDaoTestCase {
@Test
public void should_create_schema_on_h2() throws Exception {
Dialect supportedDialect = new H2();
- when(database.getDialect()).thenReturn(supportedDialect);
+ when(dbClient.database().getDialect()).thenReturn(supportedDialect);
Connection connection = mock(Connection.class);
DbSession session = mock(DbSession.class);
when(session.getConnection()).thenReturn(connection);
- when(mybatis.openSession(false)).thenReturn(session);
+ when(dbClient.openSession(false)).thenReturn(session);
when(serverUpgradeStatus.isFreshInstall()).thenReturn(true);
- DatabaseMigrator databaseMigrator = new DatabaseMigrator(mybatis, database, migrations, serverUpgradeStatus, null) {
+ DatabaseMigrator databaseMigrator = new DatabaseMigrator(dbClient, migrations, serverUpgradeStatus, null) {
@Override
protected void createSchema(Connection connection, String dialectId) {
}
@@ -100,6 +100,7 @@ public class DatabaseMigratorTest extends AbstractDaoTestCase {
public static class FakeMigration implements DatabaseMigration {
static boolean executed = false;
+
@Override
public void execute() {
executed = true;