Unverified 提交 f07266ae authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #784 from andreitokar/use_floating_ports

Hardcoded port numbers should not be used in unit tests
......@@ -1056,8 +1056,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
DeleteDbFiles.execute(TestBase.BASE_TEST_DIR, null, true);
FileUtils.deleteRecursive("trace.db", false);
if (networked) {
String[] args = ssl ? new String[] { "-tcpSSL", "-tcpPort", "9192" }
: new String[] { "-tcpPort", "9192" };
String[] args = ssl ? new String[] { "-tcpSSL" } : new String[0];
server = Server.createTcpServer(args);
try {
server.start();
......@@ -1079,6 +1078,10 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
FileUtils.deleteRecursive(TestBase.BASE_TEST_DIR, true);
}
public int getPort() {
return server == null ? 9192 : server.getPort();
}
/**
* Print system information.
*/
......
......@@ -265,9 +265,9 @@ public abstract class TestBase {
}
if (config.networked) {
if (config.ssl) {
url = "ssl://localhost:9192/" + name;
url = "ssl://localhost:"+config.getPort()+"/" + name;
} else {
url = "tcp://localhost:9192/" + name;
url = "tcp://localhost:"+config.getPort()+"/" + name;
}
} else if (config.googleAppEngine) {
url = "gae://" + name +
......
......@@ -66,17 +66,18 @@ public class TestReadOnly extends TestBase {
"jdbc:h2:zip:"+dir+"/readonly.zip!/readonlyInZip", getUser(), getPassword());
conn.createStatement().execute("select * from test where id=1");
conn.close();
Server server = Server.createTcpServer("-tcpPort", "9081", "-baseDir", dir);
Server server = Server.createTcpServer("-baseDir", dir);
server.start();
int port = server.getPort();
try {
conn = getConnection(
"jdbc:h2:tcp://localhost:9081/zip:readonly.zip!/readonlyInZip",
"jdbc:h2:tcp://localhost:" + port + "/zip:readonly.zip!/readonlyInZip",
getUser(), getPassword());
conn.createStatement().execute("select * from test where id=1");
conn.close();
FilePathZip2.register();
conn = getConnection(
"jdbc:h2:tcp://localhost:9081/zip2:readonly.zip!/readonlyInZip",
"jdbc:h2:tcp://localhost:" + port + "/zip2:readonly.zip!/readonlyInZip",
getUser(), getPassword());
conn.createStatement().execute("select * from test where id=1");
conn.close();
......
......@@ -93,8 +93,9 @@ public class TestAutoReconnect extends TestBase {
"AUTO_SERVER=TRUE;OPEN_NEW=TRUE";
restart();
} else {
server = Server.createTcpServer("-tcpPort", "8181").start();
url = "jdbc:h2:tcp://localhost:8181/" + getBaseDir() + "/" + getTestName() + ";" +
server = Server.createTcpServer().start();
int port = server.getPort();
url = "jdbc:h2:tcp://localhost:" + port + "/" + getBaseDir() + "/" + getTestName() + ";" +
"FILE_LOCK=SOCKET;AUTO_RECONNECT=TRUE";
}
......
......@@ -114,17 +114,18 @@ public class TestOldVersion extends TestBase {
}
private void testOldClientNewServer() throws Exception {
Server server = org.h2.tools.Server.createTcpServer("-tcpPort", "9001");
Server server = org.h2.tools.Server.createTcpServer();
server.start();
int port = server.getPort();
assertThrows(ErrorCode.DRIVER_VERSION_ERROR_2, driver).connect(
"jdbc:h2:tcp://localhost:9001/mem:test", null);
"jdbc:h2:tcp://localhost:" + port + "/mem:test", null);
server.stop();
Class<?> serverClass = cl.loadClass("org.h2.tools.Server");
Method m;
m = serverClass.getMethod("createTcpServer", String[].class);
Object serverOld = m.invoke(null, new Object[] { new String[] {
"-tcpPort", "9001" } });
"-tcpPort", "" + port } });
m = serverOld.getClass().getMethod("start");
m.invoke(serverOld);
Connection conn;
......
......@@ -826,10 +826,9 @@ public class TestTools extends TestBase {
int count = getSize(2, 10);
for (int i = 0; i < count; i++) {
Server tcpServer = Server.
createTcpServer("-tcpPort", "9192").start();
createTcpServer().start();
tcpServer.stop();
tcpServer = Server.createTcpServer("-tcpPassword", "abc",
"-tcpPort", "9192").start();
tcpServer = Server.createTcpServer("-tcpPassword", "abc").start();
tcpServer.stop();
}
}
......@@ -1066,67 +1065,67 @@ public class TestTools extends TestBase {
deleteDb("test");
Server tcpServer = Server.createTcpServer(
"-baseDir", getBaseDir(),
"-tcpPort", "9192",
"-tcpAllowOthers").start();
remainingServers.add(tcpServer);
conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
final int port = tcpServer.getPort();
conn = getConnection("jdbc:h2:tcp://localhost:"+ port +"/test", "sa", "");
conn.close();
// must not be able to use a different base dir
new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/../test", "sa", "");
getConnection("jdbc:h2:tcp://localhost:"+ port +"/../test", "sa", "");
}};
new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/../test2/test", "sa", "");
getConnection("jdbc:h2:tcp://localhost:"+port+"/../test2/test", "sa", "");
}};
tcpServer.stop();
Server tcpServerWithPassword = Server.createTcpServer(
"-ifExists",
"-tcpPassword", "abc",
"-baseDir", getBaseDir(),
"-tcpPort", "9192").start();
"-baseDir", getBaseDir()).start();
final int prt = tcpServerWithPassword.getPort();
remainingServers.add(tcpServerWithPassword);
// must not be able to create new db
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", "");
getConnection("jdbc:h2:tcp://localhost:"+prt+"/test2", "sa", "");
}};
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/test2;ifexists=false", "sa", "");
getConnection("jdbc:h2:tcp://localhost:"+prt+"/test2;ifexists=false", "sa", "");
}};
conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
conn = getConnection("jdbc:h2:tcp://localhost:"+prt+"/test", "sa", "");
conn.close();
new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) {
@Override
public void test() throws SQLException {
Server.shutdownTcpServer("tcp://localhost:9192", "", true, false);
Server.shutdownTcpServer("tcp://localhost:"+prt, "", true, false);
}};
conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
conn = getConnection("jdbc:h2:tcp://localhost:"+prt+"/test", "sa", "");
// conn.close();
Server.shutdownTcpServer("tcp://localhost:9192", "abc", true, false);
Server.shutdownTcpServer("tcp://localhost:"+prt, "abc", true, false);
// check that the database is closed
deleteDb("test");
// server must have been closed
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).
getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
getConnection("jdbc:h2:tcp://localhost:"+prt+"/test", "sa", "");
JdbcUtils.closeSilently(conn);
// Test filesystem prefix and escape from baseDir
deleteDb("testSplit");
server = Server.createTcpServer(
"-baseDir", getBaseDir(),
"-tcpPort", "9192",
"-tcpAllowOthers").start();
conn = getConnection("jdbc:h2:tcp://localhost:9192/split:testSplit", "sa", "");
final int p = server.getPort();
conn = getConnection("jdbc:h2:tcp://localhost:"+p+"/split:testSplit", "sa", "");
conn.close();
assertThrows(ErrorCode.IO_EXCEPTION_1, this).
getConnection("jdbc:h2:tcp://localhost:9192/../test", "sa", "");
getConnection("jdbc:h2:tcp://localhost:"+p+"/../test", "sa", "");
server.stop();
deleteDb("testSplit");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论