Search in sources :

Example 1 with MappingContext

use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.

the class DiscriminatorMapperTest method setUp.

@Before
public void setUp() {
    Mapper<Object[], Person> studentMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            StudentGS studentGS = new StudentGS();
            try {
                mapTo(source, studentGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return studentGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            StudentGS studentGS = (StudentGS) target;
            studentGS.setName((String) source[1]);
            studentGS.setId((Integer) source[2]);
        }
    };
    Mapper<Object[], Person> professorMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            ProfessorGS professorGS = new ProfessorGS();
            try {
                mapTo(source, professorGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return professorGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            ProfessorGS professorGS = (ProfessorGS) target;
            professorGS.setName((String) source[1]);
            professorGS.setId((Integer) source[2]);
        }
    };
    List<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>> mappers = new ArrayList<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>>();
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "student".equals(objects[0]);
        }
    }, studentMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "professor".equals(objects[0]);
        }
    }, professorMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mapper = new DiscriminatorMapper<Object[], Object[][], Person, RuntimeException>(mappers, new UnaryFactory<Object[][], Enumarable<Object[]>>() {

        @Override
        public Enumarable<Object[]> newInstance(Object[][] objects) {
            return new ArrayEnumarable<Object[]>(objects);
        }
    }, new UncheckedConverter<Object[], String>() {

        @Override
        public String convert(Object[] in) {
            return Arrays.toString(in);
        }
    }, RethrowConsumerErrorHandler.INSTANCE);
}
Also used : ArrayList(java.util.ArrayList) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) MappingContextFactoryFromRows(org.simpleflatmapper.map.context.MappingContextFactoryFromRows) MappingException(org.simpleflatmapper.map.MappingException) Predicate(org.simpleflatmapper.util.Predicate) MappingContext(org.simpleflatmapper.map.MappingContext) ProfessorGS(org.simpleflatmapper.test.beans.ProfessorGS) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) Mapper(org.simpleflatmapper.map.Mapper) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) UncheckedConverter(org.simpleflatmapper.converter.UncheckedConverter) Person(org.simpleflatmapper.test.beans.Person) ArrayEnumarable(org.simpleflatmapper.util.ArrayEnumarable) StudentGS(org.simpleflatmapper.test.beans.StudentGS) Before(org.junit.Before)

Example 2 with MappingContext

use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilder method constructorInjections.

@SuppressWarnings("unchecked")
private ConstructorInjections constructorInjections() {
    final Map<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>> injections = new HashMap<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>>();
    final List<FieldMapper<S, T>> fieldMappers = new ArrayList<FieldMapper<S, T>>();
    propertyMappingsBuilder.forEachConstructorProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {

        @SuppressWarnings("unchecked")
        @Override
        public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> propertyMapping) {
            if (!isTargetForMapperFieldMapper(propertyMapping)) {
                PropertyMeta<T, ?> pm = propertyMapping.getPropertyMeta();
                ConstructorPropertyMeta<T, ?> cProp = (ConstructorPropertyMeta<T, ?>) pm;
                Parameter parameter = cProp.getParameter();
                Getter<? super S, ?> getter = fieldMapperFactory.getGetterFromSource(propertyMapping.getColumnKey(), pm.getPropertyType(), propertyMapping.getColumnDefinition(), pm.getPropertyClassMetaSupplier());
                if (NullGetter.isNull(getter)) {
                    mapperConfig.mapperBuilderErrorHandler().accessorNotFound("Could not find getter for " + propertyMapping.getColumnKey() + " type " + propertyMapping.getPropertyMeta().getPropertyType() + " path " + propertyMapping.getPropertyMeta().getPath() + " See " + ErrorDoc.toUrl("FMMB_GETTER_NOT_FOUND"));
                } else {
                    injections.put(parameter, new BiFunctionGetter<S, MappingContext<? super S>, Object>(getter));
                }
                if (!NullSetter.isNull(cProp.getSetter())) {
                    fieldMappers.add(fieldMapperFactory.newFieldMapper(propertyMapping, mappingContextFactoryBuilder, mapperConfig.mapperBuilderErrorHandler()));
                }
            }
        }
    });
    for (PropertyPerOwner e : getSubPropertyPerOwner()) {
        if (e.owner.isConstructorProperty()) {
            final List<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>> properties = e.propertyMappings;
            final MappingContextFactoryBuilder currentBuilder = getMapperContextFactoryBuilder(e.owner, properties);
            final Mapper<S, ?> mapper;
            if (properties.size() == 1 && JoinUtils.isArrayElement(properties.get(0).getPropertyMeta())) {
                mapper = getterPropertyMapper(e.owner, properties.get(0));
            } else {
                mapper = subPropertyMapper(e.owner, properties, currentBuilder);
            }
            ConstructorPropertyMeta<T, ?> meta = (ConstructorPropertyMeta<T, ?>) e.owner;
            injections.put(meta.getParameter(), newMapperGetterAdapter(mapper, currentBuilder));
            fieldMappers.add(newMapperFieldMapper(properties, meta, mapper, currentBuilder));
        }
    }
    return new ConstructorInjections(injections, fieldMappers.toArray(new FieldMapper[0]));
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) NullGetter(org.simpleflatmapper.reflect.getter.NullGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) MappingContext(org.simpleflatmapper.map.MappingContext) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) BiFunction(org.simpleflatmapper.util.BiFunction) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) Parameter(org.simpleflatmapper.reflect.Parameter) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)

Example 3 with MappingContext

use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperErrorTest method testInstantiatorError.

@Test
public void testInstantiatorError() {
    MapperImpl<ResultSet, DbObject> mapper = new MapperImpl<ResultSet, DbObject>(null, null, new BiInstantiator<ResultSet, MappingContext<? super ResultSet>, DbObject>() {

        @Override
        public DbObject newInstance(ResultSet s, MappingContext<? super ResultSet> context) throws Exception {
            throw new IOException();
        }
    });
    try {
        mapper.map(null);
        fail("Expected error");
    } catch (Exception e) {
        assertTrue(e instanceof IOException);
    }
}
Also used : MappingContext(org.simpleflatmapper.map.MappingContext) MapperImpl(org.simpleflatmapper.map.mapper.MapperImpl) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with MappingContext

use of org.simpleflatmapper.map.MappingContext 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 5 with MappingContext

use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.

the class MapperBiFunctionAdapterTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws Exception {
    Predicate<Object> nullChecker = mock(Predicate.class);
    Mapper<Object, String> mapper = mock(Mapper.class);
    MappingContext mappingContext = mock(MappingContext.class);
    MapperBiFunctionAdapter<Object, String> biFunctionAdapter = new MapperBiFunctionAdapter<Object, String>(mapper, nullChecker, 0);
    Object o = new Object();
    when(mapper.map(o, mappingContext)).thenReturn("HEllo", "Bye");
    when(nullChecker.test(any())).thenReturn(false, true);
    assertEquals("HEllo", biFunctionAdapter.apply(o, mappingContext));
    assertNull(biFunctionAdapter.apply(null, null));
    biFunctionAdapter.toString();
}
Also used : MappingContext(org.simpleflatmapper.map.MappingContext) MapperBiFunctionAdapter(org.simpleflatmapper.map.mapper.MapperBiFunctionAdapter) Test(org.junit.Test)

Aggregations

MappingContext (org.simpleflatmapper.map.MappingContext)8 Test (org.junit.Test)4 FieldMapper (org.simpleflatmapper.map.FieldMapper)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 DbObject (org.simpleflatmapper.test.beans.DbObject)3 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Mapper (org.simpleflatmapper.map.Mapper)2 MappingException (org.simpleflatmapper.map.MappingException)2 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)2 Parameter (org.simpleflatmapper.reflect.Parameter)2 BiFunction (org.simpleflatmapper.util.BiFunction)2 UnaryFactory (org.simpleflatmapper.util.UnaryFactory)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Before (org.junit.Before)1 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 SfmResultSetMapperFactory (org.simpleflatmapper.jdbi.SfmResultSetMapperFactory)1 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)1