Search in sources :

Example 11 with CheckedConsumer

use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.

the class JdbcTemplateCrudTest method testCrud.

@Test
public void testCrud() throws SQLException {
    JdbcTemplateCrud<DbObject, Long> objectCrud = JdbcTemplateMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).to(template, "TEST_DB_OBJECT");
    DbObject object = DbObject.newInstance();
    assertNull(objectCrud.read(object.getId()));
    // create
    Long key = objectCrud.create(object, new CheckedConsumer<Long>() {

        Long key;

        @Override
        public void accept(Long aLong) throws Exception {
            key = aLong;
        }
    }).key;
    assertNull(key);
    key = object.getId();
    // read
    assertEquals(object, objectCrud.read(key));
    object.setName("Udpdated");
    // update
    objectCrud.update(object);
    assertEquals(object, objectCrud.read(key));
    // delete
    objectCrud.delete(key);
    assertNull(objectCrud.read(key));
    objectCrud.create(object);
    assertEquals(object, objectCrud.read(key));
    objectCrud.delete(key);
    try {
        objectCrud.createOrUpdate(object);
        assertEquals(object, objectCrud.read(key));
    } catch (UnsupportedOperationException e) {
    }
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 12 with CheckedConsumer

use of org.simpleflatmapper.util.CheckedConsumer in project SimpleFlatMapper by arnaudroger.

the class PostgresqlCrudTest method testSerial.

@Test
public void testSerial() throws SQLException {
    Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
    if (connection == null) {
        System.err.println("Db POSTGRESQL not available");
        return;
    }
    try {
        Statement st = connection.createStatement();
        try {
            st.execute("CREATE TABLE IF NOT EXISTS test_db_object_serial(  id bigserial primary key," + " name varchar(100), " + " email varchar(100)," + " creation_Time timestamp, type_ordinal int, type_name varchar(10)  )");
        } finally {
            st.close();
        }
        Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().addKeys("id").<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_serial");
        DbObject object = DbObject.newInstance();
        object.setId(-22225);
        // create
        Long key = objectCrud.create(connection, object, new CheckedConsumer<Long>() {

            Long key;

            @Override
            public void accept(Long aLong) throws Exception {
                key = aLong;
            }
        }).key;
        assertFalse(key.equals(object.getId()));
        object.setId(key);
        // read
        assertEquals(object, objectCrud.read(connection, key));
    } finally {
        connection.close();
    }
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 DbObject (org.simpleflatmapper.test.beans.DbObject)12 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)12 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 Statement (java.sql.Statement)3 ParseException (java.text.ParseException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 DataSource (javax.sql.DataSource)2 ConnectedCrud (org.simpleflatmapper.jdbc.ConnectedCrud)2 DataSourceTransactionTemplate (org.simpleflatmapper.jdbc.impl.DataSourceTransactionTemplate)2 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchElementException (java.util.NoSuchElementException)1 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)1 DbListObject (org.simpleflatmapper.test.beans.DbListObject)1 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)1