Search in sources :

Example 26 with DbObject

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

the class SettableDataSetterFactoryTest method testUDT.

@Test
public void testUDT() throws Exception {
    UserType ut = newDBObjectUserType();
    Setter<SettableByIndexData, DbObject> setter = factory.getSetter(newPM(DbObject.class, ut));
    DbObject object = DbObject.newInstance();
    setter.set(statement, object);
    ArgumentCaptor<UDTValue> argumentCaptor = ArgumentCaptor.forClass(UDTValue.class);
    verify(statement).setUDTValue(eq(0), argumentCaptor.capture());
    UDTValue value = argumentCaptor.getValue();
    assertUdtEqualsDbObject(value, object);
    setter.set(statement, null);
    verify(statement).setToNull(0);
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 27 with DbObject

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

the class ObjectClassMetaTest method testAliasProvider.

@Test
public void testAliasProvider() {
    ReflectionService reflectionService = ReflectionService.newInstance().withAliasProvider(new AliasProvider() {

        @Override
        public String getAliasForMethod(Method method) {
            if ("getName".equals(method.getName())) {
                return "myname";
            }
            return null;
        }

        @Override
        public String getAliasForField(Field field) {
            if ("id".equals(field.getName())) {
                return "myid";
            }
            return null;
        }

        @Override
        public Table getTable(Class<?> target) {
            return null;
        }
    });
    ClassMeta<DbObject> classMeta = reflectionService.getClassMeta(DbObject.class);
    PropertyFinder<DbObject> propertyFinder1 = classMeta.newPropertyFinder(propertyFilter);
    // force non direct mode
    propertyFinder1.findProperty(DefaultPropertyNameMatcher.of("email"), new Object[0]);
    assertNotNull(propertyFinder1.findProperty(DefaultPropertyNameMatcher.of("myid"), new Object[0]));
    assertNotNull(propertyFinder1.findProperty(DefaultPropertyNameMatcher.of("myname"), new Object[0]));
    classMeta = ReflectionService.newInstance().getClassMeta(DbObject.class);
    PropertyFinder<DbObject> propertyFinder2 = classMeta.newPropertyFinder(propertyFilter);
    // force non direct mode
    propertyFinder2.findProperty(DefaultPropertyNameMatcher.of("email"), new Object[0]);
    assertNull(propertyFinder2.findProperty(DefaultPropertyNameMatcher.of("myid"), new Object[0]));
    assertNull(propertyFinder2.findProperty(DefaultPropertyNameMatcher.of("myname"), new Object[0]));
}
Also used : Field(java.lang.reflect.Field) DbObject(org.simpleflatmapper.test.beans.DbObject) DbObject(org.simpleflatmapper.test.beans.DbObject) DbPartialFinalObject(org.simpleflatmapper.test.beans.DbPartialFinalObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) ReflectionService(org.simpleflatmapper.reflect.ReflectionService) Method(java.lang.reflect.Method) KeyTest(org.simpleflatmapper.reflect.test.KeyTest) Test(org.junit.Test)

Example 28 with DbObject

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

the class ConvertDelegateSetterTest method testSet.

@Test
public void testSet() throws Exception {
    ConvertDelegateSetter<DbObject, UUID, String> setter = new ConvertDelegateSetter<DbObject, UUID, String>(new Setter<DbObject, String>() {

        @Override
        public void set(DbObject target, String value) throws Exception {
            target.setName(value);
        }
    }, new Converter<UUID, String>() {

        @Override
        public String convert(UUID in) throws Exception {
            return in.toString();
        }
    });
    DbObject o = new DbObject();
    UUID uuid = UUID.randomUUID();
    setter.set(o, uuid);
    assertEquals(uuid.toString(), o.getName());
}
Also used : ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) DbObject(org.simpleflatmapper.test.beans.DbObject) UUID(java.util.UUID) Test(org.junit.Test)

Example 29 with DbObject

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

the class JdbcTemplateCrudTest method testCrudBatch.

@Test
public void testCrudBatch() 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
    final List<DbObject> objects = Arrays.asList(object);
    Long key = objectCrud.create(objects, new CheckedConsumer<Long>() {

        Long key;

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

Example 30 with DbObject

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

the class JdbcTemplateCrudTest method testLazyCrud.

@Test
public void testLazyCrud() throws SQLException {
    // drop table if exist
    try {
        template.execute("DROP TABLE TEST_DB_OBJECT_LAZY");
    } catch (Exception e) {
    }
    JdbcTemplateCrud<DbObject, Long> objectCrud = JdbcTemplateMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).lazilyTo(template, "TEST_DB_OBJECT_LAZY");
    template.execute("create table TEST_DB_OBJECT_LAZY(" + " id bigint primary key," + " name varchar(100), " + " email varchar(100)," + " creation_Time timestamp, type_ordinal int, type_name varchar(10)  )");
    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));
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) SQLException(java.sql.SQLException) 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