diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/DebuggerTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/DebuggerTests.java index 5a08d7f6d8..d1ad0ac1da 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/DebuggerTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/DebuggerTests.java @@ -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 queries = new ArrayList(Arrays.asList("0, 1", "0, 1, 2", "0, 1, 2, 3, 4")); + List columns = new ArrayList(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> fields = new ArrayList>(); + for (int i = 0; i < 10; i++) { + fields.add(inline(i)); + create().select(fields).fetch(); + } + } + }); + } + @Test public void testDebuggerBreakpoint() throws Exception { run(new DebugTestRunnable() { diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index 24570c0f85..86833e3ca9 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -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(); diff --git a/jOOQ/src/main/java/org/jooq/tools/debug/Matcher.java b/jOOQ/src/main/java/org/jooq/tools/debug/Matcher.java index e33a51035c..6d5b10ae8d 100644 --- a/jOOQ/src/main/java/org/jooq/tools/debug/Matcher.java +++ b/jOOQ/src/main/java/org/jooq/tools/debug/Matcher.java @@ -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. + *

+ * 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 null or an + * empty array, match counting is disabled and every match will + * trigger the aforementioned events. + */ + void matchCount(int... count); } diff --git a/jOOQ/src/main/java/org/jooq/tools/debug/QueryLog.java b/jOOQ/src/main/java/org/jooq/tools/debug/QueryLog.java index 95239ba6e5..42a5ca5ef8 100644 --- a/jOOQ/src/main/java/org/jooq/tools/debug/QueryLog.java +++ b/jOOQ/src/main/java/org/jooq/tools/debug/QueryLog.java @@ -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("]"); diff --git a/jOOQ/src/main/java/org/jooq/tools/debug/impl/MatcherImpl.java b/jOOQ/src/main/java/org/jooq/tools/debug/impl/MatcherImpl.java index edb34bf986..402e1630e5 100644 --- a/jOOQ/src/main/java/org/jooq/tools/debug/impl/MatcherImpl.java +++ b/jOOQ/src/main/java/org/jooq/tools/debug/impl/MatcherImpl.java @@ -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 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(); + + 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; } }