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());
}
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);
}
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());
}
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) {
}
}
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]));
}
Aggregations