From 6142f92d2fc282ad60138f676528a67d2d83a212 Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Tue, 14 Jun 2016 12:02:14 +0300 Subject: [PATCH] Improve the PreparedStatement assertion mechanism to rely on Mockito solely --- .../jdbc/internal/SessionJdbcBatchTest.java | 55 +++------ .../PessimisticWriteLockTimeoutTest.java | 2 +- .../JdbcStatisticsConnectionProvider.java | 115 ------------------ ...reparedStatementSpyConnectionProvider.java | 64 ++++++++++ .../{ => jdbc}/SQLStatementInterceptor.java | 2 +- 5 files changed, 86 insertions(+), 152 deletions(-) delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/util/JdbcStatisticsConnectionProvider.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java rename hibernate-core/src/test/java/org/hibernate/test/util/{ => jdbc}/SQLStatementInterceptor.java (93%) diff --git a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/SessionJdbcBatchTest.java b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/SessionJdbcBatchTest.java index f48c3b9ace..79244e40e6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/SessionJdbcBatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/SessionJdbcBatchTest.java @@ -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; @@ -14,10 +16,11 @@ import org.hibernate.Session; 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 @@ -25,7 +28,7 @@ import static org.junit.Assert.assertEquals; public class SessionJdbcBatchTest extends BaseNonConfigCoreFunctionalTestCase { - private JdbcStatisticsConnectionProvider connectionProvider = new JdbcStatisticsConnectionProvider(); + private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); @Override protected Class[] getAnnotatedClasses() { @@ -54,26 +57,25 @@ public class SessionJdbcBatchTest 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(); @@ -85,11 +87,10 @@ public class SessionJdbcBatchTest 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 ); @@ -102,11 +103,9 @@ public class SessionJdbcBatchTest 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) { @@ -118,20 +117,6 @@ public class SessionJdbcBatchTest } } - @Test - public void testSessionJdbcBatchOverridesSessionFactorySetting() { - - Session session = sessionFactory().openSession(); - session.beginTransaction(); - try { - - } - finally { - session.getTransaction().commit(); - session.close(); - } - } - @Entity(name = "Event") public static class Event { diff --git a/hibernate-core/src/test/java/org/hibernate/test/locking/PessimisticWriteLockTimeoutTest.java b/hibernate-core/src/test/java/org/hibernate/test/locking/PessimisticWriteLockTimeoutTest.java index 9b434bc13f..3d360368d8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/locking/PessimisticWriteLockTimeoutTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/locking/PessimisticWriteLockTimeoutTest.java @@ -10,7 +10,7 @@ import org.hibernate.dialect.SQLServer2005Dialect; 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; diff --git a/hibernate-core/src/test/java/org/hibernate/test/util/JdbcStatisticsConnectionProvider.java b/hibernate-core/src/test/java/org/hibernate/test/util/JdbcStatisticsConnectionProvider.java deleted file mode 100644 index 49e81507f5..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/util/JdbcStatisticsConnectionProvider.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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.test.util; - -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 JdbcStatisticsConnectionProvider extends - DriverManagerConnectionProviderImpl { - - public static class PreparedStatementStatistics { - private final String sql; - - private int executeUpdateCount; - - private int addBatchCount; - - private int executeBatchCount; - - public PreparedStatementStatistics(String sql) { - this.sql = sql; - } - - public int getExecuteUpdateCount() { - return executeUpdateCount; - } - - public int getAddBatchCount() { - return addBatchCount; - } - - public int getExecuteBatchCount() { - return executeBatchCount; - } - } - - private final Map 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 ); - preparedStatementStatisticsMap.putIfAbsent( statementSpy, - new PreparedStatementStatistics( - (String) invocation - .getArguments()[0] ) - ); - - doAnswer( _invocation -> { - Object mock = _invocation.getMock(); - preparedStatementStatisticsMap.get( mock ).executeUpdateCount++; - return _invocation.callRealMethod(); - } ).when( statementSpy ).executeUpdate(); - - doAnswer( _invocation -> { - Object mock = _invocation.getMock(); - preparedStatementStatisticsMap.get( mock ).addBatchCount++; - return _invocation.callRealMethod(); - } ).when( statementSpy ).addBatch(); - - doAnswer( _invocation -> { - Object mock = _invocation.getMock(); - preparedStatementStatisticsMap.get( mock ).executeBatchCount++; - return _invocation.callRealMethod(); - } ).when( statementSpy ).executeBatch(); - - return statementSpy; - } ).when( connectionSpy ).prepareStatement( anyString() ); - } - catch ( SQLException e ) { - e.printStackTrace(); - } - return connectionSpy; - } - - public void clear() { - preparedStatementStatisticsMap.clear(); - } - - public Map getPreparedStatementStatistics() { - Map statisticsMap = new HashMap<>(); - preparedStatementStatisticsMap.values() - .stream() - .forEach( stats -> statisticsMap.put( stats.sql, stats ) ); - return statisticsMap; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java b/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java new file mode 100644 index 0000000000..a09932f290 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java @@ -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 . + */ +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 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 ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/util/SQLStatementInterceptor.java b/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/SQLStatementInterceptor.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/util/SQLStatementInterceptor.java rename to hibernate-core/src/test/java/org/hibernate/test/util/jdbc/SQLStatementInterceptor.java index e6c27d1454..11c987a4a7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/util/SQLStatementInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/SQLStatementInterceptor.java @@ -1,4 +1,4 @@ -package org.hibernate.test.util; +package org.hibernate.test.util.jdbc; import java.util.LinkedList;