package testsuite.simple; import testsuite.BaseTestCase; import com.mysql.jdbc.StatementInterceptor; import com.mysql.jdbc.StatementImpl; import com.mysql.jdbc.ConnectionImpl; import com.mysql.jdbc.ResultSetInternalMethods; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class StatementInterceptorTest extends BaseTestCase { /** * Runs all test cases in this test suite * * @param args */ public static void main(String[] args) { junit.textui.TestRunner.run(StatementInterceptorTest.class); } /** * Creates a new StatementInterceptorTest object. * * @param name * DOCUMENT ME! */ public StatementInterceptorTest(String name) { super(name); } /** * DOCUMENT ME! * * @throws Exception * DOCUMENT ME! */ public void setUp() throws Exception { super.setUp(); this.stmt.executeUpdate("DROP TABLE IF EXISTS statement_interceptor_test"); this.stmt.executeUpdate("CREATE TABLE statement_interceptor_test (id int not null primary key)"); } /** * DOCUMENT ME! * * @throws Exception * DOCUMENT ME! */ public void tearDown() throws Exception { this.stmt.executeUpdate("DROP TABLE statement_interceptor_test"); super.tearDown(); } public void testStatementInterceptorPreparedStatementExecuteBatch() throws Exception { Connection interceptedConn = null; try { Properties props = new Properties(); props.setProperty("statementInterceptors", "testsuite.simple.StatementInterceptorTest$ExecuteUpdateInterceptor"); interceptedConn = getConnectionWithProps(props); PreparedStatement stmt = interceptedConn.prepareStatement("INSERT INTO statement_interceptor_test VALUES (?)"); stmt.setInt(1, 1); stmt.addBatch(); stmt.setInt(1, 2); stmt.addBatch(); stmt.executeBatch(); } finally { closeMemberJDBCResources(); if (interceptedConn != null) { interceptedConn.close(); } } } public static class ExecuteUpdateInterceptor implements StatementInterceptor { public boolean executeTopLevelOnly() { // don't intercept statements from this or other StatementInterceptors // called within our/their pre/post process methods return true; } public void init( com.mysql.jdbc.Connection connection, Properties props ) throws SQLException { } public ResultSetInternalMethods preProcess( String sql, com.mysql.jdbc.Statement interceptedStatement, com.mysql.jdbc.Connection connection ) throws SQLException { interceptedStatement.executeUpdate( sql ); return new com.mysql.jdbc.ResultSetImpl( ((StatementImpl) interceptedStatement).getLongUpdateCount(), ((StatementImpl) interceptedStatement).getLastInsertID(), (ConnectionImpl) connection, (StatementImpl) interceptedStatement ); } /** * noop */ public ResultSetInternalMethods postProcess( String sql, com.mysql.jdbc.Statement interceptedStatement, ResultSetInternalMethods originalResultSet, com.mysql.jdbc.Connection connection ) throws SQLException { return null; } public void destroy() { } } }