All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.testing.jdbc.SQLStatementInterceptor Maven / Gradle / Ivy

There is a newer version: 5.6.15.Final
Show newest version
/*
 * 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();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy