From e718d812047c720ef692a0662cfe1115dc78f7ce Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 14 Jun 2013 18:18:09 +0200 Subject: [PATCH] [#1885] [#2523] Statement.close() may be called upon previously closed statements * [#1885] Add test to count opening and closing of Statements and ResultSets by jOOQ * [#2523] Statement.close() may be called upon previously closed statements --- .../AbstractLifecycleListener.java} | 38 +++----- .../_/listeners/JDBCLifecycleListener.java | 94 +++++++++++++++++++ .../_/listeners/LifecycleWatcherListener.java | 71 ++++++++++++++ .../test/_/{ => listeners}/PrettyPrinter.java | 2 +- .../TestStatisticsListener.java | 2 +- .../_/testcases/ExecuteListenerTests.java | 66 ++++++++++--- .../src/org/jooq/test/jOOQAbstractTest.java | 38 ++++++-- .../java/org/jooq/impl/AbstractQuery.java | 2 - jOOQ/src/main/java/org/jooq/impl/Utils.java | 8 +- 9 files changed, 264 insertions(+), 57 deletions(-) rename jOOQ-test/src/org/jooq/test/_/{LifecycleWatcherListener.java => listeners/AbstractLifecycleListener.java} (67%) create mode 100644 jOOQ-test/src/org/jooq/test/_/listeners/JDBCLifecycleListener.java create mode 100644 jOOQ-test/src/org/jooq/test/_/listeners/LifecycleWatcherListener.java rename jOOQ-test/src/org/jooq/test/_/{ => listeners}/PrettyPrinter.java (96%) rename jOOQ-test/src/org/jooq/test/_/{ => listeners}/TestStatisticsListener.java (96%) diff --git a/jOOQ-test/src/org/jooq/test/_/LifecycleWatcherListener.java b/jOOQ-test/src/org/jooq/test/_/listeners/AbstractLifecycleListener.java similarity index 67% rename from jOOQ-test/src/org/jooq/test/_/LifecycleWatcherListener.java rename to jOOQ-test/src/org/jooq/test/_/listeners/AbstractLifecycleListener.java index 4571f6b3f1..ddd1f58640 100644 --- a/jOOQ-test/src/org/jooq/test/_/LifecycleWatcherListener.java +++ b/jOOQ-test/src/org/jooq/test/_/listeners/AbstractLifecycleListener.java @@ -33,14 +33,12 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -package org.jooq.test._; +package org.jooq.test._.listeners; import java.lang.reflect.Method; import java.util.Comparator; import java.util.Map; -import java.util.TreeMap; -import org.jooq.ExecuteContext; import org.jooq.impl.DefaultExecuteListener; import org.junit.Test; @@ -51,38 +49,24 @@ import org.junit.Test; * * @author Lukas Eder */ -public class LifecycleWatcherListener extends DefaultExecuteListener { +public abstract class AbstractLifecycleListener extends DefaultExecuteListener { /** * Generated UID */ - private static final long serialVersionUID = -2283264126211556442L; + private static final long serialVersionUID = -2283264126211556442L; - private static Comparator METHOD_COMPARATOR = new Comparator() { + protected static Comparator METHOD_COMPARATOR = new Comparator() { - @Override - public int compare(Method o1, Method o2) { - return o1.getName().compareTo(o2.getName()); - } - }; + @Override + public int compare(Method o1, Method o2) { + return o1.getName().compareTo(o2.getName()); + } + }; - public static final Map START_COUNT = new TreeMap(METHOD_COMPARATOR); - public static final Map END_COUNT = new TreeMap(METHOD_COMPARATOR); - private static final Object INCREMENT_MONITOR = new Object(); + private static final Object INCREMENT_MONITOR = new Object(); - @Override - public void start(ExecuteContext ctx) { - super.start(ctx); - increment(START_COUNT); - } - - @Override - public void end(ExecuteContext ctx) { - super.end(ctx); - increment(END_COUNT); - } - - private void increment(Map map) { + protected void increment(Map map) { synchronized (INCREMENT_MONITOR) { Method m = testMethod(); diff --git a/jOOQ-test/src/org/jooq/test/_/listeners/JDBCLifecycleListener.java b/jOOQ-test/src/org/jooq/test/_/listeners/JDBCLifecycleListener.java new file mode 100644 index 0000000000..cc1c8bc83d --- /dev/null +++ b/jOOQ-test/src/org/jooq/test/_/listeners/JDBCLifecycleListener.java @@ -0,0 +1,94 @@ +/** + * Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.test._.listeners; + +import java.lang.reflect.Method; +import java.sql.CallableStatement; +import java.sql.SQLException; +import java.util.Map; +import java.util.TreeMap; + +import org.jooq.ExecuteContext; +import org.jooq.tools.jdbc.DefaultCallableStatement; +import org.jooq.tools.jdbc.DefaultPreparedStatement; + +/** + * An ExecuteListener that collects data about the lifecycle of + * JDBC objects in all integration tests. + * + * @author Lukas Eder + */ +public class JDBCLifecycleListener extends AbstractLifecycleListener { + + /** + * Generated UID + */ + private static final long serialVersionUID = -2283264126211556442L; + + public static final Map STMT_START_COUNT = new TreeMap(METHOD_COMPARATOR); + public static final Map STMT_CLOSE_COUNT = new TreeMap(METHOD_COMPARATOR); + + public static final Map RS_START_COUNT = new TreeMap(METHOD_COMPARATOR); + public static final Map RS_CLOSE_COUNT = new TreeMap(METHOD_COMPARATOR); + + @Override + public void executeStart(ExecuteContext ctx) { + super.executeStart(ctx); + increment(STMT_START_COUNT); + + if (ctx.statement() instanceof CallableStatement) { + ctx.statement(new DefaultCallableStatement((CallableStatement) ctx.statement()) { + + @Override + public void close() throws SQLException { + increment(STMT_CLOSE_COUNT); + super.close(); + } + }); + + } + else { + ctx.statement(new DefaultPreparedStatement(ctx.statement()) { + + @Override + public void close() throws SQLException { + increment(STMT_CLOSE_COUNT); + super.close(); + } + }); + } + } +} diff --git a/jOOQ-test/src/org/jooq/test/_/listeners/LifecycleWatcherListener.java b/jOOQ-test/src/org/jooq/test/_/listeners/LifecycleWatcherListener.java new file mode 100644 index 0000000000..227abd45b3 --- /dev/null +++ b/jOOQ-test/src/org/jooq/test/_/listeners/LifecycleWatcherListener.java @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.test._.listeners; + +import java.lang.reflect.Method; +import java.util.Map; +import java.util.TreeMap; + +import org.jooq.ExecuteContext; + +/** + * An ExecuteListener that collects data about the lifecycle of + * execute listeners in all integration tests. + * + * @author Lukas Eder + */ +public class LifecycleWatcherListener extends AbstractLifecycleListener { + + /** + * Generated UID + */ + private static final long serialVersionUID = -2283264126211556442L; + + public static final Map START_COUNT = new TreeMap(METHOD_COMPARATOR); + public static final Map END_COUNT = new TreeMap(METHOD_COMPARATOR); + + @Override + public void start(ExecuteContext ctx) { + super.start(ctx); + increment(START_COUNT); + } + + @Override + public void end(ExecuteContext ctx) { + super.end(ctx); + increment(END_COUNT); + } +} diff --git a/jOOQ-test/src/org/jooq/test/_/PrettyPrinter.java b/jOOQ-test/src/org/jooq/test/_/listeners/PrettyPrinter.java similarity index 96% rename from jOOQ-test/src/org/jooq/test/_/PrettyPrinter.java rename to jOOQ-test/src/org/jooq/test/_/listeners/PrettyPrinter.java index ede8785746..67d966625a 100644 --- a/jOOQ-test/src/org/jooq/test/_/PrettyPrinter.java +++ b/jOOQ-test/src/org/jooq/test/_/listeners/PrettyPrinter.java @@ -33,7 +33,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -package org.jooq.test._; +package org.jooq.test._.listeners; import static org.jooq.conf.ParamType.INLINED; diff --git a/jOOQ-test/src/org/jooq/test/_/TestStatisticsListener.java b/jOOQ-test/src/org/jooq/test/_/listeners/TestStatisticsListener.java similarity index 96% rename from jOOQ-test/src/org/jooq/test/_/TestStatisticsListener.java rename to jOOQ-test/src/org/jooq/test/_/listeners/TestStatisticsListener.java index cc0ce1df70..95dbb6c6ea 100644 --- a/jOOQ-test/src/org/jooq/test/_/TestStatisticsListener.java +++ b/jOOQ-test/src/org/jooq/test/_/listeners/TestStatisticsListener.java @@ -33,7 +33,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -package org.jooq.test._; +package org.jooq.test._.listeners; import java.util.HashMap; import java.util.Map; diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java index 3e27461386..b2aaa797fe 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java @@ -462,14 +462,32 @@ extends BaseTest