Search in sources :

Example 1 with AssertThrows

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();
}
Also used : Server(org.h2.tools.Server)

Example 2 with AssertThrows

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");
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 3 with AssertThrows

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);
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 4 with AssertThrows

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();
    }
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 5 with AssertThrows

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);
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Aggregations

Connection (java.sql.Connection)40 Statement (java.sql.Statement)37 PreparedStatement (java.sql.PreparedStatement)35 ResultSet (java.sql.ResultSet)17 JdbcConnection (org.h2.jdbc.JdbcConnection)16 AssertThrows (org.h2.test.utils.AssertThrows)12 SQLException (java.sql.SQLException)8 JdbcSQLException (org.h2.jdbc.JdbcSQLException)8 SimpleResultSet (org.h2.tools.SimpleResultSet)8 CallableStatement (java.sql.CallableStatement)7 Server (org.h2.tools.Server)7 IOException (java.io.IOException)4 Clob (java.sql.Clob)4 Task (org.h2.util.Task)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 BigDecimal (java.math.BigDecimal)3 FileChannel (java.nio.channels.FileChannel)3 Savepoint (java.sql.Savepoint)3