use of org.simpleflatmapper.map.property.GetterProperty in project SimpleFlatMapper by arnaudroger.
the class CrudTest method testDbObjectCrudWithCustomGetter.
@Test
public void testDbObjectCrudWithCustomGetter() throws Exception {
JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance().addColumnProperty("name", new GetterProperty(new Getter<ResultSet, String>() {
@Override
public String get(ResultSet target) throws Exception {
return "customname";
}
}));
Connection connection = DbHelper.getDbConnection(targetDB);
if (connection == null) {
System.err.println("Db " + targetDB + " not available");
return;
}
try {
Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection);
TestDbObject testDbObject = DbObject.newInstance(new TestDbObject());
objectCrud.create(connection, testDbObject);
TestDbObject read = objectCrud.read(connection, testDbObject.getId());
assertEquals("customname", read.getName());
assertEquals(testDbObject.getEmail(), read.getEmail());
} finally {
connection.close();
}
}
use of org.simpleflatmapper.map.property.GetterProperty in project SimpleFlatMapper by arnaudroger.
the class QueryPreparerBuilderTest method testCustomGetter.
@Test
public void testCustomGetter() throws Exception {
PreparedStatementMapperBuilder<DMClass> mapperBuilder = JdbcMapperFactory.newInstance().addColumnProperty("value", new GetterProperty(new Getter<Object, String>() {
@Override
public String get(Object target) throws Exception {
return "value2";
}
})).buildFrom(DMClass.class);
mapperBuilder.addColumn("value");
Mapper<DMClass, PreparedStatement> mapper = mapperBuilder.mapper();
DMClass dmClass = new DMClass();
PreparedStatement ps = mock(PreparedStatement.class);
mapper.mapTo(dmClass, ps, null);
verify(ps).setString(1, "value2");
}
use of org.simpleflatmapper.map.property.GetterProperty in project SimpleFlatMapper by arnaudroger.
the class Issue365Test method mapperConfig.
private MapperConfig<SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> mapperConfig() {
FieldMapperColumnDefinitionProviderImpl<SampleFieldKey> provider = new FieldMapperColumnDefinitionProviderImpl<SampleFieldKey>();
provider.addColumnDefinition("benchmark", FieldMapperColumnDefinition.<SampleFieldKey>identity().add(new SetterProperty(SETTER)).add(new GetterProperty(GETTER)));
return MapperConfig.<SampleFieldKey>fieldMapperConfig().columnDefinitions(provider);
}
use of org.simpleflatmapper.map.property.GetterProperty in project SimpleFlatMapper by arnaudroger.
the class AbstractMapperBuilderTest method testCustomization.
@Test
public void testCustomization() throws Exception {
ClassMeta<DbObject> classMeta = ReflectionService.newInstance().getClassMeta(DbObject.class);
Mapper<Object[], DbObject> mapper = new SampleMapperBuilder<DbObject>(classMeta).addKey("id").addMapper(new FieldMapper<Object[], DbObject>() {
@Override
public void mapTo(Object[] source, DbObject target, MappingContext<? super Object[]> context) throws Exception {
target.setName("fieldMapper");
}
}).addMapping("email", new GetterProperty(new Getter<Object, String>() {
@Override
public String get(Object target) throws Exception {
return "getterEmail";
}
})).mapper();
DbObject dbObject = mapper.map(new Object[] { 1l });
assertEquals(1, dbObject.getId());
assertEquals("fieldMapper", dbObject.getName());
assertEquals("getterEmail", dbObject.getEmail());
mapper = new SampleMapperBuilder<DbObject>(classMeta).addKey("id").addMapping("email", (Object) FieldMapperColumnDefinition.<SampleFieldKey>identity().add(new GetterProperty(new Getter<Object, String>() {
@Override
public String get(Object target) throws Exception {
return "getterEmail";
}
}))).addMapping("name", FieldMapperColumnDefinition.<SampleFieldKey>identity().add(new GetterProperty(new Getter<Object, String>() {
@Override
public String get(Object target) throws Exception {
return "getterName";
}
}))).mapper();
dbObject = mapper.map(new Object[] { 1l });
assertEquals(1, dbObject.getId());
assertEquals("getterEmail", dbObject.getEmail());
assertEquals("getterName", dbObject.getName());
}
use of org.simpleflatmapper.map.property.GetterProperty in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperCustomMappingTest method testCustomReaderOnSetterStatic.
@SuppressWarnings("unchecked")
@Test
public void testCustomReaderOnSetterStatic() throws Exception {
final JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().newBuilder(DbObject.class).addMapping("id", new GetterProperty(new Getter<ResultSet, Long>() {
@Override
public Long get(ResultSet target) throws Exception {
return 1l;
}
})).addMapping(// email, creation_time, type_ordinal, type_name
"name").addMapping("email").addMapping("creation_time").addMapping("type_ordinal").addMapping("type_name").mapper();
DbHelper.testQuery(new TestRowHandler<PreparedStatement>() {
@Override
public void handle(PreparedStatement t) throws Exception {
ResultSet r = t.executeQuery();
r.next();
DbHelper.assertDbObjectMapping(mapper.map(r));
}
}, DbHelper.TEST_DB_OBJECT_QUERY.replace("id,", "33 as id,"));
}
Aggregations