提交 fe722349 authored 作者: Tomas Pospichal's avatar Tomas Pospichal

Ensure tool tests do not leave running servers after failure

上级 ddd4d28c
...@@ -33,6 +33,7 @@ import java.sql.Time; ...@@ -33,6 +33,7 @@ import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Random; import java.util.Random;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
...@@ -64,6 +65,7 @@ public class TestTools extends TestBase { ...@@ -64,6 +65,7 @@ public class TestTools extends TestBase {
private static String lastUrl; private static String lastUrl;
private Server server; private Server server;
private List<Server> remainingServers = new ArrayList<Server>(3);
/** /**
* Run just this test. * Run just this test.
...@@ -171,7 +173,6 @@ public class TestTools extends TestBase { ...@@ -171,7 +173,6 @@ public class TestTools extends TestBase {
assertThrows(ErrorCode.EXCEPTION_OPENING_PORT_2, c).runTool("-web", assertThrows(ErrorCode.EXCEPTION_OPENING_PORT_2, c).runTool("-web",
"-webPort", "9002", "-tcp", "-tcpPort", "9002"); "-webPort", "9002", "-tcp", "-tcpPort", "9002");
c.runTool("-web", "-webPort", "9002"); c.runTool("-web", "-webPort", "9002");
c.shutdown();
} finally { } finally {
if (old != null) { if (old != null) {
...@@ -179,6 +180,7 @@ public class TestTools extends TestBase { ...@@ -179,6 +180,7 @@ public class TestTools extends TestBase {
} else { } else {
System.clearProperty(SysProperties.H2_BROWSER); System.clearProperty(SysProperties.H2_BROWSER);
} }
c.shutdown();
} }
} }
...@@ -486,16 +488,19 @@ public class TestTools extends TestBase { ...@@ -486,16 +488,19 @@ public class TestTools extends TestBase {
} }
} }
}; };
task.execute();
Thread.sleep(100);
try { try {
getConnection("jdbc:h2:tcp://localhost:9001/test"); task.execute();
fail(); Thread.sleep(100);
} catch (SQLException e) { try {
assertEquals(ErrorCode.CONNECTION_BROKEN_1, e.getErrorCode()); getConnection("jdbc:h2:tcp://localhost:9001/test");
fail();
} catch (SQLException e) {
assertEquals(ErrorCode.CONNECTION_BROKEN_1, e.getErrorCode());
}
} finally {
serverSocket.close();
task.getException();
} }
serverSocket.close();
task.getException();
} }
private void testDeleteFiles() throws SQLException { private void testDeleteFiles() throws SQLException {
...@@ -527,73 +532,84 @@ public class TestTools extends TestBase { ...@@ -527,73 +532,84 @@ public class TestTools extends TestBase {
String result; String result;
Connection conn; Connection conn;
result = runServer(0, new String[]{"-?"}); try {
assertTrue(result.contains("Starts the H2 Console")); result = runServer(0, new String[]{"-?"});
assertTrue(result.indexOf("Unknown option") < 0); assertTrue(result.contains("Starts the H2 Console"));
assertTrue(result.indexOf("Unknown option") < 0);
result = runServer(1, new String[]{"-xy"});
assertTrue(result.contains("Starts the H2 Console")); result = runServer(1, new String[]{"-xy"});
assertTrue(result.contains("Feature not supported")); assertTrue(result.contains("Starts the H2 Console"));
result = runServer(0, new String[]{"-tcp", assertTrue(result.contains("Feature not supported"));
"-tcpPort", "9001", "-tcpPassword", "abc"}); result = runServer(0, new String[]{"-tcp",
assertTrue(result.contains("tcp://")); "-tcpPort", "9001", "-tcpPassword", "abc"});
assertTrue(result.contains(":9001")); assertTrue(result.contains("tcp://"));
assertTrue(result.contains("only local")); assertTrue(result.contains(":9001"));
assertTrue(result.indexOf("Starts the H2 Console") < 0); assertTrue(result.contains("only local"));
conn = getConnection("jdbc:h2:tcp://localhost:9001/mem:", "sa", "sa"); assertTrue(result.indexOf("Starts the H2 Console") < 0);
conn.close(); conn = getConnection("jdbc:h2:tcp://localhost:9001/mem:", "sa", "sa");
result = runServer(0, new String[]{"-tcpShutdown", conn.close();
"tcp://localhost:9001", "-tcpPassword", "abc", "-tcpShutdownForce"}); result = runServer(0, new String[]{"-tcpShutdown",
assertTrue(result.contains("Shutting down")); "tcp://localhost:9001", "-tcpPassword", "abc", "-tcpShutdownForce"});
assertTrue(result.contains("Shutting down"));
} finally {
shutdownServers();
}
} }
private void testSSL() throws SQLException { private void testSSL() throws SQLException {
String result; String result;
Connection conn; Connection conn;
result = runServer(0, new String[]{"-tcp", try {
"-tcpAllowOthers", "-tcpPort", "9001", "-tcpPassword", "abcdef", "-tcpSSL"}); result = runServer(0, new String[]{"-tcp",
assertTrue(result.contains("ssl://")); "-tcpAllowOthers", "-tcpPort", "9001", "-tcpPassword", "abcdef", "-tcpSSL"});
assertTrue(result.contains(":9001")); assertTrue(result.contains("ssl://"));
assertTrue(result.contains("others can")); assertTrue(result.contains(":9001"));
assertTrue(result.indexOf("Starts the H2 Console") < 0); assertTrue(result.contains("others can"));
conn = getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa"); assertTrue(result.indexOf("Starts the H2 Console") < 0);
conn.close(); conn = getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa");
conn.close();
result = runServer(0, new String[]{"-tcpShutdown", result = runServer(0, new String[]{"-tcpShutdown",
"ssl://localhost:9001", "-tcpPassword", "abcdef"}); "ssl://localhost:9001", "-tcpPassword", "abcdef"});
assertTrue(result.contains("Shutting down")); assertTrue(result.contains("Shutting down"));
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this). assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).
getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa"); getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa");
result = runServer(0, new String[]{ result = runServer(0, new String[]{
"-web", "-webPort", "9002", "-webAllowOthers", "-webSSL", "-web", "-webPort", "9002", "-webAllowOthers", "-webSSL",
"-pg", "-pgAllowOthers", "-pgPort", "9003", "-pg", "-pgAllowOthers", "-pgPort", "9003",
"-tcp", "-tcpAllowOthers", "-tcpPort", "9006", "-tcpPassword", "abc"}); "-tcp", "-tcpAllowOthers", "-tcpPort", "9006", "-tcpPassword", "abc"});
Server stop = server; Server stop = server;
assertTrue(result.contains("https://")); assertTrue(result.contains("https://"));
assertTrue(result.contains(":9002")); assertTrue(result.contains(":9002"));
assertTrue(result.contains("pg://")); assertTrue(result.contains("pg://"));
assertTrue(result.contains(":9003")); assertTrue(result.contains(":9003"));
assertTrue(result.contains("others can")); assertTrue(result.contains("others can"));
assertTrue(result.indexOf("only local") < 0); assertTrue(result.indexOf("only local") < 0);
assertTrue(result.contains("tcp://")); assertTrue(result.contains("tcp://"));
assertTrue(result.contains(":9006")); assertTrue(result.contains(":9006"));
conn = getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa"); conn = getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa");
conn.close(); conn.close();
result = runServer(0, new String[]{"-tcpShutdown", result = runServer(0, new String[]{"-tcpShutdown",
"tcp://localhost:9006", "-tcpPassword", "abc", "-tcpShutdownForce"}); "tcp://localhost:9006", "-tcpPassword", "abc", "-tcpShutdownForce"});
assertTrue(result.contains("Shutting down")); assertTrue(result.contains("Shutting down"));
stop.shutdown(); stop.shutdown();
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this). assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).
getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa"); getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa");
} finally {
shutdownServers();
}
} }
private String runServer(int exitCode, String... args) { private String runServer(int exitCode, String... args) {
ByteArrayOutputStream buff = new ByteArrayOutputStream(); ByteArrayOutputStream buff = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(buff); PrintStream ps = new PrintStream(buff);
if (server != null) {
remainingServers.add(server);
}
server = new Server(); server = new Server();
server.setOut(ps); server.setOut(ps);
int result = 0; int result = 0;
...@@ -609,6 +625,18 @@ public class TestTools extends TestBase { ...@@ -609,6 +625,18 @@ public class TestTools extends TestBase {
return s; return s;
} }
private void shutdownServers() {
for (Server remainingServer : remainingServers) {
if (remainingServer != null) {
remainingServer.shutdown();
}
}
remainingServers.clear();
if (server != null) {
server.shutdown();
}
}
private void testConvertTraceFile() throws Exception { private void testConvertTraceFile() throws Exception {
deleteDb("toolsConvertTraceFile"); deleteDb("toolsConvertTraceFile");
org.h2.Driver.load(); org.h2.Driver.load();
...@@ -1018,71 +1046,77 @@ public class TestTools extends TestBase { ...@@ -1018,71 +1046,77 @@ public class TestTools extends TestBase {
private void testServer() throws SQLException { private void testServer() throws SQLException {
Connection conn; Connection conn;
deleteDb("test"); try {
Server tcpServer = Server.createTcpServer( deleteDb("test");
"-baseDir", getBaseDir(), Server tcpServer = Server.createTcpServer(
"-tcpPort", "9192", "-baseDir", getBaseDir(),
"-tcpAllowOthers").start(); "-tcpPort", "9192",
conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", ""); "-tcpAllowOthers").start();
conn.close(); remainingServers.add(tcpServer);
// must not be able to use a different base dir conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
new AssertThrows(ErrorCode.IO_EXCEPTION_1) { conn.close();
@Override // must not be able to use a different base dir
public void test() throws SQLException { new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
getConnection("jdbc:h2:tcp://localhost:9192/../test", "sa", ""); @Override
}}; public void test() throws SQLException {
new AssertThrows(ErrorCode.IO_EXCEPTION_1) { getConnection("jdbc:h2:tcp://localhost:9192/../test", "sa", "");
@Override }};
public void test() throws SQLException { new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
getConnection("jdbc:h2:tcp://localhost:9192/../test2/test", "sa", ""); @Override
}}; public void test() throws SQLException {
tcpServer.stop(); getConnection("jdbc:h2:tcp://localhost:9192/../test2/test", "sa", "");
Server.createTcpServer( }};
"-ifExists", tcpServer.stop();
"-tcpPassword", "abc", Server tcpServerWithPassword = Server.createTcpServer(
"-baseDir", getBaseDir(), "-ifExists",
"-tcpPort", "9192").start(); "-tcpPassword", "abc",
// must not be able to create new db "-baseDir", getBaseDir(),
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) { "-tcpPort", "9192").start();
@Override remainingServers.add(tcpServerWithPassword);
public void test() throws SQLException { // must not be able to create new db
getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", ""); new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
}}; @Override
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) { public void test() throws SQLException {
@Override getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", "");
public void test() throws SQLException { }};
getConnection("jdbc:h2:tcp://localhost:9192/test2;ifexists=false", "sa", ""); new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
}}; @Override
conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", ""); public void test() throws SQLException {
conn.close(); getConnection("jdbc:h2:tcp://localhost:9192/test2;ifexists=false", "sa", "");
new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) { }};
@Override conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
public void test() throws SQLException { conn.close();
Server.shutdownTcpServer("tcp://localhost:9192", "", true, false); new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) {
}}; @Override
conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", ""); public void test() throws SQLException {
// conn.close(); Server.shutdownTcpServer("tcp://localhost:9192", "", true, false);
Server.shutdownTcpServer("tcp://localhost:9192", "abc", true, false); }};
// check that the database is closed conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
deleteDb("test"); // conn.close();
// server must have been closed Server.shutdownTcpServer("tcp://localhost:9192", "abc", true, false);
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this). // check that the database is closed
getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", ""); deleteDb("test");
JdbcUtils.closeSilently(conn); // server must have been closed
// Test filesystem prefix and escape from baseDir assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).
deleteDb("testSplit"); getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
server = Server.createTcpServer( JdbcUtils.closeSilently(conn);
"-baseDir", getBaseDir(), // Test filesystem prefix and escape from baseDir
"-tcpPort", "9192", deleteDb("testSplit");
"-tcpAllowOthers").start(); server = Server.createTcpServer(
conn = getConnection("jdbc:h2:tcp://localhost:9192/split:testSplit", "sa", ""); "-baseDir", getBaseDir(),
conn.close(); "-tcpPort", "9192",
"-tcpAllowOthers").start();
conn = getConnection("jdbc:h2:tcp://localhost:9192/split:testSplit", "sa", "");
conn.close();
assertThrows(ErrorCode.IO_EXCEPTION_1, this). assertThrows(ErrorCode.IO_EXCEPTION_1, this).
getConnection("jdbc:h2:tcp://localhost:9192/../test", "sa", ""); getConnection("jdbc:h2:tcp://localhost:9192/../test", "sa", "");
server.stop(); server.stop();
deleteDb("testSplit"); deleteDb("testSplit");
} finally {
shutdownServers();
}
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论