提交 96b953d6 authored 作者: Thomas Mueller's avatar Thomas Mueller

Improve code coverage.

上级 7e16c976
...@@ -18,7 +18,9 @@ Change Log ...@@ -18,7 +18,9 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>The database upgrade (to upgrade from H2 version 1.1.x) has been simplified. <ul><li>The built-in connection pool was simplified a bit.
The dispose() method no longer throws an exception (it only logs it).
</li><li>The database upgrade (to upgrade from H2 version 1.1.x) has been simplified.
</li><li>The feature to log all errors (system properties h2.logAllErrors and h2.logAllErrorsFile) has been removed. </li><li>The feature to log all errors (system properties h2.logAllErrors and h2.logAllErrorsFile) has been removed.
</li><li>When starting the H2 Console, the properties file can now be completely disabled. </li><li>When starting the H2 Console, the properties file can now be completely disabled.
</li><li>Server.openBrowser no longer writes to System.out directly if opening the URL failed. </li><li>Server.openBrowser no longer writes to System.out directly if opening the URL failed.
......
...@@ -78,7 +78,10 @@ implements ObjectFactory ...@@ -78,7 +78,10 @@ implements ObjectFactory
} }
//## Java 1.4 end ## //## Java 1.4 end ##
private TraceSystem getTraceSystem() { /**
* INTERNAL
*/
public static TraceSystem getTraceSystem() {
synchronized (JdbcDataSourceFactory.class) { synchronized (JdbcDataSourceFactory.class) {
if (cachedTraceSystem == null) { if (cachedTraceSystem == null) {
cachedTraceSystem = new TraceSystem(SysProperties.CLIENT_TRACE_DIRECTORY + "h2datasource" + Constants.SUFFIX_TRACE_FILE); cachedTraceSystem = new TraceSystem(SysProperties.CLIENT_TRACE_DIRECTORY + "h2datasource" + Constants.SUFFIX_TRACE_FILE);
......
...@@ -34,6 +34,7 @@ public class TestConnectionPool extends TestBase { ...@@ -34,6 +34,7 @@ public class TestConnectionPool extends TestBase {
public void test() throws Exception { public void test() throws Exception {
deleteDb("connectionPool"); deleteDb("connectionPool");
testShutdown();
testWrongUrl(); testWrongUrl();
testTimeout(); testTimeout();
testUncommittedTransaction(); testUncommittedTransaction();
...@@ -42,6 +43,21 @@ public class TestConnectionPool extends TestBase { ...@@ -42,6 +43,21 @@ public class TestConnectionPool extends TestBase {
testConnect(); testConnect();
testThreads(); testThreads();
deleteDb("connectionPool"); deleteDb("connectionPool");
deleteDb("connectionPool2");
}
private void testShutdown() throws SQLException {
String url = getURL("connectionPool2", true), user = getUser(), password = getPassword();
JdbcConnectionPool cp = JdbcConnectionPool.create(url, user, password);
StringWriter w = new StringWriter();
cp.setLogWriter(new PrintWriter(w));
Connection conn1 = cp.getConnection();
Connection conn2 = cp.getConnection();
conn1.close();
conn2.createStatement().execute("shutdown immediately");
cp.dispose();
assertTrue(w.toString().length() > 0);
cp.dispose();
} }
private void testWrongUrl() throws SQLException { private void testWrongUrl() throws SQLException {
...@@ -58,6 +74,12 @@ public class TestConnectionPool extends TestBase { ...@@ -58,6 +74,12 @@ public class TestConnectionPool extends TestBase {
String url = getURL("connectionPool", true), user = getUser(), password = getPassword(); String url = getURL("connectionPool", true), user = getUser(), password = getPassword();
final JdbcConnectionPool man = JdbcConnectionPool.create(url, user, password); final JdbcConnectionPool man = JdbcConnectionPool.create(url, user, password);
man.setLoginTimeout(1); man.setLoginTimeout(1);
try {
man.setMaxConnections(-1);
fail();
} catch (IllegalArgumentException e) {
// expected
}
man.setMaxConnections(2); man.setMaxConnections(2);
// connection 1 (of 2) // connection 1 (of 2)
Connection conn = man.getConnection(); Connection conn = man.getConnection();
...@@ -129,8 +151,8 @@ public class TestConnectionPool extends TestBase { ...@@ -129,8 +151,8 @@ public class TestConnectionPool extends TestBase {
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
man.getConnection().close(); man.getConnection().close();
} }
trace((int) (System.currentTimeMillis() - time));
man.dispose(); man.dispose();
trace((int) (System.currentTimeMillis() - time));
time = System.currentTimeMillis(); time = System.currentTimeMillis();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
DriverManager.getConnection(url, user, password).close(); DriverManager.getConnection(url, user, password).close();
...@@ -203,12 +225,24 @@ public class TestConnectionPool extends TestBase { ...@@ -203,12 +225,24 @@ public class TestConnectionPool extends TestBase {
} }
private void testConnect() throws SQLException { private void testConnect() throws SQLException {
JdbcConnectionPool man = getConnectionPool(3); JdbcConnectionPool pool = getConnectionPool(3);
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Connection conn = man.getConnection(); Connection conn = pool.getConnection();
conn.close(); conn.close();
} }
man.dispose(); pool.dispose();
try {
pool.getConnection();
fail();
} catch (IllegalStateException e) {
// expected
}
try {
pool.getConnection(null, null);
fail();
} catch (UnsupportedOperationException e) {
// expected
}
} }
} }
...@@ -22,6 +22,7 @@ import javax.transaction.xa.Xid; ...@@ -22,6 +22,7 @@ import javax.transaction.xa.Xid;
import org.h2.jdbcx.JdbcDataSource; import org.h2.jdbcx.JdbcDataSource;
import org.h2.jdbcx.JdbcDataSourceFactory; import org.h2.jdbcx.JdbcDataSourceFactory;
import org.h2.jdbcx.JdbcXAConnection; import org.h2.jdbcx.JdbcXAConnection;
import org.h2.message.TraceSystem;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/** /**
...@@ -63,6 +64,11 @@ public class TestDataSource extends TestBase { ...@@ -63,6 +64,11 @@ public class TestDataSource extends TestBase {
// } // }
public void test() throws Exception { public void test() throws Exception {
if (config.traceLevelFile > 0) {
TraceSystem sys = JdbcDataSourceFactory.getTraceSystem();
sys.setFileName(getBaseDir() + "/test/trace");
sys.setLevelFile(3);
}
testDataSourceFactory(); testDataSourceFactory();
testDataSource(); testDataSource();
testXAConnection(); testXAConnection();
...@@ -82,6 +88,7 @@ public class TestDataSource extends TestBase { ...@@ -82,6 +88,7 @@ public class TestDataSource extends TestBase {
ref.add(new StringRefAddr("description", "test")); ref.add(new StringRefAddr("description", "test"));
JdbcDataSource ds = (JdbcDataSource) factory.getObjectInstance(ref, null, null, null); JdbcDataSource ds = (JdbcDataSource) factory.getObjectInstance(ref, null, null, null);
assertEquals(1, ds.getLoginTimeout()); assertEquals(1, ds.getLoginTimeout());
assertEquals("test", ds.getDescription());
assertEquals("jdbc:h2:mem:", ds.getURL()); assertEquals("jdbc:h2:mem:", ds.getURL());
assertEquals("u", ds.getUser()); assertEquals("u", ds.getUser());
assertEquals("p", ds.getPassword()); assertEquals("p", ds.getPassword());
...@@ -97,17 +104,31 @@ public class TestDataSource extends TestBase { ...@@ -97,17 +104,31 @@ public class TestDataSource extends TestBase {
} }
private void testXAConnection() throws Exception { private void testXAConnection() throws Exception {
testXAConnection(false);
testXAConnection(true);
}
private void testXAConnection(boolean userInDataSource) throws Exception {
deleteDb("dataSource"); deleteDb("dataSource");
JdbcDataSource ds = new JdbcDataSource(); JdbcDataSource ds = new JdbcDataSource();
String url = getURL("dataSource", true); String url = getURL("dataSource", true);
String user = getUser(); String user = getUser();
ds.setURL(url); ds.setURL(url);
ds.setUser(user); if (userInDataSource) {
ds.setPassword(getPassword()); ds.setUser(user);
ds.setPassword(getPassword());
assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString()); }
if (userInDataSource) {
XAConnection xaConn = ds.getXAConnection(); assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString());
} else {
assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=", ds.toString());
}
XAConnection xaConn;
if (userInDataSource) {
xaConn = ds.getXAConnection();
} else {
xaConn = ds.getXAConnection(user, getPassword());
}
int traceId = ((JdbcXAConnection) xaConn).getTraceId(); int traceId = ((JdbcXAConnection) xaConn).getTraceId();
assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn")); assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn"));
...@@ -129,6 +150,7 @@ public class TestDataSource extends TestBase { ...@@ -129,6 +150,7 @@ public class TestDataSource extends TestBase {
assertFalse(res.isSameRM(null)); assertFalse(res.isSameRM(null));
Connection conn = xaConn.getConnection(); Connection conn = xaConn.getConnection();
assertEquals(user.toUpperCase(), conn.getMetaData().getUserName());
Xid[] list = res.recover(XAResource.TMSTARTRSCAN); Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
assertEquals(0, list.length); assertEquals(0, list.length);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
...@@ -146,10 +168,15 @@ public class TestDataSource extends TestBase { ...@@ -146,10 +168,15 @@ public class TestDataSource extends TestBase {
ds.setURL(getURL("dataSource", true)); ds.setURL(getURL("dataSource", true));
ds.setUser(getUser()); ds.setUser(getUser());
ds.setPassword(getPassword()); ds.setPassword(getPassword());
Connection conn = ds.getConnection(); Connection conn;
Statement stat = conn.createStatement(); conn = ds.getConnection();
Statement stat;
stat = conn.createStatement();
stat.execute("SELECT * FROM DUAL"); stat.execute("SELECT * FROM DUAL");
conn.close(); conn.close();
conn = ds.getConnection(getUser(), getPassword());
stat = conn.createStatement();
stat.execute("SELECT * FROM DUAL");
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论