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()));
}
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());
}
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();
}
}
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();
}
}
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();
}
}
Aggregations