Search in sources :

Example 46 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class QueryPreparerTest method testQueryBinderClosePsOnException.

@Test
public void testQueryBinderClosePsOnException() throws SQLException {
    NamedSqlQuery query = NamedSqlQuery.parse("select id, name, email, creation_time, type_ordinal, type_name from TEST_DB_OBJECT where id = ? ");
    QueryPreparer<DbObject> queryPreparer = jdbcMapperFactory.from(DbObject.class).to(query);
    Connection conn = mock(Connection.class);
    PreparedStatement ps = mock(PreparedStatement.class);
    when(conn.prepareStatement(anyString())).thenReturn(ps);
    doThrow(new IllegalStateException()).when(ps).setLong(anyInt(), anyLong());
    try {
        queryPreparer.prepare(conn).bind(DbObject.newInstance());
        fail();
    } catch (IllegalStateException e) {
    // expected
    }
    verify(ps).close();
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 47 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class QueryPreparerTest method testInsertAndSelectWithPreparedStatementMapper.

@Test
public void testInsertAndSelectWithPreparedStatementMapper() throws SQLException {
    NamedSqlQuery insertQuery = NamedSqlQuery.parse("INSERT INTO test_db_object(id, name, email, creation_time, type_ordinal, type_name) values(?, ?, ?, ?, ?, ?) ");
    NamedSqlQuery selectQuery = NamedSqlQuery.parse("select id, name, email, creation_time, type_ordinal, type_name from TEST_DB_OBJECT where id = ? ");
    QueryPreparer<DbObject> insertQueryPreparer = jdbcMapperFactory.from(DbObject.class).to(insertQuery);
    QueryPreparer<DbObject> selectQueryPreparer = jdbcMapperFactory.from(DbObject.class).to(selectQuery);
    DbObject dbObject = DbObject.newInstance();
    Connection connection = DbHelper.objectDb();
    try {
        PreparedStatement ps = insertQueryPreparer.prepare(connection).bind(dbObject);
        try {
            ps.execute();
        } finally {
            ps.close();
        }
        final QueryBinder<DbObject> queryBinder = selectQueryPreparer.prepare(connection);
        ps = selectQueryPreparer.prepareStatement(connection);
        try {
            queryBinder.bindTo(dbObject, ps);
            ResultSet resultSet = ps.executeQuery();
            assertTrue(resultSet.next());
            assertEquals(dbObject, JdbcMapperFactory.newInstance().newMapper(DbObject.class).map(resultSet));
        } finally {
            ps.close();
        }
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 48 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class ConnectedCrudTest method testDbObjectCrud.

@Test
public void testDbObjectCrud() throws Exception {
    DataSource dataSource = DbHelper.getHsqlDataSource();
    ConnectedCrud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(dataSource, "TEST_DB_OBJECT");
    checkCrudDbObject(objectCrud, DbObject.newInstance());
    Connection connection = dataSource.getConnection();
    try {
        CrudTest.checkCrudDbObject(connection, objectCrud.crud(), DbObject.newInstance());
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 49 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class CrudTest method testCompositeKey.

@Test
public void testCompositeKey() throws SQLException {
    Connection connection = DbHelper.getDbConnection(targetDB);
    if (connection == null) {
        System.err.println("Db " + targetDB + " not available");
        return;
    }
    try {
        Crud<DbObject, CKEY> objectCrud = JdbcMapperFactory.newInstance().<DbObject, CKEY>crud(DbObject.class, CKEY.class).table(connection, "TEST_DB_OBJECT_CKEY");
        DbObject object = DbObject.newInstance();
        CKEY ckey = new CKEY();
        ckey.id = object.getId();
        ckey.name = object.getName();
        assertNull(objectCrud.read(connection, ckey));
        // create
        CKEY key = objectCrud.create(connection, object, new CheckedConsumer<CKEY>() {

            CKEY key;

            @Override
            public void accept(CKEY aLong) throws Exception {
                key = aLong;
            }
        }).key;
        assertNull(key);
        key = ckey;
        // read
        assertEquals(object, objectCrud.read(connection, key));
        object.setEmail("Udpdated");
        // update
        objectCrud.update(connection, object);
        assertEquals(object, objectCrud.read(connection, key));
        // delete
        objectCrud.delete(connection, key);
        assertNull(objectCrud.read(connection, key));
        List<DbObject> values = Arrays.asList(DbObject.newInstance(), DbObject.newInstance());
        final List<CKEY> keys = new ArrayList<CKEY>();
        for (DbObject value : values) {
            CKEY k = new CKEY();
            k.id = value.getId();
            k.name = value.getName();
            keys.add(k);
        }
        objectCrud.create(connection, values);
        assertCollectionEquals(values, objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
        int i = 333;
        for (DbObject value : values) {
            value.setEmail(Integer.toHexString(i));
            i++;
        }
        objectCrud.update(connection, values);
        assertCollectionEquals(values, objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
        objectCrud.delete(connection, keys);
        assertCollectionEquals(Collections.<DbObject>emptyList(), objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
    } finally {
        connection.close();
    }
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 50 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class CrudTest method testDbObjectCrud.

@Test
public void testDbObjectCrud() throws SQLException {
    Connection connection = DbHelper.getDbConnection(targetDB);
    if (connection == null) {
        System.err.println("Db " + targetDB + " not available");
        return;
    }
    try {
        Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
        checkCrudDbObject(connection, objectCrud, DbObject.newInstance());
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

DbObject (org.simpleflatmapper.test.beans.DbObject)83 Test (org.junit.Test)75 Connection (java.sql.Connection)27 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)10 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)10 ArrayList (java.util.ArrayList)9 MappingException (org.simpleflatmapper.map.MappingException)6 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)6 Type (java.lang.reflect.Type)5 ParseException (java.text.ParseException)5 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)5 StringReader (java.io.StringReader)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)4 Statement (java.sql.Statement)3 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 ListCollector (org.simpleflatmapper.util.ListCollector)3