Search in sources :

Example 36 with AssertThrows

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

the class TestDate method testValueTimestamp.

@SuppressWarnings("unlikely-arg-type")
private void testValueTimestamp() {
    assertEquals("2001-02-03 04:05:06", ValueTimestamp.get(Timestamp.valueOf("2001-02-03 04:05:06")).getString());
    assertEquals("2001-02-03 04:05:06.001002003", ValueTimestamp.get(Timestamp.valueOf("2001-02-03 04:05:06.001002003")).getString());
    assertEquals("0-00-00 00:00:00", ValueTimestamp.fromDateValueAndNanos(0, 0).getString());
    assertEquals("9999-12-31 23:59:59", ValueTimestamp.parse("9999-12-31 23:59:59").getString());
    assertEquals(Integer.MAX_VALUE + "-12-31 01:02:03.04050607", ValueTimestamp.parse(Integer.MAX_VALUE + "-12-31 01:02:03.0405060708").getString());
    assertEquals(Integer.MIN_VALUE + "-12-31 01:02:03.04050607", ValueTimestamp.parse(Integer.MIN_VALUE + "-12-31 01:02:03.0405060708").getString());
    ValueTimestamp t1 = ValueTimestamp.parse("2001-01-01 01:01:01.111");
    assertEquals("2001-01-01 01:01:01.111", t1.getTimestamp().toString());
    assertEquals("2001-01-01", t1.getDate().toString());
    assertEquals("01:01:01", t1.getTime().toString());
    assertEquals("TIMESTAMP '2001-01-01 01:01:01.111'", t1.getSQL());
    assertEquals("TIMESTAMP '2001-01-01 01:01:01.111'", t1.toString());
    assertEquals(Value.TIMESTAMP, t1.getType());
    long dateValue = t1.getDateValue();
    long nanos = t1.getTimeNanos();
    assertEquals((int) ((dateValue >>> 32) ^ dateValue ^ (nanos >>> 32) ^ nanos), t1.hashCode());
    // Literals return maximum precision
    assertEquals(ValueTimestamp.MAXIMUM_PRECISION, t1.getDisplaySize());
    assertEquals(ValueTimestamp.MAXIMUM_PRECISION, t1.getPrecision());
    assertEquals(9, t1.getScale());
    assertEquals("java.sql.Timestamp", t1.getObject().getClass().getName());
    ValueTimestamp t1b = ValueTimestamp.parse("2001-01-01 01:01:01.111");
    assertTrue(t1 == t1b);
    Value.clearCache();
    t1b = ValueTimestamp.parse("2001-01-01 01:01:01.111");
    assertFalse(t1 == t1b);
    assertTrue(t1.equals(t1));
    assertTrue(t1.equals(t1b));
    assertTrue(t1b.equals(t1));
    assertEquals(0, t1.compareTo(t1b, null));
    assertEquals(0, t1b.compareTo(t1, null));
    ValueTimestamp t2 = ValueTimestamp.parse("2002-02-02 02:02:02.222");
    assertFalse(t1.equals(t2));
    assertFalse(t2.equals(t1));
    assertEquals(-1, t1.compareTo(t2, null));
    assertEquals(1, t2.compareTo(t1, null));
    t1 = ValueTimestamp.parse("2001-01-01 01:01:01.123456789");
    assertEquals("2001-01-01 01:01:01.123456789", t1.getString());
    assertEquals("2001-01-01 01:01:01.123456789", t1.convertScale(true, 10).getString());
    assertEquals("2001-01-01 01:01:01.123456789", t1.convertScale(true, 9).getString());
    assertEquals("2001-01-01 01:01:01.12345679", t1.convertScale(true, 8).getString());
    assertEquals("2001-01-01 01:01:01.1234568", t1.convertScale(true, 7).getString());
    assertEquals("2001-01-01 01:01:01.123457", t1.convertScale(true, 6).getString());
    assertEquals("2001-01-01 01:01:01.12346", t1.convertScale(true, 5).getString());
    assertEquals("2001-01-01 01:01:01.1235", t1.convertScale(true, 4).getString());
    assertEquals("2001-01-01 01:01:01.123", t1.convertScale(true, 3).getString());
    assertEquals("2001-01-01 01:01:01.12", t1.convertScale(true, 2).getString());
    assertEquals("2001-01-01 01:01:01.1", t1.convertScale(true, 1).getString());
    assertEquals("2001-01-01 01:01:01", t1.convertScale(true, 0).getString());
    t1 = ValueTimestamp.parse("-2001-01-01 01:01:01.123456789");
    assertEquals("-2001-01-01 01:01:01.123457", t1.convertScale(true, 6).getString());
    // classes do not match
    assertFalse(ValueTimestamp.parse("2001-01-01").equals(ValueDate.parse("2001-01-01")));
    assertEquals("2001-01-01 01:01:01", ValueTimestamp.parse("2001-01-01").add(ValueTime.parse("01:01:01")).getString());
    assertEquals("1010-10-10 00:00:00", ValueTimestamp.parse("1010-10-10 10:10:10").subtract(ValueTime.parse("10:10:10")).getString());
    assertEquals("-2001-01-01 01:01:01", ValueTimestamp.parse("-2001-01-01").add(ValueTime.parse("01:01:01")).getString());
    assertEquals("-1010-10-10 00:00:00", ValueTimestamp.parse("-1010-10-10 10:10:10").subtract(ValueTime.parse("10:10:10")).getString());
    if (SysProperties.UNLIMITED_TIME_RANGE) {
        assertEquals("2001-01-02 01:01:01", ValueTimestamp.parse("2001-01-01").add(ValueTime.parse("25:01:01")).getString());
        assertEquals("1010-10-10 10:00:00", ValueTimestamp.parse("1010-10-11 10:10:10").subtract(ValueTime.parse("24:10:10")).getString());
    }
    assertEquals(0, DateTimeUtils.absoluteDayFromDateValue(ValueTimestamp.parse("1970-01-01").getDateValue()));
    assertEquals(0, ValueTimestamp.parse("1970-01-01").getTimeNanos());
    assertEquals(0, ValueTimestamp.parse("1970-01-01 00:00:00.000 UTC").getTimestamp().getTime());
    assertEquals(0, ValueTimestamp.parse("+1970-01-01T00:00:00.000Z").getTimestamp().getTime());
    assertEquals(0, ValueTimestamp.parse("1970-01-01T00:00:00.000+00:00").getTimestamp().getTime());
    assertEquals(0, ValueTimestamp.parse("1970-01-01T00:00:00.000-00:00").getTimestamp().getTime());
    new AssertThrows(ErrorCode.INVALID_DATETIME_CONSTANT_2) {

        @Override
        public void test() {
            ValueTimestamp.parse("1970-01-01 00:00:00.000 ABC");
        }
    };
    new AssertThrows(ErrorCode.INVALID_DATETIME_CONSTANT_2) {

        @Override
        public void test() {
            ValueTimestamp.parse("1970-01-01T00:00:00.000+ABC");
        }
    };
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) ValueTimestamp(org.h2.value.ValueTimestamp)

Example 37 with AssertThrows

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

the class TestAutoReconnect method testWrongUrl.

private void testWrongUrl() throws Exception {
    deleteDb(getTestName());
    Server tcp = Server.createTcpServer().start();
    try {
        conn = getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";AUTO_SERVER=TRUE");
        assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";OPEN_NEW=TRUE");
        assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";OPEN_NEW=TRUE");
        conn.close();
        conn = getConnection("jdbc:h2:tcp://localhost/" + getBaseDir() + "/" + getTestName());
        assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";AUTO_SERVER=TRUE;OPEN_NEW=TRUE");
        conn.close();
    } finally {
        tcp.stop();
    }
}
Also used : Server(org.h2.tools.Server)

Example 38 with AssertThrows

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

the class TestMVTableEngine method testTimeout.

private void testTimeout() throws Exception {
    Connection conn;
    Connection conn2;
    Statement stat;
    Statement stat2;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE;MVCC=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id identity, name varchar)");
    conn2 = getConnection(url);
    stat2 = conn2.createStatement();
    conn.setAutoCommit(false);
    conn2.setAutoCommit(false);
    stat.execute("insert into test values(1, 'Hello')");
    assertThrows(ErrorCode.LOCK_TIMEOUT_1, stat2).execute("insert into test values(1, 'Hello')");
    conn2.close();
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 39 with AssertThrows

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

the class TestMultiDimension method testHelperMethods.

private void testHelperMethods() {
    MultiDimension m = MultiDimension.getInstance();
    assertEquals(Integer.MAX_VALUE, m.getMaxValue(2));
    assertEquals(0, m.normalize(2, 0, 0, 100));
    assertEquals(Integer.MAX_VALUE / 2, m.normalize(2, 50, 0, 100));
    assertEquals(Integer.MAX_VALUE, m.normalize(2, 100, 0, 100));
    assertEquals(Integer.MAX_VALUE / 10, m.normalize(2, 0.1, 0, 1));
    assertEquals(0, m.normalize(2, 1, 1, 1));
    assertEquals(0, m.normalize(2, 0, 0, 0));
    assertEquals(3, m.interleave(1, 1));
    assertEquals(3, m.interleave(new int[] { 1, 1 }));
    assertEquals(5, m.interleave(3, 0));
    assertEquals(5, m.interleave(new int[] { 3, 0 }));
    assertEquals(10, m.interleave(0, 3));
    assertEquals(10, m.interleave(new int[] { 0, 3 }));
    long v = Integer.MAX_VALUE | ((long) Integer.MAX_VALUE << 31L);
    assertEquals(v, m.interleave(Integer.MAX_VALUE, Integer.MAX_VALUE));
    assertEquals(v, m.interleave(new int[] { Integer.MAX_VALUE, Integer.MAX_VALUE }));
    Random random = new Random(1);
    for (int i = 0; i < 1000; i++) {
        int x = random.nextInt(Integer.MAX_VALUE), y = random.nextInt(Integer.MAX_VALUE);
        v = m.interleave(new int[] { x, y });
        long v2 = m.interleave(x, y);
        assertEquals(v, v2);
        int x1 = m.deinterleave(2, v, 0);
        int y1 = m.deinterleave(2, v, 1);
        assertEquals(x, x1);
        assertEquals(y, y1);
    }
    for (int i = 0; i < 1000; i++) {
        int x = random.nextInt(1000), y = random.nextInt(1000), z = random.nextInt(1000);
        MultiDimension tool = MultiDimension.getInstance();
        long xyz = tool.interleave(new int[] { x, y, z });
        assertEquals(x, tool.deinterleave(3, xyz, 0));
        assertEquals(y, tool.deinterleave(3, xyz, 1));
        assertEquals(z, tool.deinterleave(3, xyz, 2));
    }
    createClassProxy(MultiDimension.class);
    assertThrows(IllegalArgumentException.class, m).getMaxValue(1);
    assertThrows(IllegalArgumentException.class, m).getMaxValue(33);
    assertThrows(IllegalArgumentException.class, m).normalize(2, 10, 11, 12);
    assertThrows(IllegalArgumentException.class, m).normalize(2, 5, 10, 0);
    assertThrows(IllegalArgumentException.class, m).normalize(2, 10, 0, 9);
    assertThrows(IllegalArgumentException.class, m).interleave(-1, 5);
    assertThrows(IllegalArgumentException.class, m).interleave(5, -1);
    assertThrows(IllegalArgumentException.class, m).interleave(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
}
Also used : Random(java.util.Random) MultiDimension(org.h2.tools.MultiDimension)

Example 40 with AssertThrows

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

the class TestMVStore method testIterate.

private void testIterate() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = openStore(fileName);
    MVMap<Integer, String> m = s.openMap("data");
    Iterator<Integer> it = m.keyIterator(null);
    assertFalse(it.hasNext());
    for (int i = 0; i < 10; i++) {
        m.put(i, "hello " + i);
    }
    s.commit();
    it = m.keyIterator(null);
    it.next();
    assertThrows(UnsupportedOperationException.class, it).remove();
    it = m.keyIterator(null);
    for (int i = 0; i < 10; i++) {
        assertTrue(it.hasNext());
        assertEquals(i, it.next().intValue());
    }
    assertFalse(it.hasNext());
    assertNull(it.next());
    for (int j = 0; j < 10; j++) {
        it = m.keyIterator(j);
        for (int i = j; i < 10; i++) {
            assertTrue(it.hasNext());
            assertEquals(i, it.next().intValue());
        }
        assertFalse(it.hasNext());
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

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