]> source.dussan.org Git - sonarqube.git/commitdiff
Fix execution of tests on non-H2 databases
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 6 Jul 2015 13:05:53 +0000 (15:05 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 6 Jul 2015 13:05:53 +0000 (15:05 +0200)
sonar-db/src/test/java/org/sonar/db/DbTester.java
sonar-db/src/test/java/org/sonar/db/TestDb.java

index 30bc65fa1e364e0d30b292b2f37c09170d52cae8..498f5f46eec98ba1f9186f90372fd65f6173a164 100644 (file)
@@ -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();
-  }
-
 }
index 3ab4fc413463c195756ba8d79a66dd01be31bd5f..93509a42fab4c09dd21c80140c4498e65528137d 100644 (file)
@@ -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();
     }