提交 3523c1df authored 作者: Thomas Mueller's avatar Thomas Mueller

Improve code coverage.

上级 d526e92a
......@@ -11,13 +11,11 @@ import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;
import org.h2.engine.Constants;
import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException;
import org.h2.message.TraceSystem;
import org.h2.upgrade.DbUpgrade;
import org.h2.util.StringUtils;
/**
* The database driver. An application should not use this class directly. The
......@@ -57,17 +55,9 @@ public class Driver implements java.sql.Driver {
if (!acceptsURL(url)) {
return null;
}
boolean noUpgrade = StringUtils.toUpperEnglish(url).indexOf(";NO_UPGRADE=TRUE") >= 0;
url = StringUtils.replaceAllIgnoreCase(url, ";NO_UPGRADE=TRUE", "");
if (DbUpgrade.areUpgradeClassesPresent()) {
if (noUpgrade) {
Connection connection = DbUpgrade.connectWithOldVersion(url, info);
if (connection != null) {
return connection;
}
} else {
DbUpgrade.upgrade(url, info);
}
Connection conn = DbUpgrade.connectOrUpgrade(url, info);
if (conn != null) {
return conn;
}
return new JdbcConnection(url, info);
} catch (Exception e) {
......
......@@ -27,22 +27,33 @@ import org.h2.util.Utils;
public class DbUpgrade {
private static boolean upgradeClassesPresent;
private static Map<String, DbUpgradeFromVersion1> runningConversions;
static {
// static initialize block
upgradeClassesPresent = Utils.isClassPresent("org.h2.upgrade.v1_1.Driver");
runningConversions = Collections.synchronizedMap(new Hashtable<String, DbUpgradeFromVersion1>(1));
}
/**
* Check if the H2 version 1.1 driver is in in the classpath.
* If the upgrade classes are present, upgrade the database, or connect
* using the old version (if the parameter NO_UPGRADE is set to true). If
* the database is upgraded, or if no upgrade is possible or needed, this
* methods returns null.
*
* @return true if it is
* @param url the database URL
* @param info the properties
* @return the connection if NO_UPGRADE is set
*/
public static boolean areUpgradeClassesPresent() {
return upgradeClassesPresent;
public static synchronized Connection connectOrUpgrade(String url, Properties info) throws SQLException {
if (!upgradeClassesPresent) {
return null;
}
if (StringUtils.toUpperEnglish(url).indexOf(";NO_UPGRADE=TRUE") >= 0) {
url = StringUtils.replaceAllIgnoreCase(url, ";NO_UPGRADE=TRUE", "");
return connectWithOldVersion(url, info);
}
upgrade(url, info);
return null;
}
/**
......@@ -53,7 +64,7 @@ public class DbUpgrade {
* @return the connection via the upgrade classes
* @throws SQLException
*/
public static Connection connectWithOldVersion(String url, Properties info) throws SQLException {
private static Connection connectWithOldVersion(String url, Properties info) throws SQLException {
try {
String oldStartUrlPrefix = (String) Utils.getStaticField("org.h2.upgrade.v1_1.engine.Constants.START_URL");
url = StringUtils.replaceAll(url, org.h2.engine.Constants.START_URL, oldStartUrlPrefix);
......@@ -97,8 +108,7 @@ public class DbUpgrade {
* @param info The connection properties
* @throws SQLException
*/
public static synchronized void upgrade(String url, Properties info) throws SQLException {
if (upgradeClassesPresent) {
private static synchronized void upgrade(String url, Properties info) throws SQLException {
if (runningConversions.containsKey(url)) {
// do not migrate, because we are currently migrating, and this is
// the connection where "runscript from" will be executed
......@@ -112,6 +122,5 @@ public class DbUpgrade {
runningConversions.remove(url);
}
}
}
}
......@@ -243,6 +243,10 @@ public class Build extends BuildBase {
}
private void downloadTest() {
// for TestUpgrade
download("ext/h2mig_pagestore_addon.jar",
"http://h2database.com/h2mig_pagestore_addon.jar",
"6dfafe1b86959c3ba4f7cf03e99535e8b9719965");
// for TestOldVersion
download("ext/h2-1.2.127.jar",
"http://repo1.maven.org/maven2/com/h2database/h2/1.2.127/h2-1.2.127.jar",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论