【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“BaseTestCase.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;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.Blob;
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 com.mysql.jdbc.NonRegisteringDriver;
import junit.framework.TestCase;
/**
* Base class for all test cases. Creates connections, statements, etc. and
* closes them.
*
* @author Mark Matthews
* @version $Id: BaseTestCase.java 4627 2005-11-28 23:43:36Z mmatthews $
*/
public abstract class BaseTestCase extends TestCase {
private final static String ADMIN_CONNECTION_PROPERTY_NAME = "com.mysql.jdbc.testsuite.admin-url";
private final static String NO_MULTI_HOST_PROPERTY_NAME = "com.mysql.jdbc.testsuite.no-multi-hosts-tests";
/**
* JDBC URL, initialized from com.mysql.jdbc.testsuite.url system property,
* or defaults to jdbc:mysql:///test
*/
protected static String dbUrl = "jdbc:mysql:///test";
/** Instance counter */
private static int instanceCount = 1;
/** Connection to server, initialized in setUp() Cleaned up in tearDown(). */
protected Connection conn = null;
/** list of Tables to be dropped in tearDown */
private List createdTables;
/** The driver to use */
protected String dbClass = "com.mysql.jdbc.Driver";
/** My instance number */
private int myInstanceNumber = 0;
private boolean runningOnJdk131 = false;
/**
* PreparedStatement to be used in tests, not initialized. Cleaned up in
* tearDown().
*/
protected PreparedStatement pstmt = null;
/**
* ResultSet to be used in tests, not initialized. Cleaned up in tearDown().
*/
protected ResultSet rs = null;
/**
* Statement to be used in tests, initialized in setUp(). Cleaned up in
* tearDown().
*/
protected Statement stmt = null;
/**
* Creates a new BaseTestCase object.
*
* @param name
* The name of the JUnit test case
*/
public BaseTestCase(String name) {
super(name);
this.myInstanceNumber = instanceCount++;
String newDbUrl = System.getProperty("com.mysql.jdbc.testsuite.url");
if ((newDbUrl != null) && (newDbUrl.trim().length() != 0)) {
dbUrl = newDbUrl;
} else {
String defaultDbUrl = System.getProperty("com.mysql.jdbc.testsuite.url.default");
if ((defaultDbUrl != null) && (defaultDbUrl.trim().length() != 0)) {
dbUrl = defaultDbUrl;
}
}
String newDriver = System
.getProperty("com.mysql.jdbc.testsuite.driver");
if ((newDriver != null) && (newDriver.trim().length() != 0)) {
this.dbClass = newDriver;
}
try {
Blob.class.getMethod("truncate", new Class[] {Long.TYPE});
this.runningOnJdk131 = false;
} catch (NoSuchMethodException nsme) {
this.runningOnJdk131 = true;
}
}
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);
this.stmt.executeUpdate(createSql.toString());
}
protected void dropTable(String tableName) throws SQLException {
this.stmt.executeUpdate("DROP TABLE IF EXISTS " + tableName);
}
protected Connection getAdminConnection() throws SQLException {
return getAdminConnectionWithProps(new Properties());
}
protected Connection getAdminConnectionWithProps(Properties props)
throws SQLException {
String adminUrl = System.getProperty(ADMIN_CONNECTION_PROPERTY_NAME);
if (adminUrl != null) {
return DriverManager.getConnection(adminUrl, props);
} else {
return null;
}
}
/**
* 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
* DOCUMENT ME!
*/
protected Connection getConnectionWithProps(Properties props)
throws SQLException {
return DriverManager.getConnection(dbUrl, props);
}
protected Connection getConnectionWithProps(String url, Properties props)
throws SQLException {
return DriverManager.getConnection(url, props);
}
/**
* Returns the per-instance counter (for messages when multi-threading
* stress tests)
*
* @return int the instance number
*/
protected int getInstanceNumber() {
return this.myInstanceNumber;
}
protected String getMysqlVariable(Connection c, String variableName)
throws SQLException {
Object value = getSingleIndexedValueWithQuery(c, 2,
"SHOW VARIABLES LIKE '" + variableName + "'");
if (value != null) {
return value.toString();
}
return null;
}
/**
* Returns the named MySQL variable from the currently connected server.
*
* @param variableName
* the name of the variable to return
*
* @return the value of the given variable, or NULL if it doesn't exist
*
* @throws SQLException
* if an error occurs
*/
protected String getMysqlVariable(String variableName) throws SQLException {
return getMysqlVariable(this.conn, variableName);
}
/**
* Returns the properties that represent the default URL used for
* connections for all testcases.
*
* @return properties parsed from com.mysql.jdbc.testsuite.url
*
* @throws SQLException
* if parsing fails
*/
protected Properties getPropertiesFromTestsuiteUrl() throws SQLException {
Properties props = new NonRegisteringDriver().parseURL(dbUrl, null);
String hostname = props
.getProperty(NonRegisteringDriver.HOST
上一篇:
BaseTagHandler.java
下一篇:
经济民主一种关于劳动的政治经济学