SQLExecTestpublic class SQLExecTest extends TestCase Simple testcase to test for driver caching.
To test for your own database, you may need to tweak getProperties(int)
and add a couple of keys. see testOracle and testMySQL for an example.
It would be much better to extend this testcase by using HSQL
as the test db, so that a db is really used. |
Fields Summary |
---|
public static final int | NULL | public static final int | ORACLE | public static final int | MYSQL | public static final String | DRIVER | public static final String | USER | public static final String | PASSWORD | public static final String | URL | public static final String | PATH | public static final String | SQL | public static final String | NULL_DRIVER |
Constructors Summary |
---|
public SQLExecTest(String s)
super(s);
|
Methods Summary |
---|
protected SQLExec | createTask(java.util.Properties props)Create a task from a set of properties
SQLExec sql = new SQLExec();
sql.setProject( new Project() );
sql.setDriver( props.getProperty(DRIVER) );
sql.setUserid( props.getProperty(USER) );
sql.setPassword( props.getProperty(PASSWORD) );
sql.setUrl( props.getProperty(URL) );
sql.createClasspath().setLocation( new File(props.getProperty(PATH)) );
sql.addText( props.getProperty(SQL) );
return sql;
| protected void | doMultipleCalls(int calls, int database, boolean caching, boolean catchexception)run a sql tasks multiple times.
Properties props = getProperties(database);
for (int i = 0; i < calls; i++){
SQLExec sql = createTask(props);
sql.setCaching(caching);
try {
sql.execute();
} catch (BuildException e){
if (!catchexception){
throw e;
}
}
}
| protected java.lang.String | findResourcePath(java.lang.String resource)try to find the path from a resource (jar file or directory name)
so that it can be used as a classpath to load the resource.
resource = resource.replace('.", '/") + ".class";
URL url = getClass().getClassLoader().getResource(resource);
if (url == null) {
return null;
}
String u = url.toString();
if (u.startsWith("jar:file:")) {
int pling = u.indexOf("!");
return u.substring("jar:file:".length(), pling);
} else if (u.startsWith("file:")) {
int tail = u.indexOf(resource);
return u.substring("file:".length(), tail);
}
return null;
| protected java.util.Properties | getProperties(int database)returns a configuration associated to a specific database.
If you want to test on your specific base, you'd better
tweak this to make it run or add your own database.
The driver lib should be dropped into the system classloader.
Properties props = null;
switch (database){
case ORACLE:
props = getProperties("oracle.jdbc.driver.OracleDriver", "test", "test", "jdbc:oracle:thin:@127.0.0.1:1521:orcl");
break;
case MYSQL:
props = getProperties("org.gjt.mm.mysql.Driver", "test", "test", "jdbc:mysql://127.0.0.1:3306/test");
break;
case NULL:
default:
props = getProperties(NULL_DRIVER, "test", "test", "jdbc:database://hostname:port/name");
}
// look for the driver path...
String path = findResourcePath(props.getProperty(DRIVER));
props.put(PATH, path);
props.put(SQL, "create table OOME_TEST(X INTEGER NOT NULL);\ndrop table if exists OOME_TEST;");
return props;
| protected java.util.Properties | getProperties(java.lang.String driver, java.lang.String user, java.lang.String pwd, java.lang.String url)helper method to build properties
Properties props = new Properties();
props.put(DRIVER, driver);
props.put(USER, user);
props.put(PASSWORD, pwd);
props.put(URL, url);
return props;
| protected void | setUp()
// make sure the cache is cleared.
JDBCTask.getLoaderMap().clear();
| public void | testDriverCaching()
SQLExec sql = createTask(getProperties(NULL));
assertTrue(!SQLExec.getLoaderMap().containsKey(NULL_DRIVER));
try {
sql.execute();
} catch (BuildException e){
assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1);
}
assertTrue(SQLExec.getLoaderMap().containsKey(NULL_DRIVER));
assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER));
ClassLoader loader1 = sql.getLoader();
// 2nd run..
sql = createTask(getProperties(NULL));
// the driver must still be cached.
assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER));
try {
sql.execute();
} catch (BuildException e){
assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1);
}
assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER));
assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER));
assertSame(loader1, sql.getLoader());
| public void | testNull()
doMultipleCalls(1000, NULL, true, true);
|
|