Search in sources :

Example 56 with DbObject

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());
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) GetterFactory(org.simpleflatmapper.reflect.getter.GetterFactory) Type(java.lang.reflect.Type) DbObject(org.simpleflatmapper.test.beans.DbObject) Getter(org.simpleflatmapper.reflect.Getter) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) Test(org.junit.Test)

Example 57 with DbObject

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));
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) MappingContext(org.simpleflatmapper.map.MappingContext) ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) FieldMapper(org.simpleflatmapper.map.FieldMapper) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 58 with DbObject

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);
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject)

Example 59 with DbObject

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());
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) MapperBuildingException(org.simpleflatmapper.map.MapperBuildingException) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) DbListObject(org.simpleflatmapper.test.beans.DbListObject) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinal1DeepObject(org.simpleflatmapper.test.beans.DbFinal1DeepObject) DbPartialFinalObject(org.simpleflatmapper.test.beans.DbPartialFinalObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) Test(org.junit.Test)

Example 60 with DbObject

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"));
}
Also used : SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) DbObject(org.simpleflatmapper.test.beans.DbObject) 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