use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestFunctions method testSignal.
private void testSignal() throws SQLException {
deleteDb("functions");
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
assertThrows(ErrorCode.INVALID_VALUE_2, stat).execute("select signal('00145', 'success class is invalid')");
assertThrows(ErrorCode.INVALID_VALUE_2, stat).execute("select signal('foo', 'SQLSTATE has 5 chars')");
assertThrows(ErrorCode.INVALID_VALUE_2, stat).execute("select signal('Ab123', 'SQLSTATE has only digits or upper-case letters')");
try {
stat.execute("select signal('AB123', 'some custom error')");
fail("Should have thrown");
} catch (SQLException e) {
assertEquals("AB123", e.getSQLState());
assertContains(e.getMessage(), "some custom error");
}
conn.close();
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestCallableStatement method testPrepare.
private void testPrepare(Connection conn) throws Exception {
Statement stat = conn.createStatement();
CallableStatement call;
ResultSet rs;
stat.execute("CREATE TABLE TEST(ID INT, NAME VARCHAR)");
call = conn.prepareCall("INSERT INTO TEST VALUES(?, ?)");
call.setInt(1, 1);
call.setString(2, "Hello");
call.execute();
call = conn.prepareCall("SELECT * FROM TEST", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
rs = call.executeQuery();
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals("Hello", rs.getString(2));
assertFalse(rs.next());
call = conn.prepareCall("SELECT * FROM TEST", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
rs = call.executeQuery();
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals("Hello", rs.getString(2));
assertFalse(rs.next());
stat.execute("CREATE ALIAS testCall FOR \"" + getClass().getName() + ".testCall\"");
call = conn.prepareCall("{CALL testCall(?, ?, ?, ?)}");
call.setInt("A", 50);
call.setString("B", "abc");
long t = System.currentTimeMillis();
call.setTimestamp("C", new Timestamp(t));
call.setTimestamp("D", Timestamp.valueOf("2001-02-03 10:20:30.0"));
call.registerOutParameter(1, Types.INTEGER);
call.registerOutParameter("B", Types.VARCHAR);
call.executeUpdate();
try {
call.getTimestamp("C");
fail("not registered out parameter accessible");
} catch (SQLException e) {
// expected exception
}
call.registerOutParameter(3, Types.TIMESTAMP);
call.registerOutParameter(4, Types.TIMESTAMP);
call.executeUpdate();
assertEquals(t + 1, call.getTimestamp(3).getTime());
assertEquals(t + 1, call.getTimestamp("C").getTime());
assertEquals("2001-02-03 10:20:30.0", call.getTimestamp(4).toString());
assertEquals("2001-02-03 10:20:30.0", call.getTimestamp("D").toString());
if (LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("2001-02-03T10:20:30", call.getObject(4, LocalDateTimeUtils.LOCAL_DATE_TIME).toString());
assertEquals("2001-02-03T10:20:30", call.getObject("D", LocalDateTimeUtils.LOCAL_DATE_TIME).toString());
}
assertEquals("10:20:30", call.getTime(4).toString());
assertEquals("10:20:30", call.getTime("D").toString());
if (LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("10:20:30", call.getObject(4, LocalDateTimeUtils.LOCAL_TIME).toString());
assertEquals("10:20:30", call.getObject("D", LocalDateTimeUtils.LOCAL_TIME).toString());
}
assertEquals("2001-02-03", call.getDate(4).toString());
assertEquals("2001-02-03", call.getDate("D").toString());
if (LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("2001-02-03", call.getObject(4, LocalDateTimeUtils.LOCAL_DATE).toString());
assertEquals("2001-02-03", call.getObject("D", LocalDateTimeUtils.LOCAL_DATE).toString());
}
assertEquals(100, call.getInt(1));
assertEquals(100, call.getInt("A"));
assertEquals(100, call.getLong(1));
assertEquals(100, call.getLong("A"));
assertEquals("100", call.getBigDecimal(1).toString());
assertEquals("100", call.getBigDecimal("A").toString());
assertEquals(100, call.getFloat(1));
assertEquals(100, call.getFloat("A"));
assertEquals(100, call.getDouble(1));
assertEquals(100, call.getDouble("A"));
assertEquals(100, call.getByte(1));
assertEquals(100, call.getByte("A"));
assertEquals(100, call.getShort(1));
assertEquals(100, call.getShort("A"));
assertTrue(call.getBoolean(1));
assertTrue(call.getBoolean("A"));
assertEquals("ABC", call.getString(2));
Reader r = call.getCharacterStream(2);
assertEquals("ABC", IOUtils.readStringAndClose(r, -1));
r = call.getNCharacterStream(2);
assertEquals("ABC", IOUtils.readStringAndClose(r, -1));
assertEquals("ABC", call.getString("B"));
assertEquals("ABC", call.getNString(2));
assertEquals("ABC", call.getNString("B"));
assertEquals("ABC", call.getClob(2).getSubString(1, 3));
assertEquals("ABC", call.getClob("B").getSubString(1, 3));
assertEquals("ABC", call.getNClob(2).getSubString(1, 3));
assertEquals("ABC", call.getNClob("B").getSubString(1, 3));
try {
call.getString(100);
fail("incorrect parameter index value");
} catch (SQLException e) {
// expected exception
}
try {
call.getString(0);
fail("incorrect parameter index value");
} catch (SQLException e) {
// expected exception
}
try {
call.getBoolean("X");
fail("incorrect parameter name value");
} catch (SQLException e) {
// expected exception
}
call.setCharacterStream("B", new StringReader("xyz"));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setCharacterStream("B", new StringReader("xyz-"), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setCharacterStream("B", new StringReader("xyz-"), 3L);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", new ByteArrayInputStream("xyz".getBytes(StandardCharsets.UTF_8)));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", new ByteArrayInputStream("xyz-".getBytes(StandardCharsets.UTF_8)), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", new ByteArrayInputStream("xyz-".getBytes(StandardCharsets.UTF_8)), 3L);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setClob("B", new StringReader("xyz"));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setClob("B", new StringReader("xyz-"), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setNClob("B", new StringReader("xyz"));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setNClob("B", new StringReader("xyz-"), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setString("B", "xyz");
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setNString("B", "xyz");
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
// test for exceptions after closing
call.close();
assertThrows(ErrorCode.OBJECT_CLOSED, call).executeUpdate();
assertThrows(ErrorCode.OBJECT_CLOSED, call).registerOutParameter(1, Types.INTEGER);
assertThrows(ErrorCode.OBJECT_CLOSED, call).getString("X");
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTriggersConstraints method testMultiPartForeignKeys.
/**
* Regression test: we had a bug where the AlterTableAddConstraint class
* used to sometimes pick the wrong unique index for a foreign key.
*/
private void testMultiPartForeignKeys() throws SQLException {
Connection conn = getConnection("trigger");
Statement stat = conn.createStatement();
stat.execute("DROP TABLE IF EXISTS TEST1");
stat.execute("DROP TABLE IF EXISTS TEST2");
stat.execute("create table test1(id int primary key, col1 int)");
stat.execute("alter table test1 add constraint unique_test1 " + "unique (id,col1)");
stat.execute("create table test2(id int primary key, col1 int)");
stat.execute("alter table test2 add constraint fk_test2 " + "foreign key(id,col1) references test1 (id,col1)");
stat.execute("insert into test1 values (1,1)");
stat.execute("insert into test1 values (2,2)");
stat.execute("insert into test1 values (3,3)");
stat.execute("insert into test2 values (1,1)");
assertThrows(23506, stat).execute("insert into test2 values (2,1)");
assertSingleValue(stat, "select count(*) from test1", 3);
assertSingleValue(stat, "select count(*) from test2", 1);
stat.execute("drop table test1");
stat.execute("drop table test2");
conn.close();
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTriggersConstraints method testCheckConstraintErrorMessage.
private void testCheckConstraintErrorMessage() throws SQLException {
Connection conn = getConnection("trigger");
Statement stat = conn.createStatement();
stat.execute("create table companies(id identity)");
stat.execute("create table departments(id identity, " + "company_id int not null, " + "foreign key(company_id) references companies(id))");
stat.execute("create table connections (id identity, company_id int not null, " + "first int not null, second int not null, " + "foreign key (company_id) references companies(id), " + "foreign key (first) references departments(id), " + "foreign key (second) references departments(id), " + "check (select departments.company_id from departments, companies where " + " departments.id in (first, second)) = company_id)");
stat.execute("insert into companies(id) values(1)");
stat.execute("insert into departments(id, company_id) " + "values(10, 1)");
stat.execute("insert into departments(id, company_id) " + "values(20, 1)");
assertThrows(ErrorCode.CHECK_CONSTRAINT_INVALID, stat).execute("insert into connections(id, company_id, first, second) " + "values(100, 1, 10, 20)");
stat.execute("drop table connections");
stat.execute("drop table departments");
stat.execute("drop table companies");
conn.close();
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestSynonymForTable method testDropSchema.
private void testDropSchema() throws SQLException {
Connection conn = getConnection("synonym");
Statement stat = conn.createStatement();
stat.execute("CREATE SCHEMA IF NOT EXISTS s1");
stat.execute("CREATE TABLE IF NOT EXISTS s1.backingtable(id INT PRIMARY KEY)");
stat.execute("CREATE OR REPLACE SYNONYM testsynonym FOR s1.backingtable");
stat.execute("DROP SCHEMA s1 CASCADE");
assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym");
conn.close();
}
Aggregations