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