use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperFactoryTest method testCustomGetterFactory.
@Test
public void testCustomGetterFactory() throws SQLException {
JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().getterFactory(new GetterFactory<ResultSet, JdbcColumnKey>() {
@SuppressWarnings("unchecked")
@Override
public <P> Getter<ResultSet, P> newGetter(Type target, JdbcColumnKey key, Object... properties) {
return new Getter() {
@Override
public Object get(Object target) throws Exception {
return "Hello!";
}
};
}
}).newBuilder(DbObject.class).addMapping("name").mapper();
ResultSet rs = mock(ResultSet.class);
when(rs.next()).thenReturn(true, false);
DbObject object = mapper.iterator(rs).next();
assertEquals("Hello!", object.getName());
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperFactoryTest method testFieldErrorHandling.
@Test
public void testFieldErrorHandling() throws SQLException, Exception, ParseException {
@SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
final Exception exception = new Exception("Error!");
JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {
@Override
public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws Exception {
throw exception;
}
}).newBuilder(DbObject.class).addMapping("id").mapper();
List<DbObject> list = mapper.forEach(new MockDbObjectResultSet(1), new ListCollector<DbObject>()).getList();
assertNotNull(list.get(0));
verify(fieldMapperErrorHandler).errorMappingField(eq(new JdbcColumnKey("id", 1)), any(), same(list.get(0)), same(exception));
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class SetRowMapperTest method checkSetRowMapperIdName.
private void checkSetRowMapperIdName(SetRowMapper<Object[], Object[][], DbObject, RuntimeException> staticSetRowMapper) throws Exception {
checkIdNameResult(staticSetRowMapper.forEach(ID_NAME_DATA, new ListCollector<DbObject>()).getList());
checkIdNameResult(staticSetRowMapper.iterator(ID_NAME_DATA));
// IFJAVA8_START
checkIdNameResult(staticSetRowMapper.stream(ID_NAME_DATA).collect(Collectors.<DbObject>toList()));
// IFJAVA8_END
checkIdNameRow(1l, staticSetRowMapper.map(ID_NAME_DATA[0]));
checkIdNameRow(1l, staticSetRowMapper.map(ID_NAME_DATA[0], null));
DbObject d = new DbObject();
staticSetRowMapper.mapTo(ID_NAME_DATA[0], d, null);
checkIdNameRow(1l, d);
}
use of org.simpleflatmapper.test.beans.DbObject 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.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class SqlParameterSourceTest method testAlias.
@Test
public void testAlias() {
SqlParameterSourceFactory<DbObject> parameterSourceFactory = JdbcTemplateMapperFactory.newInstance().addAlias("e", "email").newSqlParameterSourceFactory(DbObject.class);
DbObject dbObject = getDbObject();
SqlParameterSource parameterSource = parameterSourceFactory.newSqlParameterSource(dbObject);
assertEquals(12345l, parameterSource.getValue("id"));
assertEquals("name", parameterSource.getValue("name"));
assertEquals("email", parameterSource.getValue("e"));
assertEquals("email", parameterSource.getValue("email"));
}
Aggregations