Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6R1spapM7nBteaqeo
96 lines
3.0 KiB
Java
96 lines
3.0 KiB
Java
package com.ankurm.nplusone;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import javax.sql.DataSource;
|
|
import net.ttddyy.dsproxy.QueryCount;
|
|
import net.ttddyy.dsproxy.QueryCountHolder;
|
|
import net.ttddyy.dsproxy.QueryInfo;
|
|
import net.ttddyy.dsproxy.listener.QueryExecutionListener;
|
|
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
|
|
import org.opentest4j.AssertionFailedError;
|
|
|
|
/**
|
|
* Counts every statement that reaches the JDBC driver, whoever sent it.
|
|
*
|
|
* <p>{@link #wrap(DataSource)} puts datasource-proxy between Hibernate and the real DataSource.
|
|
* datasource-proxy keeps per-type counters (select / insert / update / delete) in
|
|
* {@link QueryCountHolder}; a small listener additionally remembers the SQL text so a failed
|
|
* assertion can print the statements that broke the budget. Counters are per thread: the code
|
|
* under test and the assertion must run on the same thread.
|
|
*/
|
|
public final class QueryGuard {
|
|
|
|
static final String DATASOURCE_NAME = "guarded";
|
|
|
|
private static final ThreadLocal<List<String>> SQL = ThreadLocal.withInitial(ArrayList::new);
|
|
|
|
private QueryGuard() {
|
|
}
|
|
|
|
/** Wraps a DataSource so every statement is counted and recorded. */
|
|
public static DataSource wrap(DataSource real) {
|
|
return ProxyDataSourceBuilder.create(real)
|
|
.name(DATASOURCE_NAME)
|
|
.countQuery()
|
|
.listener(new QueryExecutionListener() {
|
|
@Override
|
|
public void beforeQuery(net.ttddyy.dsproxy.ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
|
|
}
|
|
|
|
@Override
|
|
public void afterQuery(net.ttddyy.dsproxy.ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
|
|
for (QueryInfo q : queryInfoList) {
|
|
SQL.get().add(q.getQuery().replaceAll("\\s+", " ").trim());
|
|
}
|
|
}
|
|
})
|
|
.build();
|
|
}
|
|
|
|
/** Zeroes the counters and the SQL log for the current thread. */
|
|
public static void reset() {
|
|
QueryCountHolder.clear();
|
|
SQL.get().clear();
|
|
}
|
|
|
|
public static QueryCount counts() {
|
|
QueryCount c = QueryCountHolder.get(DATASOURCE_NAME);
|
|
return c != null ? c : new QueryCount();
|
|
}
|
|
|
|
public static long selects() {
|
|
return counts().getSelect();
|
|
}
|
|
|
|
public static List<String> sql() {
|
|
return List.copyOf(SQL.get());
|
|
}
|
|
|
|
/** Runs the action from a clean slate and returns the SELECT count it caused. */
|
|
public static long countSelects(Runnable action) {
|
|
reset();
|
|
action.run();
|
|
return selects();
|
|
}
|
|
|
|
/** Fails with the full statement list when the current thread has sent more than {@code max} SELECTs. */
|
|
public static void assertMaxSelects(long max, String what) {
|
|
long actual = selects();
|
|
if (actual > max) {
|
|
throw new AssertionFailedError(message(max, actual, what));
|
|
}
|
|
}
|
|
|
|
static String message(long max, long actual, String what) {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(what).append(": expected at most ").append(max).append(" SELECT statement(s) but ")
|
|
.append(actual).append(" were sent.");
|
|
int i = 1;
|
|
for (String s : SQL.get()) {
|
|
sb.append(System.lineSeparator()).append(" ").append(i++).append(") ").append(s);
|
|
}
|
|
return sb.toString();
|
|
}
|
|
}
|