Search in sources :

Example 11 with AssertThrows

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

the class TestOldVersion method testOldClientNewServer.

private void testOldClientNewServer() throws Exception {
    Server server = org.h2.tools.Server.createTcpServer();
    server.start();
    int port = server.getPort();
    assertThrows(ErrorCode.DRIVER_VERSION_ERROR_2, driver).connect("jdbc:h2:tcp://localhost:" + port + "/mem:test", null);
    server.stop();
    Class<?> serverClass = cl.loadClass("org.h2.tools.Server");
    Method m;
    m = serverClass.getMethod("createTcpServer", String[].class);
    Object serverOld = m.invoke(null, new Object[] { new String[] { "-tcpPort", "" + port } });
    m = serverOld.getClass().getMethod("start");
    m.invoke(serverOld);
    Connection conn;
    conn = org.h2.Driver.load().connect("jdbc:h2:mem:", null);
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("call 1");
    rs.next();
    assertEquals(1, rs.getInt(1));
    conn.close();
    m = serverOld.getClass().getMethod("stop");
    m.invoke(serverOld);
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Method(java.lang.reflect.Method)

Example 12 with AssertThrows

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

the class TestFileLockSerialized method testKillWriter.

private void testKillWriter() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0";
    Connection conn = getConnection(writeUrl, "sa", "sa");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key)");
    ((JdbcConnection) conn).setPowerOffCount(1);
    assertThrows(ErrorCode.DATABASE_IS_CLOSED, stat).execute("insert into test values(1)");
    Connection conn2 = getConnection(writeUrl, "sa", "sa");
    Statement stat2 = conn2.createStatement();
    stat2.execute("insert into test values(1)");
    printResult(stat2, "select * from test");
    conn2.close();
    assertThrows(ErrorCode.DATABASE_IS_CLOSED, conn).close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 13 with AssertThrows

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

the class TestMVTableEngine method testGarbageCollectionForLOB.

private void testGarbageCollectionForLOB() throws SQLException {
    if (config.memory) {
        return;
    }
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int, data blob)");
    stat.execute("insert into test select x, repeat('0', 10000) " + "from system_range(1, 10)");
    stat.execute("drop table test");
    stat.execute("create table test2(id int, data blob)");
    PreparedStatement prep = conn.prepareStatement("insert into test2 values(?, ?)");
    prep.setInt(1, 1);
    assertThrows(ErrorCode.IO_EXCEPTION_1, prep).setBinaryStream(1, createFailingStream(new IOException()));
    prep.setInt(1, 2);
    assertThrows(ErrorCode.IO_EXCEPTION_1, prep).setBinaryStream(1, createFailingStream(new IllegalStateException()));
    conn.close();
    MVStore s = MVStore.open(getBaseDir() + "/" + getTestName() + ".mv.db");
    assertTrue(s.hasMap("lobData"));
    MVMap<Long, byte[]> lobData = s.openMap("lobData");
    assertEquals(0, lobData.sizeAsLong());
    assertTrue(s.hasMap("lobMap"));
    MVMap<Long, byte[]> lobMap = s.openMap("lobMap");
    assertEquals(0, lobMap.sizeAsLong());
    assertTrue(s.hasMap("lobRef"));
    MVMap<Long, byte[]> lobRef = s.openMap("lobRef");
    assertEquals(0, lobRef.sizeAsLong());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 14 with AssertThrows

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

the class TestMVStore method testVersionsToKeep2.

private void testVersionsToKeep2() {
    MVStore s = new MVStore.Builder().autoCommitDisabled().open();
    s.setVersionsToKeep(2);
    final MVMap<Integer, String> m = s.openMap("data");
    s.commit();
    assertEquals(1, s.getCurrentVersion());
    m.put(1, "version 1");
    s.commit();
    assertEquals(2, s.getCurrentVersion());
    m.put(1, "version 2");
    s.commit();
    assertEquals(3, s.getCurrentVersion());
    m.put(1, "version 3");
    s.commit();
    m.put(1, "version 4");
    assertEquals("version 4", m.openVersion(4).get(1));
    assertEquals("version 3", m.openVersion(3).get(1));
    assertEquals("version 2", m.openVersion(2).get(1));
    new AssertThrows(IllegalArgumentException.class) {

        @Override
        public void test() throws Exception {
            m.openVersion(1);
        }
    };
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertThrows(org.h2.test.utils.AssertThrows)

Example 15 with AssertThrows

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

the class TestStatement method testStatement.

private void testStatement() throws SQLException {
    Statement stat = conn.createStatement();
    assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, conn.getHoldability());
    conn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
    assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn.getHoldability());
    assertFalse(stat.isPoolable());
    stat.setPoolable(true);
    assertFalse(stat.isPoolable());
    // ignored
    stat.setCursorName("x");
    // fixed return value
    assertEquals(stat.getFetchDirection(), ResultSet.FETCH_FORWARD);
    // ignored
    stat.setFetchDirection(ResultSet.FETCH_REVERSE);
    // ignored
    stat.setMaxFieldSize(100);
    assertEquals(SysProperties.SERVER_RESULT_SET_FETCH_SIZE, stat.getFetchSize());
    stat.setFetchSize(10);
    assertEquals(10, stat.getFetchSize());
    stat.setFetchSize(0);
    assertEquals(SysProperties.SERVER_RESULT_SET_FETCH_SIZE, stat.getFetchSize());
    assertEquals(ResultSet.TYPE_FORWARD_ONLY, stat.getResultSetType());
    Statement stat2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
    assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, stat2.getResultSetType());
    assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, stat2.getResultSetHoldability());
    assertEquals(ResultSet.CONCUR_READ_ONLY, stat2.getResultSetConcurrency());
    assertEquals(0, stat.getMaxFieldSize());
    assertFalse(((JdbcStatement) stat2).isClosed());
    stat2.close();
    assertTrue(((JdbcStatement) stat2).isClosed());
    ResultSet rs;
    int count;
    long largeCount;
    boolean result;
    stat.execute("CREATE TABLE TEST(ID INT)");
    stat.execute("SELECT * FROM TEST");
    stat.execute("DROP TABLE TEST");
    conn.getTypeMap();
    // this method should not throw an exception - if not supported, this
    // calls are ignored
    assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, stat.getResultSetHoldability());
    assertEquals(ResultSet.CONCUR_READ_ONLY, stat.getResultSetConcurrency());
    stat.cancel();
    stat.setQueryTimeout(10);
    assertTrue(stat.getQueryTimeout() == 10);
    stat.setQueryTimeout(0);
    assertTrue(stat.getQueryTimeout() == 0);
    assertThrows(ErrorCode.INVALID_VALUE_2, stat).setQueryTimeout(-1);
    assertTrue(stat.getQueryTimeout() == 0);
    trace("executeUpdate");
    count = stat.executeUpdate("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE VARCHAR(255))");
    assertEquals(0, count);
    count = stat.executeUpdate("INSERT INTO TEST VALUES(1,'Hello')");
    assertEquals(1, count);
    count = stat.executeUpdate("INSERT INTO TEST(VALUE,ID) VALUES('JDBC',2)");
    assertEquals(1, count);
    count = stat.executeUpdate("UPDATE TEST SET VALUE='LDBC' WHERE ID=2 OR ID=1");
    assertEquals(2, count);
    count = stat.executeUpdate("UPDATE TEST SET VALUE='\\LDBC\\' WHERE VALUE LIKE 'LDBC' ");
    assertEquals(2, count);
    count = stat.executeUpdate("UPDATE TEST SET VALUE='LDBC' WHERE VALUE LIKE '\\\\LDBC\\\\'");
    trace("count:" + count);
    assertEquals(2, count);
    count = stat.executeUpdate("DELETE FROM TEST WHERE ID=-1");
    assertEquals(0, count);
    count = stat.executeUpdate("DELETE FROM TEST WHERE ID=2");
    assertEquals(1, count);
    JdbcStatementBackwardsCompat statBC = (JdbcStatementBackwardsCompat) stat;
    largeCount = statBC.executeLargeUpdate("DELETE FROM TEST WHERE ID=-1");
    assertEquals(0, largeCount);
    assertEquals(0, statBC.getLargeUpdateCount());
    largeCount = statBC.executeLargeUpdate("INSERT INTO TEST(VALUE,ID) VALUES('JDBC',2)");
    assertEquals(1, largeCount);
    assertEquals(1, statBC.getLargeUpdateCount());
    largeCount = statBC.executeLargeUpdate("DELETE FROM TEST WHERE ID=2");
    assertEquals(1, largeCount);
    assertEquals(1, statBC.getLargeUpdateCount());
    assertThrows(ErrorCode.METHOD_NOT_ALLOWED_FOR_QUERY, stat).executeUpdate("SELECT * FROM TEST");
    count = stat.executeUpdate("DROP TABLE TEST");
    assertTrue(count == 0);
    trace("execute");
    result = stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE VARCHAR(255))");
    assertFalse(result);
    result = stat.execute("INSERT INTO TEST VALUES(1,'Hello')");
    assertFalse(result);
    result = stat.execute("INSERT INTO TEST(VALUE,ID) VALUES('JDBC',2)");
    assertFalse(result);
    result = stat.execute("UPDATE TEST SET VALUE='LDBC' WHERE ID=2");
    assertFalse(result);
    result = stat.execute("DELETE FROM TEST WHERE ID=3");
    assertFalse(result);
    result = stat.execute("SELECT * FROM TEST");
    assertTrue(result);
    result = stat.execute("DROP TABLE TEST");
    assertFalse(result);
    assertThrows(ErrorCode.METHOD_ONLY_ALLOWED_FOR_QUERY, stat).executeQuery("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE VARCHAR(255))");
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE VARCHAR(255))");
    assertThrows(ErrorCode.METHOD_ONLY_ALLOWED_FOR_QUERY, stat).executeQuery("INSERT INTO TEST VALUES(1,'Hello')");
    assertThrows(ErrorCode.METHOD_ONLY_ALLOWED_FOR_QUERY, stat).executeQuery("UPDATE TEST SET VALUE='LDBC' WHERE ID=2");
    assertThrows(ErrorCode.METHOD_ONLY_ALLOWED_FOR_QUERY, stat).executeQuery("DELETE FROM TEST WHERE ID=3");
    stat.executeQuery("SELECT * FROM TEST");
    assertThrows(ErrorCode.METHOD_ONLY_ALLOWED_FOR_QUERY, stat).executeQuery("DROP TABLE TEST");
    // getMoreResults
    rs = stat.executeQuery("SELECT * FROM TEST");
    assertFalse(stat.getMoreResults());
    assertThrows(ErrorCode.OBJECT_CLOSED, rs).next();
    assertTrue(stat.getUpdateCount() == -1);
    count = stat.executeUpdate("DELETE FROM TEST");
    assertFalse(stat.getMoreResults());
    assertTrue(stat.getUpdateCount() == -1);
    stat.execute("DROP TABLE TEST");
    stat.executeUpdate("DROP TABLE IF EXISTS TEST");
    assertNull(stat.getWarnings());
    stat.clearWarnings();
    assertNull(stat.getWarnings());
    assertTrue(conn == stat.getConnection());
    assertEquals("SOME_ID", statBC.enquoteIdentifier("SOME_ID", false));
    assertEquals("\"SOME ID\"", statBC.enquoteIdentifier("SOME ID", false));
    assertEquals("\"SOME_ID\"", statBC.enquoteIdentifier("SOME_ID", true));
    assertEquals("\"FROM\"", statBC.enquoteIdentifier("FROM", false));
    assertEquals("\"Test\"", statBC.enquoteIdentifier("Test", false));
    assertEquals("\"TODAY\"", statBC.enquoteIdentifier("TODAY", false));
    assertTrue(statBC.isSimpleIdentifier("SOME_ID"));
    assertFalse(statBC.isSimpleIdentifier("SOME ID"));
    assertFalse(statBC.isSimpleIdentifier("FROM"));
    assertFalse(statBC.isSimpleIdentifier("Test"));
    assertFalse(statBC.isSimpleIdentifier("TODAY"));
    stat.close();
}
Also used : JdbcStatementBackwardsCompat(org.h2.jdbc.JdbcStatementBackwardsCompat) PreparedStatement(java.sql.PreparedStatement) JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint)

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