[#1885] Add test to count opening and closing of Statements and
ResultSets by jOOQ
This commit is contained in:
parent
4472a06174
commit
79950eef77
@ -66,7 +66,7 @@ public abstract class AbstractLifecycleListener extends DefaultExecuteListener {
|
||||
|
||||
private static final Object INCREMENT_MONITOR = new Object();
|
||||
|
||||
protected void increment(Map<Method, Integer> map) {
|
||||
protected final void increment(Map<Method, Integer> map) {
|
||||
synchronized (INCREMENT_MONITOR) {
|
||||
Method m = testMethod();
|
||||
|
||||
|
||||
@ -44,6 +44,7 @@ import java.util.TreeMap;
|
||||
import org.jooq.ExecuteContext;
|
||||
import org.jooq.tools.jdbc.DefaultCallableStatement;
|
||||
import org.jooq.tools.jdbc.DefaultPreparedStatement;
|
||||
import org.jooq.tools.jdbc.DefaultResultSet;
|
||||
|
||||
/**
|
||||
* An <code>ExecuteListener</code> that collects data about the lifecycle of
|
||||
@ -91,4 +92,19 @@ public class JDBCLifecycleListener extends AbstractLifecycleListener {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fetchStart(ExecuteContext ctx) {
|
||||
super.fetchStart(ctx);
|
||||
increment(RS_START_COUNT);
|
||||
|
||||
ctx.resultSet(new DefaultResultSet(ctx.resultSet()) {
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
increment(RS_CLOSE_COUNT);
|
||||
super.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,20 +52,20 @@ public class LifecycleWatcherListener extends AbstractLifecycleListener {
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -2283264126211556442L;
|
||||
private static final long serialVersionUID = -2283264126211556442L;
|
||||
|
||||
public static final Map<Method, Integer> START_COUNT = new TreeMap<Method, Integer>(METHOD_COMPARATOR);
|
||||
public static final Map<Method, Integer> END_COUNT = new TreeMap<Method, Integer>(METHOD_COMPARATOR);
|
||||
public static final Map<Method, Integer> LISTENER_START_COUNT = new TreeMap<Method, Integer>(METHOD_COMPARATOR);
|
||||
public static final Map<Method, Integer> LISTENER_END_COUNT = new TreeMap<Method, Integer>(METHOD_COMPARATOR);
|
||||
|
||||
@Override
|
||||
public void start(ExecuteContext ctx) {
|
||||
super.start(ctx);
|
||||
increment(START_COUNT);
|
||||
increment(LISTENER_START_COUNT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void end(ExecuteContext ctx) {
|
||||
super.end(ctx);
|
||||
increment(END_COUNT);
|
||||
increment(LISTENER_END_COUNT);
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,6 +38,12 @@ package org.jooq.test;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.jooq.SQLDialect.CUBRID;
|
||||
import static org.jooq.SQLDialect.FIREBIRD;
|
||||
import static org.jooq.test._.listeners.JDBCLifecycleListener.RS_CLOSE_COUNT;
|
||||
import static org.jooq.test._.listeners.JDBCLifecycleListener.RS_START_COUNT;
|
||||
import static org.jooq.test._.listeners.JDBCLifecycleListener.STMT_CLOSE_COUNT;
|
||||
import static org.jooq.test._.listeners.JDBCLifecycleListener.STMT_START_COUNT;
|
||||
import static org.jooq.test._.listeners.LifecycleWatcherListener.LISTENER_END_COUNT;
|
||||
import static org.jooq.test._.listeners.LifecycleWatcherListener.LISTENER_START_COUNT;
|
||||
import static org.jooq.tools.reflect.Reflect.on;
|
||||
|
||||
import java.io.File;
|
||||
@ -464,47 +470,44 @@ public abstract class jOOQAbstractTest<
|
||||
logStat.info("---------------");
|
||||
logStat.info("Total", total);
|
||||
|
||||
int unbalanced = 0;
|
||||
JooqLogger logLife = JooqLogger.getLogger(LifecycleWatcherListener.class);
|
||||
logStat.info("");
|
||||
|
||||
logLife.info("");
|
||||
logLife.info("EXECUTE LISTENER LIFECYCLE STATS");
|
||||
logLife.info("--------------------------------");
|
||||
unbalanced = extracted(logLife, unbalanced, LISTENER_START_COUNT, LISTENER_END_COUNT);
|
||||
|
||||
int unbalanced = 0;
|
||||
for (Method m : LifecycleWatcherListener.START_COUNT.keySet()) {
|
||||
Integer starts = LifecycleWatcherListener.START_COUNT.get(m);
|
||||
Integer ends = LifecycleWatcherListener.END_COUNT.get(m);
|
||||
logLife.info("");
|
||||
logLife.info("JDBC STATEMENT LIFECYCLE STATS");
|
||||
logLife.info("------------------------------");
|
||||
unbalanced = extracted(logLife, unbalanced, STMT_START_COUNT, STMT_CLOSE_COUNT);
|
||||
|
||||
logLife.info("");
|
||||
logLife.info("JDBC RESULTSET LIFECYCLE STATS");
|
||||
logLife.info("------------------------------");
|
||||
unbalanced = extracted(logLife, unbalanced, RS_START_COUNT, RS_CLOSE_COUNT);
|
||||
|
||||
logLife.info("");
|
||||
logLife.info("Unbalanced test: ", unbalanced);
|
||||
}
|
||||
|
||||
private static int extracted(JooqLogger logger, int unbalanced, Map<Method, Integer> startCount, Map<Method, Integer> endCount) {
|
||||
for (Method m : startCount.keySet()) {
|
||||
Integer starts = startCount.get(m);
|
||||
Integer ends = endCount.get(m);
|
||||
|
||||
if (!StringUtils.equals(starts, ends)) {
|
||||
unbalanced++;
|
||||
|
||||
logLife.info(
|
||||
"Unbalanced", String.format("(start, end): (%1$3s, %2$3s) at %3$s",
|
||||
starts,
|
||||
ends == null ? 0 : ends,
|
||||
m.toString().replace("public void ", "").replaceAll("( throws.*)?", "")));
|
||||
}
|
||||
}
|
||||
|
||||
logStat.info("");
|
||||
logLife.info("JDBC OBJECT LIFECYCLE STATS");
|
||||
logLife.info("---------------------------");
|
||||
for (Method m : JDBCLifecycleListener.STMT_START_COUNT.keySet()) {
|
||||
Integer starts = JDBCLifecycleListener.STMT_START_COUNT.get(m);
|
||||
Integer ends = JDBCLifecycleListener.STMT_CLOSE_COUNT.get(m);
|
||||
|
||||
if (!StringUtils.equals(starts, ends)) {
|
||||
unbalanced++;
|
||||
|
||||
logLife.info(
|
||||
logger.info(
|
||||
"Unbalanced", String.format("(open, close): (%1$3s, %2$3s) at %3$s",
|
||||
starts,
|
||||
ends == null ? 0 : ends,
|
||||
m.toString().replace("public void ", "").replaceAll("( throws.*)?", "")));
|
||||
}
|
||||
}
|
||||
|
||||
logStat.info("");
|
||||
logLife.info("Unbalanced test: ", unbalanced);
|
||||
return unbalanced;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user