From dbc6400e060db1ad3f424b01f7a42a08202a67d6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 5 Nov 2014 12:11:16 +0100 Subject: [PATCH] Fix management of dbunit connection --- .../core/persistence/AbstractDaoTestCase.java | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java b/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java index 6052700cbf8..cd94144c676 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java @@ -87,7 +87,6 @@ public abstract class AbstractDaoTestCase { private static DatabaseCommands databaseCommands; private static IDatabaseTester databaseTester; private static MyBatis myBatis; - private static IDatabaseConnection connection; private WorkQueue queue = new NullQueue(); @Before @@ -121,16 +120,6 @@ public abstract class AbstractDaoTestCase { myBatis.start(); } - - if (connection == null || connection.getConnection().isClosed()) { - connection = databaseTester.getConnection(); - connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, databaseCommands.getDbUnitFactory()); - if (MySql.ID.equals(database.getDialect().getId())) { - connection.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, false); - connection.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler()); - } - } - databaseCommands.truncateDatabase(database.getDataSource()); } @@ -214,6 +203,7 @@ public abstract class AbstractDaoTestCase { } protected void checkTables(String testName, String[] excludedColumnNames, String... tables) { + IDatabaseConnection connection = openDbUnitConnection(); try { IDataSet dataSet = connection.createDataSet(); IDataSet expectedDataSet = getExpectedData(testName); @@ -226,12 +216,14 @@ public abstract class AbstractDaoTestCase { fail(e.getMessage()); } catch (SQLException e) { throw translateException("Error while checking results", e); + } finally { + closeDbUnitConnection(connection); } } protected void checkTable(String testName, String table, String... columns) { + IDatabaseConnection connection = openDbUnitConnection(); try { - IDataSet dataSet = connection.createDataSet(); IDataSet expectedDataSet = getExpectedData(testName); ITable filteredTable = DefaultColumnFilter.includedColumnsTable(dataSet.getTable(table), columns); @@ -241,12 +233,14 @@ public abstract class AbstractDaoTestCase { fail(e.getMessage()); } catch (SQLException e) { throw translateException("Error while checking results", e); + } finally { + closeDbUnitConnection(connection); } } protected void assertEmptyTables(String... emptyTables) { + IDatabaseConnection connection = openDbUnitConnection(); try { - IDataSet dataSet = connection.createDataSet(); for (String table : emptyTables) { try { @@ -257,6 +251,30 @@ public abstract class AbstractDaoTestCase { } } catch (SQLException e) { throw translateException("Error while checking results", e); + } finally { + closeDbUnitConnection(connection); + } + } + + private IDatabaseConnection openDbUnitConnection() { + try { + IDatabaseConnection connection = databaseTester.getConnection(); + connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, databaseCommands.getDbUnitFactory()); + if (MySql.ID.equals(database.getDialect().getId())) { + connection.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, false); + connection.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler()); + } + return connection; + } catch (Exception e) { + throw new IllegalStateException("Fail to open dbunit connection", e); + } + } + + private void closeDbUnitConnection(IDatabaseConnection c) { + try { + c.close(); + } catch (SQLException e) { + throw new IllegalStateException("Fail to close dbunit connection", e); } } -- 2.39.5