]> source.dussan.org Git - sonarqube.git/commitdiff
More stable dao tests
authorDavid Gageot <david@gageot.net>
Wed, 4 Jul 2012 12:11:49 +0000 (14:11 +0200)
committerDavid Gageot <david@gageot.net>
Wed, 4 Jul 2012 12:11:49 +0000 (14:11 +0200)
sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java
sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java

index a6528c11172c22157af6e09b7295e51d4af38bd8..a9690c06edbaa664cb470db3650b4f7303a910c5 100644 (file)
@@ -51,7 +51,7 @@ public class H2Database implements Database {
   /**
    * IMPORTANT: DB name changed from "sonar" to "sonar2" in order to not conflict with {@link DefaultDatabaseTest}
    */
-  void startDatabase() {
+  private void startDatabase() {
     try {
       Properties properties = new Properties();
       properties.put("driverClassName", "org.h2.Driver");
@@ -69,7 +69,7 @@ public class H2Database implements Database {
     }
   }
 
-  void createSchema() {
+  private void createSchema() {
     Connection connection = null;
     try {
       connection = datasource.getConnection();
@@ -77,11 +77,17 @@ public class H2Database implements Database {
     } catch (SQLException e) {
       throw new IllegalStateException("Fail to create schema", e);
     } finally {
-      closeQuietly(connection);
+      if (connection != null) {
+        try {
+          connection.close();
+        } catch (SQLException e) {
+          // ignore
+        }
+      }
     }
   }
 
-  public static void stopDatabase() {
+  public H2Database stop() {
     try {
       if (datasource != null) {
         datasource.close();
@@ -90,9 +96,6 @@ public class H2Database implements Database {
     } catch (SQLException e) {
       // Ignore error
     }
-  }
-
-  public H2Database stop() {
     return this;
   }
 
@@ -115,15 +118,4 @@ public class H2Database implements Database {
     properties.put(Environment.CONNECTION_PROVIDER, CustomHibernateConnectionProvider.class.getName());
     return properties;
   }
-
-  private static void closeQuietly(Connection connection) {
-    if (connection != null) {
-      try {
-        connection.close();
-      } catch (SQLException e) {
-        // ignore
-      }
-    }
-  }
-
 }
index e815dd452e73ba5e375b4b76e585f102e6f79764..f08c05f68550bb9810d73d62fddc6f90d9829e39 100644 (file)
 package org.sonar.core.persistence;
 
 import org.apache.commons.dbcp.BasicDataSource;
-import org.hamcrest.core.Is;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
-import static org.hamcrest.number.OrderingComparisons.greaterThan;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
 
 public class H2DatabaseTest {
+  H2Database db = new H2Database();
+
+  @Before
+  public void startDb() {
+    db.start();
+  }
+
+  @Before
+  public void stopDb() {
+    db.stop();
+  }
 
   @Test
   public void shouldExecuteDdlAtStartup() throws SQLException {
+    Connection connection = db.getDataSource().getConnection();
+
     int tables = 0;
-    H2Database db = new H2Database();
-    try {
-      db.start();
-      assertNotNull(db.getDataSource());
-      Connection connection = db.getDataSource().getConnection();
-      assertNotNull(connection);
+    ResultSet resultSet = connection.getMetaData().getTables(null, null, null, new String[] {"TABLE"});
+    while (resultSet.next()) {
+      tables++;
+    }
 
-      ResultSet resultSet = connection.getMetaData().getTables("", null, null, new String[] {"TABLE"});
-      while (resultSet.next()) {
-        tables++;
-      }
+    connection.close();
 
-    } finally {
-      db.stop();
-    }
-    assertThat(tables, greaterThan(30));
+    assertThat(tables).isGreaterThan(30);
   }
 
   @Test
   public void shouldLimitThePoolSize() {
-    H2Database db = new H2Database();
-    try {
-      db.startDatabase();
-      assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(2));
-      assertThat(((BasicDataSource) db.getDataSource()).getMaxIdle(), Is.is(2));
-
-    } finally {
-      db.stop();
-    }
+    assertThat(((BasicDataSource) db.getDataSource()).getMaxActive()).isEqualTo(2);
+    assertThat(((BasicDataSource) db.getDataSource()).getMaxIdle()).isEqualTo(2);
   }
 }
index 14d1e9820b9ba94c725776b29f713f1982188719..9341f511b371f44885618fe24e2558e8be5d54df 100644 (file)
@@ -50,7 +50,7 @@ public class DatabaseSessionTest extends AbstractDbUnitTestCase {
   }
 
   @Test
-  public void performaceTestOnBatchInserts() throws Exception {
+  public void performanceTestOnBatchInserts() throws Exception {
     getSession().save(project1);
     Snapshot snapshot = new Snapshot(project1, true, "", new Date(1));
     getSession().save(snapshot);