org.hibernate.testing.jdbc.SQLStatementInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-testing-jakarta Show documentation
Show all versions of hibernate-testing-jakarta Show documentation
Support for testing Hibernate ORM Jakarta functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.testing.jdbc;
import java.util.LinkedList;
import java.util.Map;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.resource.jdbc.spi.StatementInspector;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Vlad Mihalcea
*/
public class SQLStatementInterceptor {
private final LinkedList sqlQueries = new LinkedList<>();
public SQLStatementInterceptor(SessionFactoryBuilder sessionFactoryBuilder) {
sessionFactoryBuilder.applyStatementInspector( (StatementInspector) sql -> {
sqlQueries.add( sql );
return sql;
} );
}
public SQLStatementInterceptor(Map settings) {
settings.put( AvailableSettings.STATEMENT_INSPECTOR, (StatementInspector) sql -> {
sqlQueries.add( sql );
return sql;
} );
}
public SQLStatementInterceptor(StandardServiceRegistryBuilder ssrb) {
ssrb.applySetting(
AvailableSettings.STATEMENT_INSPECTOR,
(StatementInspector) sql -> {
sqlQueries.add( sql );
return sql;
}
);
}
public SQLStatementInterceptor(Configuration configuration) {
this( configuration.getProperties() );
}
public LinkedList getSqlQueries() {
return sqlQueries;
}
public void clear() {
sqlQueries.clear();
}
public void assertExecuted(String expected) {
assertTrue(sqlQueries.contains( expected ));
}
public void assertExecutedCount(int expected) {
assertEquals(expected, sqlQueries.size());
}
public int getQueryCount() {
return sqlQueries.size();
}
}