Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<!-- Database driver versions -->
<db2DriverVersion>12.1.5.0</db2DriverVersion>
<derbyDriverVersion>10.14.2.0</derbyDriverVersion>
<h2DriverVersion>1.4.200</h2DriverVersion>
<h2DriverVersion>2.4.240</h2DriverVersion>
<hsqldbDriverVersion>2.7.4</hsqldbDriverVersion>
<mariadbDriverVersion>3.5.3</mariadbDriverVersion>
<mysqlDriverVersion>8.0.31</mysqlDriverVersion>
Expand Down
6 changes: 6 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@
<action dev="jeffjensen" type="add" issue="921" system="github" due-to="jeffjensen">
Add IsActualEqualToExpectedJsonValueComparer, a ValueComparer that parses expected and actual column values as JSON and compares the resulting document trees instead of their raw text: object member order is ignored while array element order stays significant, matching JSON's own equality semantics. Prompted by reviewing a Stack Overflow report of DbUnit failing on a MySQL JSON column; MySQL Connector/J already reports native JSON columns as Types.LONGVARCHAR, which DbUnit's existing StringDataType handles for reads/writes with no DataTypeFactory change needed, but MySQL (like PostgreSQL jsonb and H2 JSON) reformats the text on storage - sorting object keys and stripping insignificant whitespace - so a literal string comparison against an expected dataset value spuriously fails even when the JSON is semantically identical. Not exposed as a ValueComparers constant, since that class eagerly instantiates every constant it declares and would force the optional jackson-databind dependency onto every consumer, not only those comparing JSON columns.
</action>
<action dev="jeffjensen" type="add" issue="923" system="github" due-to="jeffjensen">
Add support for H2 2.x, whose rewritten, SQL-standard-compliant INFORMATION_SCHEMA reports most of its internal tables with the same JDBC TABLE_TYPE ("BASE TABLE") as real user tables instead of the "SYSTEM TABLE" type H2 1.x used, so those 15 tables (CONSTANTS, ENUM_VALUES, INDEXES, INDEX_COLUMNS, INFORMATION_SCHEMA_CATALOG_NAME, IN_DOUBT, LOCKS, QUERY_STATISTICS, RIGHTS, ROLES, SESSIONS, SESSION_STATE, SETTINGS, SYNONYMS, USERS) now pass DatabaseConfig#PROPERTY_TABLE_TYPE's default {"TABLE"} filter and leak into DatabaseDataSet#getTableNames()/createDataSet() whenever a connection queries without a fixed schema (schema is null, e.g. an admin/multi-schema connection with FEATURE_QUALIFIED_TABLE_NAMES). Bump h2DriverVersion from 1.4.200 to 2.4.240 and add H2MetadataHandler (wired into H2Connection) whose getTables() excludes the INFORMATION_SCHEMA schema; it builds the filtered result via a new InMemoryMetadataResultSet#filter(ResultSet, RowFilter) factory, generalized from the merge() factory MultiSchemaMySqlMetadataHandler already used (now a standalone org.dbunit.database class rather than that handler's private nested one).
</action>
<action dev="jeffjensen" type="add" issue="923" system="github" due-to="jeffjensen">
Add the IMetadataHandler Core Components page (components/imetadatahandler.adoc): the interface's method groups, a built-in-implementations table (DefaultMetadataHandler, Db2/MySql/MultiSchemaMySql/Netezza/H2MetadataHandler), and InMemoryMetadataResultSet's two factories (merge() for combining several real result sets, filter() for dropping rows out of one) with guidance on writing a custom handler. Update databases.adoc, databases/h2.adoc, properties.adoc, and components.adoc to reflect H2MetadataHandler and cross-reference the new page; properties.adoc's metadataHandler entry also gains the previously-undocumented MultiSchemaMySqlMetadataHandler. Add a Test Conventions section (codingstandards/testconventions.adoc) on preferring a delegating wrapper over subclassing a concrete vendor handler for test doubles, using DatabaseDataSet_MultiSchemaTest's TestMetadataHandler as the worked example.
</action>
</release>
<release version="3.4.0" date="Jul 28, 2026" description="Test-suite hardening (un-skip and strengthen dozens of disabled/no-op tests); add CachingConnectionProvider and reduce DefaultPrepAndExpectedTestCase's per-test connection churn; pin identifier case-folding to Locale.ENGLISH for Turkish-locale correctness; and a broad set of correctness fixes across export formats (XML, YAML, CSV, XLS, Ant), TimestampDataType timezone handling, InsertOperation/TransactionOperation, and resource-leak cleanups">
<action dev="jeffjensen" type="fix" issue="797" system="github" due-to="jeffjensen">
Expand Down
279 changes: 279 additions & 0 deletions src/main/java/org/dbunit/database/InMemoryMetadataResultSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
/*
*
* The DbUnit Database Testing Framework
* Copyright (C)2002-2026, DbUnit.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

package org.dbunit.database;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.dbunit.util.SQLHelper;

/**
* An in-memory {@link ResultSet}, backed by rows copied out of one or more source result sets
* ahead of time. It supports only the handful of {@link ResultSet}/{@link ResultSetMetaData}
* methods dbunit itself calls against an {@link IMetadataHandler} result - {@code next()},
* {@code getString(int/String)}, {@code getInt(int/String)}, {@code getMetaData()}/
* {@code getColumnCount()}, {@code close()}, plus {@code equals()}/{@code hashCode()}/
* {@code toString()} for safe use as a log argument or map key - since implementing the rest of
* {@link ResultSet}'s ~150 methods would serve no caller. Any other method throws
* {@link UnsupportedOperationException}.
* <p>
* A single instance answers both {@link ResultSet} calls and, since {@code getMetaData()}
* returns the proxy itself, the {@link ResultSetMetaData} calls made against its result.
*
* @since 3.4.1
*/
public final class InMemoryMetadataResultSet implements InvocationHandler
{
/**
* Tests whether a source result set's current row, as positioned by a preceding
* {@link ResultSet#next()}, should be kept.
*
* @since 3.4.1
*/
@FunctionalInterface
public interface RowFilter
{
/**
* Tests the row the given result set is currently positioned on.
*
* @param resultSet The source result set, positioned on the row to test.
* @return {@code true} if the row should be kept.
* @throws SQLException if a database access error occurs.
*/
boolean accept(ResultSet resultSet) throws SQLException;
}

private final List<Object[]> rows;
private final int columnCount;
private final Map<String, Integer> columnIndexByLabel;
private int cursor = -1;

private InMemoryMetadataResultSet(final List<Object[]> rows, final int columnCount,
final Map<String, Integer> columnIndexByLabel)
{
this.rows = rows;
this.columnCount = columnCount;
this.columnIndexByLabel = columnIndexByLabel;
}

/**
* Copies every row of each given result set, closing each as it is consumed, and returns a
* single merged {@link ResultSet} positioned before the first row.
*
* @param sources The result sets to merge, in the order their rows should appear.
* @return The merged result set.
* @throws SQLException if a source result set cannot be read.
*/
public static ResultSet merge(final List<ResultSet> sources) throws SQLException
{
return copy(sources, null);
}

/**
* Copies the rows of the given result set that match the given filter, closing it as it is
* consumed, and returns a filtered {@link ResultSet} positioned before the first row.
*
* @param source The result set to filter.
* @param filter The filter a row must match to be kept.
* @return The filtered result set.
* @throws SQLException if the source result set cannot be read.
*/
public static ResultSet filter(final ResultSet source, final RowFilter filter)
throws SQLException
{
return copy(Collections.singletonList(source), filter);
}

private static ResultSet copy(final List<ResultSet> sources, final RowFilter filter)
throws SQLException
{
final List<Object[]> rows = new ArrayList<Object[]>();
final Map<String, Integer> columnIndexByLabel = new HashMap<String, Integer>();
int columnCount = 0;
boolean first = true;
try
{
for (final ResultSet source : sources)
{
if (first)
{
final ResultSetMetaData metaData = source.getMetaData();
columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++)
{
columnIndexByLabel.put(
metaData.getColumnLabel(i).toUpperCase(Locale.ENGLISH), i);
}
first = false;
}
while (source.next())
{
if (filter != null && !filter.accept(source))
{
continue;
}
final Object[] row = new Object[columnCount];
for (int i = 1; i <= columnCount; i++)
{
row[i - 1] = source.getObject(i);
}
rows.add(row);
}
}
}
finally
{
closeAll(sources);
}

final InMemoryMetadataResultSet handler =
new InMemoryMetadataResultSet(rows, columnCount, columnIndexByLabel);
return (ResultSet) Proxy.newProxyInstance(InMemoryMetadataResultSet.class.getClassLoader(),
new Class<?>[] {ResultSet.class, ResultSetMetaData.class}, handler);
}

/**
* Closes every result set in the given list, null- and already-closed-safe. Closing one
* result set is attempted even if closing an earlier one in the list failed, so a single
* failure does not leak the rest.
*
* @param resultSets The result sets to close.
* @throws SQLException the first failure encountered while closing, if any.
*/
private static void closeAll(final List<ResultSet> resultSets) throws SQLException
{
SQLException firstFailure = null;
for (final ResultSet resultSet : resultSets)
{
try
{
SQLHelper.close(resultSet);
}
catch (final SQLException e)
{
if (firstFailure == null)
{
firstFailure = e;
}
}
}
if (firstFailure != null)
{
throw firstFailure;
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/**
* {@inheritDoc}
* Answers exactly the methods documented on this class; any other method throws
* {@link UnsupportedOperationException}.
*/
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args)
{
final String name = method.getName();
if ("next".equals(name))
{
cursor++;
return cursor < rows.size();
}
if ("getString".equals(name))
{
final Object value = currentValue(args[0]);
return value == null ? null : String.valueOf(value);
}
if ("getInt".equals(name))
{
final Object value = currentValue(args[0]);
return value == null ? 0 : toInt(value);
}
if ("getMetaData".equals(name))
{
return proxy;
}
if ("getColumnCount".equals(name))
{
return columnCount;
}
if ("close".equals(name))
{
return null;
}
if ("toString".equals(name))
{
return "InMemoryMetadataResultSet[rows=" + rows.size() + "]";
}
if ("hashCode".equals(name))
{
return System.identityHashCode(proxy);
}
if ("equals".equals(name))
{
return proxy == args[0];
}
throw new UnsupportedOperationException(
"InMemoryMetadataResultSet does not support " + name + "()");
}

private Object currentValue(final Object columnArg)
{
if (cursor < 0 || cursor >= rows.size())
{
throw new IllegalStateException("ResultSet is not positioned on a valid row.");
}
return rows.get(cursor)[columnIndex(columnArg) - 1];
}

private int columnIndex(final Object columnArg)
{
if (columnArg instanceof Integer)
{
return (Integer) columnArg;
}
final String label = String.valueOf(columnArg).toUpperCase(Locale.ENGLISH);
final Integer index = columnIndexByLabel.get(label);
if (index == null)
{
throw new IllegalArgumentException("Unknown column '" + columnArg + "'.");
}
return index;
}

private static int toInt(final Object value)
{
if (value instanceof Number)
{
return ((Number) value).intValue();
}
return Integer.parseInt(String.valueOf(value));
}
}
5 changes: 4 additions & 1 deletion src/main/java/org/dbunit/ext/h2/H2Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
public class H2Connection extends DatabaseConnection
{
/**
* Creates an H2 connection, pre-configuring the H2-specific data type factory.
* Creates an H2 connection, pre-configuring the H2-specific data type factory and metadata
* handler.
*
* @param connection the adapted JDBC connection.
* @param schema the database schema.
Expand All @@ -49,5 +50,7 @@ public H2Connection(Connection connection, String schema) throws DatabaseUnitExc
super(connection, schema);
getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new H2DataTypeFactory());
getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER,
new H2MetadataHandler());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
61 changes: 61 additions & 0 deletions src/main/java/org/dbunit/ext/h2/H2MetadataHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
* The DbUnit Database Testing Framework
* Copyright (C)2002-2026, DbUnit.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

package org.dbunit.ext.h2;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.dbunit.database.DefaultMetadataHandler;
import org.dbunit.database.InMemoryMetadataResultSet;

/**
* Special metadata handler for H2.
* <p>
* H2 2.x rewrote {@code INFORMATION_SCHEMA} to be SQL-standard-compliant, and most of its internal
* tables now report a JDBC {@code TABLE_TYPE} of {@code "BASE TABLE"} instead of the
* {@code "SYSTEM TABLE"} type H2 1.x used. Since
* {@link org.dbunit.database.DatabaseConfig#PROPERTY_TABLE_TYPE} defaults to {@code {"TABLE"}},
* those tables now pass dbunit's default system-table filter and leak into table listings that are
* not scoped to a single schema (i.e. {@code schemaName} is {@code null}). This handler excludes
* the {@code INFORMATION_SCHEMA} schema from {@link #getTables} results to restore the pre-2.x
* behavior.
*
* @since 3.4.1
*/
public class H2MetadataHandler extends DefaultMetadataHandler
{
private static final String INFORMATION_SCHEMA = "INFORMATION_SCHEMA";

/**
* {@inheritDoc}
* Excludes tables belonging to the {@code INFORMATION_SCHEMA} schema.
*/
@Override
public ResultSet getTables(final DatabaseMetaData metaData, final String schemaName,
final String[] tableType) throws SQLException
{
final ResultSet resultSet = super.getTables(metaData, schemaName, tableType);
return InMemoryMetadataResultSet.filter(resultSet,
row -> !INFORMATION_SCHEMA.equalsIgnoreCase(getSchema(row)));
}
}
Loading
Loading