[#1472] Move org.jooq.tools.debug API from jOOQ Console to core -

Restored match count feature
This commit is contained in:
Lukas Eder 2012-09-19 16:37:28 +02:00
parent 142594e67d
commit dda85af4b4
5 changed files with 97 additions and 1 deletions

View File

@ -36,9 +36,16 @@
package org.jooq.test._.testcases;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.jooq.impl.Factory.inline;
import static org.jooq.tools.debug.impl.DebuggerFactory.localDebugger;
import static org.jooq.tools.debug.impl.DebuggerFactory.remoteDebugger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.TableRecord;
@ -115,6 +122,42 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
void run(Debugger remote, Debugger local) throws Exception;
}
@Test
public void testDebuggerMatchCount() throws Exception {
class Count234 implements LoggerListener {
List<String> queries = new ArrayList<String>(Arrays.asList("0, 1", "0, 1, 2", "0, 1, 2, 3, 4"));
List<Integer> columns = new ArrayList<Integer>(Arrays.asList(2, 3, 5));
@Override
public void logQuery(QueryLog l) {
assertTrue(l.getQuery().getSQL().contains(queries.remove(0)));
}
@Override
public void logResult(ResultLog l) {
assertEquals((int) columns.remove(0), l.columns());
}
}
run(new DebugTestRunnable() {
@Override
public void run(Debugger d1, Debugger d2) throws Exception {
Matcher matcher = d1.newMatcher();
matcher.matchCount(2, 3, 5);
matcher.newLogger().listener(new Count234());
List<Field<?>> fields = new ArrayList<Field<?>>();
for (int i = 0; i < 10; i++) {
fields.add(inline(i));
create().select(fields).fetch();
}
}
});
}
@Test
public void testDebuggerBreakpoint() throws Exception {
run(new DebugTestRunnable() {

View File

@ -1748,6 +1748,11 @@ public abstract class jOOQAbstractTest<
new DebuggerTests(this).testDebuggerLogger();
}
@Test
public void testDebuggerMatchCount() throws Exception {
new DebuggerTests(this).testDebuggerMatchCount();
}
@Test
public void testDebuggerProcessor() throws Exception {
new DebuggerTests(this).testDebuggerProcessor();

View File

@ -108,4 +108,18 @@ public interface Matcher extends DebuggerObject {
* @param regex A regular expression
*/
void matchSQL(String regex);
/**
* Define the match count before this matcher will trigger actions.
* <p>
* The match count specifies how many times {@link #matchThreadName(String)}
* and {@link #matchSQL(String)} need to match a statement before this
* matcher triggers its {@link #breakpoints()}, {@link #loggers()}, and
* {@link #processors()}
*
* @param count A list of match counts. If this is <code>null</code> or an
* empty array, match counting is disabled and every match will
* trigger the aforementioned events.
*/
void matchCount(int... count);
}

View File

@ -117,7 +117,7 @@ public final class QueryLog implements Serializable {
sb.append(StopWatch.format(executeTime));
sb.append(", query=[");
sb.append(query);
sb.append(", origin=");
sb.append("], origin=");
sb.append(origin);
sb.append("]");

View File

@ -37,7 +37,9 @@
package org.jooq.tools.debug.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import org.jooq.ExecuteContext;
@ -65,6 +67,8 @@ class MatcherImpl extends AbstractDebuggerObject implements Matcher {
private Pattern matchThreadName;
private Pattern matchSQL;
private Set<Integer> matchCount;
private transient int matchCounter;
@Override
public Logger newLogger() {
@ -104,6 +108,8 @@ class MatcherImpl extends AbstractDebuggerObject implements Matcher {
else {
this.matchThreadName = null;
}
apply();
}
@Override
@ -114,6 +120,24 @@ class MatcherImpl extends AbstractDebuggerObject implements Matcher {
else {
this.matchSQL = null;
}
apply();
}
@Override
public void matchCount(int... count) {
if (count == null || count.length == 0) {
matchCount = null;
}
else {
matchCount = new HashSet<Integer>();
for (int i : count) {
matchCount.add(i);
}
}
apply();
}
boolean matches(ExecuteContext ctx) {
@ -138,6 +162,16 @@ class MatcherImpl extends AbstractDebuggerObject implements Matcher {
}
}
if (matchCount != null) {
synchronized (matchCount) {
matchCounter += 1;
if (!matchCount.contains(matchCounter)) {
return false;
}
}
}
return true;
}
}