Search in sources :

Example 76 with DbObject

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

the class OptionalClassMetaTest method testFindProperty.

@Test
public void testFindProperty() throws Exception {
    PropertyMeta<Optional<DbObject>, String> email = objectClassMeta.newPropertyFinder(isValidPropertyMeta).findProperty(DefaultPropertyNameMatcher.of("email"), new Object[0]);
    DbObject dbObject = new DbObject();
    dbObject.setEmail("houlala 2 la mission!");
    Optional<DbObject> optional = Optional.of(dbObject);
    assertEquals("houlala 2 la mission!", email.getGetter().get(optional));
    email.getSetter().set(optional, "OuiOui");
    assertEquals("OuiOui", email.getGetter().get(optional));
    assertEquals(null, email.getGetter().get(Optional.<DbObject>empty()));
    PropertyMeta<Optional<String>, String> strValue = stringClassMeta.newPropertyFinder(isValidPropertyMeta).findProperty(DefaultPropertyNameMatcher.of("value"), new Object[0]);
    assertEquals("str", strValue.getGetter().get(Optional.of("str")));
    assertTrue(NullSetter.isNull(strValue.getSetter()));
}
Also used : Optional(java.util.Optional) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 77 with DbObject

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

the class SubPropertyMetaTest method testSubProperty.

@Test
@SuppressWarnings("unchecked")
public void testSubProperty() throws Exception {
    ClassMeta<Db1DeepObject> classMeta = ReflectionService.newInstance().getClassMeta(Db1DeepObject.class);
    PropertyMeta<Db1DeepObject, String> property = classMeta.newPropertyFinder(new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return true;
        }
    }).findProperty(new DefaultPropertyNameMatcher("dbObject_name", 0, false, false), new Object[0]);
    assertTrue(property instanceof SubPropertyMeta);
    assertTrue(property.isSubProperty());
    assertTrue(property.toString().startsWith("SubPropertyMeta{" + "ownerProperty=ObjectPropertyMeta{"));
    SubPropertyMeta<Db1DeepObject, DbObject, String> subPropertyMeta = (SubPropertyMeta<Db1DeepObject, DbObject, String>) property;
    assertEquals(String.class, subPropertyMeta.getPropertyType());
    assertEquals("dbObject.name", subPropertyMeta.getPath());
    Db1DeepObject object = new Db1DeepObject();
    object.setDbObject(new DbObject());
    subPropertyMeta.getSetter().set(object, "n1");
    Getter<Db1DeepObject, String> getter = subPropertyMeta.getGetter();
    assertEquals("n1", getter.get(object));
    Db1DeepObject objectNull = new Db1DeepObject();
    assertEquals(null, getter.get(objectNull));
    assertTrue(getter.toString().startsWith("GetterOnGetter{g1="));
    ClassMeta<?> meta = subPropertyMeta.getPropertyClassMeta();
    assertEquals(String.class, meta.getType());
}
Also used : Db1DeepObject(org.simpleflatmapper.test.beans.Db1DeepObject) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) DbObject(org.simpleflatmapper.test.beans.DbObject) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Example 78 with DbObject

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

the class ResultSetMapperFactoryTest method testMapToDbObjectStatic.

@Test
public void testMapToDbObjectStatic() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    dbi.registerMapper(new SfmResultSetMapperFactory(new UnaryFactory<Class<?>, Mapper<ResultSet, ?>>() {

        @Override
        public Mapper<ResultSet, ?> newInstance(Class<?> aClass) {
            return new Mapper<ResultSet, DbObject>() {

                @Override
                public DbObject map(ResultSet source) throws MappingException {
                    return map(source, null);
                }

                @Override
                public DbObject map(ResultSet source, MappingContext<? super ResultSet> context) throws MappingException {
                    DbObject dbObject = new DbObject();
                    try {
                        mapTo(source, dbObject, context);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return dbObject;
                }

                @Override
                public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> context) throws Exception {
                    target.setId(source.getInt("id"));
                    target.setCreationTime(source.getTimestamp("creation_time"));
                    target.setEmail(source.getString("email"));
                    target.setName(source.getString("name"));
                    String type_name = source.getString("type_name");
                    if (type_name != null) {
                        target.setTypeName(DbObject.Type.valueOf(type_name));
                    }
                    target.setTypeOrdinal(DbObject.Type.values()[source.getInt("type_ordinal")]);
                }
            };
        }
    }));
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
        DbHelper.assertDbObjectMapping(dbObject);
        SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
        attach.insert(DbObject.newInstance());
        assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
    } finally {
        handle.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) SfmResultSetMapperFactory(org.simpleflatmapper.jdbi.SfmResultSetMapperFactory) DBI(org.skife.jdbi.v2.DBI) MappingException(org.simpleflatmapper.map.MappingException) Handle(org.skife.jdbi.v2.Handle) MappingContext(org.simpleflatmapper.map.MappingContext) Mapper(org.simpleflatmapper.map.Mapper) ResultSet(java.sql.ResultSet) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) Test(org.junit.Test)

Example 79 with DbObject

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

the class ResultSetMapperFactoryTest method testMapToDbObject.

@Test
public void testMapToDbObject() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    dbi.registerMapper(new SfmResultSetMapperFactory());
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
        DbHelper.assertDbObjectMapping(dbObject);
        SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
        attach.insert(DbObject.newInstance());
        assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
    } finally {
        handle.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) SfmResultSetMapperFactory(org.simpleflatmapper.jdbi.SfmResultSetMapperFactory) DBI(org.skife.jdbi.v2.DBI) Handle(org.skife.jdbi.v2.Handle) Test(org.junit.Test)

Example 80 with DbObject

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

the class SfmBindTest method testBindDbObject.

@Test
public void testBindDbObject() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    // dbi.registerMapper(new SfmResultSetMapperFactory());
    Handle handle = dbi.open();
    try {
        SfmBindExample attach = handle.attach(SfmBindExample.class);
        DbObject dbObject1 = DbObject.newInstance();
        DbObject dbObject2 = DbObject.newInstance();
        checkObjectNotThere(handle, dbObject1);
        checkObjectNotThere(handle, dbObject2);
        attach.insert(dbObject1);
        checkObjectInserted(attach, handle, dbObject1);
        attach.insert(dbObject2);
        checkObjectInserted(attach, handle, dbObject2);
    } finally {
        handle.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) DBI(org.skife.jdbi.v2.DBI) Handle(org.skife.jdbi.v2.Handle) 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