【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了UpdatabilityTest.java提供大家参考,希望对大家有所帮助!
/*
Copyright (C) 2002-2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
software distribution.
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
*/
package testsuite.simple;
import com.
mysql.jdbc.NotUpdatable;
import testsuite.BaseTestCase;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Tests for updatable result sets
*
* @author Mark Matthews
* @version $Id: UpdatabilityTest.java,v 1.1.2.1 2005/05/13 18:58:37 mmatthews
* Exp $
*/
public class UpdatabilityTest extends BaseTestCase {
/**
* Creates a new UpdatabilityTest object.
*
* @param name
* DOCUMENT ME!
*/
public UpdatabilityTest(String name) {
super(name);
}
/**
* Runs all test cases in this test suite
*
* @param args
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(UpdatabilityTest.class);
}
/**
* DOCUMENT ME!
*
* @throws Exception
* DOCUMENT ME!
*/
public void setUp() throws Exception {
super.setUp();
createTestTable();
}
/**
* If using MySQL-4.1, tests if aliased tables work as updatable result
* sets.
*
* @throws Exception
* if an error occurs
*/
public void testAliasedTables() throws Exception {
DatabaseMetaData dbmd = this.conn.getMetaData();
if ((dbmd.getDatabaseMajorVersion() >= 4)
&& (dbmd.getDatabaseMinorVersion() >= 1)) {
Statement scrollableStmt = null;
try {
scrollableStmt = this.conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
this.rs = scrollableStmt
.executeQuery("SELECT pos1 AS p1, pos2 AS P2, char_field AS cf FROM UPDATABLE AS UPD LIMIT 1");
this.rs.next();
this.rs.close();
this.rs = null;
scrollableStmt.close();
scrollableStmt = null;
} finally {
if (this.rs != null) {
try {
this.rs.close();
} catch (SQLException
sqlEx) {
; // ignore
}
this.rs = null;
}
if (scrollableStmt != null) {
try {
scrollableStmt.close();
} catch (SQLException sqlEx) {
; // ignore
}
scrollableStmt = null;
}
}
}
}
/**
* Tests that the driver does not let you update result sets that come from
* tables that don't have primary keys
*
* @throws SQLException
* if an error occurs
*/
public void testBogusTable() throws SQLException {
this.stmt.executeUpdate("DROP TABLE IF EXISTS BOGUS_UPDATABLE");
this.stmt.executeUpdate("CREATE TABLE BOGUS_UPDATABLE (field1 int)");
Statement scrollableStmt = null;
try {
scrollableStmt = this.conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
this.rs = scrollableStmt
.executeQuery("SELECT * FROM BOGUS_UPDATABLE");
try {
this.rs.moveToInsertRow();
fail("ResultSet.moveToInsertRow() should not succeed on non-updatable table");
} catch (NotUpdatable noUpdate) {
// ignore
}
} finally {
if (scrollableStmt != null) {
try {
scrollableStmt.close();
} catch (SQLException sqlEx) {
;
}
}
this.stmt.executeUpdate("DROP TABLE IF EXISTS BOGUS_UPDATABLE");
}
}
/**
* Tests that the driver does not let you update result sets that come from
* queries that haven't selected all primary keys
*
* @throws SQLException
* if an error occurs
*/
public void testMultiKeyTable() throws SQLException {
this.stmt.executeUpdate("DROP TABLE IF EXISTS MULTI_UPDATABLE");
this.stmt
.executeUpdate("CREATE TABLE MULTI_UPDATABLE (field1 int NOT NULL, field2 int NOT NULL, PRIMARY KEY (field1, field2))");
Statement scrollableStmt = null;
try {
scrollableStmt = this.conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
this.rs = scrollableStmt
.executeQuery("SELECT field1 FROM MULTI_UPDATABLE");
try {