Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the PreparedStatement assertion mechanism to rely on Mockito …
…solely
- Loading branch information
1 parent
be93105
commit 6142f92
Showing
5 changed files
with
86 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 0 additions & 115 deletions
115
hibernate-core/src/test/java/org/hibernate/test/util/JdbcStatisticsConnectionProvider.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
...re/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...te/test/util/SQLStatementInterceptor.java → ...st/util/jdbc/SQLStatementInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.hibernate.test.util; | ||
package org.hibernate.test.util.jdbc; | ||
|
||
import java.util.LinkedList; | ||
|
||
|