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