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
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;
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;
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;
@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() {
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;
RuleDao.class,
RuleRepositoryDao.class,
SnapshotDao.class,
+ SchemaMigrationDao.class,
UserDao.class,
UserGroupDao.class,
UserPermissionDao.class,
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;
private final Database database;
private final MyBatis myBatis;
+ private final SchemaMigrationDao schemaMigrationDao;
private final AuthorizationDao authorizationDao;
private final OrganizationDao organizationDao;
private final QualityProfileDao qualityProfileDao;
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);
return database;
}
+ public SchemaMigrationDao schemaMigrationDao() {
+ return schemaMigrationDao;
+ }
+
public AuthorizationDao authorizationDao() {
return authorizationDao;
}
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 {
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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;
+
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 {
"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
}
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);
}
}
+++ /dev/null
-/*
- * 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;
- }
-}
+++ /dev/null
-/*
- * 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);
-}
--- /dev/null
+<?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>
+
+++ /dev/null
-<?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>
-
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);
}
}
--- /dev/null
+/*
+ * 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);
+ }
+}
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();
}
+++ /dev/null
-<dataset>
-
- <schema_migrations version="1"/>
- <schema_migrations version="2"/>
- <schema_migrations version="4"/>
- <schema_migrations version="123"/>
- <schema_migrations version="50"/>
-</dataset>
+++ /dev/null
-<dataset></dataset>
\ No newline at end of file