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();
}
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();
}
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");
}
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();
}
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);
}
}
Aggregations