use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestView method testReferenceView.
private void testReferenceView() throws SQLException {
deleteDb("view");
Connection conn = getConnection("view");
Statement s = conn.createStatement();
s.execute("create table t0(id int primary key)");
s.execute("create view t1 as select * from t0");
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, s).execute("create table t2(id int primary key, " + "col1 int not null, foreign key (col1) references t1(id))");
conn.close();
deleteDb("view");
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testSSL.
private void testSSL() throws SQLException {
String result;
Connection conn;
try {
result = runServer(0, new String[] { "-tcp", "-tcpAllowOthers", "-tcpPort", "9001", "-tcpPassword", "abcdef", "-tcpSSL" });
assertContains(result, "ssl://");
assertContains(result, ":9001");
assertContains(result, "others can");
assertTrue(result.indexOf("Starts the H2 Console") < 0);
conn = getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa");
conn.close();
result = runServer(0, new String[] { "-tcpShutdown", "ssl://localhost:9001", "-tcpPassword", "abcdef" });
assertContains(result, "Shutting down");
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa");
result = runServer(0, new String[] { "-web", "-webPort", "9002", "-webAllowOthers", "-webSSL", "-pg", "-pgAllowOthers", "-pgPort", "9003", "-tcp", "-tcpAllowOthers", "-tcpPort", "9006", "-tcpPassword", "abc" });
Server stop = server;
assertContains(result, "https://");
assertContains(result, ":9002");
assertContains(result, "pg://");
assertContains(result, ":9003");
assertContains(result, "others can");
assertTrue(result.indexOf("only local") < 0);
assertContains(result, "tcp://");
assertContains(result, ":9006");
conn = getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa");
conn.close();
result = runServer(0, new String[] { "-tcpShutdown", "tcp://localhost:9006", "-tcpPassword", "abc", "-tcpShutdownForce" });
assertContains(result, "Shutting down");
stop.shutdown();
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa");
} finally {
shutdownServers();
}
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testServer.
private void testServer() throws SQLException {
Connection conn;
try {
deleteDb("test");
Server tcpServer = Server.createTcpServer("-baseDir", getBaseDir(), "-tcpAllowOthers").start();
remainingServers.add(tcpServer);
final int port = tcpServer.getPort();
conn = getConnection("jdbc:h2:tcp://localhost:" + port + "/test", "sa", "");
conn.close();
// must not be able to use a different base dir
new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:" + port + "/../test", "sa", "");
}
};
new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:" + port + "/../test2/test", "sa", "");
}
};
tcpServer.stop();
Server tcpServerWithPassword = Server.createTcpServer("-ifExists", "-tcpPassword", "abc", "-baseDir", getBaseDir()).start();
final int prt = tcpServerWithPassword.getPort();
remainingServers.add(tcpServerWithPassword);
// must not be able to create new db
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:" + prt + "/test2", "sa", "");
}
};
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
@Override
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:" + prt + "/test2;ifexists=false", "sa", "");
}
};
conn = getConnection("jdbc:h2:tcp://localhost:" + prt + "/test", "sa", "");
conn.close();
new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) {
@Override
public void test() throws SQLException {
Server.shutdownTcpServer("tcp://localhost:" + prt, "", true, false);
}
};
conn = getConnection("jdbc:h2:tcp://localhost:" + prt + "/test", "sa", "");
// conn.close();
Server.shutdownTcpServer("tcp://localhost:" + prt, "abc", true, false);
// check that the database is closed
deleteDb("test");
// server must have been closed
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:" + prt + "/test", "sa", "");
JdbcUtils.closeSilently(conn);
// Test filesystem prefix and escape from baseDir
deleteDb("testSplit");
server = Server.createTcpServer("-baseDir", getBaseDir(), "-tcpAllowOthers").start();
final int p = server.getPort();
conn = getConnection("jdbc:h2:tcp://localhost:" + p + "/split:testSplit", "sa", "");
conn.close();
assertThrows(ErrorCode.IO_EXCEPTION_1, this).getConnection("jdbc:h2:tcp://localhost:" + p + "/../test", "sa", "");
server.stop();
deleteDb("testSplit");
} finally {
shutdownServers();
}
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testConsole.
private void testConsole() throws Exception {
String old = System.getProperty(SysProperties.H2_BROWSER);
Console c = new Console();
c.setOut(new PrintStream(new ByteArrayOutputStream()));
try {
// start including browser
lastUrl = "-";
System.setProperty(SysProperties.H2_BROWSER, "call:" + TestTools.class.getName() + ".openBrowser");
c.runTool("-web", "-webPort", "9002", "-tool", "-browser", "-tcp", "-tcpPort", "9003", "-pg", "-pgPort", "9004");
assertContains(lastUrl, ":9002");
shutdownConsole(c);
// check if starting the browser works
c.runTool("-web", "-webPort", "9002", "-tool");
lastUrl = "-";
c.actionPerformed(new ActionEvent(this, 0, "console"));
assertContains(lastUrl, ":9002");
lastUrl = "-";
// double-click prevention is 100 ms
Thread.sleep(200);
try {
MouseEvent me = new MouseEvent(new Button(), 0, 0, 0, 0, 0, 0, false, MouseEvent.BUTTON1);
c.mouseClicked(me);
assertContains(lastUrl, ":9002");
lastUrl = "-";
// no delay - ignore because it looks like a double click
c.mouseClicked(me);
assertEquals("-", lastUrl);
// open the window
c.actionPerformed(new ActionEvent(this, 0, "status"));
c.actionPerformed(new ActionEvent(this, 0, "exit"));
// check if the service was stopped
c.runTool("-webPort", "9002");
} catch (HeadlessException e) {
// ignore
}
shutdownConsole(c);
// trying to use the same port for two services should fail,
// but also stop the first service
createClassProxy(c.getClass());
assertThrows(ErrorCode.EXCEPTION_OPENING_PORT_2, c).runTool("-web", "-webPort", "9002", "-tcp", "-tcpPort", "9002");
c.runTool("-web", "-webPort", "9002");
} finally {
if (old != null) {
System.setProperty(SysProperties.H2_BROWSER, old);
} else {
System.clearProperty(SysProperties.H2_BROWSER);
}
shutdownConsole(c);
}
}
use of org.h2.test.utils.AssertThrows in project h2database by h2database.
the class TestTools method testSimpleResultSet.
private void testSimpleResultSet() throws Exception {
SimpleResultSet rs;
rs = new SimpleResultSet();
rs.addColumn(null, 0, 0, 0);
rs.addRow(1);
createClassProxy(rs.getClass());
assertThrows(IllegalStateException.class, rs).addColumn(null, 0, 0, 0);
assertEquals(ResultSet.TYPE_FORWARD_ONLY, rs.getType());
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals("1", rs.getString(1));
assertEquals("1", rs.getString("C1"));
assertFalse(rs.wasNull());
assertEquals("C1", rs.getMetaData().getColumnLabel(1));
assertEquals("C1", rs.getColumnName(1));
assertEquals(ResultSetMetaData.columnNullableUnknown, rs.getMetaData().isNullable(1));
assertFalse(rs.getMetaData().isAutoIncrement(1));
assertTrue(rs.getMetaData().isCaseSensitive(1));
assertFalse(rs.getMetaData().isCurrency(1));
assertFalse(rs.getMetaData().isDefinitelyWritable(1));
assertTrue(rs.getMetaData().isReadOnly(1));
assertTrue(rs.getMetaData().isSearchable(1));
assertTrue(rs.getMetaData().isSigned(1));
assertFalse(rs.getMetaData().isWritable(1));
assertEquals(null, rs.getMetaData().getCatalogName(1));
assertEquals(null, rs.getMetaData().getColumnClassName(1));
assertEquals("NULL", rs.getMetaData().getColumnTypeName(1));
assertEquals(null, rs.getMetaData().getSchemaName(1));
assertEquals(null, rs.getMetaData().getTableName(1));
assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, rs.getHoldability());
assertEquals(1, rs.getColumnCount());
rs = new SimpleResultSet();
rs.setAutoClose(false);
rs.addColumn("a", Types.BIGINT, 0, 0);
rs.addColumn("b", Types.BINARY, 0, 0);
rs.addColumn("c", Types.BOOLEAN, 0, 0);
rs.addColumn("d", Types.DATE, 0, 0);
rs.addColumn("e", Types.DECIMAL, 0, 0);
rs.addColumn("f", Types.FLOAT, 0, 0);
rs.addColumn("g", Types.VARCHAR, 0, 0);
rs.addColumn("h", Types.ARRAY, 0, 0);
rs.addColumn("i", Types.TIME, 0, 0);
rs.addColumn("j", Types.TIMESTAMP, 0, 0);
rs.addColumn("k", Types.CLOB, 0, 0);
rs.addColumn("l", Types.BLOB, 0, 0);
Date d = Date.valueOf("2001-02-03");
byte[] b = { (byte) 0xab };
Object[] a = { 1, 2 };
Time t = Time.valueOf("10:20:30");
Timestamp ts = Timestamp.valueOf("2002-03-04 10:20:30");
Clob clob = new SimpleClob("Hello World");
Blob blob = new SimpleBlob(new byte[] { (byte) 1, (byte) 2 });
rs.addRow(1, b, true, d, "10.3", Math.PI, "-3", a, t, ts, clob, blob);
rs.addRow(BigInteger.ONE, null, true, null, BigDecimal.ONE, 1d, null, null, null, null, null);
rs.addRow(BigInteger.ZERO, null, false, null, BigDecimal.ZERO, 0d, null, null, null, null, null);
rs.addRow(null, null, null, null, null, null, null, null, null, null, null);
rs.next();
assertEquals(1, rs.getLong(1));
assertEquals((byte) 1, rs.getByte(1));
assertEquals((short) 1, rs.getShort(1));
assertEquals(1, rs.getLong("a"));
assertEquals((byte) 1, rs.getByte("a"));
assertEquals(1, rs.getInt("a"));
assertEquals((short) 1, rs.getShort("a"));
assertTrue(rs.getObject(1).getClass() == Integer.class);
assertTrue(rs.getObject("a").getClass() == Integer.class);
assertTrue(rs.getBoolean(1));
assertEquals(b, rs.getBytes(2));
assertEquals(b, rs.getBytes("b"));
assertTrue(rs.getBoolean(3));
assertTrue(rs.getBoolean("c"));
assertEquals(d.getTime(), rs.getDate(4).getTime());
assertEquals(d.getTime(), rs.getDate("d").getTime());
assertTrue(new BigDecimal("10.3").equals(rs.getBigDecimal(5)));
assertTrue(new BigDecimal("10.3").equals(rs.getBigDecimal("e")));
assertEquals(10.3, rs.getDouble(5));
assertEquals((float) 10.3, rs.getFloat(5));
assertTrue(Math.PI == rs.getDouble(6));
assertTrue(Math.PI == rs.getDouble("f"));
assertTrue((float) Math.PI == rs.getFloat(6));
assertTrue((float) Math.PI == rs.getFloat("f"));
assertTrue(rs.getBoolean(6));
assertEquals(-3, rs.getInt(7));
assertEquals(-3, rs.getByte(7));
assertEquals(-3, rs.getShort(7));
assertEquals(-3, rs.getLong(7));
Object[] a2 = (Object[]) rs.getArray(8).getArray();
assertEquals(2, a2.length);
assertTrue(a == a2);
SimpleArray array = (SimpleArray) rs.getArray("h");
assertEquals(Types.NULL, array.getBaseType());
assertEquals("NULL", array.getBaseTypeName());
a2 = (Object[]) array.getArray();
array.free();
assertEquals(2, a2.length);
assertTrue(a == a2);
assertTrue(t == rs.getTime("i"));
assertTrue(t == rs.getTime(9));
assertTrue(ts == rs.getTimestamp("j"));
assertTrue(ts == rs.getTimestamp(10));
assertTrue(clob == rs.getClob("k"));
assertTrue(clob == rs.getClob(11));
assertEquals("Hello World", rs.getString("k"));
assertEquals("Hello World", rs.getString(11));
assertTrue(blob == rs.getBlob("l"));
assertTrue(blob == rs.getBlob(12));
assertThrows(ErrorCode.INVALID_VALUE_2, (ResultSet) rs).getString(13);
assertThrows(ErrorCode.COLUMN_NOT_FOUND_1, (ResultSet) rs).getString("NOT_FOUND");
rs.next();
assertTrue(rs.getBoolean(1));
assertTrue(rs.getBoolean(3));
assertTrue(rs.getBoolean(5));
assertTrue(rs.getBoolean(6));
rs.next();
assertFalse(rs.getBoolean(1));
assertFalse(rs.getBoolean(3));
assertFalse(rs.getBoolean(5));
assertFalse(rs.getBoolean(6));
rs.next();
assertEquals(0, rs.getLong(1));
assertTrue(rs.wasNull());
assertEquals(null, rs.getBytes(2));
assertTrue(rs.wasNull());
assertFalse(rs.getBoolean(3));
assertTrue(rs.wasNull());
assertNull(rs.getDate(4));
assertTrue(rs.wasNull());
assertNull(rs.getBigDecimal(5));
assertTrue(rs.wasNull());
assertEquals(0.0, rs.getDouble(5));
assertTrue(rs.wasNull());
assertEquals(0.0, rs.getDouble(6));
assertTrue(rs.wasNull());
assertEquals(0.0, rs.getFloat(6));
assertTrue(rs.wasNull());
assertEquals(0, rs.getInt(7));
assertTrue(rs.wasNull());
assertNull(rs.getArray(8));
assertTrue(rs.wasNull());
assertNull(rs.getTime(9));
assertTrue(rs.wasNull());
assertNull(rs.getTimestamp(10));
assertTrue(rs.wasNull());
assertNull(rs.getClob(11));
assertTrue(rs.wasNull());
assertNull(rs.getCharacterStream(11));
assertTrue(rs.wasNull());
assertNull(rs.getBlob(12));
assertTrue(rs.wasNull());
assertNull(rs.getBinaryStream(12));
assertTrue(rs.wasNull());
// all updateX methods
for (Method m : rs.getClass().getMethods()) {
if (m.getName().startsWith("update")) {
if (m.getName().equals("updateRow")) {
continue;
}
int len = m.getParameterTypes().length;
if (m.getName().equals("updateObject") && m.getParameterTypes().length > 2) {
Class<?> p3 = m.getParameterTypes()[2];
if (p3.toString().indexOf("SQLType") >= 0) {
continue;
}
}
Object[] params = new Object[len];
int i = 0;
String expectedValue = null;
for (Class<?> type : m.getParameterTypes()) {
Object o;
String e = null;
if (type == int.class) {
o = 1;
e = "1";
} else if (type == byte.class) {
o = (byte) 2;
e = "2";
} else if (type == double.class) {
o = (double) 3;
e = "3.0";
} else if (type == float.class) {
o = (float) 4;
e = "4.0";
} else if (type == long.class) {
o = (long) 5;
e = "5";
} else if (type == short.class) {
o = (short) 6;
e = "6";
} else if (type == boolean.class) {
o = false;
e = "false";
} else if (type == String.class) {
// columnName or value
o = "a";
e = "a";
} else {
o = null;
}
if (i == 1) {
expectedValue = e;
}
params[i] = o;
i++;
}
m.invoke(rs, params);
if (params.length == 1) {
// updateNull
assertEquals(null, rs.getString(1));
} else {
assertEquals(expectedValue, rs.getString(1));
}
// invalid column name / index
Object invalidColumn;
if (m.getParameterTypes()[0] == String.class) {
invalidColumn = "x";
} else {
invalidColumn = 0;
}
params[0] = invalidColumn;
try {
m.invoke(rs, params);
fail();
} catch (InvocationTargetException e) {
SQLException e2 = (SQLException) e.getTargetException();
if (invalidColumn instanceof String) {
assertEquals(ErrorCode.COLUMN_NOT_FOUND_1, e2.getErrorCode());
} else {
assertEquals(ErrorCode.INVALID_VALUE_2, e2.getErrorCode());
}
}
}
}
assertEquals(ResultSet.FETCH_FORWARD, rs.getFetchDirection());
assertEquals(0, rs.getFetchSize());
assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
assertNull(rs.getStatement());
assertFalse(rs.isClosed());
rs.beforeFirst();
assertEquals(0, rs.getRow());
assertTrue(rs.next());
assertFalse(rs.isClosed());
assertEquals(1, rs.getRow());
assertTrue(rs.next());
assertTrue(rs.next());
assertTrue(rs.next());
assertFalse(rs.next());
assertThrows(ErrorCode.NO_DATA_AVAILABLE, (ResultSet) rs).getInt(1);
assertEquals(0, rs.getRow());
assertFalse(rs.isClosed());
rs.close();
assertTrue(rs.isClosed());
rs = new SimpleResultSet();
rs.addColumn("TEST", Types.BINARY, 0, 0);
UUID uuid = UUID.randomUUID();
rs.addRow(uuid);
rs.next();
assertEquals(uuid, rs.getObject(1));
assertEquals(uuid, ValueUuid.get(rs.getBytes(1)).getObject());
}
Aggregations