/**
* 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");
}
}
- void createSchema() {
+ private void createSchema() {
Connection connection = null;
try {
connection = datasource.getConnection();
} 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();
} catch (SQLException e) {
// Ignore error
}
- }
-
- public H2Database stop() {
return this;
}
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
- }
- }
- }
-
}
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);
}
}