From: David Gageot Date: Wed, 4 Jul 2012 12:11:49 +0000 (+0200) Subject: More stable dao tests X-Git-Tag: 3.2~261 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=af3aaeb7c44f54df973d9922c795386b2a4feb17;p=sonarqube.git More stable dao tests --- diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java index a6528c11172..a9690c06edb 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java @@ -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 - } - } - } - } diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java index e815dd452e7..f08c05f6855 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java @@ -20,50 +20,46 @@ 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); } } diff --git a/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java b/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java index 14d1e9820b9..9341f511b37 100644 --- a/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java +++ b/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java @@ -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);