use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testTcpServerWithoutPort.
private void testTcpServerWithoutPort() throws Exception {
Server s1 = Server.createTcpServer().start();
Server s2 = Server.createTcpServer().start();
assertTrue(s1.getPort() != s2.getPort());
s1.stop();
s2.stop();
s1 = Server.createTcpServer("-tcpPort", "9123").start();
assertEquals(9123, s1.getPort());
createClassProxy(Server.class);
assertThrows(ErrorCode.EXCEPTION_OPENING_PORT_2, Server.createTcpServer("-tcpPort", "9123")).start();
s1.stop();
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testBackupRestore.
private void testBackupRestore() throws SQLException {
org.h2.Driver.load();
String url = "jdbc:h2:" + getBaseDir() + "/testBackupRestore";
String user = "sa", password = "abc";
final String fileName = getBaseDir() + "/b2.zip";
DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testBackupRestore", "-quiet");
Connection conn = getConnection(url, user, password);
conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')");
conn.close();
Backup.main("-file", fileName, "-dir", getBaseDir(), "-db", "testBackupRestore", "-quiet");
DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testBackupRestore", "-quiet");
Restore.main("-file", fileName, "-dir", getBaseDir(), "-db", "testBackupRestore", "-quiet");
conn = getConnection("jdbc:h2:" + getBaseDir() + "/testBackupRestore", "sa", "abc");
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
assertTrue(rs.next());
assertFalse(rs.next());
new AssertThrows(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1) {
@Override
public void test() throws SQLException {
// must fail when the database is in use
Backup.main("-file", fileName, "-dir", getBaseDir(), "-db", "testBackupRestore");
}
};
conn.close();
DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testBackupRestore", "-quiet");
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testChangeFileEncryption.
private void testChangeFileEncryption(boolean split) throws SQLException {
org.h2.Driver.load();
final String dir = (split ? "split:19:" : "") + getBaseDir();
String url = "jdbc:h2:" + dir + "/testChangeFileEncryption;CIPHER=AES";
DeleteDbFiles.execute(dir, "testChangeFileEncryption", true);
Connection conn = getConnection(url, "sa", "abc 123");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB) " + "AS SELECT X, SPACE(3000) FROM SYSTEM_RANGE(1, 300)");
conn.close();
String[] args = { "-dir", dir, "-db", "testChangeFileEncryption", "-cipher", "AES", "-decrypt", "abc", "-quiet" };
ChangeFileEncryption.main(args);
args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-cipher", "AES", "-encrypt", "def", "-quiet" };
ChangeFileEncryption.main(args);
conn = getConnection(url, "sa", "def 123");
stat = conn.createStatement();
stat.execute("SELECT * FROM TEST");
new AssertThrows(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1) {
@Override
public void test() throws SQLException {
ChangeFileEncryption.main(new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-cipher", "AES", "-decrypt", "def", "-quiet" });
}
};
conn.close();
args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-quiet" };
DeleteDbFiles.main(args);
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testWrongServer.
private void testWrongServer() throws Exception {
// try to connect when the server is not running
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:9001/test");
final ServerSocket serverSocket = new ServerSocket(9001);
Task task = new Task() {
@Override
public void call() throws Exception {
while (!stop) {
Socket socket = serverSocket.accept();
byte[] data = new byte[1024];
data[0] = 'x';
OutputStream out = socket.getOutputStream();
out.write(data);
out.close();
socket.close();
}
}
};
try {
task.execute();
Thread.sleep(100);
try {
getConnection("jdbc:h2:tcp://localhost:9001/test");
fail();
} catch (SQLException e) {
assertEquals(ErrorCode.CONNECTION_BROKEN_1, e.getErrorCode());
}
} finally {
serverSocket.close();
task.getException();
}
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testChangeFileEncryptionWithWrongPassword.
private void testChangeFileEncryptionWithWrongPassword() throws SQLException {
if (config.mvStore) {
// doesn't detect wrong passwords
return;
}
org.h2.Driver.load();
final String dir = getBaseDir();
// TODO: this doesn't seem to work in MVSTORE mode yet
String url = "jdbc:h2:" + dir + "/testChangeFileEncryption;CIPHER=AES";
DeleteDbFiles.execute(dir, "testChangeFileEncryption", true);
Connection conn = getConnection(url, "sa", "abc 123");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB) " + "AS SELECT X, SPACE(3000) FROM SYSTEM_RANGE(1, 300)");
conn.close();
// try with wrong password, this used to have a bug where it kept the
// file handle open
new AssertThrows(SQLException.class) {
@Override
public void test() throws SQLException {
ChangeFileEncryption.execute(dir, "testChangeFileEncryption", "AES", "wrong".toCharArray(), "def".toCharArray(), true);
}
};
ChangeFileEncryption.execute(dir, "testChangeFileEncryption", "AES", "abc".toCharArray(), "def".toCharArray(), true);
conn = getConnection(url, "sa", "def 123");
stat = conn.createStatement();
stat.execute("SELECT * FROM TEST");
conn.close();
String[] args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-quiet" };
DeleteDbFiles.main(args);
}
Aggregations