Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3349 Test the number of SQL statements triggered by entity lo…
…ading in search queries
- Loading branch information
Showing
5 changed files
with
95 additions
and
19 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
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
66 changes: 66 additions & 0 deletions
66
...m/src/main/java/org/hibernate/search/util/impl/integrationtest/orm/OrmSoftAssertions.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,66 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* 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.search.util.impl.integrationtest.orm; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
import java.util.stream.Collectors; | ||
|
||
import org.hibernate.BaseSessionEventListener; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionEventListener; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.resource.jdbc.spi.StatementInspector; | ||
|
||
import org.assertj.core.api.AbstractIntegerAssert; | ||
import org.assertj.core.api.AutoCloseableSoftAssertions; | ||
|
||
public class OrmSoftAssertions extends AutoCloseableSoftAssertions { | ||
|
||
public static void withinSession(SessionFactory sessionFactory, | ||
BiConsumer<Session, OrmSoftAssertions> action) { | ||
try ( OrmSoftAssertions softAssertions = new OrmSoftAssertions() ) { | ||
try ( Session session = sessionFactory.withOptions() | ||
.eventListeners( softAssertions.sessionEventListener ) | ||
.statementInspector( softAssertions.statementInspector ) | ||
.openSession() ) { | ||
action.accept( session, softAssertions ); | ||
} | ||
} | ||
} | ||
|
||
private final StatementInspector statementInspector; | ||
private final SessionEventListener sessionEventListener; | ||
|
||
private int statementExecutionCount = 0; | ||
private final List<String> statements = new ArrayList<>(); | ||
|
||
private OrmSoftAssertions() { | ||
sessionEventListener = new BaseSessionEventListener() { | ||
@Override | ||
public void jdbcPrepareStatementStart() { | ||
++statementExecutionCount; | ||
} | ||
}; | ||
statementInspector = this::inspectSql; | ||
} | ||
|
||
public AbstractIntegerAssert<?> assertStatementExecutionCount() { | ||
// Don't use statement.size(), just in case a statement is executed twice... not sure it can happen, though. | ||
return assertThat( statementExecutionCount ) | ||
.as( "Statement execution count for statements [\n" | ||
+ statements.stream().collect( Collectors.joining( "\n" ) ) | ||
+ "\n]" ); | ||
} | ||
|
||
private String inspectSql(String sql) { | ||
statements.add( sql ); | ||
return sql; | ||
} | ||
|
||
} |