use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class PreparedStatementFieldMapperFactoryTest method newPropertyMapping.
@SuppressWarnings("unchecked")
private <T, P> PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> newPropertyMapping(Getter getter, Class<P> clazz, int sqltype, Object... properties) {
PropertyMeta<T, P> propertyMeta = mock(PropertyMeta.class);
when(propertyMeta.getGetter()).thenReturn(getter);
when(propertyMeta.getPropertyType()).thenReturn(clazz);
return new PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(propertyMeta, new JdbcColumnKey("col", index++, sqltype), FieldMapperColumnDefinition.<JdbcColumnKey>of(properties));
}
use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class ResultSetFieldMapperFactoryTest method testPrimitiveField.
@Test
public void testPrimitiveField() {
ClassMeta<DbObject> classMeta = ReflectionService.newInstance(false).getClassMeta(DbObject.class);
PropertyMeta<DbObject, Long> id = classMeta.newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).<Long>findProperty(new DefaultPropertyNameMatcher("id", 0, false, false), new Object[0]);
FieldMapperColumnDefinition<JdbcColumnKey> identity = FieldMapperColumnDefinition.identity();
PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 1), identity);
FieldMapper<ResultSet, DbObject> fieldMapper = factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
assertTrue(fieldMapper instanceof LongFieldMapper);
PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping1 = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 0), identity);
fieldMapper = factory.newFieldMapper(propertyMapping1, null, RethrowMapperBuilderErrorHandler.INSTANCE);
assertTrue(fieldMapper instanceof LongFieldMapper);
}
use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class JoinJdbcMapperCustomKeyTest method test.
@Test
public void test() throws SQLException {
JdbcMapper<Person> mapper = JdbcMapperFactory.newInstance().addColumnDefinition(new Predicate<JdbcColumnKey>() {
@Override
public boolean test(JdbcColumnKey jdbcColumnKey) {
return jdbcColumnKey.getIndex() <= 2;
}
}, FieldMapperColumnDefinition.<JdbcColumnKey>key(new Predicate<PropertyMeta<?, ?>>() {
@Override
public boolean test(PropertyMeta<?, ?> propertyMeta) {
return propertyMeta.getPath().startsWith("key.");
}
})).newBuilder(Person.class).addMapping("key_tag").addMapping("key_n").addMapping("name").addMapping("phones_value").mapper();
ResultSet rs = JoinJdbcMapperOnTupleTest.setUpResultSetMock(new String[] { "key_tag", "key_n", "name", "phones_value" }, new Object[][] { { "t1", 1, "t11", "p111" }, { "t1", 1, "t11", "p112" }, { "t1", 2, "t12", "p121" } });
final List<Person> list = mapper.forEach(rs, new ListCollector<Person>()).getList();
assertEquals(2, list.size());
assertEquals("t1", list.get(0).key.tag);
assertEquals(1, list.get(0).key.n);
assertEquals("t11", list.get(0).name);
assertEquals(Arrays.asList("p111", "p112"), list.get(0).phones);
assertEquals("t1", list.get(1).key.tag);
assertEquals(2, list.get(1).key.n);
assertEquals("t12", list.get(1).name);
assertEquals(Arrays.asList("p121"), list.get(1).phones);
}
use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperFactoryTest method testFieldErrorHandlingOnResultSet.
@Test
public void testFieldErrorHandlingOnResultSet() throws SQLException, Exception, ParseException {
@SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
ResultSet rs = mock(ResultSet.class);
final Exception exception = new SQLException("Error!");
JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).newBuilder(DbObject.class).addMapping("id").mapper();
when(rs.next()).thenReturn(true, false);
when(rs.getLong(1)).thenThrow(exception);
List<DbObject> list = mapper.forEach(rs, 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.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperSubObjectTest method testMapInnerObjectWithColumnDefinition.
@Test
public void testMapInnerObjectWithColumnDefinition() throws Exception {
JdbcMapperBuilder<Db1DeepObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(Db1DeepObject.class);
FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition = FieldMapperColumnDefinition.customGetter(new Getter<ResultSet, String>() {
@Override
public String get(ResultSet target) throws Exception {
return "ov1";
}
});
builder.addMapping("db_object_name", columnDefinition);
final JdbcMapper<Db1DeepObject> mapper = builder.mapper();
ResultSet rs = mock(ResultSet.class);
when(rs.getString(1)).thenReturn("name1");
when(rs.next()).thenReturn(true, false);
Db1DeepObject next = mapper.iterator(rs).next();
assertEquals("ov1", next.getDbObject().getName());
}
Aggregations