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

Improve code coverage.

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