Search in sources :

Example 41 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestStreamStore method testExceptionDuringStore.

private void testExceptionDuringStore() throws IOException {
    // test that if there is an IOException while storing
    // the data, the entries in the map are "rolled back"
    HashMap<Long, byte[]> map = new HashMap<>();
    StreamStore s = new StreamStore(map);
    s.setMaxBlockSize(1024);
    assertThrows(IOException.class, s).put(createFailingStream(new IOException()));
    assertEquals(0, map.size());
    // the runtime exception is converted to an IOException
    assertThrows(IOException.class, s).put(createFailingStream(new IllegalStateException()));
    assertEquals(0, map.size());
}
Also used : StreamStore(org.h2.mvstore.StreamStore) HashMap(java.util.HashMap) IOException(java.io.IOException)

Example 42 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestStatement method testSavepoint.

private void testSavepoint() throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
    conn.setAutoCommit(false);
    stat.execute("INSERT INTO TEST VALUES(0, 'Hi')");
    Savepoint savepoint1 = conn.setSavepoint();
    int id1 = savepoint1.getSavepointId();
    assertThrows(ErrorCode.SAVEPOINT_IS_UNNAMED, savepoint1).getSavepointName();
    stat.execute("DELETE FROM TEST");
    conn.rollback(savepoint1);
    stat.execute("UPDATE TEST SET NAME='Hello'");
    Savepoint savepoint2a = conn.setSavepoint();
    Savepoint savepoint2 = conn.setSavepoint();
    conn.releaseSavepoint(savepoint2a);
    assertThrows(ErrorCode.SAVEPOINT_IS_INVALID_1, savepoint2a).getSavepointId();
    int id2 = savepoint2.getSavepointId();
    assertTrue(id1 != id2);
    stat.execute("UPDATE TEST SET NAME='Hallo' WHERE NAME='Hello'");
    Savepoint savepointTest = conn.setSavepoint("Joe's");
    assertTrue(savepointTest.toString().endsWith("name=Joe's"));
    stat.execute("DELETE FROM TEST");
    assertEquals(savepointTest.getSavepointName(), "Joe's");
    assertThrows(ErrorCode.SAVEPOINT_IS_NAMED, savepointTest).getSavepointId();
    conn.rollback(savepointTest);
    conn.commit();
    ResultSet rs = stat.executeQuery("SELECT NAME FROM TEST");
    rs.next();
    String name = rs.getString(1);
    assertEquals(name, "Hallo");
    assertFalse(rs.next());
    assertThrows(ErrorCode.SAVEPOINT_IS_INVALID_1, conn).rollback(savepoint2);
    stat.execute("DROP TABLE TEST");
    conn.setAutoCommit(true);
}
Also used : PreparedStatement(java.sql.PreparedStatement) JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint) Savepoint(java.sql.Savepoint)

Example 43 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestStatement method testUnwrap.

private void testUnwrap() throws SQLException {
    Statement stat = conn.createStatement();
    assertTrue(stat.isWrapperFor(Object.class));
    assertTrue(stat.isWrapperFor(Statement.class));
    assertTrue(stat.isWrapperFor(stat.getClass()));
    assertFalse(stat.isWrapperFor(Integer.class));
    assertTrue(stat == stat.unwrap(Object.class));
    assertTrue(stat == stat.unwrap(Statement.class));
    assertTrue(stat == stat.unwrap(stat.getClass()));
    assertThrows(ErrorCode.INVALID_VALUE_2, stat).unwrap(Integer.class);
}
Also used : PreparedStatement(java.sql.PreparedStatement) JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement)

Example 44 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestConnectionPool method testConnect.

private void testConnect() throws SQLException {
    JdbcConnectionPool pool = getConnectionPool(3);
    for (int i = 0; i < 100; i++) {
        Connection conn = pool.getConnection();
        conn.close();
    }
    pool.dispose();
    DataSource ds = pool;
    assertThrows(IllegalStateException.class, ds).getConnection();
    assertThrows(UnsupportedOperationException.class, ds).getConnection(null, null);
}
Also used : JdbcConnectionPool(org.h2.jdbcx.JdbcConnectionPool) Connection(java.sql.Connection) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource)

Example 45 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestConnectionPool method testTimeout.

private void testTimeout() throws Exception {
    String url = getURL("connectionPool", true), user = getUser();
    String password = getPassword();
    final JdbcConnectionPool man = JdbcConnectionPool.create(url, user, password);
    man.setLoginTimeout(1);
    createClassProxy(man.getClass());
    assertThrows(IllegalArgumentException.class, man).setMaxConnections(-1);
    man.setMaxConnections(2);
    // connection 1 (of 2)
    Connection conn = man.getConnection();
    Task t = new Task() {

        @Override
        public void call() {
            while (!stop) {
                // this calls notifyAll
                man.setMaxConnections(1);
                man.setMaxConnections(2);
            }
        }
    };
    t.execute();
    long time = System.nanoTime();
    Connection conn2 = null;
    try {
        // connection 2 (of 1 or 2) may fail
        conn2 = man.getConnection();
        // connection 3 (of 1 or 2) must fail
        man.getConnection();
        fail();
    } catch (SQLException e) {
        if (conn2 != null) {
            conn2.close();
        }
        assertContains(e.toString().toLowerCase(), "timeout");
        time = System.nanoTime() - time;
        assertTrue("timeout after " + TimeUnit.NANOSECONDS.toMillis(time) + " ms", time > TimeUnit.SECONDS.toNanos(1));
    } finally {
        conn.close();
        t.get();
    }
    man.dispose();
}
Also used : Task(org.h2.util.Task) JdbcConnectionPool(org.h2.jdbcx.JdbcConnectionPool) SQLException(java.sql.SQLException) 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