HHH-11542 - Fix tests

This commit is contained in:
Andrea Boriero 2017-04-14 10:01:18 +02:00
parent 97942914c8
commit 71cfef8338
4 changed files with 121 additions and 2 deletions

View File

@ -17,7 +17,7 @@ import javax.sql.DataSource;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test; import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;

View File

@ -8,6 +8,7 @@ dependencies {
compile project( ':hibernate-core' ) compile project( ':hibernate-core' )
compile( libraries.hikaricp ) compile( libraries.hikaricp )
testCompile project( ':hibernate-testing' ) testCompile project( ':hibernate-testing' )
testCompile(libraries.mockito)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Java 9 ftw! // Java 9 ftw!

View File

@ -15,8 +15,8 @@ import javax.persistence.Id;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.testing.jdbc.PreparedStatementSpyConnectionProvider;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.util.PreparedStatementSpyConnectionProvider;
import org.junit.Test; import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;

View File

@ -0,0 +1,118 @@
/*
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.jdbc.ConnectionProviderDelegate;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.mockito.internal.util.MockUtil;
/**
* This {@link ConnectionProvider} extends any other ConnectionProvider that would be used by default taken the current configuration properties, and it
* intercept the underlying {@link PreparedStatement} method calls.
*
* @author Vlad Mihalcea
*/
public class PreparedStatementSpyConnectionProvider
extends ConnectionProviderDelegate {
private final Map<PreparedStatement, String> preparedStatementMap = new LinkedHashMap<>();
private final List<Connection> acquiredConnections = new ArrayList<>( );
private final List<Connection> releasedConnections = new ArrayList<>( );
public PreparedStatementSpyConnectionProvider() {
}
protected Connection actualConnection() throws SQLException {
return super.getConnection();
}
@Override
public Connection getConnection() throws SQLException {
Connection connection = spy( actualConnection() );
acquiredConnections.add( connection );
return connection;
}
@Override
public void closeConnection(Connection conn) throws SQLException {
acquiredConnections.remove( conn );
releasedConnections.add( conn );
super.closeConnection( conn );
}
@Override
public void stop() {
clear();
super.stop();
}
private Connection spy(Connection connection) {
if ( MockUtil.isMock( connection ) ) {
return connection;
}
Connection connectionSpy = Mockito.spy( connection );
try {
Mockito.doAnswer( invocation -> {
PreparedStatement statement = (PreparedStatement) invocation.callRealMethod();
PreparedStatement statementSpy = Mockito.spy( statement );
String sql = (String) invocation.getArguments()[0];
preparedStatementMap.put( statementSpy, sql );
return statementSpy;
} ).when( connectionSpy ).prepareStatement( ArgumentMatchers.anyString() );
Mockito.doAnswer( invocation -> {
Statement statement = (Statement) invocation.callRealMethod();
Statement statementSpy = Mockito.spy( statement );
return statementSpy;
} ).when( connectionSpy ).createStatement();
}
catch ( SQLException e ) {
throw new IllegalArgumentException( e );
}
return connectionSpy;
}
/**
* Clears the recorded PreparedStatements and reset the associated Mocks.
*/
public void clear() {
acquiredConnections.clear();
releasedConnections.clear();
preparedStatementMap.keySet().forEach( Mockito::reset );
preparedStatementMap.clear();
}
/**
* Get a list of current acquired Connections.
* @return list of current acquired Connections
*/
public List<Connection> getAcquiredConnections() {
return acquiredConnections;
}
/**
* Get a list of current released Connections.
* @return list of current released Connections
*/
public List<Connection> getReleasedConnections() {
return releasedConnections;
}
}