Search in sources :

Example 1 with SampleFieldKey

use of org.simpleflatmapper.test.map.SampleFieldKey in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilderTest method testMapInnerObjectWithMapper.

@Test
public void testMapInnerObjectWithMapper() throws Exception {
    ConstantSourceMapperBuilder<Object, MyObjectWithInner, SampleFieldKey> constantSourceMapperBuilder = new ConstantSourceMapperBuilder<Object, MyObjectWithInner, SampleFieldKey>(mapperSource, classMeta, MapperConfig.config(new IdentityFieldMapperColumnDefinitionProvider<SampleFieldKey>()), new MappingContextFactoryBuilder<Object, SampleFieldKey>(null), SampleFieldKey.KEY_FACTORY);
    constantSourceMapperBuilder.addMapping(new SampleFieldKey("prop_date", 0), FieldMapperColumnDefinition.<SampleFieldKey>identity());
    Mapper<Object, MyObjectWithInner> mapper = constantSourceMapperBuilder.mapper();
    MyObjectWithInner o = mapper.map(null);
    assertEquals(DATE, o.prop.date);
    assertNull(o.prop.str);
}
Also used : IdentityFieldMapperColumnDefinitionProvider(org.simpleflatmapper.map.impl.IdentityFieldMapperColumnDefinitionProvider) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) ConstantSourceMapperBuilder(org.simpleflatmapper.map.mapper.ConstantSourceMapperBuilder) Test(org.junit.Test)

Example 2 with SampleFieldKey

use of org.simpleflatmapper.test.map.SampleFieldKey in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilderTest method testFieldMapperErrorHandler.

@Test
public void testFieldMapperErrorHandler() throws Exception {
    ConstantSourceMapperBuilder<Object, MyObjectWithInner, SampleFieldKey> constantSourceMapperBuilder = new ConstantSourceMapperBuilder<Object, MyObjectWithInner, SampleFieldKey>(mapperSource, classMeta, MapperConfig.config(new IdentityFieldMapperColumnDefinitionProvider<SampleFieldKey>()).fieldMapperErrorHandler(new FieldMapperErrorHandler<SampleFieldKey>() {

        @Override
        public void errorMappingField(SampleFieldKey key, Object source, Object target, Exception error) throws MappingException {
        }
    }), new MappingContextFactoryBuilder<Object, SampleFieldKey>(null), SampleFieldKey.KEY_FACTORY);
    constantSourceMapperBuilder.addMapping(new SampleFieldKey("prop_date", 16), FieldMapperColumnDefinition.<SampleFieldKey>identity());
    Mapper<Object, MyObjectWithInner> mapper = constantSourceMapperBuilder.mapper();
    MyObjectWithInner o = mapper.map(null);
    System.out.println("mapper = " + mapper);
    assertNull(o.prop);
}
Also used : SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) ConstantSourceMapperBuilder(org.simpleflatmapper.map.mapper.ConstantSourceMapperBuilder) MappingException(org.simpleflatmapper.map.MappingException) Test(org.junit.Test)

Example 3 with SampleFieldKey

use of org.simpleflatmapper.test.map.SampleFieldKey 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 4 with SampleFieldKey

use of org.simpleflatmapper.test.map.SampleFieldKey in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperFactoryTest method testAliases.

@Test
public void testAliases() {
    MapperFactory mapperFactory = new MapperFactory();
    mapperFactory.addAlias("a", "aa");
    mapperFactory.addAliases(new HashMap<String, String>() {

        {
            put("b", "bb");
            put("c", "cc");
        }
    });
    Assert.assertArrayEquals(new Object[] { new RenameProperty("aa") }, mapperFactory.columnDefinitions().getColumnDefinition(new SampleFieldKey("a", 0)).properties());
    Assert.assertArrayEquals(new Object[] { new RenameProperty("bb") }, mapperFactory.columnDefinitions().getColumnDefinition(new SampleFieldKey("b", 0)).properties());
    Assert.assertArrayEquals(new Object[] { new RenameProperty("cc") }, mapperFactory.columnDefinitions().getColumnDefinition(new SampleFieldKey("c", 0)).properties());
    Assert.assertArrayEquals(new Object[] {}, mapperFactory.columnDefinitions().getColumnDefinition(new SampleFieldKey("d", 0)).properties());
}
Also used : RenameProperty(org.simpleflatmapper.map.property.RenameProperty) AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) Test(org.junit.Test)

Example 5 with SampleFieldKey

use of org.simpleflatmapper.test.map.SampleFieldKey in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperFactoryTest method testColumnProperty.

@Test
public void testColumnProperty() {
    MapperFactory mapperFactory = new MapperFactory();
    Object prop1 = new Object();
    Object prop2 = new Object();
    final Object prop3 = new Object();
    mapperFactory.addColumnProperty("mykey", prop1);
    mapperFactory.addColumnProperty(new Predicate<SampleFieldKey>() {

        @Override
        public boolean test(SampleFieldKey sampleFieldKey) {
            return sampleFieldKey.getIndex() < 1;
        }
    }, prop2);
    mapperFactory.addColumnProperty(new Predicate<SampleFieldKey>() {

        @Override
        public boolean test(SampleFieldKey sampleFieldKey) {
            return sampleFieldKey.getIndex() < 1;
        }
    }, new UnaryFactory<SampleFieldKey, Object>() {

        @Override
        public Object newInstance(SampleFieldKey sampleFieldKey) {
            return prop3;
        }
    });
    Assert.assertArrayEquals(new Object[] { prop3, prop2, prop1 }, mapperFactory.columnDefinitions().getColumnDefinition(new SampleFieldKey("mykey", 0)).properties());
    Assert.assertArrayEquals(new Object[] { prop1 }, mapperFactory.columnDefinitions().getColumnDefinition(new SampleFieldKey("mykey", 1)).properties());
}
Also used : AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) Test(org.junit.Test)

Aggregations

SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)21 Test (org.junit.Test)19 AbstractMapperFactory (org.simpleflatmapper.map.mapper.AbstractMapperFactory)7 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)6 DbObject (org.simpleflatmapper.test.beans.DbObject)4 ConstantSourceMapperBuilder (org.simpleflatmapper.map.mapper.ConstantSourceMapperBuilder)3 DbFinal1DeepObject (org.simpleflatmapper.test.beans.DbFinal1DeepObject)3 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)3 DbListObject (org.simpleflatmapper.test.beans.DbListObject)3 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)3 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)2 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)2 MappingException (org.simpleflatmapper.map.MappingException)2 BreakDetectorMappingContextFactory (org.simpleflatmapper.map.context.impl.BreakDetectorMappingContextFactory)2 RethrowConsumerErrorHandler (org.simpleflatmapper.map.error.RethrowConsumerErrorHandler)2 RethrowMapperBuilderErrorHandler (org.simpleflatmapper.map.error.RethrowMapperBuilderErrorHandler)2 IdentityFieldMapperColumnDefinitionProvider (org.simpleflatmapper.map.impl.IdentityFieldMapperColumnDefinitionProvider)2 FieldMapperColumnDefinitionProviderImpl (org.simpleflatmapper.map.mapper.FieldMapperColumnDefinitionProviderImpl)2