diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-06 17:17:41 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-06 17:17:53 +0200 |
commit | 0f5e9229cbf616e2f03f7e884e91794579371425 (patch) | |
tree | df623b75ecfb4814c1e3248446e2f607034e80ef /sonar-db | |
parent | 64b9b96480d43ac378fa447940e137d1c4f89322 (diff) | |
download | sonarqube-0f5e9229cbf616e2f03f7e884e91794579371425.tar.gz sonarqube-0f5e9229cbf616e2f03f7e884e91794579371425.zip |
Fix execution of tests on non-H2 databases
Diffstat (limited to 'sonar-db')
16 files changed, 84 insertions, 89 deletions
diff --git a/sonar-db/src/test/java/org/sonar/core/user/DeprecatedUserFinderTest.java b/sonar-db/src/test/java/org/sonar/core/user/DeprecatedUserFinderTest.java index 0d978a6124a..895bc460e05 100644 --- a/sonar-db/src/test/java/org/sonar/core/user/DeprecatedUserFinderTest.java +++ b/sonar-db/src/test/java/org/sonar/core/user/DeprecatedUserFinderTest.java @@ -19,8 +19,8 @@ */ package org.sonar.core.user; -import org.junit.BeforeClass; -import org.junit.ClassRule; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.database.model.User; @@ -37,11 +37,11 @@ import static org.mockito.Mockito.mock; @Category(DbTests.class) public class DeprecatedUserFinderTest { - @ClassRule - public static final DbTester dbTester = new DbTester(); + @Rule + public DbTester dbTester = new DbTester(); - @BeforeClass - public static void init() { + @Before + public void init() { dbTester.prepareDbUnit(DeprecatedUserFinderTest.class, "fixture.xml"); } diff --git a/sonar-db/src/test/java/org/sonar/db/DbTester.java b/sonar-db/src/test/java/org/sonar/db/DbTester.java index 30bc65fa1e3..ac990c2a55c 100644 --- a/sonar-db/src/test/java/org/sonar/db/DbTester.java +++ b/sonar-db/src/test/java/org/sonar/db/DbTester.java @@ -96,6 +96,7 @@ public class DbTester extends ExternalResource { @Override protected void before() throws Throwable { + db.start(); truncateTables(); } @@ -104,7 +105,7 @@ public class DbTester extends ExternalResource { if (session != null) { MyBatis.closeQuietly(session); } - db.close(); + db.stop(); } public DbSession getSession() { @@ -225,8 +226,6 @@ public class DbTester extends ExternalResource { public void prepareDbUnit(Class testClass, String... testNames) { InputStream[] streams = new InputStream[testNames.length]; try { - db.truncateTables(); - for (int i = 0; i < testNames.length; i++) { String path = "/" + testClass.getName().replace('.', '/') + "/" + testNames[i]; streams[i] = testClass.getResourceAsStream(path); diff --git a/sonar-db/src/test/java/org/sonar/db/IsAliveMapperTest.java b/sonar-db/src/test/java/org/sonar/db/IsAliveMapperTest.java index 02b9c6b4427..f71a58bd8cb 100644 --- a/sonar-db/src/test/java/org/sonar/db/IsAliveMapperTest.java +++ b/sonar-db/src/test/java/org/sonar/db/IsAliveMapperTest.java @@ -21,9 +21,10 @@ package org.sonar.db; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.sonar.api.utils.System2; import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; @@ -31,8 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class IsAliveMapperTest { - @ClassRule - public static DbTester dbTester = new DbTester(); + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); DbSession session; IsAliveMapper underTest; diff --git a/sonar-db/src/test/java/org/sonar/db/ResultSetIteratorTest.java b/sonar-db/src/test/java/org/sonar/db/ResultSetIteratorTest.java index 4326aa15b88..c1e330732e3 100644 --- a/sonar-db/src/test/java/org/sonar/db/ResultSetIteratorTest.java +++ b/sonar-db/src/test/java/org/sonar/db/ResultSetIteratorTest.java @@ -24,7 +24,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.NoSuchElementException; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.utils.System2; @@ -36,8 +36,8 @@ import static org.junit.Assert.fail; @Category(DbTests.class) public class ResultSetIteratorTest { - @ClassRule - public static DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, ResultSetIteratorTest.class, "schema.sql"); + @Rule + public DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, ResultSetIteratorTest.class, "schema.sql"); @Test public void create_iterator_from_statement() throws Exception { diff --git a/sonar-db/src/test/java/org/sonar/db/TestDb.java b/sonar-db/src/test/java/org/sonar/db/TestDb.java index 3ab4fc41346..685a81220e0 100644 --- a/sonar-db/src/test/java/org/sonar/db/TestDb.java +++ b/sonar-db/src/test/java/org/sonar/db/TestDb.java @@ -41,6 +41,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.db.deprecated.NullQueue; +import org.sonar.db.dialect.H2; /** * This class should be call using @ClassRule in order to create the schema once (ft @Rule is used @@ -95,7 +96,7 @@ class TestDb { ((H2Database) db).executeScript(schemaPath); } else { db.stop(); - throw new AssumptionViolatedException("Test disabled because it supports only H2"); + } } isDefault = (schemaPath == null); @@ -109,20 +110,26 @@ class TestDb { } } - void truncateTables() { - try { - commands.truncateDatabase(db.getDataSource()); - } catch (SQLException e) { - throw new IllegalStateException("Fail to truncate db tables", e); + public void start() { + if (!isDefault && !H2.ID.equals(db.getDialect().getId())) { + throw new AssumptionViolatedException("Test disabled because it supports only H2"); } } - void close() { + void stop() { if (!isDefault) { db.stop(); } } + void truncateTables() { + try { + commands.truncateDatabase(db.getDataSource()); + } catch (SQLException e) { + throw new IllegalStateException("Fail to truncate db tables", e); + } + } + Database getDatabase() { return db; } @@ -174,4 +181,6 @@ class TestDb { IOUtils.closeQuietly(input); } } + + } diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java index 931d4f0c508..dc53978fcc9 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java @@ -21,7 +21,7 @@ package org.sonar.db.component; import java.util.List; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.utils.System2; @@ -33,8 +33,8 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class ComponentLinkDaoTest { - @ClassRule - public static DbTester dbTester = DbTester.create(System2.INSTANCE); + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); ComponentLinkDao dao = dbTester.getDbClient().componentLinkDao(); diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java index 21ec3e349a9..d4e65fc2f15 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java @@ -24,7 +24,7 @@ import java.util.Date; import java.util.List; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.utils.DateUtils; @@ -40,8 +40,8 @@ import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.DESC; @Category(DbTests.class) public class SnapshotDaoTest { - @ClassRule - public static DbTester db = new DbTester(); + @Rule + public DbTester db = new DbTester(); DbSession session; diff --git a/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java b/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java index fe061532489..23b118402d6 100644 --- a/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java @@ -21,17 +21,13 @@ package org.sonar.db.compute; import java.util.List; -import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.TemporaryFolder; import org.sonar.api.utils.System2; -import org.sonar.db.DbSession; import org.sonar.db.DbTester; -import org.sonar.db.MyBatis; import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; @@ -45,30 +41,20 @@ public class AnalysisReportDaoTest { private static final String DEFAULT_PROJECT_KEY = "123456789-987654321"; - @ClassRule - public static DbTester db = new DbTester(); + System2 system2 = mock(System2.class); @Rule - public TemporaryFolder temp = new TemporaryFolder(); + public DbTester db = DbTester.create(system2); - AnalysisReportDao sut; - DbSession session; - System2 system2; + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + AnalysisReportDao sut = db.getDbClient().analysisReportDao(); @Before public void before() { - this.session = db.myBatis().openSession(false); - this.system2 = mock(System2.class); - this.sut = new AnalysisReportDao(system2); - when(system2.now()).thenReturn(1_500_000_000_000L); } - @After - public void after() { - MyBatis.closeQuietly(session); - } - @Test public void insert_multiple_reports() { db.prepareDbUnit(getClass(), "empty.xml"); @@ -76,9 +62,9 @@ public class AnalysisReportDaoTest { AnalysisReportDto report1 = new AnalysisReportDto().setProjectKey("ProjectKey1").setProjectName("Project 1").setUuid("UUID_1").setStatus(PENDING); AnalysisReportDto report2 = new AnalysisReportDto().setProjectKey("ProjectKey2").setProjectName("Project 2").setUuid("UUID_2").setStatus(PENDING); - sut.insert(session, report1); - sut.insert(session, report2); - session.commit(); + sut.insert(db.getSession(), report1); + sut.insert(db.getSession(), report2); + db.getSession().commit(); db.assertDbUnit(getClass(), "insert-result.xml", "analysis_reports"); } @@ -87,8 +73,8 @@ public class AnalysisReportDaoTest { public void resetAllToPendingStatus() { db.prepareDbUnit(getClass(), "update-all-to-status-pending.xml"); - sut.resetAllToPendingStatus(session); - session.commit(); + sut.resetAllToPendingStatus(db.getSession()); + db.getSession().commit(); db.assertDbUnit(getClass(), "update-all-to-status-pending-result.xml", "analysis_reports"); } @@ -97,8 +83,8 @@ public class AnalysisReportDaoTest { public void truncate() { db.prepareDbUnit(getClass(), "any-analysis-reports.xml"); - sut.truncate(session); - session.commit(); + sut.truncate(db.getSession()); + db.getSession().commit(); db.assertDbUnit(getClass(), "truncate-result.xml", "analysis_reports"); } @@ -108,7 +94,7 @@ public class AnalysisReportDaoTest { db.prepareDbUnit(getClass(), "select.xml"); final String projectKey = "123456789-987654321"; - List<AnalysisReportDto> reports = sut.selectByProjectKey(session, projectKey); + List<AnalysisReportDto> reports = sut.selectByProjectKey(db.getSession(), projectKey); AnalysisReportDto report = reports.get(0); assertThat(reports).hasSize(1); @@ -123,7 +109,7 @@ public class AnalysisReportDaoTest { db.prepareDbUnit(getClass(), "select.xml"); final String projectKey = "987654321-123456789"; - List<AnalysisReportDto> reports = sut.selectByProjectKey(session, projectKey); + List<AnalysisReportDto> reports = sut.selectByProjectKey(db.getSession(), projectKey); assertThat(reports).hasSize(2); } @@ -132,7 +118,7 @@ public class AnalysisReportDaoTest { public void pop_oldest_pending() { db.prepareDbUnit(getClass(), "pop_oldest_pending.xml"); - AnalysisReportDto nextAvailableReport = sut.pop(session); + AnalysisReportDto nextAvailableReport = sut.pop(db.getSession()); assertThat(nextAvailableReport.getId()).isEqualTo(3); assertThat(nextAvailableReport.getProjectKey()).isEqualTo("P2"); @@ -142,7 +128,7 @@ public class AnalysisReportDaoTest { public void pop_null_if_no_pending_reports() { db.prepareDbUnit(getClass(), "pop_null_if_no_pending_reports.xml"); - AnalysisReportDto nextAvailableReport = sut.pop(session); + AnalysisReportDto nextAvailableReport = sut.pop(db.getSession()); assertThat(nextAvailableReport).isNull(); } @@ -151,7 +137,7 @@ public class AnalysisReportDaoTest { public void getById_maps_all_the_fields_except_the_data() { db.prepareDbUnit(getClass(), "one_analysis_report.xml"); - AnalysisReportDto report = sut.selectById(session, 1L); + AnalysisReportDto report = sut.selectById(db.getSession(), 1L); assertThat(report.getProjectKey()).isEqualTo(DEFAULT_PROJECT_KEY); assertThat(report.getCreatedAt()).isEqualTo(1_500_000_000_001L); @@ -165,7 +151,7 @@ public class AnalysisReportDaoTest { public void getById_returns_null_when_id_not_found() { db.prepareDbUnit(getClass(), "select.xml"); - AnalysisReportDto report = sut.selectById(session, 4L); + AnalysisReportDto report = sut.selectById(db.getSession(), 4L); assertThat(report).isNull(); } @@ -174,8 +160,8 @@ public class AnalysisReportDaoTest { public void delete_one_analysis_report() { db.prepareDbUnit(getClass(), "one_analysis_report.xml"); - sut.delete(session, 1); - session.commit(); + sut.delete(db.getSession(), 1); + db.getSession().commit(); db.assertDbUnit(getClass(), "truncate-result.xml", "analysis_reports"); } @@ -184,7 +170,7 @@ public class AnalysisReportDaoTest { public void findAll_one_analysis_report() { db.prepareDbUnit(getClass(), "one_analysis_report.xml"); - List<AnalysisReportDto> reports = sut.selectAll(session); + List<AnalysisReportDto> reports = sut.selectAll(db.getSession()); assertThat(reports).hasSize(1); } @@ -193,7 +179,7 @@ public class AnalysisReportDaoTest { public void findAll_empty_table() { db.prepareDbUnit(getClass(), "empty.xml"); - List<AnalysisReportDto> reports = sut.selectAll(session); + List<AnalysisReportDto> reports = sut.selectAll(db.getSession()); assertThat(reports).isEmpty(); } @@ -202,7 +188,7 @@ public class AnalysisReportDaoTest { public void findAll_three_analysis_reports() { db.prepareDbUnit(getClass(), "three_analysis_reports.xml"); - List<AnalysisReportDto> reports = sut.selectAll(session); + List<AnalysisReportDto> reports = sut.selectAll(db.getSession()); assertThat(reports).hasSize(3); } diff --git a/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java b/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java index 8f320764923..2c1bb7f6415 100644 --- a/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java @@ -20,7 +20,7 @@ package org.sonar.db.dashboard; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.db.DbTester; @@ -31,8 +31,8 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class ActiveDashboardDaoTest { - @ClassRule - public static final DbTester dbTester = new DbTester(); + @Rule + public DbTester dbTester = new DbTester(); private ActiveDashboardDao dao; diff --git a/sonar-db/src/test/java/org/sonar/db/dashboard/DashboardDaoTest.java b/sonar-db/src/test/java/org/sonar/db/dashboard/DashboardDaoTest.java index bcb505fc176..98612b0a1b3 100644 --- a/sonar-db/src/test/java/org/sonar/db/dashboard/DashboardDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/dashboard/DashboardDaoTest.java @@ -21,7 +21,7 @@ package org.sonar.db.dashboard; import java.util.Date; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.db.DbTester; @@ -35,8 +35,8 @@ import static org.junit.Assert.assertThat; @Category(DbTests.class) public class DashboardDaoTest { - @ClassRule - public static final DbTester dbTester = new DbTester(); + @Rule + public DbTester dbTester = new DbTester(); private DashboardDao dao; diff --git a/sonar-db/src/test/java/org/sonar/db/debt/CharacteristicDaoTest.java b/sonar-db/src/test/java/org/sonar/db/debt/CharacteristicDaoTest.java index 504fe7d9246..4105328e632 100644 --- a/sonar-db/src/test/java/org/sonar/db/debt/CharacteristicDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/debt/CharacteristicDaoTest.java @@ -23,7 +23,7 @@ package org.sonar.db.debt; import java.util.List; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.utils.DateUtils; @@ -39,8 +39,8 @@ public class CharacteristicDaoTest { private static final String[] EXCLUDED_COLUMNS = new String[] {"id", "root_id", "rule_id", "function_key", "factor_unit", "factor_value", "offset_unit", "offset_value"}; - @ClassRule - public static DbTester db = new DbTester(); + @Rule + public DbTester db = new DbTester(); CharacteristicDao dao; diff --git a/sonar-db/src/test/java/org/sonar/db/event/EventDaoTest.java b/sonar-db/src/test/java/org/sonar/db/event/EventDaoTest.java index 4915c0e2291..f0910c39f91 100644 --- a/sonar-db/src/test/java/org/sonar/db/event/EventDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/event/EventDaoTest.java @@ -23,7 +23,7 @@ package org.sonar.db.event; import java.util.List; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.db.DbSession; @@ -35,8 +35,8 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class EventDaoTest { - @ClassRule - public static DbTester dbTester = new DbTester(); + @Rule + public DbTester dbTester = new DbTester(); DbSession session; diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java index 5867761b3b8..4eab62e294c 100644 --- a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java @@ -28,7 +28,7 @@ import java.util.Map; import javax.annotation.Nullable; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.db.DbSession; @@ -41,8 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class MeasureDaoTest { - @ClassRule - public static DbTester db = new DbTester(); + @Rule + public DbTester db = new DbTester(); DbSession session; MeasureDao sut; diff --git a/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipDaoTest.java index f5327223d39..ff4c4ba4875 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipDaoTest.java @@ -25,7 +25,7 @@ import java.util.Arrays; import java.util.List; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.db.DbSession; @@ -39,8 +39,8 @@ import static org.assertj.core.data.MapEntry.entry; @Category(DbTests.class) public class GroupMembershipDaoTest { - @ClassRule - public static final DbTester dbTester = new DbTester(); + @Rule + public DbTester dbTester = new DbTester(); private GroupMembershipDao dao; diff --git a/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java index df2470f74f7..24d2ebefcdd 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java @@ -22,7 +22,7 @@ package org.sonar.db.user; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.security.DefaultGroups; @@ -37,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class RoleDaoTest { - @ClassRule - public static final DbTester dbTester = new DbTester(); + @Rule + public DbTester dbTester = new DbTester(); DbSession session; diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserGroupDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserGroupDaoTest.java index ff1c8cfecef..fc600f98f09 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/UserGroupDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/UserGroupDaoTest.java @@ -22,7 +22,7 @@ package org.sonar.db.user; import org.junit.After; import org.junit.Before; -import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.db.DbSession; @@ -32,8 +32,8 @@ import org.sonar.test.DbTests; @Category(DbTests.class) public class UserGroupDaoTest { - @ClassRule - public static DbTester db = new DbTester(); + @Rule + public DbTester db = new DbTester(); private UserGroupDao dao; private DbSession session; |