diff --git a/pom.xml b/pom.xml index 4626a09..67a1684 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.dbunit dbunit - 2.4.9 + 2.4.9_Urbancode jar dbUnit Extension http://dbunit.sourceforge.net diff --git a/src/main/java/org/dbunit/database/DatabaseTableMetaData.java b/src/main/java/org/dbunit/database/DatabaseTableMetaData.java index 4702263..255053e 100644 --- a/src/main/java/org/dbunit/database/DatabaseTableMetaData.java +++ b/src/main/java/org/dbunit/database/DatabaseTableMetaData.java @@ -121,7 +121,7 @@ public class DatabaseTableMetaData extends AbstractTableMetaData Connection jdbcConnection = connection.getConnection(); if(!caseSensitiveMetaData) { - _originalTableName = SQLHelper.correctCase(tableName, jdbcConnection); + _originalTableName = SQLHelper.correctCase(tableName, jdbcConnection, connection.getSchema()); SQLHelper.logDebugIfValueChanged(tableName, _originalTableName, "Corrected table name:", DatabaseTableMetaData.class); } else diff --git a/src/main/java/org/dbunit/util/SQLHelper.java b/src/main/java/org/dbunit/util/SQLHelper.java index d414d97..60a780b 100644 --- a/src/main/java/org/dbunit/util/SQLHelper.java +++ b/src/main/java/org/dbunit/util/SQLHelper.java @@ -527,6 +527,10 @@ public class SQLHelper { } } } + + public static final String correctCase(final String databaseIdentifier, Connection connection) { + return correctCase(databaseIdentifier, connection, null); + } /** * Corrects the case of the given String according to the way in which the database stores metadata. @@ -537,13 +541,13 @@ public class SQLHelper { * @return The database identifier in the correct case for the RDBMS * @since 2.4.4 */ - public static final String correctCase(final String databaseIdentifier, Connection connection) + public static final String correctCase(final String databaseIdentifier, Connection connection, String schemaName) { logger.trace("correctCase(tableName={}, connection={}) - start", databaseIdentifier, connection); try { - return correctCase(databaseIdentifier, connection.getMetaData()); + return correctCase(databaseIdentifier, connection.getMetaData(), schemaName); } catch (SQLException e) { @@ -551,6 +555,10 @@ public class SQLHelper { } } + public static final String correctCase(final String databaseIdentifier, DatabaseMetaData databaseMetaData) { + return correctCase(databaseIdentifier, databaseMetaData, null); + } + /** * Corrects the case of the given String according to the way in which the database stores metadata. * @param databaseIdentifier A database identifier such as a table name or a schema name for @@ -560,7 +568,7 @@ public class SQLHelper { * @return The database identifier in the correct case for the RDBMS * @since 2.4.4 */ - public static final String correctCase(final String databaseIdentifier, DatabaseMetaData databaseMetaData) + public static final String correctCase(final String databaseIdentifier, DatabaseMetaData databaseMetaData, String schemaName) { logger.trace("correctCase(tableName={}, databaseMetaData={}) - start", databaseIdentifier, databaseMetaData); @@ -585,6 +593,30 @@ public class SQLHelper { { resultTableName = databaseIdentifier.toUpperCase(Locale.ENGLISH); } + else if(databaseMetaData.storesMixedCaseIdentifiers()) + { + // UrbanCode addition - makes the following assumptions: + // 1) We don't have any case-ambiguous table names + // (Aka, doesn't contain both tables "EXAMPLE_TABLE" and "example_table") + // 2) Tables are not stored in mixed-case. Just uppercase or lowercase. + ResultSet tables; + tables = databaseMetaData.getTables(null, schemaName, databaseIdentifier, null); + boolean tableFound = tables.next(); + + if (!tableFound) { + tables = databaseMetaData.getTables(null, schemaName, databaseIdentifier.toLowerCase(Locale.ENGLISH), null); + tableFound = tables.next(); + } + + if (!tableFound) { + tables = databaseMetaData.getTables(null, schemaName, databaseIdentifier.toUpperCase(Locale.ENGLISH), null); + tableFound = tables.next(); + } + + if (tableFound) { + resultTableName = tables.getString("TABLE_NAME"); + } + } else { logger.debug("Database does not store upperCase or lowerCase identifiers. " +