import java.sql.BatchUpdateException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Properties; import testsuite.regression.CachedRowsetTest; import junit.framework.TestCase; /* Copyright (C) 2004 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class TestBug6823 extends TestCase { /** Connection to server, initialized in setUp() Cleaned up in tearDown(). */ protected Connection ddlConn = null; /** Connection to server, Created in tests, Cleaned up in tearDown(). */ protected Connection testConn = null; /** list of Tables to be dropped in tearDown */ private List createdTables; private String dbUrl = "jdbc:mysql:///test"; private String dbClass = "com.mysql.jdbc.Driver"; /** * @throws Exception if an error occurs. */ public void setUp() throws Exception { Class.forName(dbClass).newInstance(); createdTables = new ArrayList(); this.ddlConn = DriverManager.getConnection(dbUrl); Statement stmt = this.ddlConn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT VERSION()"); rs.next(); rs.close(); } public void tearDown() throws Exception { for (int i = 0; i < createdTables.size(); i++) { try { dropTable((String)createdTables.get(i)); } catch (SQLException SQLE) { ; } } if (this.ddlConn != null) { try { this.ddlConn.close(); } catch (SQLException SQLE) { ; } } if (this.testConn != null) { try { this.testConn.close(); } catch (SQLException SQLE) { ; } } } protected void dropTable(String tableName) throws SQLException { Statement stmt = ddlConn.createStatement(); stmt.executeUpdate("DROP TABLE IF EXISTS " + tableName); stmt.close(); } protected void createTable(String tableName, String columnsAndOtherStuff) throws SQLException { createdTables.add(tableName); dropTable(tableName); StringBuffer createSql = new StringBuffer(tableName.length() + columnsAndOtherStuff.length() + 10); createSql.append("CREATE TABLE "); createSql.append(tableName); createSql.append(" "); createSql.append(columnsAndOtherStuff); Statement stmt = ddlConn.createStatement(); stmt.executeUpdate(createSql.toString()); stmt.close(); } /** * Returns a new connection with the given properties * * @param props the properties to use (the URL will come from the standard * for this testcase). * * @return a new connection using the given properties. * * @throws SQLException */ protected Connection getConnectionWithProps(Properties props) throws SQLException { return DriverManager.getConnection(dbUrl, props); } public void testBug6823() throws SQLException { innerBug6823(true); innerBug6823(false); } /** * @param continueBatchOnError * @throws SQLException */ private void innerBug6823(boolean continueBatchOnError) throws SQLException { Properties continueBatchOnErrorProps = new Properties(); continueBatchOnErrorProps.setProperty("continueBatchOnError", Boolean .toString(continueBatchOnError)); this.testConn = getConnectionWithProps(continueBatchOnErrorProps); Statement statement = this.testConn.createStatement(); String tableName = "testBug6823"; createTable(tableName, "(id int not null primary key auto_increment," + " strdata1 varchar(255) not null, strdata2 varchar(255)," + " UNIQUE INDEX (strdata1))"); PreparedStatement pStmt = this.testConn.prepareStatement("INSERT INTO " + tableName + " (strdata1, strdata2) VALUES (?,?)"); int c = 0; addBatchItems(statement, pStmt, tableName, ++c); addBatchItems(statement, pStmt, tableName, ++c); addBatchItems(statement, pStmt, tableName, ++c); addBatchItems(statement, pStmt, tableName, c); // duplicate entry addBatchItems(statement, pStmt, tableName, ++c); addBatchItems(statement, pStmt, tableName, ++c); int expectedUpdateCounts = continueBatchOnError ? 6 : 3; BatchUpdateException e1 = null; BatchUpdateException e2 = null; int[] updateCountsPstmt = null; try { updateCountsPstmt = pStmt.executeBatch(); } catch (BatchUpdateException e) { e1 = e; updateCountsPstmt = e1.getUpdateCounts(); } int[] updateCountsStmt = null; try { updateCountsStmt = statement.executeBatch(); } catch (BatchUpdateException e) { e2 = e; updateCountsStmt = e1.getUpdateCounts(); } assertNotNull(e1); assertNotNull(e2); assertEquals(expectedUpdateCounts, updateCountsPstmt.length); assertEquals(expectedUpdateCounts, updateCountsStmt.length); if (continueBatchOnError) { assertTrue(updateCountsPstmt[3] == Statement.EXECUTE_FAILED); assertTrue(updateCountsStmt[3] == Statement.EXECUTE_FAILED); } int psRows = 0; ResultSet rs = statement.executeQuery("SELECT * from " + tableName + " WHERE strdata1 like \"ps_%\""); while (rs.next()) { psRows++; } rs.close(); assertTrue(psRows > 0); int sRows = 0; rs = statement.executeQuery("SELECT * from " + tableName + " WHERE strdata1 like \"s_%\""); while (rs.next()) { sRows++; } rs.close(); assertTrue(sRows > 0); assertTrue(psRows + "!=" + sRows, psRows == sRows); } private void addBatchItems(Statement statement, PreparedStatement pStmt, String tableName, int i) throws SQLException { pStmt.setString(1, "ps_batch_" + i); pStmt.setString(2, "ps_batch_" + i); pStmt.addBatch(); statement.addBatch("INSERT INTO " + tableName + " (strdata1, strdata2) VALUES " + "(\"s_batch_" + i + "\",\"s_batch_" + i + "\")"); } public static void main(String[] args) { junit.textui.TestRunner.run(TestBug6823.class); } }