Search in sources :

Example 16 with SampleFieldKey

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

the class Issue366 method testBreakDetector.

@Test
public void testBreakDetector() throws Exception {
    KeySourceGetter<SampleFieldKey, Object> keySourceGetter = mock(KeySourceGetter.class);
    Object object = new Object();
    SampleFieldKey key = new SampleFieldKey("k", 1);
    when(keySourceGetter.getValue(key, object)).thenReturn(new byte[] { 1, 2 }, new byte[] { 1, 2 }, new byte[] { 1, 3 });
    KeyDefinition<Object, SampleFieldKey> keyDefinition = new KeyDefinition<Object, SampleFieldKey>(new SampleFieldKey[] { key }, keySourceGetter, 0);
    BreakDetector<Object> breakDetector = new BreakDetector<Object>(keyDefinition);
    assertTrue(breakDetector.broke(object));
    assertFalse(breakDetector.broke(object));
    assertTrue(breakDetector.broke(object));
}
Also used : BreakDetector(org.simpleflatmapper.map.context.impl.BreakDetector) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) KeyDefinition(org.simpleflatmapper.map.context.KeyDefinition) Test(org.junit.Test)

Example 17 with SampleFieldKey

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

the class AbstractMapperBuilderTest method testConversionCharacterToJodaTime.

@Test
public void testConversionCharacterToJodaTime() {
    ClassMeta<List<DateTime>> classMeta = ReflectionService.disableAsm().<List<DateTime>>getClassMeta(new TypeReference<List<DateTime>>() {
    }.getType());
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMdd HH:mm:ss.SSSS Z");
    Mapper<Object[], List<DateTime>> mapper = new SampleMapperBuilder<List<DateTime>>(classMeta).addMapping(new SampleFieldKey("0", 0, new Class[0], String.class), dateTimeFormatter).mapper();
    DateTime now = DateTime.now();
    Object[] objects = new Object[] { dateTimeFormatter.print(now) };
    List<DateTime> map = mapper.map(objects);
    assertEquals(now, map.get(0));
}
Also used : List(java.util.List) 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) TypeReference(org.simpleflatmapper.util.TypeReference) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 18 with SampleFieldKey

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

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

the class AbstractMapperFactoryTest method testDefaultMapperConfig.

@Test
public void testDefaultMapperConfig() {
    MapperFactory mapperFactory = new MapperFactory();
    MapperConfig<SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> mapperConfig = mapperFactory.mapperConfig();
    final ArrayList<Object> properties = new ArrayList<Object>();
    mapperConfig.columnDefinitions().forEach(Object.class, new BiConsumer<Predicate<? super SampleFieldKey>, Object>() {

        @Override
        public void accept(Predicate<? super SampleFieldKey> predicate, Object o) {
            properties.add(o);
        }
    });
    assertTrue(properties.isEmpty());
    assertFalse(mapperConfig.failOnAsm());
    assertEquals(NO_ASM_MAPPER_THRESHOLD, mapperConfig.asmMapperNbFieldsLimit());
    assertNull(mapperConfig.fieldMapperErrorHandler());
    assertFalse(mapperConfig.hasFieldMapperErrorHandler());
    assertTrue(mapperConfig.mapperBuilderErrorHandler() instanceof RethrowMapperBuilderErrorHandler);
    assertTrue(mapperConfig.consumerErrorHandler() instanceof RethrowConsumerErrorHandler);
    assertEquals(MapperConfig.MAX_METHOD_SIZE, mapperConfig.maxMethodSize());
    assertTrue(mapperConfig.propertyNameMatcherFactory() instanceof DefaultPropertyNameMatcherFactory);
    assertTrue(mapperFactory.getReflectionService().isAsmActivated());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ArrayList(java.util.ArrayList) Predicate(org.simpleflatmapper.util.Predicate) RethrowMapperBuilderErrorHandler(org.simpleflatmapper.map.error.RethrowMapperBuilderErrorHandler) AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) RethrowConsumerErrorHandler(org.simpleflatmapper.map.error.RethrowConsumerErrorHandler) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) DefaultPropertyNameMatcherFactory(org.simpleflatmapper.map.mapper.DefaultPropertyNameMatcherFactory) Test(org.junit.Test)

Example 20 with SampleFieldKey

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

the class AbstractMapperFactoryTest method testColumnDefinition.

@Test
public void testColumnDefinition() {
    MapperFactory mapperFactory = new MapperFactory();
    Object prop1 = new Object();
    Object prop2 = new Object();
    mapperFactory.addColumnDefinition("mykey", FieldMapperColumnDefinition.<SampleFieldKey>identity().add(prop1));
    Assert.assertArrayEquals(new Object[] { prop1 }, mapperFactory.columnDefinitions().getColumnDefinition(new SampleFieldKey("mykey", 0)).properties());
    mapperFactory.addColumnDefinition(new Predicate<SampleFieldKey>() {

        @Override
        public boolean test(SampleFieldKey sampleFieldKey) {
            return sampleFieldKey.getIndex() < 1;
        }
    }, FieldMapperColumnDefinition.<SampleFieldKey>identity().add(prop2));
    Assert.assertArrayEquals(new Object[] { 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