Search in sources :

Example 81 with In

use of org.h2.dev.util.BinaryArithmeticStream.In 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 82 with In

use of org.h2.dev.util.BinaryArithmeticStream.In 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)

Example 83 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class TestTraceSystem method testAdapter.

private void testAdapter() {
    TraceSystem ts = new TraceSystem(null);
    ts.setName("test");
    ts.setLevelFile(TraceSystem.ADAPTER);
    ts.getTrace("test").debug("test");
    ts.getTrace("test").info("test");
    ts.getTrace("test").error(new Exception(), "test");
    // The used SLF4J-nop logger has all log levels disabled,
    // so this should be reflected in the trace system.
    assertFalse(ts.isEnabled(TraceSystem.INFO));
    assertFalse(ts.getTrace("test").isInfoEnabled());
    ts.close();
}
Also used : TraceSystem(org.h2.message.TraceSystem)

Example 84 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class TestPgServer method createPgServer.

private Server createPgServer(String... args) throws SQLException {
    Server server = Server.createPgServer(args);
    int failures = 0;
    for (; ; ) {
        try {
            server.start();
            return server;
        } catch (SQLException e) {
            // the sleeps are too mitigate "port in use" exceptions on Jenkins
            if (e.getErrorCode() != ErrorCode.EXCEPTION_OPENING_PORT_2 || ++failures > 10) {
                throw e;
            }
            println("Sleeping");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e2) {
                throw new RuntimeException(e2);
            }
        }
    }
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException)

Example 85 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class TestReader method test.

@Override
public void test() throws Exception {
    String s = "\u00ef\u00f6\u00fc";
    StringReader r = new StringReader(s);
    InputStream in = new ReaderInputStream(r);
    byte[] buff = IOUtils.readBytesAndClose(in, 0);
    InputStream in2 = new ByteArrayInputStream(buff);
    Reader r2 = IOUtils.getBufferedReader(in2);
    String s2 = IOUtils.readStringAndClose(r2, Integer.MAX_VALUE);
    assertEquals(s, s2);
}
Also used : ReaderInputStream(org.h2.dev.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInputStream(org.h2.dev.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader)

Aggregations

SQLException (java.sql.SQLException)63 Connection (java.sql.Connection)59 DbException (org.h2.message.DbException)56 PreparedStatement (java.sql.PreparedStatement)54 ResultSet (java.sql.ResultSet)47 Statement (java.sql.Statement)44 Value (org.h2.value.Value)40 IOException (java.io.IOException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)30 InputStream (java.io.InputStream)29 Column (org.h2.table.Column)24 ArrayList (java.util.ArrayList)23 SimpleResultSet (org.h2.tools.SimpleResultSet)23 Random (java.util.Random)19 Expression (org.h2.expression.Expression)18 JdbcConnection (org.h2.jdbc.JdbcConnection)18 Index (org.h2.index.Index)16 ValueString (org.h2.value.ValueString)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)15