Search in sources :

Example 21 with AssertThrows

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

the class TestLob method testRemovedAfterTimeout.

private void testRemovedAfterTimeout() throws Exception {
    if (config.lazy) {
        return;
    }
    deleteDb("lob");
    final String url = getURL("lob;lob_timeout=50", true);
    Connection conn = getConnection(url);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, data clob)");
    PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?)");
    prep.setInt(1, 1);
    prep.setString(2, "aaa" + new String(new char[1024 * 16]).replace((char) 0, 'x'));
    prep.execute();
    prep.setInt(1, 2);
    prep.setString(2, "bbb" + new String(new char[1024 * 16]).replace((char) 0, 'x'));
    prep.execute();
    ResultSet rs = stat.executeQuery("select * from test order by id");
    rs.next();
    Clob c1 = rs.getClob(2);
    assertEquals("aaa", c1.getSubString(1, 3));
    rs.next();
    assertEquals("aaa", c1.getSubString(1, 3));
    rs.close();
    assertEquals("aaa", c1.getSubString(1, 3));
    stat.execute("delete from test");
    c1.getSubString(1, 3);
    // wait until it times out
    Thread.sleep(100);
    // start a new transaction, to be sure
    stat.execute("delete from test");
    assertThrows(SQLException.class, c1).getSubString(1, 3);
    conn.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob)

Example 22 with AssertThrows

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

the class TestLob method testCreateIndexOnLob.

private void testCreateIndexOnLob() throws Exception {
    if (config.memory) {
        return;
    }
    deleteDb("lob");
    Connection conn;
    conn = getConnection("lob");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int, name clob)");
    assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, stat).execute("create index idx_n on test(name)");
    stat.execute("drop table test");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 23 with AssertThrows

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

the class TestExclusive method test.

@Override
public void test() throws Exception {
    deleteDb("exclusive");
    Connection conn = getConnection("exclusive");
    Statement stat = conn.createStatement();
    stat.execute("set exclusive true");
    assertThrows(ErrorCode.DATABASE_IS_IN_EXCLUSIVE_MODE, this).getConnection("exclusive");
    stat.execute("set exclusive false");
    Connection conn2 = getConnection("exclusive");
    final Statement stat2 = conn2.createStatement();
    stat.execute("set exclusive true");
    final AtomicInteger state = new AtomicInteger(0);
    Task task = new Task() {

        @Override
        public void call() throws SQLException {
            stat2.execute("select * from dual");
            if (state.get() != 1) {
                new Error("unexpected state: " + state.get()).printStackTrace();
            }
        }
    };
    task.execute();
    state.set(1);
    stat.execute("set exclusive false");
    task.get();
    stat.execute("set exclusive true");
    conn.close();
    // check that exclusive mode is off when disconnected
    stat2.execute("select * from dual");
    conn2.close();
    deleteDb("exclusive");
}
Also used : Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 24 with AssertThrows

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

the class TestSynonymForTable method testExistingTableName.

private void testExistingTableName() throws SQLException {
    Connection conn = getConnection("synonym");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE IF NOT EXISTS backingtable(id INT PRIMARY KEY)");
    assertThrows(JdbcSQLException.class, stat).execute("CREATE SYNONYM backingtable FOR backingtable");
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 25 with AssertThrows

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

the class TestBase method assertThrows.

/**
 * Verify the next method call on the object will throw an exception.
 *
 * @param <T> the class of the object
 * @param verifier the result verifier to call
 * @param obj the object to wrap
 * @return a proxy for the object
 */
@SuppressWarnings("unchecked")
protected <T> T assertThrows(final ResultVerifier verifier, final T obj) {
    Class<?> c = obj.getClass();
    InvocationHandler ih = new InvocationHandler() {

        private Exception called = new Exception("No method called");

        @Override
        protected void finalize() {
            if (called != null) {
                called.printStackTrace(System.err);
            }
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
            try {
                called = null;
                Object ret = method.invoke(obj, args);
                verifier.verify(ret, null, method, args);
                return ret;
            } catch (InvocationTargetException e) {
                verifier.verify(null, e.getTargetException(), method, args);
                Class<?> retClass = method.getReturnType();
                if (!retClass.isPrimitive()) {
                    return null;
                }
                if (retClass == boolean.class) {
                    return false;
                } else if (retClass == byte.class) {
                    return (byte) 0;
                } else if (retClass == char.class) {
                    return (char) 0;
                } else if (retClass == short.class) {
                    return (short) 0;
                } else if (retClass == int.class) {
                    return 0;
                } else if (retClass == long.class) {
                    return 0L;
                } else if (retClass == float.class) {
                    return 0F;
                } else if (retClass == double.class) {
                    return 0D;
                }
                return null;
            }
        }
    };
    if (!ProxyCodeGenerator.isGenerated(c)) {
        Class<?>[] interfaces = c.getInterfaces();
        if (Modifier.isFinal(c.getModifiers()) || (interfaces.length > 0 && getClass() != c)) {
            // interface class proxies
            if (interfaces.length == 0) {
                throw new RuntimeException("Can not create a proxy for the class " + c.getSimpleName() + " because it doesn't implement any interfaces and is final");
            }
            return (T) Proxy.newProxyInstance(c.getClassLoader(), interfaces, ih);
        }
    }
    try {
        Class<?> pc = ProxyCodeGenerator.getClassProxy(c);
        Constructor<?> cons = pc.getConstructor(new Class<?>[] { InvocationHandler.class });
        return (T) cons.newInstance(new Object[] { ih });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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