Skip to content

Commit

Permalink
Improve the PreparedStatement assertion mechanism to rely on Mockito …
Browse files Browse the repository at this point in the history
…solely
  • Loading branch information
vladmihalcea committed Jun 14, 2016
1 parent be93105 commit 6142f92
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 152 deletions.
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.test.jdbc.internal;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.Id;
Expand All @@ -14,18 +16,19 @@
import org.hibernate.cfg.AvailableSettings;

import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.util.JdbcStatisticsConnectionProvider;
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* @author Vlad Mihalcea
*/
public class SessionJdbcBatchTest
extends BaseNonConfigCoreFunctionalTestCase {

private JdbcStatisticsConnectionProvider connectionProvider = new JdbcStatisticsConnectionProvider();
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();

@Override
protected Class<?>[] getAnnotatedClasses() {
Expand Down Expand Up @@ -54,26 +57,25 @@ protected boolean isCleanupTestDataRequired() {
private long id;

@Test
public void testSessionFactorySetting() {
public void testSessionFactorySetting() throws SQLException {
Session session = sessionFactory().openSession();
session.beginTransaction();
try {
addEvents( session );
}
finally {
connectionProvider.getPreparedStatementStatistics().clear();
connectionProvider.clear();
session.getTransaction().commit();
session.close();
}
JdbcStatisticsConnectionProvider.PreparedStatementStatistics statementStatistics =
connectionProvider.getPreparedStatementStatistics().get(
"insert into Event (name, id) values (?, ?)" );
assertEquals( 5, statementStatistics.getAddBatchCount() );
assertEquals( 3, statementStatistics.getExecuteBatchCount() );
PreparedStatement preparedStatement = connectionProvider.getPreparedStatement( "insert into Event (name, id) values (?, ?)" );
verify(preparedStatement, times( 5 )).addBatch();
verify(preparedStatement, times( 3 )).executeBatch();
}

@Test
public void testSessionSettingOverridesSessionFactorySetting() {
public void testSessionSettingOverridesSessionFactorySetting()
throws SQLException {
Session session = sessionFactory().openSession();
session.setJdbcBatchSize( 3 );
session.beginTransaction();
Expand All @@ -85,11 +87,10 @@ public void testSessionSettingOverridesSessionFactorySetting() {
session.getTransaction().commit();
session.close();
}
JdbcStatisticsConnectionProvider.PreparedStatementStatistics statementStatistics =
connectionProvider.getPreparedStatementStatistics().get(
"insert into Event (name, id) values (?, ?)" );
assertEquals( 5, statementStatistics.getAddBatchCount() );
assertEquals( 2, statementStatistics.getExecuteBatchCount() );

PreparedStatement preparedStatement = connectionProvider.getPreparedStatement( "insert into Event (name, id) values (?, ?)" );
verify(preparedStatement, times( 5 )).addBatch();
verify(preparedStatement, times( 2 )).executeBatch();

session = sessionFactory().openSession();
session.setJdbcBatchSize( null );
Expand All @@ -102,11 +103,9 @@ public void testSessionSettingOverridesSessionFactorySetting() {
session.getTransaction().commit();
session.close();
}
statementStatistics =
connectionProvider.getPreparedStatementStatistics().get(
"insert into Event (name, id) values (?, ?)" );
assertEquals( 5, statementStatistics.getAddBatchCount() );
assertEquals( 3, statementStatistics.getExecuteBatchCount() );
preparedStatement = connectionProvider.getPreparedStatement( "insert into Event (name, id) values (?, ?)" );
verify(preparedStatement, times( 5 )).addBatch();
verify(preparedStatement, times( 3 )).executeBatch();
}

private void addEvents(Session session) {
Expand All @@ -118,20 +117,6 @@ private void addEvents(Session session) {
}
}

@Test
public void testSessionJdbcBatchOverridesSessionFactorySetting() {

Session session = sessionFactory().openSession();
session.beginTransaction();
try {

}
finally {
session.getTransaction().commit();
session.close();
}
}

@Entity(name = "Event")
public static class Event {

Expand Down
Expand Up @@ -10,7 +10,7 @@

import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.util.SQLStatementInterceptor;
import org.hibernate.test.util.jdbc.SQLStatementInterceptor;
import org.junit.Before;
import org.junit.Test;

Expand Down

This file was deleted.

@@ -0,0 +1,64 @@
/*
* 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.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;

import org.mockito.Mockito;
import org.mockito.internal.util.MockUtil;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;

/**
* @author Vlad Mihalcea
*/
public class PreparedStatementSpyConnectionProvider extends
DriverManagerConnectionProviderImpl {

private final Map<String, PreparedStatement> preparedStatementStatisticsMap = new HashMap<>();

@Override
public Connection getConnection() throws SQLException {
return spy( super.getConnection() );
}

private Connection spy(Connection connection) {
if ( new MockUtil().isMock( connection ) ) {
return connection;
}
Connection connectionSpy = Mockito.spy( connection );
try {
doAnswer( invocation -> {
PreparedStatement statement = (PreparedStatement) invocation.callRealMethod();
PreparedStatement statementSpy = Mockito.spy( statement );
String sql = (String) invocation.getArguments()[0];
preparedStatementStatisticsMap.put( sql, statementSpy );
return statementSpy;
} ).when( connectionSpy ).prepareStatement( anyString() );
}
catch ( SQLException e ) {
throw new IllegalArgumentException( e );
}
return connectionSpy;
}

public void clear() {
preparedStatementStatisticsMap.values().forEach( Mockito::reset );
preparedStatementStatisticsMap.clear();
}

public PreparedStatement getPreparedStatement(String sql) {
return preparedStatementStatisticsMap.get( sql );
}
}
@@ -1,4 +1,4 @@
package org.hibernate.test.util;
package org.hibernate.test.util.jdbc;

import java.util.LinkedList;

Expand Down

0 comments on commit 6142f92

Please sign in to comment.