Last active
August 13, 2023 17:23
-
-
Save msdousti/20ce056ccce22b444f4a17a82334c7d4 to your computer and use it in GitHub Desktop.
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
import ch.qos.logback.classic.Level; | |
import ch.qos.logback.classic.Logger; | |
import ch.qos.logback.classic.spi.ILoggingEvent; | |
import ch.qos.logback.core.read.ListAppender; | |
import jakarta.annotation.Nonnull; | |
import org.slf4j.LoggerFactory; | |
import java.util.List; | |
import java.util.function.Supplier; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class SqlStatementCountUtils { | |
@Nonnull | |
private final List<ILoggingEvent> sqlStatementsList; | |
public SqlStatementCountUtils() { | |
final ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); | |
listAppender.start(); | |
final Logger logger = (Logger) LoggerFactory.getLogger("org.hibernate.SQL"); | |
logger.setLevel(Level.DEBUG); | |
logger.addAppender(listAppender); | |
sqlStatementsList = listAppender.list; | |
} | |
public <T> T assertSqlStatementsCount(int expectedCount, @Nonnull final Supplier<T> supplier) { | |
final var returnValue = supplier.get(); | |
assertSizeAndClear(expectedCount); | |
return returnValue; | |
} | |
public void assertSqlStatementsCount(int expectedCount, @Nonnull final Runnable runnable) { | |
runnable.run(); | |
assertSizeAndClear(expectedCount); | |
} | |
private void assertSizeAndClear(int expectedCount) { | |
assertThat(sqlStatementsList).hasSize(expectedCount); | |
sqlStatementsList.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment