Search in sources :

Example 1 with FieldMapperColumnDefinition

use of org.simpleflatmapper.map.property.FieldMapperColumnDefinition in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperBuilderTest method testDefaultValue.

@Test
public void testDefaultValue() throws Exception {
    ClassMeta<DbObject> classMeta = ReflectionService.newInstance().<DbObject>getClassMeta(DbObject.class);
    FieldMapperColumnDefinitionProviderImpl<SampleFieldKey> definitionProvider = new FieldMapperColumnDefinitionProviderImpl<SampleFieldKey>();
    definitionProvider.addColumnProperty("type_name", new DefaultValueProperty<DbObject.Type>(DbObject.Type.type4));
    MapperConfig<SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> mapperConfig = MapperConfig.<SampleFieldKey>fieldMapperConfig().columnDefinitions(definitionProvider);
    Mapper<Object[], DbObject> mapper = new SampleMapperBuilder<DbObject>(classMeta, mapperConfig).addMapping("id").mapper();
    Object[] data = new Object[] { 3l };
    DbObject dbObject = mapper.map(data);
    assertEquals(3l, dbObject.getId());
    assertEquals(DbObject.Type.type4, dbObject.getTypeName());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) DbObject(org.simpleflatmapper.test.beans.DbObject) Type(java.lang.reflect.Type) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) 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) Test(org.junit.Test)

Example 2 with FieldMapperColumnDefinition

use of org.simpleflatmapper.map.property.FieldMapperColumnDefinition in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperFactoryTest method testErrorHandlers.

@Test
public void testErrorHandlers() {
    MapperFactory mapperFactory = new MapperFactory();
    FieldMapperErrorHandler<SampleFieldKey> fieldMapperErrorHandler = new FieldMapperErrorHandler<SampleFieldKey>() {

        @Override
        public void errorMappingField(SampleFieldKey key, Object source, Object target, Exception error) throws MappingException {
        }
    };
    MapperBuilderErrorHandler mapperBuilderErrorHandler = new MapperBuilderErrorHandler() {

        @Override
        public void accessorNotFound(String msg) {
        }

        @Override
        public void propertyNotFound(Type target, String property) {
        }

        @Override
        public void customFieldError(FieldKey<?> key, String message) {
        }
    };
    ConsumerErrorHandler consumerErrorHandler = new ConsumerErrorHandler() {

        @Override
        public void handlerError(Throwable error, Object target) {
        }
    };
    mapperFactory.fieldMapperErrorHandler(fieldMapperErrorHandler);
    mapperFactory.mapperBuilderErrorHandler(mapperBuilderErrorHandler);
    mapperFactory.consumerErrorHandler(consumerErrorHandler);
    MapperConfig<SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> mapperConfig = mapperFactory.mapperConfig();
    assertEquals(fieldMapperErrorHandler, mapperConfig.fieldMapperErrorHandler());
    assertEquals(mapperBuilderErrorHandler, mapperConfig.mapperBuilderErrorHandler());
    assertEquals(consumerErrorHandler, mapperConfig.consumerErrorHandler());
    Assert.assertEquals(consumerErrorHandler, mapperFactory.consumerErrorHandler());
    mapperFactory = new MapperFactory();
    assertTrue(mapperFactory.ignorePropertyNotFound().mapperConfig().mapperBuilderErrorHandler() instanceof IgnoreMapperBuilderErrorHandler);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) RethrowMapperBuilderErrorHandler(org.simpleflatmapper.map.error.RethrowMapperBuilderErrorHandler) IgnoreMapperBuilderErrorHandler(org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler) MapperBuilderErrorHandler(org.simpleflatmapper.map.MapperBuilderErrorHandler) IgnoreMapperBuilderErrorHandler(org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler) MappingException(org.simpleflatmapper.map.MappingException) Type(java.lang.reflect.Type) RethrowConsumerErrorHandler(org.simpleflatmapper.map.error.RethrowConsumerErrorHandler) ConsumerErrorHandler(org.simpleflatmapper.map.ConsumerErrorHandler) AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) FieldKey(org.simpleflatmapper.map.FieldKey) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) Test(org.junit.Test)

Example 3 with FieldMapperColumnDefinition

use of org.simpleflatmapper.map.property.FieldMapperColumnDefinition in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperFactoryTest method testAsmSettings.

@Test
public void testAsmSettings() {
    ReflectionService reflectionService = new MapperFactory().useAsm(false).getReflectionService();
    assertFalse(reflectionService.isAsmActivated());
    MapperConfig<SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> mapperConfig = new MapperFactory().asmMapperNbFieldsLimit(33).failOnAsm(true).maxMethodSize(13).mapperConfig();
    assertEquals(33, mapperConfig.asmMapperNbFieldsLimit());
    assertEquals(13, mapperConfig.maxMethodSize());
    assertEquals(true, mapperConfig.failOnAsm());
    reflectionService = new ReflectionService(null);
    assertSame(reflectionService, new MapperFactory().reflectionService(reflectionService).getReflectionService());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) ReflectionService(org.simpleflatmapper.reflect.ReflectionService) Test(org.junit.Test)

Example 4 with FieldMapperColumnDefinition

use of org.simpleflatmapper.map.property.FieldMapperColumnDefinition in project SimpleFlatMapper by arnaudroger.

the class ConstantTargetFieldMapperFactoryImplTest method testGetterNotFound.

@Test
public void testGetterNotFound() {
    ConstantTargetFieldMapperFactory<Appendable, SampleFieldKey> factory = ConstantTargetFieldMapperFactoryImpl.<Appendable, SampleFieldKey>newInstance(setterFactory, Appendable.class);
    try {
        PropertyMeta<Object, Object> pm = mock(PropertyMeta.class);
        PropertyMapping<Object, Object, SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> propertyMapping = new PropertyMapping<Object, Object, SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>>(pm, new SampleFieldKey("hh", 0), FieldMapperColumnDefinition.<SampleFieldKey>identity());
        factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
        fail();
    } catch (MapperBuildingException e) {
    }
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) MapperBuildingException(org.simpleflatmapper.map.MapperBuildingException) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) DbObject(org.simpleflatmapper.test.beans.DbObject) ConstantSourceFieldMapperFactoryImplTest(org.simpleflatmapper.test.map.fieldmapper.ConstantSourceFieldMapperFactoryImplTest) Test(org.junit.Test)

Example 5 with FieldMapperColumnDefinition

use of org.simpleflatmapper.map.property.FieldMapperColumnDefinition 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)

Aggregations

FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)20 Test (org.junit.Test)8 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)6 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)5 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)5 FieldMapper (org.simpleflatmapper.map.FieldMapper)4 DbObject (org.simpleflatmapper.test.beans.DbObject)4 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 MappingContext (org.simpleflatmapper.map.MappingContext)3 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)3 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)3 AbstractMapperFactory (org.simpleflatmapper.map.mapper.AbstractMapperFactory)3 Getter (org.simpleflatmapper.reflect.Getter)3 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 DatastaxColumnKey (org.simpleflatmapper.datastax.DatastaxColumnKey)2 FieldKey (org.simpleflatmapper.map.FieldKey)2 IgnoreMapperBuilderErrorHandler (org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler)2 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)2 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)2