Search in sources :

Example 6 with AssertThrows

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

the class TestValue method testUUID.

private void testUUID() {
    long maxHigh = 0, maxLow = 0, minHigh = -1L, minLow = -1L;
    for (int i = 0; i < 100; i++) {
        ValueUuid uuid = ValueUuid.getNewRandom();
        maxHigh |= uuid.getHigh();
        maxLow |= uuid.getLow();
        minHigh &= uuid.getHigh();
        minLow &= uuid.getLow();
    }
    ValueUuid max = ValueUuid.get(maxHigh, maxLow);
    assertEquals("ffffffff-ffff-4fff-bfff-ffffffffffff", max.getString());
    ValueUuid min = ValueUuid.get(minHigh, minLow);
    assertEquals("00000000-0000-4000-8000-000000000000", min.getString());
    // Test conversion from ValueJavaObject to ValueUuid
    String uuidStr = "12345678-1234-4321-8765-123456789012";
    UUID origUUID = UUID.fromString(uuidStr);
    ValueJavaObject valObj = ValueJavaObject.getNoCopy(origUUID, null, null);
    Value valUUID = valObj.convertTo(Value.UUID);
    assertTrue(valUUID instanceof ValueUuid);
    assertTrue(valUUID.getString().equals(uuidStr));
    assertTrue(valUUID.getObject().equals(origUUID));
    ValueJavaObject voString = ValueJavaObject.getNoCopy(new String("This is not a ValueUuid object"), null, null);
    assertThrows(DbException.class, voString).convertTo(Value.UUID);
}
Also used : ValueUuid(org.h2.value.ValueUuid) ValueJavaObject(org.h2.value.ValueJavaObject) Value(org.h2.value.Value) ValueString(org.h2.value.ValueString) UUID(java.util.UUID) DbException(org.h2.message.DbException)

Example 7 with AssertThrows

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

the class TestValue method testModulusDouble.

private void testModulusDouble() {
    final ValueDouble vd1 = ValueDouble.get(12);
    new AssertThrows(ErrorCode.DIVISION_BY_ZERO_1) {

        @Override
        public void test() {
            vd1.modulus(ValueDouble.get(0));
        }
    };
    ValueDouble vd2 = ValueDouble.get(10);
    ValueDouble vd3 = vd1.modulus(vd2);
    assertEquals(2, vd3.getDouble());
}
Also used : ValueDouble(org.h2.value.ValueDouble) AssertThrows(org.h2.test.utils.AssertThrows)

Example 8 with AssertThrows

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

the class TestValue method testCastTrim.

private void testCastTrim() {
    Value v;
    String spaces = new String(new char[100]).replace((char) 0, ' ');
    v = ValueArray.get(new Value[] { ValueString.get("hello"), ValueString.get("world") });
    assertEquals(10, v.getPrecision());
    assertEquals(5, v.convertPrecision(5, true).getPrecision());
    v = ValueArray.get(new Value[] { ValueString.get(""), ValueString.get("") });
    assertEquals(0, v.getPrecision());
    assertEquals("('')", v.convertPrecision(1, true).toString());
    v = ValueBytes.get(spaces.getBytes());
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getBytes().length);
    assertEquals(32, v.convertPrecision(10, false).getBytes()[9]);
    assertEquals(10, v.convertPrecision(10, true).getPrecision());
    final Value vd = ValueDecimal.get(new BigDecimal("1234567890.123456789"));
    assertEquals(19, vd.getPrecision());
    assertEquals("1234567890.1234567", vd.convertPrecision(10, true).getString());
    new AssertThrows(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1) {

        @Override
        public void test() {
            vd.convertPrecision(10, false);
        }
    };
    v = ValueLobDb.createSmallLob(Value.CLOB, spaces.getBytes(), 100);
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getString().length());
    assertEquals("          ", v.convertPrecision(10, false).getString());
    assertEquals(10, v.convertPrecision(10, true).getPrecision());
    v = ValueLobDb.createSmallLob(Value.BLOB, spaces.getBytes(), 100);
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getBytes().length);
    assertEquals(32, v.convertPrecision(10, false).getBytes()[9]);
    assertEquals(10, v.convertPrecision(10, true).getPrecision());
    ResultSet rs = new SimpleResultSet();
    v = ValueResultSet.get(rs);
    assertEquals(Integer.MAX_VALUE, v.getPrecision());
    assertEquals(Integer.MAX_VALUE, v.convertPrecision(10, false).getPrecision());
    assertTrue(rs == v.convertPrecision(10, false).getObject());
    assertFalse(rs == v.convertPrecision(10, true).getObject());
    assertEquals(Integer.MAX_VALUE, v.convertPrecision(10, true).getPrecision());
    v = ValueString.get(spaces);
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals("          ", v.convertPrecision(10, false).getString());
    assertEquals("          ", v.convertPrecision(10, true).getString());
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) SimpleResultSet(org.h2.tools.SimpleResultSet) Value(org.h2.value.Value) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) ValueResultSet(org.h2.value.ValueResultSet) ValueString(org.h2.value.ValueString) BigDecimal(java.math.BigDecimal)

Example 9 with AssertThrows

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

the class TestValue method testModulusDecimal.

private void testModulusDecimal() {
    final ValueDecimal vd1 = ValueDecimal.get(new BigDecimal(12));
    new AssertThrows(ErrorCode.DIVISION_BY_ZERO_1) {

        @Override
        public void test() {
            vd1.modulus(ValueDecimal.get(new BigDecimal(0)));
        }
    };
    ValueDecimal vd2 = ValueDecimal.get(new BigDecimal(10));
    ValueDecimal vd3 = vd1.modulus(vd2);
    assertEquals(2, vd3.getDouble());
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) ValueDecimal(org.h2.value.ValueDecimal) BigDecimal(java.math.BigDecimal)

Example 10 with AssertThrows

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

the class TestServlet method test.

@Override
public void test() throws SQLException {
    if (config.networked || config.memory) {
        return;
    }
    DbStarter listener = new DbStarter();
    TestServletContext context = new TestServletContext();
    String url = getURL("servlet", true);
    context.setInitParameter("db.url", url);
    context.setInitParameter("db.user", getUser());
    context.setInitParameter("db.password", getPassword());
    context.setInitParameter("db.tcpServer", "-tcpPort 8888");
    ServletContextEvent event = new ServletContextEvent(context);
    listener.contextInitialized(event);
    Connection conn1 = listener.getConnection();
    Connection conn1a = (Connection) context.getAttribute("connection");
    assertTrue(conn1 == conn1a);
    Statement stat1 = conn1.createStatement();
    stat1.execute("CREATE TABLE T(ID INT)");
    String u2 = url.substring(url.indexOf("servlet"));
    u2 = "jdbc:h2:tcp://localhost:8888/" + getBaseDir() + "/" + u2;
    Connection conn2 = DriverManager.getConnection(u2, getUser(), getPassword());
    Statement stat2 = conn2.createStatement();
    stat2.execute("SELECT * FROM T");
    stat2.execute("DROP TABLE T");
    assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, stat1).execute("SELECT * FROM T");
    conn2.close();
    listener.contextDestroyed(event);
    // listener must be stopped
    assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:8888/" + getBaseDir() + "/servlet", getUser(), getPassword());
    // connection must be closed
    assertThrows(ErrorCode.OBJECT_CLOSED, stat1).execute("SELECT * FROM DUAL");
    deleteDb("servlet");
}
Also used : DbStarter(org.h2.server.web.DbStarter) Statement(java.sql.Statement) Connection(java.sql.Connection) ServletContextEvent(javax.servlet.ServletContextEvent)

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