From: Simon Brandhof Date: Mon, 6 Jul 2015 13:05:53 +0000 (+0200) Subject: Fix execution of tests on non-H2 databases X-Git-Tag: 5.2-RC1~1210 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=34ac5d84f639df6643a7976593c6f00b4283f04b;p=sonarqube.git Fix execution of tests on non-H2 databases --- 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..498f5f46eec 100644 --- a/sonar-db/src/test/java/org/sonar/db/DbTester.java +++ b/sonar-db/src/test/java/org/sonar/db/DbTester.java @@ -96,7 +96,7 @@ public class DbTester extends ExternalResource { @Override protected void before() throws Throwable { - truncateTables(); + db.start(); } @Override @@ -104,7 +104,7 @@ public class DbTester extends ExternalResource { if (session != null) { MyBatis.closeQuietly(session); } - db.close(); + db.stop(); } public DbSession getSession() { @@ -323,8 +323,8 @@ public class DbTester extends ExternalResource { public void assertColumnDefinition(String table, String column, int expectedType, @Nullable Integer expectedSize) { try (Connection connection = db.getDatabase().getDataSource().getConnection(); - PreparedStatement stmt = connection.prepareStatement("select * from " + table); - ResultSet res = stmt.executeQuery()) { + PreparedStatement stmt = connection.prepareStatement("select * from " + table); + ResultSet res = stmt.executeQuery()) { Integer columnIndex = getColumnIndex(res, column); if (columnIndex == null) { fail("The column '" + column + "' does not exist"); @@ -336,7 +336,7 @@ public class DbTester extends ExternalResource { } } catch (Exception e) { - throw new IllegalStateException("Fail to check column"); + throw new IllegalStateException("Fail to check column", e); } } @@ -353,7 +353,7 @@ public class DbTester extends ExternalResource { return null; } catch (Exception e) { - throw new IllegalStateException("Fail to get column idnex"); + throw new IllegalStateException("Fail to get column index", e); } } @@ -387,6 +387,7 @@ public class DbTester extends ExternalResource { } } catch (SQLException e) { // ignore + e.printStackTrace(); } } @@ -419,8 +420,4 @@ public class DbTester extends ExternalResource { return db.getDatabase(); } - public DatabaseCommands getCommands() { - return db.getCommands(); - } - } 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..93509a42fab 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 @@ -91,11 +92,11 @@ class TestDb { db.start(); if (schemaPath != null) { // will fail if not H2 - if (db.getDialect().getId().equals("h2")) { + if (H2.ID.equals(db.getDialect().getId())) { ((H2Database) db).executeScript(schemaPath); } else { db.stop(); - throw new AssumptionViolatedException("Test disabled because it supports only H2"); + } } isDefault = (schemaPath == null); @@ -109,6 +110,12 @@ class TestDb { } } + void start() { + if (!isDefault && !H2.ID.equals(db.getDialect().getId())) { + throw new AssumptionViolatedException("Test disabled because it supports only H2"); + } + } + void truncateTables() { try { commands.truncateDatabase(db.getDataSource()); @@ -117,7 +124,7 @@ class TestDb { } } - void close() { + void stop() { if (!isDefault) { db.stop(); }